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

Side by Side Diff: webrtc/rtc_base/asyncinvoker.h

Issue 2885143005: Fixing race between ~AsyncInvoker and ~AsyncClosure, using ref-counting. (Closed)
Patch Set: Only clear current thread's message queue in destructor loop Created 3 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 | « no previous file | webrtc/rtc_base/asyncinvoker.cc » ('j') | webrtc/rtc_base/asyncinvoker.cc » ('J')
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 #ifndef WEBRTC_RTC_BASE_ASYNCINVOKER_H_ 11 #ifndef WEBRTC_RTC_BASE_ASYNCINVOKER_H_
12 #define WEBRTC_RTC_BASE_ASYNCINVOKER_H_ 12 #define WEBRTC_RTC_BASE_ASYNCINVOKER_H_
13 13
14 #include <memory> 14 #include <memory>
15 #include <utility> 15 #include <utility>
16 16
17 #include "webrtc/rtc_base/asyncinvoker-inl.h" 17 #include "webrtc/rtc_base/asyncinvoker-inl.h"
18 #include "webrtc/rtc_base/bind.h" 18 #include "webrtc/rtc_base/bind.h"
19 #include "webrtc/rtc_base/constructormagic.h" 19 #include "webrtc/rtc_base/constructormagic.h"
20 #include "webrtc/rtc_base/event.h" 20 #include "webrtc/rtc_base/event.h"
21 #include "webrtc/rtc_base/refcountedobject.h"
22 #include "webrtc/rtc_base/scoped_ref_ptr.h"
21 #include "webrtc/rtc_base/sigslot.h" 23 #include "webrtc/rtc_base/sigslot.h"
22 #include "webrtc/rtc_base/thread.h" 24 #include "webrtc/rtc_base/thread.h"
23 25
24 namespace rtc { 26 namespace rtc {
25 27
26 // Invokes function objects (aka functors) asynchronously on a Thread, and 28 // Invokes function objects (aka functors) asynchronously on a Thread, and
27 // owns the lifetime of calls (ie, when this object is destroyed, calls in 29 // owns the lifetime of calls (ie, when this object is destroyed, calls in
28 // flight are cancelled). AsyncInvoker can optionally execute a user-specified 30 // flight are cancelled). AsyncInvoker can optionally execute a user-specified
29 // function when the asynchronous call is complete, or operates in 31 // function when the asynchronous call is complete, or operates in
30 // fire-and-forget mode otherwise. 32 // fire-and-forget mode otherwise.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 void DoInvoke(const Location& posted_from, 114 void DoInvoke(const Location& posted_from,
113 Thread* thread, 115 Thread* thread,
114 std::unique_ptr<AsyncClosure> closure, 116 std::unique_ptr<AsyncClosure> closure,
115 uint32_t id); 117 uint32_t id);
116 void DoInvokeDelayed(const Location& posted_from, 118 void DoInvokeDelayed(const Location& posted_from,
117 Thread* thread, 119 Thread* thread,
118 std::unique_ptr<AsyncClosure> closure, 120 std::unique_ptr<AsyncClosure> closure,
119 uint32_t delay_ms, 121 uint32_t delay_ms,
120 uint32_t id); 122 uint32_t id);
121 volatile int pending_invocations_ = 0; 123 volatile int pending_invocations_ = 0;
122 Event invocation_complete_; 124
123 bool destroying_ = false; 125 // Reference counted so that if the AsyncInvoker destructor finishes before
126 // an AsyncClosure's destructor that's about to call
127 // "invocation_complete_->Set()", it's not dereferenced after being
128 // destroyed.
129 scoped_refptr<RefCountedObject<Event>> invocation_complete_;
kwiberg-webrtc 2017/08/04 09:28:09 Umm... isn't this exactly how scoped_refptr isn't
Taylor Brandstetter 2017/08/04 19:14:22 Sorry, I still don't understand what the problem i
kwiberg-webrtc 2017/08/06 04:17:12 No, it's just me who's confused. The requirements
130
131 // This flag is used to ensure that if an application AsyncInvokes tasks that
132 // recursively AsyncInvoke other tasks ad infinitum, the cycle eventually
133 // terminates.
kwiberg-webrtc 2017/08/04 09:28:09 That's why you need it. Could you also explain wha
134 int destroying_ = 0;
kwiberg-webrtc 2017/08/04 09:28:09 Ooohhh... when reading the .cc file, I found that
Taylor Brandstetter 2017/08/04 19:14:22 Oh, apparently <atomic> is a feature chromium allo
135
124 friend class AsyncClosure; 136 friend class AsyncClosure;
125 137
126 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); 138 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker);
127 }; 139 };
128 140
129 // Similar to AsyncInvoker, but guards against the Thread being destroyed while 141 // Similar to AsyncInvoker, but guards against the Thread being destroyed while
130 // there are outstanding dangling pointers to it. It will connect to the current 142 // there are outstanding dangling pointers to it. It will connect to the current
131 // thread in the constructor, and will get notified when that thread is 143 // thread in the constructor, and will get notified when that thread is
132 // destroyed. After GuardedAsyncInvoker is constructed, it can be used from 144 // destroyed. After GuardedAsyncInvoker is constructed, it can be used from
133 // other threads to post functors to the thread it was constructed on. If that 145 // other threads to post functors to the thread it was constructed on. If that
134 // thread dies, any further calls to AsyncInvoke() will be safely ignored. 146 // thread dies, any further calls to AsyncInvoke() will be safely ignored.
135 class GuardedAsyncInvoker : public sigslot::has_slots<> { 147 class GuardedAsyncInvoker : public sigslot::has_slots<> {
136 public: 148 public:
137 GuardedAsyncInvoker(); 149 GuardedAsyncInvoker();
138 ~GuardedAsyncInvoker() override; 150 ~GuardedAsyncInvoker() override;
139 151
140 // Synchronously execute all outstanding calls we own, and wait for calls to 152 // Synchronously execute all outstanding calls we own, and wait for calls to
141 // complete before returning. Optionally filter by message id. The destructor 153 // complete before returning. Optionally filter by message id. The destructor
142 // will not wait for outstanding calls, so if that behavior is desired, call 154 // will not wait for outstanding calls, so if that behavior is desired, call
143 // Flush() first. Returns false if the thread has died. 155 // Flush() first. Returns false if the thread has died.
144 bool Flush(uint32_t id = MQID_ANY); 156 bool Flush(uint32_t id = MQID_ANY);
145 157
146 // Call |functor| asynchronously with no callback upon completion. Returns 158 // Call |functor| asynchronously with no callback upon completion. Returns
147 // immediately. Returns false if the thread has died. 159 // immediately. Returns false if the thread has died.
148 template <class ReturnT, class FunctorT> 160 template <class ReturnT, class FunctorT>
149 bool AsyncInvoke(const Location& posted_from, 161 bool AsyncInvoke(const Location& posted_from,
150 const FunctorT& functor, 162 const FunctorT& functor,
151 uint32_t id = 0) { 163 uint32_t id = 0) {
152 rtc::CritScope cs(&crit_); 164 CritScope cs(&crit_);
153 if (thread_ == nullptr) 165 if (thread_ == nullptr)
154 return false; 166 return false;
155 invoker_.AsyncInvoke<ReturnT, FunctorT>(posted_from, thread_, functor, id); 167 invoker_.AsyncInvoke<ReturnT, FunctorT>(posted_from, thread_, functor, id);
156 return true; 168 return true;
157 } 169 }
158 170
159 // Call |functor| asynchronously with |delay_ms|, with no callback upon 171 // Call |functor| asynchronously with |delay_ms|, with no callback upon
160 // completion. Returns immediately. Returns false if the thread has died. 172 // completion. Returns immediately. Returns false if the thread has died.
161 template <class ReturnT, class FunctorT> 173 template <class ReturnT, class FunctorT>
162 bool AsyncInvokeDelayed(const Location& posted_from, 174 bool AsyncInvokeDelayed(const Location& posted_from,
163 const FunctorT& functor, 175 const FunctorT& functor,
164 uint32_t delay_ms, 176 uint32_t delay_ms,
165 uint32_t id = 0) { 177 uint32_t id = 0) {
166 rtc::CritScope cs(&crit_); 178 CritScope cs(&crit_);
167 if (thread_ == nullptr) 179 if (thread_ == nullptr)
168 return false; 180 return false;
169 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(posted_from, thread_, 181 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(posted_from, thread_,
170 functor, delay_ms, id); 182 functor, delay_ms, id);
171 return true; 183 return true;
172 } 184 }
173 185
174 // Call |functor| asynchronously, calling |callback| when done. Returns false 186 // Call |functor| asynchronously, calling |callback| when done. Returns false
175 // if the thread has died. 187 // if the thread has died.
176 template <class ReturnT, class FunctorT, class HostT> 188 template <class ReturnT, class FunctorT, class HostT>
177 bool AsyncInvoke(const Location& posted_from, 189 bool AsyncInvoke(const Location& posted_from,
178 const Location& callback_posted_from, 190 const Location& callback_posted_from,
179 const FunctorT& functor, 191 const FunctorT& functor,
180 void (HostT::*callback)(ReturnT), 192 void (HostT::*callback)(ReturnT),
181 HostT* callback_host, 193 HostT* callback_host,
182 uint32_t id = 0) { 194 uint32_t id = 0) {
183 rtc::CritScope cs(&crit_); 195 CritScope cs(&crit_);
184 if (thread_ == nullptr) 196 if (thread_ == nullptr)
185 return false; 197 return false;
186 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>( 198 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
187 posted_from, callback_posted_from, thread_, functor, callback, 199 posted_from, callback_posted_from, thread_, functor, callback,
188 callback_host, id); 200 callback_host, id);
189 return true; 201 return true;
190 } 202 }
191 203
192 // Call |functor| asynchronously calling |callback| when done. Overloaded for 204 // Call |functor| asynchronously calling |callback| when done. Overloaded for
193 // void return. Returns false if the thread has died. 205 // void return. Returns false if the thread has died.
194 template <class ReturnT, class FunctorT, class HostT> 206 template <class ReturnT, class FunctorT, class HostT>
195 bool AsyncInvoke(const Location& posted_from, 207 bool AsyncInvoke(const Location& posted_from,
196 const Location& callback_posted_from, 208 const Location& callback_posted_from,
197 const FunctorT& functor, 209 const FunctorT& functor,
198 void (HostT::*callback)(), 210 void (HostT::*callback)(),
199 HostT* callback_host, 211 HostT* callback_host,
200 uint32_t id = 0) { 212 uint32_t id = 0) {
201 rtc::CritScope cs(&crit_); 213 CritScope cs(&crit_);
202 if (thread_ == nullptr) 214 if (thread_ == nullptr)
203 return false; 215 return false;
204 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>( 216 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
205 posted_from, callback_posted_from, thread_, functor, callback, 217 posted_from, callback_posted_from, thread_, functor, callback,
206 callback_host, id); 218 callback_host, id);
207 return true; 219 return true;
208 } 220 }
209 221
210 private: 222 private:
211 // Callback when |thread_| is destroyed. 223 // Callback when |thread_| is destroyed.
212 void ThreadDestroyed(); 224 void ThreadDestroyed();
213 225
214 CriticalSection crit_; 226 CriticalSection crit_;
215 Thread* thread_ GUARDED_BY(crit_); 227 Thread* thread_ GUARDED_BY(crit_);
216 AsyncInvoker invoker_ GUARDED_BY(crit_); 228 AsyncInvoker invoker_ GUARDED_BY(crit_);
217 }; 229 };
218 230
219 } // namespace rtc 231 } // namespace rtc
220 232
221 #endif // WEBRTC_RTC_BASE_ASYNCINVOKER_H_ 233 #endif // WEBRTC_RTC_BASE_ASYNCINVOKER_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/rtc_base/asyncinvoker.cc » ('j') | webrtc/rtc_base/asyncinvoker.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698