| 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 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 // Base class for closures that may trigger a callback for the calling thread. | 52 // Base class for closures that may trigger a callback for the calling thread. |
| 53 // Listens for the "destroyed" signals from the calling thread and the invoker, | 53 // Listens for the "destroyed" signals from the calling thread and the invoker, |
| 54 // and cancels the callback to the calling thread if either is destroyed. | 54 // and cancels the callback to the calling thread if either is destroyed. |
| 55 class NotifyingAsyncClosureBase : public AsyncClosure, | 55 class NotifyingAsyncClosureBase : public AsyncClosure, |
| 56 public sigslot::has_slots<> { | 56 public sigslot::has_slots<> { |
| 57 public: | 57 public: |
| 58 ~NotifyingAsyncClosureBase() override; | 58 ~NotifyingAsyncClosureBase() override; |
| 59 | 59 |
| 60 protected: | 60 protected: |
| 61 NotifyingAsyncClosureBase(AsyncInvoker* invoker, Thread* calling_thread); | 61 NotifyingAsyncClosureBase(AsyncInvoker* invoker, |
| 62 const Location& callback_posted_from, |
| 63 Thread* calling_thread); |
| 62 void TriggerCallback(); | 64 void TriggerCallback(); |
| 63 void SetCallback(const Callback0<void>& callback) { | 65 void SetCallback(const Callback0<void>& callback) { |
| 64 CritScope cs(&crit_); | 66 CritScope cs(&crit_); |
| 65 callback_ = callback; | 67 callback_ = callback; |
| 66 } | 68 } |
| 67 bool CallbackCanceled() const { return calling_thread_ == NULL; } | 69 bool CallbackCanceled() const { return calling_thread_ == NULL; } |
| 68 | 70 |
| 69 private: | 71 private: |
| 72 AsyncInvoker* invoker_; |
| 73 Location callback_posted_from_; |
| 70 Callback0<void> callback_; | 74 Callback0<void> callback_; |
| 71 CriticalSection crit_; | 75 CriticalSection crit_; |
| 72 AsyncInvoker* invoker_; | |
| 73 Thread* calling_thread_; | 76 Thread* calling_thread_; |
| 74 | 77 |
| 75 void CancelCallback(); | 78 void CancelCallback(); |
| 76 }; | 79 }; |
| 77 | 80 |
| 78 // Closures that have a non-void return value and require a callback. | 81 // Closures that have a non-void return value and require a callback. |
| 79 template <class ReturnT, class FunctorT, class HostT> | 82 template <class ReturnT, class FunctorT, class HostT> |
| 80 class NotifyingAsyncClosure : public NotifyingAsyncClosureBase { | 83 class NotifyingAsyncClosure : public NotifyingAsyncClosureBase { |
| 81 public: | 84 public: |
| 82 NotifyingAsyncClosure(AsyncInvoker* invoker, | 85 NotifyingAsyncClosure(AsyncInvoker* invoker, |
| 86 const Location& callback_posted_from, |
| 83 Thread* calling_thread, | 87 Thread* calling_thread, |
| 84 const FunctorT& functor, | 88 const FunctorT& functor, |
| 85 void (HostT::*callback)(ReturnT), | 89 void (HostT::*callback)(ReturnT), |
| 86 HostT* callback_host) | 90 HostT* callback_host) |
| 87 : NotifyingAsyncClosureBase(invoker, calling_thread), | 91 : NotifyingAsyncClosureBase(invoker, |
| 88 functor_(functor), | 92 callback_posted_from, |
| 89 callback_(callback), | 93 calling_thread), |
| 90 callback_host_(callback_host) {} | 94 functor_(functor), |
| 95 callback_(callback), |
| 96 callback_host_(callback_host) {} |
| 91 virtual void Execute() { | 97 virtual void Execute() { |
| 92 ReturnT result = functor_(); | 98 ReturnT result = functor_(); |
| 93 if (!CallbackCanceled()) { | 99 if (!CallbackCanceled()) { |
| 94 SetCallback(Callback0<void>(Bind(callback_, callback_host_, result))); | 100 SetCallback(Callback0<void>(Bind(callback_, callback_host_, result))); |
| 95 TriggerCallback(); | 101 TriggerCallback(); |
| 96 } | 102 } |
| 97 } | 103 } |
| 98 | 104 |
| 99 private: | 105 private: |
| 100 FunctorT functor_; | 106 FunctorT functor_; |
| 101 void (HostT::*callback_)(ReturnT); | 107 void (HostT::*callback_)(ReturnT); |
| 102 HostT* callback_host_; | 108 HostT* callback_host_; |
| 103 }; | 109 }; |
| 104 | 110 |
| 105 // Closures that have a void return value and require a callback. | 111 // Closures that have a void return value and require a callback. |
| 106 template <class FunctorT, class HostT> | 112 template <class FunctorT, class HostT> |
| 107 class NotifyingAsyncClosure<void, FunctorT, HostT> | 113 class NotifyingAsyncClosure<void, FunctorT, HostT> |
| 108 : public NotifyingAsyncClosureBase { | 114 : public NotifyingAsyncClosureBase { |
| 109 public: | 115 public: |
| 110 NotifyingAsyncClosure(AsyncInvoker* invoker, | 116 NotifyingAsyncClosure(AsyncInvoker* invoker, |
| 117 const Location& callback_posted_from, |
| 111 Thread* calling_thread, | 118 Thread* calling_thread, |
| 112 const FunctorT& functor, | 119 const FunctorT& functor, |
| 113 void (HostT::*callback)(), | 120 void (HostT::*callback)(), |
| 114 HostT* callback_host) | 121 HostT* callback_host) |
| 115 : NotifyingAsyncClosureBase(invoker, calling_thread), | 122 : NotifyingAsyncClosureBase(invoker, |
| 123 callback_posted_from, |
| 124 calling_thread), |
| 116 functor_(functor) { | 125 functor_(functor) { |
| 117 SetCallback(Callback0<void>(Bind(callback, callback_host))); | 126 SetCallback(Callback0<void>(Bind(callback, callback_host))); |
| 118 } | 127 } |
| 119 virtual void Execute() { | 128 virtual void Execute() { |
| 120 functor_(); | 129 functor_(); |
| 121 TriggerCallback(); | 130 TriggerCallback(); |
| 122 } | 131 } |
| 123 | 132 |
| 124 private: | 133 private: |
| 125 FunctorT functor_; | 134 FunctorT functor_; |
| 126 }; | 135 }; |
| 127 | 136 |
| 128 } // namespace rtc | 137 } // namespace rtc |
| 129 | 138 |
| 130 #endif // WEBRTC_BASE_ASYNCINVOKER_INL_H_ | 139 #endif // WEBRTC_BASE_ASYNCINVOKER_INL_H_ |
| OLD | NEW |