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

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: Remove AttachToThread() functionality 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
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 void GuardedAsyncInvoker::Flush(uint32 id) {
89 rtc::CritScope cs(&crit_);
90 if (thread_ != nullptr)
91 invoker_.Flush(thread_, id);
92 }
93
94 void GuardedAsyncInvoker::ThreadDestroyed() {
95 rtc::CritScope cs(&crit_);
96 thread_ = nullptr;
tommi (sloooow) - chröme 2015/08/19 20:01:54 should we DCHECK that thread_ is not nullptr first
magjed_webrtc 2015/08/20 10:28:33 Done.
97 }
98
79 NotifyingAsyncClosureBase::NotifyingAsyncClosureBase(AsyncInvoker* invoker, 99 NotifyingAsyncClosureBase::NotifyingAsyncClosureBase(AsyncInvoker* invoker,
80 Thread* calling_thread) 100 Thread* calling_thread)
81 : invoker_(invoker), calling_thread_(calling_thread) { 101 : invoker_(invoker), calling_thread_(calling_thread) {
82 calling_thread->SignalQueueDestroyed.connect( 102 calling_thread->SignalQueueDestroyed.connect(
83 this, &NotifyingAsyncClosureBase::CancelCallback); 103 this, &NotifyingAsyncClosureBase::CancelCallback);
84 invoker->SignalInvokerDestroyed.connect( 104 invoker->SignalInvokerDestroyed.connect(
85 this, &NotifyingAsyncClosureBase::CancelCallback); 105 this, &NotifyingAsyncClosureBase::CancelCallback);
86 } 106 }
87 107
88 NotifyingAsyncClosureBase::~NotifyingAsyncClosureBase() { 108 NotifyingAsyncClosureBase::~NotifyingAsyncClosureBase() {
(...skipping 10 matching lines...) Expand all
99 void NotifyingAsyncClosureBase::CancelCallback() { 119 void NotifyingAsyncClosureBase::CancelCallback() {
100 // If the callback is triggering when this is called, block the 120 // If the callback is triggering when this is called, block the
101 // destructor of the dying object here by waiting until the callback 121 // destructor of the dying object here by waiting until the callback
102 // is done triggering. 122 // is done triggering.
103 CritScope cs(&crit_); 123 CritScope cs(&crit_);
104 // calling_thread_ == NULL means do not trigger the callback. 124 // calling_thread_ == NULL means do not trigger the callback.
105 calling_thread_ = NULL; 125 calling_thread_ = NULL;
106 } 126 }
107 127
108 } // namespace rtc 128 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698