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