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

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

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
« no previous file with comments | « no previous file | webrtc/base/asyncinvoker.cc » ('j') | webrtc/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
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 uint32 id); 137 uint32 id);
138 void DoInvokeDelayed(Thread* thread, 138 void DoInvokeDelayed(Thread* thread,
139 const scoped_refptr<AsyncClosure>& closure, 139 const scoped_refptr<AsyncClosure>& closure,
140 uint32 delay_ms, 140 uint32 delay_ms,
141 uint32 id); 141 uint32 id);
142 bool destroying_; 142 bool destroying_;
143 143
144 DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); 144 DISALLOW_COPY_AND_ASSIGN(AsyncInvoker);
145 }; 145 };
146 146
147 // Similar to AsyncInvoker, but guards against the Thread being destroyed. It
148 // will connect to the current thread in the constructor, and will get notified
149 // when that thread is destroyed. After GuardedAsyncInvoker is constructed, it
150 // can be used from other threads to post functors to the thread it was
151 // constructed on. If that thread dies, any further calls to AsyncInvoke() will
152 // be safely ignored.
tommi (sloooow) - chröme 2015/08/19 20:01:54 Good comment. Can we add something like "...being
magjed_webrtc 2015/08/20 10:28:33 Done.
153 class GuardedAsyncInvoker : public sigslot::has_slots<> {
154 public:
155 GuardedAsyncInvoker();
156 ~GuardedAsyncInvoker() override;
157
158 // Synchronously execute all outstanding calls we own, and wait for calls to
159 // complete before returning. Optionally filter by message id. The destructor
160 // will not wait for outstanding calls, so if that behavior is desired, call
161 // Flush() first.
162 void Flush(uint32 id = MQID_ANY);
163
164 // Call |functor| asynchronously with no callback upon completion. Returns
165 // immediately.
166 template <class ReturnT, class FunctorT>
167 void AsyncInvoke(const FunctorT& functor, uint32 id = 0) {
tommi (sloooow) - chröme 2015/08/19 20:01:54 what about making this method return bool and retu
magjed_webrtc 2015/08/20 10:28:33 Done.
168 rtc::CritScope cs(&crit_);
169 if (thread_ != nullptr)
170 invoker_.AsyncInvoke<ReturnT, FunctorT>(thread_, functor, id);
171 }
172
173 // Call |functor| asynchronously with |delay_ms|, with no callback upon
174 // completion. Returns immediately.
175 template <class ReturnT, class FunctorT>
176 void AsyncInvokeDelayed(const FunctorT& functor,
177 uint32 delay_ms,
178 uint32 id = 0) {
179 rtc::CritScope cs(&crit_);
180 if (thread_ != nullptr)
tommi (sloooow) - chröme 2015/08/19 20:01:54 {} (and below)
magjed_webrtc 2015/08/20 10:28:33 Acknowledged.
181 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(thread_, functor,
182 delay_ms, id);
183 }
184
185 // Call |functor| asynchronously, calling |callback| when done.
186 template <class ReturnT, class FunctorT, class HostT>
187 void AsyncInvoke(const FunctorT& functor,
188 void (HostT::*callback)(ReturnT),
189 HostT* callback_host,
190 uint32 id = 0) {
191 rtc::CritScope cs(&crit_);
192 if (thread_ != nullptr)
193 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
194 thread_, functor, callback, callback_host, id);
195 }
196
197 // Call |functor| asynchronously calling |callback| when done. Overloaded for
198 // void return.
199 template <class ReturnT, class FunctorT, class HostT>
200 void AsyncInvoke(const FunctorT& functor,
201 void (HostT::*callback)(),
202 HostT* callback_host,
203 uint32 id = 0) {
204 rtc::CritScope cs(&crit_);
205 if (thread_ != nullptr)
206 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
207 thread_, functor, callback, callback_host, id);
208 }
209
210 private:
211 // Callback when |thread_| is destroyed.
212 void ThreadDestroyed();
213
214 CriticalSection crit_;
215 Thread* thread_ GUARDED_BY(crit_);
216 AsyncInvoker invoker_ GUARDED_BY(crit_);
217 };
218
147 } // namespace rtc 219 } // namespace rtc
148 220
149
150 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ 221 #endif // WEBRTC_BASE_ASYNCINVOKER_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/asyncinvoker.cc » ('j') | webrtc/base/asyncinvoker.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698