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/atomicops.h" |
15 #include "webrtc/base/bind.h" | 15 #include "webrtc/base/bind.h" |
16 #include "webrtc/base/callback.h" | |
17 #include "webrtc/base/criticalsection.h" | 16 #include "webrtc/base/criticalsection.h" |
18 #include "webrtc/base/messagehandler.h" | 17 #include "webrtc/base/messagehandler.h" |
19 #include "webrtc/base/sigslot.h" | 18 #include "webrtc/base/sigslot.h" |
20 #include "webrtc/base/thread.h" | 19 #include "webrtc/base/thread.h" |
21 #include "webrtc/base/thread_annotations.h" | 20 #include "webrtc/base/thread_annotations.h" |
22 | 21 |
23 namespace rtc { | 22 namespace rtc { |
24 | 23 |
25 class AsyncInvoker; | 24 class AsyncInvoker; |
26 | 25 |
(...skipping 18 matching lines...) Expand all Loading... |
45 explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker, | 44 explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker, |
46 const FunctorT& functor) | 45 const FunctorT& functor) |
47 : AsyncClosure(invoker), functor_(functor) {} | 46 : AsyncClosure(invoker), functor_(functor) {} |
48 virtual void Execute() { | 47 virtual void Execute() { |
49 functor_(); | 48 functor_(); |
50 } | 49 } |
51 private: | 50 private: |
52 FunctorT functor_; | 51 FunctorT functor_; |
53 }; | 52 }; |
54 | 53 |
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: | |
61 ~NotifyingAsyncClosureBase() override; | |
62 | |
63 protected: | |
64 NotifyingAsyncClosureBase(AsyncInvoker* invoker, | |
65 const Location& callback_posted_from, | |
66 Thread* calling_thread); | |
67 void TriggerCallback(); | |
68 void SetCallback(const Callback0<void>& callback) { | |
69 CritScope cs(&crit_); | |
70 callback_ = callback; | |
71 } | |
72 bool CallbackCanceled() const { | |
73 CritScope cs(&crit_); | |
74 return calling_thread_ == nullptr; | |
75 } | |
76 | |
77 private: | |
78 Location callback_posted_from_; | |
79 CriticalSection crit_; | |
80 Callback0<void> callback_ GUARDED_BY(crit_); | |
81 Thread* calling_thread_ GUARDED_BY(crit_); | |
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, | |
91 const Location& callback_posted_from, | |
92 Thread* calling_thread, | |
93 const FunctorT& functor, | |
94 void (HostT::*callback)(ReturnT), | |
95 HostT* callback_host) | |
96 : NotifyingAsyncClosureBase(invoker, | |
97 callback_posted_from, | |
98 calling_thread), | |
99 functor_(functor), | |
100 callback_(callback), | |
101 callback_host_(callback_host) {} | |
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, | |
122 const Location& callback_posted_from, | |
123 Thread* calling_thread, | |
124 const FunctorT& functor, | |
125 void (HostT::*callback)(), | |
126 HostT* callback_host) | |
127 : NotifyingAsyncClosureBase(invoker, | |
128 callback_posted_from, | |
129 calling_thread), | |
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 | 54 } // namespace rtc |
143 | 55 |
144 #endif // WEBRTC_BASE_ASYNCINVOKER_INL_H_ | 56 #endif // WEBRTC_BASE_ASYNCINVOKER_INL_H_ |
OLD | NEW |