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_task_runner_observer.h"
8 #include "base/synchronization/waitable_event.h"
12 ScopedTaskRunnerObserver::ScopedTaskRunnerObserver(
13 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
14 : task_runner_(task_runner) {
15 ObserveLoopDestruction(true, NULL);
18 ScopedTaskRunnerObserver::~ScopedTaskRunnerObserver() {
19 ObserveLoopDestruction(false, NULL);
22 void ScopedTaskRunnerObserver::ObserveLoopDestruction(
24 base::WaitableEvent* done) {
25 // Note: |done| may be NULL.
26 if (task_runner_->BelongsToCurrentThread()) {
27 base::MessageLoop* loop = base::MessageLoop::current();
29 loop->AddDestructionObserver(this);
31 loop->RemoveDestructionObserver(this);
34 base::WaitableEvent event(false, false);
35 if (task_runner_->PostTask(FROM_HERE,
36 base::Bind(&ScopedTaskRunnerObserver::ObserveLoopDestruction,
37 base::Unretained(this), enable, &event))) {
40 // The message loop's thread has already terminated, so no need to wait.