1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/audio/scoped_loop_observer.h"
8 #include "base/synchronization/waitable_event.h"
12 ScopedLoopObserver::ScopedLoopObserver(
13 const scoped_refptr<base::MessageLoopProxy>& loop)
15 ObserveLoopDestruction(true, NULL);
18 ScopedLoopObserver::~ScopedLoopObserver() {
19 ObserveLoopDestruction(false, NULL);
22 void ScopedLoopObserver::ObserveLoopDestruction(bool enable,
23 base::WaitableEvent* done) {
24 // Note: |done| may be NULL.
25 if (loop_->BelongsToCurrentThread()) {
26 base::MessageLoop* loop = base::MessageLoop::current();
28 loop->AddDestructionObserver(this);
30 loop->RemoveDestructionObserver(this);
33 base::WaitableEvent event(false, false);
34 if (loop_->PostTask(FROM_HERE,
35 base::Bind(&ScopedLoopObserver::ObserveLoopDestruction,
36 base::Unretained(this), enable, &event))) {
39 // The message loop's thread has already terminated, so no need to wait.