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

Side by Side Diff: webrtc/base/asyncinvoker.cc

Issue 1303443003: Add helper class GuardedAsyncInvoker to protect against thread dying (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: tommi@s comments Created 5 years, 4 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 unified diff | Download patch
« no previous file with comments | « webrtc/base/asyncinvoker.h ('k') | webrtc/base/thread_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/base/asyncinvoker.h" 11 #include "webrtc/base/asyncinvoker.h"
12 12
13 #include "webrtc/base/checks.h"
13 #include "webrtc/base/logging.h" 14 #include "webrtc/base/logging.h"
14 15
15 namespace rtc { 16 namespace rtc {
16 17
17 AsyncInvoker::AsyncInvoker() : destroying_(false) {} 18 AsyncInvoker::AsyncInvoker() : destroying_(false) {}
18 19
19 AsyncInvoker::~AsyncInvoker() { 20 AsyncInvoker::~AsyncInvoker() {
20 destroying_ = true; 21 destroying_ = true;
21 SignalInvokerDestroyed(); 22 SignalInvokerDestroyed();
22 // Messages for this need to be cleared *before* our destructor is complete. 23 // Messages for this need to be cleared *before* our destructor is complete.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 uint32 delay_ms, 70 uint32 delay_ms,
70 uint32 id) { 71 uint32 id) {
71 if (destroying_) { 72 if (destroying_) {
72 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; 73 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
73 return; 74 return;
74 } 75 }
75 thread->PostDelayed(delay_ms, this, id, 76 thread->PostDelayed(delay_ms, this, id,
76 new ScopedRefMessageData<AsyncClosure>(closure)); 77 new ScopedRefMessageData<AsyncClosure>(closure));
77 } 78 }
78 79
80 GuardedAsyncInvoker::GuardedAsyncInvoker() : thread_(Thread::Current()) {
81 thread_->SignalQueueDestroyed.connect(this,
82 &GuardedAsyncInvoker::ThreadDestroyed);
83 }
84
85 GuardedAsyncInvoker::~GuardedAsyncInvoker() {
86 }
87
88 bool GuardedAsyncInvoker::Flush(uint32 id) {
89 rtc::CritScope cs(&crit_);
90 if (thread_ == nullptr)
91 return false;
92 invoker_.Flush(thread_, id);
93 return true;
94 }
95
96 void GuardedAsyncInvoker::ThreadDestroyed() {
97 rtc::CritScope cs(&crit_);
98 // We should never get more than one notification about the thread dying.
99 DCHECK(thread_ != nullptr);
100 thread_ = nullptr;
101 }
102
79 NotifyingAsyncClosureBase::NotifyingAsyncClosureBase(AsyncInvoker* invoker, 103 NotifyingAsyncClosureBase::NotifyingAsyncClosureBase(AsyncInvoker* invoker,
80 Thread* calling_thread) 104 Thread* calling_thread)
81 : invoker_(invoker), calling_thread_(calling_thread) { 105 : invoker_(invoker), calling_thread_(calling_thread) {
82 calling_thread->SignalQueueDestroyed.connect( 106 calling_thread->SignalQueueDestroyed.connect(
83 this, &NotifyingAsyncClosureBase::CancelCallback); 107 this, &NotifyingAsyncClosureBase::CancelCallback);
84 invoker->SignalInvokerDestroyed.connect( 108 invoker->SignalInvokerDestroyed.connect(
85 this, &NotifyingAsyncClosureBase::CancelCallback); 109 this, &NotifyingAsyncClosureBase::CancelCallback);
86 } 110 }
87 111
88 NotifyingAsyncClosureBase::~NotifyingAsyncClosureBase() { 112 NotifyingAsyncClosureBase::~NotifyingAsyncClosureBase() {
(...skipping 10 matching lines...) Expand all
99 void NotifyingAsyncClosureBase::CancelCallback() { 123 void NotifyingAsyncClosureBase::CancelCallback() {
100 // If the callback is triggering when this is called, block the 124 // If the callback is triggering when this is called, block the
101 // destructor of the dying object here by waiting until the callback 125 // destructor of the dying object here by waiting until the callback
102 // is done triggering. 126 // is done triggering.
103 CritScope cs(&crit_); 127 CritScope cs(&crit_);
104 // calling_thread_ == NULL means do not trigger the callback. 128 // calling_thread_ == NULL means do not trigger the callback.
105 calling_thread_ = NULL; 129 calling_thread_ = NULL;
106 } 130 }
107 131
108 } // namespace rtc 132 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/asyncinvoker.h ('k') | webrtc/base/thread_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698