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_INL_H_ |
| 12 | #define WEBRTC_BASE_ASYNCINVOKER_INL_H_ |
| 13 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 14 | #include "webrtc/base/atomicops.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #include "webrtc/base/bind.h" |
| 16 | #include "webrtc/base/callback.h" |
| 17 | #include "webrtc/base/criticalsection.h" |
| 18 | #include "webrtc/base/messagehandler.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | #include "webrtc/base/sigslot.h" |
| 20 | #include "webrtc/base/thread.h" |
| 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | class AsyncInvoker; |
| 25 | |
| 26 | // Helper class for AsyncInvoker. Runs a task and triggers a callback |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 27 | // on the calling thread if necessary. |
| 28 | class AsyncClosure { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | public: |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 30 | explicit AsyncClosure(AsyncInvoker* invoker) : invoker_(invoker) {} |
| 31 | virtual ~AsyncClosure(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | // Runs the asynchronous task, and triggers a callback to the calling |
| 33 | // thread if needed. Should be called from the target thread. |
| 34 | virtual void Execute() = 0; |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 35 | |
| 36 | protected: |
| 37 | AsyncInvoker* invoker_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | // Simple closure that doesn't trigger a callback for the calling thread. |
| 41 | template <class FunctorT> |
| 42 | class FireAndForgetAsyncClosure : public AsyncClosure { |
| 43 | public: |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 44 | explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker, |
| 45 | const FunctorT& functor) |
| 46 | : AsyncClosure(invoker), functor_(functor) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | virtual void Execute() { |
| 48 | functor_(); |
| 49 | } |
| 50 | private: |
| 51 | FunctorT functor_; |
| 52 | }; |
| 53 | |
| 54 | // Base class for closures that may trigger a callback for the calling thread. |
| 55 | // Listens for the "destroyed" signals from the calling thread and the invoker, |
| 56 | // and cancels the callback to the calling thread if either is destroyed. |
| 57 | class NotifyingAsyncClosureBase : public AsyncClosure, |
| 58 | public sigslot::has_slots<> { |
| 59 | public: |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 60 | ~NotifyingAsyncClosureBase() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 61 | |
| 62 | protected: |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 63 | NotifyingAsyncClosureBase(AsyncInvoker* invoker, |
| 64 | const Location& callback_posted_from, |
| 65 | Thread* calling_thread); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | void TriggerCallback(); |
| 67 | void SetCallback(const Callback0<void>& callback) { |
| 68 | CritScope cs(&crit_); |
| 69 | callback_ = callback; |
| 70 | } |
| 71 | bool CallbackCanceled() const { return calling_thread_ == NULL; } |
| 72 | |
| 73 | private: |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 74 | Location callback_posted_from_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 75 | Callback0<void> callback_; |
| 76 | CriticalSection crit_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 77 | Thread* calling_thread_; |
| 78 | |
| 79 | void CancelCallback(); |
| 80 | }; |
| 81 | |
| 82 | // Closures that have a non-void return value and require a callback. |
| 83 | template <class ReturnT, class FunctorT, class HostT> |
| 84 | class NotifyingAsyncClosure : public NotifyingAsyncClosureBase { |
| 85 | public: |
| 86 | NotifyingAsyncClosure(AsyncInvoker* invoker, |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 87 | const Location& callback_posted_from, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | Thread* calling_thread, |
| 89 | const FunctorT& functor, |
| 90 | void (HostT::*callback)(ReturnT), |
| 91 | HostT* callback_host) |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 92 | : NotifyingAsyncClosureBase(invoker, |
| 93 | callback_posted_from, |
| 94 | calling_thread), |
| 95 | functor_(functor), |
| 96 | callback_(callback), |
| 97 | callback_host_(callback_host) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 98 | virtual void Execute() { |
| 99 | ReturnT result = functor_(); |
| 100 | if (!CallbackCanceled()) { |
| 101 | SetCallback(Callback0<void>(Bind(callback_, callback_host_, result))); |
| 102 | TriggerCallback(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | FunctorT functor_; |
| 108 | void (HostT::*callback_)(ReturnT); |
| 109 | HostT* callback_host_; |
| 110 | }; |
| 111 | |
| 112 | // Closures that have a void return value and require a callback. |
| 113 | template <class FunctorT, class HostT> |
| 114 | class NotifyingAsyncClosure<void, FunctorT, HostT> |
| 115 | : public NotifyingAsyncClosureBase { |
| 116 | public: |
| 117 | NotifyingAsyncClosure(AsyncInvoker* invoker, |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 118 | const Location& callback_posted_from, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | Thread* calling_thread, |
| 120 | const FunctorT& functor, |
| 121 | void (HostT::*callback)(), |
| 122 | HostT* callback_host) |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 123 | : NotifyingAsyncClosureBase(invoker, |
| 124 | callback_posted_from, |
| 125 | calling_thread), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | functor_(functor) { |
| 127 | SetCallback(Callback0<void>(Bind(callback, callback_host))); |
| 128 | } |
| 129 | virtual void Execute() { |
| 130 | functor_(); |
| 131 | TriggerCallback(); |
| 132 | } |
| 133 | |
| 134 | private: |
| 135 | FunctorT functor_; |
| 136 | }; |
| 137 | |
| 138 | } // namespace rtc |
| 139 | |
| 140 | #endif // WEBRTC_BASE_ASYNCINVOKER_INL_H_ |