sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_BASE_ASYNCINVOKER_H_ |
| 29 | #define TALK_BASE_ASYNCINVOKER_H_ |
| 30 | |
| 31 | #include "talk/base/asyncinvoker-inl.h" |
| 32 | #include "talk/base/bind.h" |
| 33 | #include "talk/base/scopedptrcollection.h" |
| 34 | #include "talk/base/thread.h" |
| 35 | |
| 36 | namespace talk_base { |
| 37 | |
| 38 | // Invokes function objects (aka functors) asynchronously on a Thread, and |
| 39 | // owns the lifetime of calls (ie, when this object is destroyed, calls in |
| 40 | // flight are cancelled). AsyncInvoker can optionally execute a user-specified |
| 41 | // function when the asynchronous call is complete, or operates in |
| 42 | // fire-and-forget mode otherwise. |
| 43 | // |
| 44 | // AsyncInvoker does not own the thread it calls functors on. |
| 45 | // |
| 46 | // A note about async calls and object lifetimes: users should |
| 47 | // be mindful of object lifetimes when calling functions asynchronously and |
| 48 | // ensure objects used by the function _cannot_ be deleted between the |
| 49 | // invocation and execution of the functor. AsyncInvoker is designed to |
| 50 | // help: any calls in flight will be cancelled when the AsyncInvoker used to |
| 51 | // make the call is destructed, and any calls executing will be allowed to |
| 52 | // complete before AsyncInvoker destructs. |
| 53 | // |
| 54 | // The easiest way to ensure lifetimes are handled correctly is to create a |
| 55 | // class that owns the Thread and AsyncInvoker objects, and then call its |
| 56 | // methods asynchronously as needed. |
| 57 | // |
| 58 | // Example: |
| 59 | // class MyClass { |
| 60 | // public: |
| 61 | // void FireAsyncTaskWithResult(Thread* thread, int x) { |
| 62 | // // Specify a callback to get the result upon completion. |
| 63 | // invoker_.AsyncInvoke<int>( |
| 64 | // thread, Bind(&MyClass::AsyncTaskWithResult, this, x), |
| 65 | // &MyClass::OnTaskComplete, this); |
| 66 | // } |
| 67 | // void FireAnotherAsyncTask(Thread* thread) { |
| 68 | // // No callback specified means fire-and-forget. |
| 69 | // invoker_.AsyncInvoke<void>( |
| 70 | // thread, Bind(&MyClass::AnotherAsyncTask, this)); |
| 71 | // |
| 72 | // private: |
| 73 | // int AsyncTaskWithResult(int x) { |
| 74 | // // Some long running process... |
| 75 | // return x * x; |
| 76 | // } |
| 77 | // void AnotherAsyncTask() { |
| 78 | // // Some other long running process... |
| 79 | // } |
| 80 | // void OnTaskComplete(int result) { result_ = result; } |
| 81 | // |
| 82 | // AsyncInvoker invoker_; |
| 83 | // int result_; |
| 84 | // }; |
| 85 | class AsyncInvoker { |
| 86 | public: |
| 87 | // Call |functor| asynchronously on |thread|, with no callback upon |
| 88 | // completion. Returns immediately. |
| 89 | template <class ReturnT, class FunctorT> |
| 90 | void AsyncInvoke(Thread* thread, |
| 91 | const FunctorT& functor, |
| 92 | uint32 id = 0) { |
| 93 | FunctorMessageHandler<ReturnT, FunctorT>* handler = |
| 94 | new FunctorMessageHandler<ReturnT, FunctorT>(functor); |
| 95 | handler->SetCallback(Bind(&AsyncInvoker::RemoveHandler, this, handler)); |
| 96 | InvokeHandler(thread, handler, id); |
| 97 | } |
| 98 | |
| 99 | // Call |functor| asynchronously on |thread|, calling |callback| when done. |
| 100 | template <class ReturnT, class FunctorT, class HostT> |
| 101 | void AsyncInvoke(Thread* thread, |
| 102 | const FunctorT& functor, |
| 103 | void (HostT::*callback)(ReturnT), |
| 104 | HostT* callback_host, |
| 105 | uint32 id = 0) { |
| 106 | AsyncFunctorMessageHandler<ReturnT, FunctorT>* handler = |
| 107 | new AsyncFunctorMessageHandler<ReturnT, FunctorT>(functor); |
| 108 | handler->WrapCallback( |
| 109 | Bind(&AsyncInvoker::OnAsyncCallCompleted<ReturnT, FunctorT, HostT>, |
| 110 | this, handler, callback, callback_host)); |
| 111 | InvokeHandler(thread, handler, id); |
| 112 | } |
| 113 | |
| 114 | // Call |functor| asynchronously on |thread|, calling |callback| when done. |
| 115 | // Overloaded for void return. |
| 116 | template <class ReturnT, class FunctorT, class HostT> |
| 117 | void AsyncInvoke(Thread* thread, |
| 118 | const FunctorT& functor, |
| 119 | void (HostT::*callback)(), |
| 120 | HostT* callback_host, |
| 121 | uint32 id = 0) { |
| 122 | AsyncFunctorMessageHandler<void, FunctorT>* handler = |
| 123 | new AsyncFunctorMessageHandler<ReturnT, FunctorT>(functor); |
| 124 | handler->WrapCallback( |
| 125 | Bind(&AsyncInvoker::OnAsyncVoidCallCompleted<FunctorT, HostT>, |
| 126 | this, handler, callback, callback_host)); |
| 127 | InvokeHandler(thread, handler, id); |
| 128 | } |
| 129 | |
| 130 | // Synchronously execute on |thread| all outstanding calls we own |
| 131 | // that are pending on |thread|, and wait for calls to complete |
| 132 | // before returning. Optionally filter by message id. |
| 133 | // The destructor will not wait for outstanding calls, so if that |
| 134 | // behavior is desired, call Flush() before destroying this object. |
| 135 | void Flush(Thread* thread, uint32 id = MQID_ANY); |
| 136 | |
| 137 | private: |
| 138 | void InvokeHandler(Thread* thread, MessageHandler* handler, uint32 id); |
| 139 | void RemoveHandler(MessageHandler* handler); |
| 140 | |
| 141 | template <class ReturnT, class FunctorT, class HostT> |
| 142 | void OnAsyncCallCompleted( |
| 143 | AsyncFunctorMessageHandler<ReturnT, FunctorT>* handler, |
| 144 | void (HostT::*callback)(ReturnT), |
| 145 | HostT* callback_host) { |
| 146 | AsyncInvoke<void>(handler->thread(), |
| 147 | Bind(callback, callback_host, handler->result())); |
| 148 | RemoveHandler(handler); |
| 149 | } |
| 150 | |
| 151 | template <class FunctorT, class HostT> |
| 152 | void OnAsyncVoidCallCompleted( |
| 153 | AsyncFunctorMessageHandler<void, FunctorT>* handler, |
| 154 | void (HostT::*callback)(), |
| 155 | HostT* callback_host) { |
| 156 | AsyncInvoke<void>(handler->thread(), Bind(callback, callback_host)); |
| 157 | RemoveHandler(handler); |
| 158 | } |
| 159 | |
| 160 | CriticalSection crit_; |
| 161 | ScopedPtrCollection<MessageHandler> handlers_; |
| 162 | }; |
| 163 | |
| 164 | } // namespace talk_base |
| 165 | |
| 166 | |
| 167 | #endif // TALK_BASE_ASYNCINVOKER_H_ |