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