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" |
| 16 | #include "webrtc/base/sigslot.h" |
| 17 | #include "webrtc/base/scopedptrcollection.h" |
| 18 | #include "webrtc/base/thread.h" |
| 19 | |
| 20 | namespace rtc { |
| 21 | |
| 22 | // Invokes function objects (aka functors) asynchronously on a Thread, and |
| 23 | // owns the lifetime of calls (ie, when this object is destroyed, calls in |
| 24 | // flight are cancelled). AsyncInvoker can optionally execute a user-specified |
| 25 | // function when the asynchronous call is complete, or operates in |
| 26 | // fire-and-forget mode otherwise. |
| 27 | // |
| 28 | // AsyncInvoker does not own the thread it calls functors on. |
| 29 | // |
| 30 | // A note about async calls and object lifetimes: users should |
| 31 | // be mindful of object lifetimes when calling functions asynchronously and |
| 32 | // ensure objects used by the function _cannot_ be deleted between the |
| 33 | // invocation and execution of the functor. AsyncInvoker is designed to |
| 34 | // help: any calls in flight will be cancelled when the AsyncInvoker used to |
| 35 | // make the call is destructed, and any calls executing will be allowed to |
| 36 | // complete before AsyncInvoker destructs. |
| 37 | // |
| 38 | // The easiest way to ensure lifetimes are handled correctly is to create a |
| 39 | // class that owns the Thread and AsyncInvoker objects, and then call its |
| 40 | // methods asynchronously as needed. |
| 41 | // |
| 42 | // Example: |
| 43 | // class MyClass { |
| 44 | // public: |
| 45 | // void FireAsyncTaskWithResult(Thread* thread, int x) { |
| 46 | // // Specify a callback to get the result upon completion. |
| 47 | // invoker_.AsyncInvoke<int>( |
| 48 | // thread, Bind(&MyClass::AsyncTaskWithResult, this, x), |
| 49 | // &MyClass::OnTaskComplete, this); |
| 50 | // } |
| 51 | // void FireAnotherAsyncTask(Thread* thread) { |
| 52 | // // No callback specified means fire-and-forget. |
| 53 | // invoker_.AsyncInvoke<void>( |
| 54 | // thread, Bind(&MyClass::AnotherAsyncTask, this)); |
| 55 | // |
| 56 | // private: |
| 57 | // int AsyncTaskWithResult(int x) { |
| 58 | // // Some long running process... |
| 59 | // return x * x; |
| 60 | // } |
| 61 | // void AnotherAsyncTask() { |
| 62 | // // Some other long running process... |
| 63 | // } |
| 64 | // void OnTaskComplete(int result) { result_ = result; } |
| 65 | // |
| 66 | // AsyncInvoker invoker_; |
| 67 | // int result_; |
| 68 | // }; |
| 69 | class AsyncInvoker : public MessageHandler { |
| 70 | public: |
| 71 | AsyncInvoker(); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 72 | ~AsyncInvoker() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 73 | |
| 74 | // Call |functor| asynchronously on |thread|, with no callback upon |
| 75 | // completion. Returns immediately. |
| 76 | template <class ReturnT, class FunctorT> |
| 77 | void AsyncInvoke(Thread* thread, |
| 78 | const FunctorT& functor, |
| 79 | uint32 id = 0) { |
perkj@webrtc.org | 827d7e8 | 2015-01-29 08:53:45 +0000 | [diff] [blame] | 80 | scoped_refptr<AsyncClosure> closure( |
| 81 | new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 82 | DoInvoke(thread, closure, id); |
| 83 | } |
| 84 | |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 85 | // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback |
| 86 | // upon completion. Returns immediately. |
| 87 | template <class ReturnT, class FunctorT> |
| 88 | void AsyncInvokeDelayed(Thread* thread, |
| 89 | const FunctorT& functor, |
| 90 | uint32 delay_ms, |
| 91 | uint32 id = 0) { |
| 92 | scoped_refptr<AsyncClosure> closure( |
| 93 | new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); |
| 94 | DoInvokeDelayed(thread, closure, delay_ms, id); |
| 95 | } |
| 96 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 97 | // Call |functor| asynchronously on |thread|, calling |callback| when done. |
| 98 | template <class ReturnT, class FunctorT, class HostT> |
| 99 | void AsyncInvoke(Thread* thread, |
| 100 | const FunctorT& functor, |
| 101 | void (HostT::*callback)(ReturnT), |
| 102 | HostT* callback_host, |
| 103 | uint32 id = 0) { |
perkj@webrtc.org | 827d7e8 | 2015-01-29 08:53:45 +0000 | [diff] [blame] | 104 | scoped_refptr<AsyncClosure> closure( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 105 | new RefCountedObject<NotifyingAsyncClosure<ReturnT, FunctorT, HostT> >( |
perkj@webrtc.org | 827d7e8 | 2015-01-29 08:53:45 +0000 | [diff] [blame] | 106 | this, Thread::Current(), functor, callback, callback_host)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 107 | DoInvoke(thread, closure, id); |
| 108 | } |
| 109 | |
| 110 | // Call |functor| asynchronously on |thread|, calling |callback| when done. |
| 111 | // Overloaded for void return. |
| 112 | template <class ReturnT, class FunctorT, class HostT> |
| 113 | void AsyncInvoke(Thread* thread, |
| 114 | const FunctorT& functor, |
| 115 | void (HostT::*callback)(), |
| 116 | HostT* callback_host, |
| 117 | uint32 id = 0) { |
perkj@webrtc.org | 827d7e8 | 2015-01-29 08:53:45 +0000 | [diff] [blame] | 118 | scoped_refptr<AsyncClosure> closure( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | new RefCountedObject<NotifyingAsyncClosure<void, FunctorT, HostT> >( |
perkj@webrtc.org | 827d7e8 | 2015-01-29 08:53:45 +0000 | [diff] [blame] | 120 | this, Thread::Current(), functor, callback, callback_host)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 121 | DoInvoke(thread, closure, id); |
| 122 | } |
| 123 | |
| 124 | // Synchronously execute on |thread| all outstanding calls we own |
| 125 | // that are pending on |thread|, and wait for calls to complete |
| 126 | // before returning. Optionally filter by message id. |
| 127 | // The destructor will not wait for outstanding calls, so if that |
| 128 | // behavior is desired, call Flush() before destroying this object. |
| 129 | void Flush(Thread* thread, uint32 id = MQID_ANY); |
| 130 | |
| 131 | // Signaled when this object is destructed. |
| 132 | sigslot::signal0<> SignalInvokerDestroyed; |
| 133 | |
| 134 | private: |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 135 | void OnMessage(Message* msg) override; |
perkj@webrtc.org | 827d7e8 | 2015-01-29 08:53:45 +0000 | [diff] [blame] | 136 | void DoInvoke(Thread* thread, const scoped_refptr<AsyncClosure>& closure, |
| 137 | uint32 id); |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 138 | void DoInvokeDelayed(Thread* thread, |
| 139 | const scoped_refptr<AsyncClosure>& closure, |
| 140 | uint32 delay_ms, |
| 141 | uint32 id); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | bool destroying_; |
| 143 | |
| 144 | DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); |
| 145 | }; |
| 146 | |
| 147 | } // namespace rtc |
| 148 | |
| 149 | |
| 150 | #endif // WEBRTC_BASE_ASYNCINVOKER_H_ |