henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_ASYNCINVOKER_H_ |
| 12 | #define WEBRTC_BASE_ASYNCINVOKER_H_ |
| 13 | |
| 14 | #include "webrtc/base/asyncinvoker-inl.h" |
| 15 | #include "webrtc/base/bind.h" |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 16 | #include "webrtc/base/constructormagic.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | #include "webrtc/base/sigslot.h" |
| 18 | #include "webrtc/base/scopedptrcollection.h" |
| 19 | #include "webrtc/base/thread.h" |
| 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | // Invokes function objects (aka functors) asynchronously on a Thread, and |
| 24 | // owns the lifetime of calls (ie, when this object is destroyed, calls in |
| 25 | // flight are cancelled). AsyncInvoker can optionally execute a user-specified |
| 26 | // function when the asynchronous call is complete, or operates in |
| 27 | // fire-and-forget mode otherwise. |
| 28 | // |
| 29 | // AsyncInvoker does not own the thread it calls functors on. |
| 30 | // |
| 31 | // A note about async calls and object lifetimes: users should |
| 32 | // be mindful of object lifetimes when calling functions asynchronously and |
| 33 | // ensure objects used by the function _cannot_ be deleted between the |
| 34 | // invocation and execution of the functor. AsyncInvoker is designed to |
| 35 | // help: any calls in flight will be cancelled when the AsyncInvoker used to |
| 36 | // make the call is destructed, and any calls executing will be allowed to |
| 37 | // complete before AsyncInvoker destructs. |
| 38 | // |
| 39 | // The easiest way to ensure lifetimes are handled correctly is to create a |
| 40 | // class that owns the Thread and AsyncInvoker objects, and then call its |
| 41 | // methods asynchronously as needed. |
| 42 | // |
| 43 | // Example: |
| 44 | // class MyClass { |
| 45 | // public: |
| 46 | // void FireAsyncTaskWithResult(Thread* thread, int x) { |
| 47 | // // Specify a callback to get the result upon completion. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 48 | // invoker_.AsyncInvoke<int>(RTC_FROM_HERE, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 49 | // thread, Bind(&MyClass::AsyncTaskWithResult, this, x), |
| 50 | // &MyClass::OnTaskComplete, this); |
| 51 | // } |
| 52 | // void FireAnotherAsyncTask(Thread* thread) { |
| 53 | // // No callback specified means fire-and-forget. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 54 | // invoker_.AsyncInvoke<void>(RTC_FROM_HERE, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | // thread, Bind(&MyClass::AnotherAsyncTask, this)); |
| 56 | // |
| 57 | // private: |
| 58 | // int AsyncTaskWithResult(int x) { |
| 59 | // // Some long running process... |
| 60 | // return x * x; |
| 61 | // } |
| 62 | // void AnotherAsyncTask() { |
| 63 | // // Some other long running process... |
| 64 | // } |
| 65 | // void OnTaskComplete(int result) { result_ = result; } |
| 66 | // |
| 67 | // AsyncInvoker invoker_; |
| 68 | // int result_; |
| 69 | // }; |
| 70 | class AsyncInvoker : public MessageHandler { |
| 71 | public: |
| 72 | AsyncInvoker(); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 73 | ~AsyncInvoker() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | |
| 75 | // Call |functor| asynchronously on |thread|, with no callback upon |
| 76 | // completion. Returns immediately. |
| 77 | template <class ReturnT, class FunctorT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 78 | void AsyncInvoke(const Location& posted_from, |
| 79 | Thread* thread, |
| 80 | const FunctorT& functor, |
| 81 | uint32_t id = 0) { |
perkj@webrtc.org | 827d7e8 | 2015-01-29 08:53:45 +0000 | [diff] [blame] | 82 | scoped_refptr<AsyncClosure> closure( |
| 83 | new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 84 | DoInvoke(posted_from, thread, closure, id); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 87 | // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback |
| 88 | // upon completion. Returns immediately. |
| 89 | template <class ReturnT, class FunctorT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 90 | void AsyncInvokeDelayed(const Location& posted_from, |
| 91 | Thread* thread, |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 92 | const FunctorT& functor, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 93 | uint32_t delay_ms, |
| 94 | uint32_t id = 0) { |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 95 | scoped_refptr<AsyncClosure> closure( |
| 96 | new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 97 | DoInvokeDelayed(posted_from, thread, closure, delay_ms, id); |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 98 | } |
| 99 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | // Call |functor| asynchronously on |thread|, calling |callback| when done. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 101 | // Uses a separate Location for |callback_posted_from| so that the functor |
| 102 | // invoke and the callback invoke can be differentiated. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | template <class ReturnT, class FunctorT, class HostT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 104 | void AsyncInvoke(const Location& posted_from, |
| 105 | const Location& callback_posted_from, |
| 106 | Thread* thread, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 107 | const FunctorT& functor, |
| 108 | void (HostT::*callback)(ReturnT), |
| 109 | HostT* callback_host, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 110 | uint32_t id = 0) { |
perkj@webrtc.org | 827d7e8 | 2015-01-29 08:53:45 +0000 | [diff] [blame] | 111 | scoped_refptr<AsyncClosure> closure( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 112 | new RefCountedObject<NotifyingAsyncClosure<ReturnT, FunctorT, HostT> >( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 113 | this, callback_posted_from, Thread::Current(), functor, callback, |
| 114 | callback_host)); |
| 115 | DoInvoke(posted_from, thread, closure, id); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Call |functor| asynchronously on |thread|, calling |callback| when done. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 119 | // Uses a separate Location for |callback_posted_from| so that the functor |
| 120 | // invoke and the callback invoke can be differentiated. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 121 | // Overloaded for void return. |
| 122 | template <class ReturnT, class FunctorT, class HostT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 123 | void AsyncInvoke(const Location& posted_from, |
| 124 | const Location& callback_posted_from, |
| 125 | Thread* thread, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | const FunctorT& functor, |
| 127 | void (HostT::*callback)(), |
| 128 | HostT* callback_host, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 129 | uint32_t id = 0) { |
perkj@webrtc.org | 827d7e8 | 2015-01-29 08:53:45 +0000 | [diff] [blame] | 130 | scoped_refptr<AsyncClosure> closure( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 131 | new RefCountedObject<NotifyingAsyncClosure<void, FunctorT, HostT> >( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 132 | this, callback_posted_from, Thread::Current(), functor, callback, |
| 133 | callback_host)); |
| 134 | DoInvoke(posted_from, thread, closure, id); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Synchronously execute on |thread| all outstanding calls we own |
| 138 | // that are pending on |thread|, and wait for calls to complete |
| 139 | // before returning. Optionally filter by message id. |
| 140 | // The destructor will not wait for outstanding calls, so if that |
| 141 | // behavior is desired, call Flush() before destroying this object. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 142 | void Flush(Thread* thread, uint32_t id = MQID_ANY); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | |
| 144 | // Signaled when this object is destructed. |
| 145 | sigslot::signal0<> SignalInvokerDestroyed; |
| 146 | |
| 147 | private: |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 148 | void OnMessage(Message* msg) override; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 149 | void DoInvoke(const Location& posted_from, |
| 150 | Thread* thread, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 151 | const scoped_refptr<AsyncClosure>& closure, |
| 152 | uint32_t id); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 153 | void DoInvokeDelayed(const Location& posted_from, |
| 154 | Thread* thread, |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 155 | const scoped_refptr<AsyncClosure>& closure, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 156 | uint32_t delay_ms, |
| 157 | uint32_t id); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 158 | bool destroying_; |
| 159 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 160 | RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 163 | // Similar to AsyncInvoker, but guards against the Thread being destroyed while |
| 164 | // there are outstanding dangling pointers to it. It will connect to the current |
| 165 | // thread in the constructor, and will get notified when that thread is |
| 166 | // destroyed. After GuardedAsyncInvoker is constructed, it can be used from |
| 167 | // other threads to post functors to the thread it was constructed on. If that |
| 168 | // thread dies, any further calls to AsyncInvoke() will be safely ignored. |
| 169 | class GuardedAsyncInvoker : public sigslot::has_slots<> { |
| 170 | public: |
| 171 | GuardedAsyncInvoker(); |
| 172 | ~GuardedAsyncInvoker() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 173 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 174 | // Synchronously execute all outstanding calls we own, and wait for calls to |
| 175 | // complete before returning. Optionally filter by message id. The destructor |
| 176 | // will not wait for outstanding calls, so if that behavior is desired, call |
| 177 | // Flush() first. Returns false if the thread has died. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 178 | bool Flush(uint32_t id = MQID_ANY); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 179 | |
| 180 | // Call |functor| asynchronously with no callback upon completion. Returns |
| 181 | // immediately. Returns false if the thread has died. |
| 182 | template <class ReturnT, class FunctorT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 183 | bool AsyncInvoke(const Location& posted_from, |
| 184 | const FunctorT& functor, |
| 185 | uint32_t id = 0) { |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 186 | rtc::CritScope cs(&crit_); |
| 187 | if (thread_ == nullptr) |
| 188 | return false; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 189 | invoker_.AsyncInvoke<ReturnT, FunctorT>(posted_from, thread_, functor, id); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
| 193 | // Call |functor| asynchronously with |delay_ms|, with no callback upon |
| 194 | // completion. Returns immediately. Returns false if the thread has died. |
| 195 | template <class ReturnT, class FunctorT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 196 | bool AsyncInvokeDelayed(const Location& posted_from, |
| 197 | const FunctorT& functor, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 198 | uint32_t delay_ms, |
| 199 | uint32_t id = 0) { |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 200 | rtc::CritScope cs(&crit_); |
| 201 | if (thread_ == nullptr) |
| 202 | return false; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 203 | invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(posted_from, thread_, |
| 204 | functor, delay_ms, id); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | |
| 208 | // Call |functor| asynchronously, calling |callback| when done. Returns false |
| 209 | // if the thread has died. |
| 210 | template <class ReturnT, class FunctorT, class HostT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 211 | bool AsyncInvoke(const Location& posted_from, |
| 212 | const Location& callback_posted_from, |
| 213 | const FunctorT& functor, |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 214 | void (HostT::*callback)(ReturnT), |
| 215 | HostT* callback_host, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 216 | uint32_t id = 0) { |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 217 | rtc::CritScope cs(&crit_); |
| 218 | if (thread_ == nullptr) |
| 219 | return false; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 220 | invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>( |
| 221 | posted_from, callback_posted_from, thread_, functor, callback, |
| 222 | callback_host, id); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 223 | return true; |
| 224 | } |
| 225 | |
| 226 | // Call |functor| asynchronously calling |callback| when done. Overloaded for |
| 227 | // void return. Returns false if the thread has died. |
| 228 | template <class ReturnT, class FunctorT, class HostT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 229 | bool AsyncInvoke(const Location& posted_from, |
| 230 | const Location& callback_posted_from, |
| 231 | const FunctorT& functor, |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 232 | void (HostT::*callback)(), |
| 233 | HostT* callback_host, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 234 | uint32_t id = 0) { |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 235 | rtc::CritScope cs(&crit_); |
| 236 | if (thread_ == nullptr) |
| 237 | return false; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 238 | invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>( |
| 239 | posted_from, callback_posted_from, thread_, functor, callback, |
| 240 | callback_host, id); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | |
| 244 | private: |
| 245 | // Callback when |thread_| is destroyed. |
| 246 | void ThreadDestroyed(); |
| 247 | |
| 248 | CriticalSection crit_; |
| 249 | Thread* thread_ GUARDED_BY(crit_); |
| 250 | AsyncInvoker invoker_ GUARDED_BY(crit_); |
| 251 | }; |
| 252 | |
| 253 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 254 | |
| 255 | #endif // WEBRTC_BASE_ASYNCINVOKER_H_ |