| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_BASE_ASYNCINVOKER_INL_H_ | 11 #ifndef WEBRTC_BASE_ASYNCINVOKER_INL_H_ |
| 12 #define WEBRTC_BASE_ASYNCINVOKER_INL_H_ | 12 #define WEBRTC_BASE_ASYNCINVOKER_INL_H_ |
| 13 | 13 |
| 14 #include "webrtc/base/atomicops.h" |
| 14 #include "webrtc/base/bind.h" | 15 #include "webrtc/base/bind.h" |
| 15 #include "webrtc/base/callback.h" | 16 #include "webrtc/base/callback.h" |
| 16 #include "webrtc/base/criticalsection.h" | 17 #include "webrtc/base/criticalsection.h" |
| 17 #include "webrtc/base/messagehandler.h" | 18 #include "webrtc/base/messagehandler.h" |
| 18 #include "webrtc/base/sigslot.h" | 19 #include "webrtc/base/sigslot.h" |
| 19 #include "webrtc/base/thread.h" | 20 #include "webrtc/base/thread.h" |
| 20 | 21 |
| 21 namespace rtc { | 22 namespace rtc { |
| 22 | 23 |
| 23 class AsyncInvoker; | 24 class AsyncInvoker; |
| 24 | 25 |
| 25 // Helper class for AsyncInvoker. Runs a task and triggers a callback | 26 // Helper class for AsyncInvoker. Runs a task and triggers a callback |
| 26 // on the calling thread if necessary. | 27 // on the calling thread if necessary. |
| 27 class AsyncClosure { | 28 class AsyncClosure { |
| 28 public: | 29 public: |
| 29 virtual ~AsyncClosure() {} | 30 explicit AsyncClosure(AsyncInvoker* invoker) : invoker_(invoker) {} |
| 31 virtual ~AsyncClosure(); |
| 30 // Runs the asynchronous task, and triggers a callback to the calling | 32 // Runs the asynchronous task, and triggers a callback to the calling |
| 31 // thread if needed. Should be called from the target thread. | 33 // thread if needed. Should be called from the target thread. |
| 32 virtual void Execute() = 0; | 34 virtual void Execute() = 0; |
| 35 |
| 36 protected: |
| 37 AsyncInvoker* invoker_; |
| 33 }; | 38 }; |
| 34 | 39 |
| 35 // Simple closure that doesn't trigger a callback for the calling thread. | 40 // Simple closure that doesn't trigger a callback for the calling thread. |
| 36 template <class FunctorT> | 41 template <class FunctorT> |
| 37 class FireAndForgetAsyncClosure : public AsyncClosure { | 42 class FireAndForgetAsyncClosure : public AsyncClosure { |
| 38 public: | 43 public: |
| 39 explicit FireAndForgetAsyncClosure(const FunctorT& functor) | 44 explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker, |
| 40 : functor_(functor) {} | 45 const FunctorT& functor) |
| 46 : AsyncClosure(invoker), functor_(functor) {} |
| 41 virtual void Execute() { | 47 virtual void Execute() { |
| 42 functor_(); | 48 functor_(); |
| 43 } | 49 } |
| 44 private: | 50 private: |
| 45 FunctorT functor_; | 51 FunctorT functor_; |
| 46 }; | 52 }; |
| 47 | 53 |
| 48 // Base class for closures that may trigger a callback for the calling thread. | 54 // 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, | 55 // 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. | 56 // and cancels the callback to the calling thread if either is destroyed. |
| 51 class NotifyingAsyncClosureBase : public AsyncClosure, | 57 class NotifyingAsyncClosureBase : public AsyncClosure, |
| 52 public sigslot::has_slots<> { | 58 public sigslot::has_slots<> { |
| 53 public: | 59 public: |
| 54 ~NotifyingAsyncClosureBase() override; | 60 ~NotifyingAsyncClosureBase() override; |
| 55 | 61 |
| 56 protected: | 62 protected: |
| 57 NotifyingAsyncClosureBase(AsyncInvoker* invoker, | 63 NotifyingAsyncClosureBase(AsyncInvoker* invoker, |
| 58 const Location& callback_posted_from, | 64 const Location& callback_posted_from, |
| 59 Thread* calling_thread); | 65 Thread* calling_thread); |
| 60 void TriggerCallback(); | 66 void TriggerCallback(); |
| 61 void SetCallback(const Callback0<void>& callback) { | 67 void SetCallback(const Callback0<void>& callback) { |
| 62 CritScope cs(&crit_); | 68 CritScope cs(&crit_); |
| 63 callback_ = callback; | 69 callback_ = callback; |
| 64 } | 70 } |
| 65 bool CallbackCanceled() const { return calling_thread_ == NULL; } | 71 bool CallbackCanceled() const { return calling_thread_ == NULL; } |
| 66 | 72 |
| 67 private: | 73 private: |
| 68 AsyncInvoker* invoker_; | |
| 69 Location callback_posted_from_; | 74 Location callback_posted_from_; |
| 70 Callback0<void> callback_; | 75 Callback0<void> callback_; |
| 71 CriticalSection crit_; | 76 CriticalSection crit_; |
| 72 Thread* calling_thread_; | 77 Thread* calling_thread_; |
| 73 | 78 |
| 74 void CancelCallback(); | 79 void CancelCallback(); |
| 75 }; | 80 }; |
| 76 | 81 |
| 77 // Closures that have a non-void return value and require a callback. | 82 // Closures that have a non-void return value and require a callback. |
| 78 template <class ReturnT, class FunctorT, class HostT> | 83 template <class ReturnT, class FunctorT, class HostT> |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 TriggerCallback(); | 131 TriggerCallback(); |
| 127 } | 132 } |
| 128 | 133 |
| 129 private: | 134 private: |
| 130 FunctorT functor_; | 135 FunctorT functor_; |
| 131 }; | 136 }; |
| 132 | 137 |
| 133 } // namespace rtc | 138 } // namespace rtc |
| 134 | 139 |
| 135 #endif // WEBRTC_BASE_ASYNCINVOKER_INL_H_ | 140 #endif // WEBRTC_BASE_ASYNCINVOKER_INL_H_ |
| OLD | NEW |