Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2734)

Unified Diff: content/renderer/media/scoped_loop_observer.cc

Issue 9104043: Monitor the IO message loop in the AudioDevice classes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/renderer/media/scoped_loop_observer.cc
diff --git a/content/renderer/media/scoped_loop_observer.cc b/content/renderer/media/scoped_loop_observer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1e02592664c3a4b7812c68601c4c66a99081ad39
--- /dev/null
+++ b/content/renderer/media/scoped_loop_observer.cc
@@ -0,0 +1,42 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/renderer/media/scoped_loop_observer.h"
+
+#include "base/bind.h"
+#include "base/synchronization/waitable_event.h"
+
+ScopedLoopObserver::ScopedLoopObserver(MessageLoop* loop)
+ : loop_(loop->message_loop_proxy()) {
+ ObserveLoopDestruction(true, NULL);
+}
+
+ScopedLoopObserver::~ScopedLoopObserver() {
+ ObserveLoopDestruction(false, NULL);
+}
+
+void ScopedLoopObserver::ObserveLoopDestruction(bool enable,
+ base::WaitableEvent* done) {
+ // Note: |done| may be NULL.
+ if (loop_->BelongsToCurrentThread()) {
+ MessageLoop* loop = MessageLoop::current();
+ if (enable) {
+ loop->AddDestructionObserver(this);
+ } else {
+ loop->RemoveDestructionObserver(this);
+ }
+ } else {
+ base::WaitableEvent event(false, false);
+ if (loop_->PostTask(FROM_HERE,
+ base::Bind(&ScopedLoopObserver::ObserveLoopDestruction,
+ base::Unretained(this), enable, &event))) {
+ event.Wait();
+ } else {
+ // The message loop's thread has already terminated, so no need to wait.
+ }
+ }
+
+ if (done)
+ done->Signal();
+}

Powered by Google App Engine
This is Rietveld 408576698