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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | webrtc/base/asyncinvoker.cc » ('j') | webrtc/base/asyncinvoker.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/asyncinvoker.h
diff --git a/webrtc/base/asyncinvoker.h b/webrtc/base/asyncinvoker.h
index fe1506d99f8688ca53f92d6f7b3da401dbe84107..cac754b850bbd58cfadcb5956e01a8c7181fd753 100644
--- a/webrtc/base/asyncinvoker.h
+++ b/webrtc/base/asyncinvoker.h
@@ -144,7 +144,78 @@ class AsyncInvoker : public MessageHandler {
DISALLOW_COPY_AND_ASSIGN(AsyncInvoker);
};
-} // namespace rtc
+// Similar to AsyncInvoker, but guards against the Thread being destroyed. It
+// will connect to the current thread in the constructor, and will get notified
+// when that thread is destroyed. After GuardedAsyncInvoker is constructed, it
+// can be used from other threads to post functors to the thread it was
+// constructed on. If that thread dies, any further calls to AsyncInvoke() will
+// 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.
+class GuardedAsyncInvoker : public sigslot::has_slots<> {
+ public:
+ GuardedAsyncInvoker();
+ ~GuardedAsyncInvoker() override;
+
+ // Synchronously execute all outstanding calls we own, and wait for calls to
+ // complete before returning. Optionally filter by message id. The destructor
+ // will not wait for outstanding calls, so if that behavior is desired, call
+ // Flush() first.
+ void Flush(uint32 id = MQID_ANY);
+
+ // Call |functor| asynchronously with no callback upon completion. Returns
+ // immediately.
+ template <class ReturnT, class FunctorT>
+ 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.
+ rtc::CritScope cs(&crit_);
+ if (thread_ != nullptr)
+ invoker_.AsyncInvoke<ReturnT, FunctorT>(thread_, functor, id);
+ }
+ // Call |functor| asynchronously with |delay_ms|, with no callback upon
+ // completion. Returns immediately.
+ template <class ReturnT, class FunctorT>
+ void AsyncInvokeDelayed(const FunctorT& functor,
+ uint32 delay_ms,
+ uint32 id = 0) {
+ rtc::CritScope cs(&crit_);
+ if (thread_ != nullptr)
tommi (sloooow) - chröme 2015/08/19 20:01:54 {} (and below)
magjed_webrtc 2015/08/20 10:28:33 Acknowledged.
+ invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(thread_, functor,
+ delay_ms, id);
+ }
+
+ // Call |functor| asynchronously, calling |callback| when done.
+ template <class ReturnT, class FunctorT, class HostT>
+ void AsyncInvoke(const FunctorT& functor,
+ void (HostT::*callback)(ReturnT),
+ HostT* callback_host,
+ uint32 id = 0) {
+ rtc::CritScope cs(&crit_);
+ if (thread_ != nullptr)
+ invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
+ thread_, functor, callback, callback_host, id);
+ }
+
+ // Call |functor| asynchronously calling |callback| when done. Overloaded for
+ // void return.
+ template <class ReturnT, class FunctorT, class HostT>
+ void AsyncInvoke(const FunctorT& functor,
+ void (HostT::*callback)(),
+ HostT* callback_host,
+ uint32 id = 0) {
+ rtc::CritScope cs(&crit_);
+ if (thread_ != nullptr)
+ invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
+ thread_, functor, callback, callback_host, id);
+ }
+
+ private:
+ // Callback when |thread_| is destroyed.
+ void ThreadDestroyed();
+
+ CriticalSection crit_;
+ Thread* thread_ GUARDED_BY(crit_);
+ AsyncInvoker invoker_ GUARDED_BY(crit_);
+};
+
+} // namespace rtc
#endif // WEBRTC_BASE_ASYNCINVOKER_H_
« 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