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