OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/audio/scoped_loop_observer.h" | 5 #include "media/audio/scoped_task_runner_observer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 | 9 |
10 namespace media { | 10 namespace media { |
11 | 11 |
12 ScopedLoopObserver::ScopedLoopObserver( | 12 ScopedTaskRunnerObserver::ScopedTaskRunnerObserver( |
13 const scoped_refptr<base::MessageLoopProxy>& loop) | 13 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
14 : loop_(loop) { | 14 : task_runner_(task_runner) { |
15 ObserveLoopDestruction(true, NULL); | 15 ObserveLoopDestruction(true, NULL); |
16 } | 16 } |
17 | 17 |
18 ScopedLoopObserver::~ScopedLoopObserver() { | 18 ScopedTaskRunnerObserver::~ScopedTaskRunnerObserver() { |
19 ObserveLoopDestruction(false, NULL); | 19 ObserveLoopDestruction(false, NULL); |
20 } | 20 } |
21 | 21 |
22 void ScopedLoopObserver::ObserveLoopDestruction(bool enable, | 22 void ScopedTaskRunnerObserver::ObserveLoopDestruction( |
23 base::WaitableEvent* done) { | 23 bool enable, |
| 24 base::WaitableEvent* done) { |
24 // Note: |done| may be NULL. | 25 // Note: |done| may be NULL. |
25 if (loop_->BelongsToCurrentThread()) { | 26 if (task_runner_->BelongsToCurrentThread()) { |
26 base::MessageLoop* loop = base::MessageLoop::current(); | 27 base::MessageLoop* loop = base::MessageLoop::current(); |
27 if (enable) { | 28 if (enable) { |
28 loop->AddDestructionObserver(this); | 29 loop->AddDestructionObserver(this); |
29 } else { | 30 } else { |
30 loop->RemoveDestructionObserver(this); | 31 loop->RemoveDestructionObserver(this); |
31 } | 32 } |
32 } else { | 33 } else { |
33 base::WaitableEvent event(false, false); | 34 base::WaitableEvent event(false, false); |
34 if (loop_->PostTask(FROM_HERE, | 35 if (task_runner_->PostTask(FROM_HERE, |
35 base::Bind(&ScopedLoopObserver::ObserveLoopDestruction, | 36 base::Bind(&ScopedTaskRunnerObserver::ObserveLoopDestruction, |
36 base::Unretained(this), enable, &event))) { | 37 base::Unretained(this), enable, &event))) { |
37 event.Wait(); | 38 event.Wait(); |
38 } else { | 39 } else { |
39 // The message loop's thread has already terminated, so no need to wait. | 40 // The message loop's thread has already terminated, so no need to wait. |
40 } | 41 } |
41 } | 42 } |
42 | 43 |
43 if (done) | 44 if (done) |
44 done->Signal(); | 45 done->Signal(); |
45 } | 46 } |
46 | 47 |
47 } // namespace media. | 48 } // namespace media. |
OLD | NEW |