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 | // Synchronously execute on |thread| all outstanding calls we own |
| 104 | // that are pending on |thread|, and wait for calls to complete |
| 105 | // before returning. Optionally filter by message id. |
| 106 | // The destructor will not wait for outstanding calls, so if that |
| 107 | // behavior is desired, call Flush() before destroying this object. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 108 | void Flush(Thread* thread, uint32_t id = MQID_ANY); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 109 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 110 | private: |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 111 | void OnMessage(Message* msg) override; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 112 | void DoInvoke(const Location& posted_from, |
| 113 | Thread* thread, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 114 | std::unique_ptr<AsyncClosure> closure, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 115 | uint32_t id); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 116 | void DoInvokeDelayed(const Location& posted_from, |
| 117 | Thread* thread, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 118 | std::unique_ptr<AsyncClosure> closure, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 119 | uint32_t delay_ms, |
| 120 | uint32_t id); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 121 | volatile int pending_invocations_ = 0; |
| 122 | Event invocation_complete_; |
deadbeef | ef37ca5 | 2017-05-22 15:32:51 -0700 | [diff] [blame] | 123 | int destroying_ = 0; |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 124 | friend class AsyncClosure; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 125 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 126 | RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 129 | // 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 |
| 131 | // thread in the constructor, and will get notified when that thread is |
| 132 | // 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 |
| 134 | // thread dies, any further calls to AsyncInvoke() will be safely ignored. |
| 135 | class GuardedAsyncInvoker : public sigslot::has_slots<> { |
| 136 | public: |
| 137 | GuardedAsyncInvoker(); |
| 138 | ~GuardedAsyncInvoker() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 139 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 140 | // Synchronously execute all outstanding calls we own, and wait for calls to |
| 141 | // complete before returning. Optionally filter by message id. The destructor |
| 142 | // will not wait for outstanding calls, so if that behavior is desired, call |
| 143 | // Flush() first. Returns false if the thread has died. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 144 | bool Flush(uint32_t id = MQID_ANY); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 145 | |
| 146 | // Call |functor| asynchronously with no callback upon completion. Returns |
| 147 | // immediately. Returns false if the thread has died. |
| 148 | template <class ReturnT, class FunctorT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 149 | bool AsyncInvoke(const Location& posted_from, |
| 150 | const FunctorT& functor, |
| 151 | uint32_t id = 0) { |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 152 | rtc::CritScope cs(&crit_); |
| 153 | if (thread_ == nullptr) |
| 154 | return false; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 155 | invoker_.AsyncInvoke<ReturnT, FunctorT>(posted_from, thread_, functor, id); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 156 | return true; |
| 157 | } |
| 158 | |
| 159 | // Call |functor| asynchronously with |delay_ms|, with no callback upon |
| 160 | // completion. Returns immediately. Returns false if the thread has died. |
| 161 | template <class ReturnT, class FunctorT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 162 | bool AsyncInvokeDelayed(const Location& posted_from, |
| 163 | const FunctorT& functor, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 164 | uint32_t delay_ms, |
| 165 | uint32_t id = 0) { |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 166 | rtc::CritScope cs(&crit_); |
| 167 | if (thread_ == nullptr) |
| 168 | return false; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 169 | invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(posted_from, thread_, |
| 170 | functor, delay_ms, id); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | |
| 174 | // Call |functor| asynchronously, calling |callback| when done. Returns false |
| 175 | // if the thread has died. |
| 176 | template <class ReturnT, class FunctorT, class HostT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 177 | bool AsyncInvoke(const Location& posted_from, |
| 178 | const Location& callback_posted_from, |
| 179 | const FunctorT& functor, |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 180 | void (HostT::*callback)(ReturnT), |
| 181 | HostT* callback_host, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 182 | uint32_t id = 0) { |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 183 | rtc::CritScope cs(&crit_); |
| 184 | if (thread_ == nullptr) |
| 185 | return false; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 186 | invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>( |
| 187 | posted_from, callback_posted_from, thread_, functor, callback, |
| 188 | callback_host, id); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
| 192 | // Call |functor| asynchronously calling |callback| when done. Overloaded for |
| 193 | // void return. Returns false if the thread has died. |
| 194 | template <class ReturnT, class FunctorT, class HostT> |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 195 | bool AsyncInvoke(const Location& posted_from, |
| 196 | const Location& callback_posted_from, |
| 197 | const FunctorT& functor, |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 198 | void (HostT::*callback)(), |
| 199 | HostT* callback_host, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 200 | uint32_t id = 0) { |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 201 | rtc::CritScope cs(&crit_); |
| 202 | if (thread_ == nullptr) |
| 203 | return false; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 204 | invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>( |
| 205 | posted_from, callback_posted_from, thread_, functor, callback, |
| 206 | callback_host, id); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 207 | return true; |
| 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 | |
| 219 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 220 | |
| 221 | #endif // WEBRTC_BASE_ASYNCINVOKER_H_ |