cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / media / audio / scoped_loop_observer.cc
blob01187ec8f991be37bad58a9cf648c7be72ed5786
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"
7 #include "base/bind.h"
8 #include "base/synchronization/waitable_event.h"
10 namespace media {
12 ScopedLoopObserver::ScopedLoopObserver(
13 const scoped_refptr<base::MessageLoopProxy>& loop)
14 : loop_(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();
27 if (enable) {
28 loop->AddDestructionObserver(this);
29 } else {
30 loop->RemoveDestructionObserver(this);
32 } else {
33 base::WaitableEvent event(false, false);
34 if (loop_->PostTask(FROM_HERE,
35 base::Bind(&ScopedLoopObserver::ObserveLoopDestruction,
36 base::Unretained(this), enable, &event))) {
37 event.Wait();
38 } else {
39 // The message loop's thread has already terminated, so no need to wait.
43 if (done)
44 done->Signal();
47 } // namespace media.