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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 // int result_; | 67 // int result_; |
68 // }; | 68 // }; |
69 class AsyncInvoker : public MessageHandler { | 69 class AsyncInvoker : public MessageHandler { |
70 public: | 70 public: |
71 AsyncInvoker(); | 71 AsyncInvoker(); |
72 ~AsyncInvoker() override; | 72 ~AsyncInvoker() override; |
73 | 73 |
74 // Call |functor| asynchronously on |thread|, with no callback upon | 74 // Call |functor| asynchronously on |thread|, with no callback upon |
75 // completion. Returns immediately. | 75 // completion. Returns immediately. |
76 template <class ReturnT, class FunctorT> | 76 template <class ReturnT, class FunctorT> |
77 void AsyncInvoke(Thread* thread, | 77 void AsyncInvoke(Thread* thread, const FunctorT& functor, uint32_t id = 0) { |
78 const FunctorT& functor, | |
79 uint32 id = 0) { | |
80 scoped_refptr<AsyncClosure> closure( | 78 scoped_refptr<AsyncClosure> closure( |
81 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); | 79 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); |
82 DoInvoke(thread, closure, id); | 80 DoInvoke(thread, closure, id); |
83 } | 81 } |
84 | 82 |
85 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback | 83 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback |
86 // upon completion. Returns immediately. | 84 // upon completion. Returns immediately. |
87 template <class ReturnT, class FunctorT> | 85 template <class ReturnT, class FunctorT> |
88 void AsyncInvokeDelayed(Thread* thread, | 86 void AsyncInvokeDelayed(Thread* thread, |
89 const FunctorT& functor, | 87 const FunctorT& functor, |
90 uint32 delay_ms, | 88 uint32_t delay_ms, |
91 uint32 id = 0) { | 89 uint32_t id = 0) { |
92 scoped_refptr<AsyncClosure> closure( | 90 scoped_refptr<AsyncClosure> closure( |
93 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); | 91 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); |
94 DoInvokeDelayed(thread, closure, delay_ms, id); | 92 DoInvokeDelayed(thread, closure, delay_ms, id); |
95 } | 93 } |
96 | 94 |
97 // Call |functor| asynchronously on |thread|, calling |callback| when done. | 95 // Call |functor| asynchronously on |thread|, calling |callback| when done. |
98 template <class ReturnT, class FunctorT, class HostT> | 96 template <class ReturnT, class FunctorT, class HostT> |
99 void AsyncInvoke(Thread* thread, | 97 void AsyncInvoke(Thread* thread, |
100 const FunctorT& functor, | 98 const FunctorT& functor, |
101 void (HostT::*callback)(ReturnT), | 99 void (HostT::*callback)(ReturnT), |
102 HostT* callback_host, | 100 HostT* callback_host, |
103 uint32 id = 0) { | 101 uint32_t id = 0) { |
104 scoped_refptr<AsyncClosure> closure( | 102 scoped_refptr<AsyncClosure> closure( |
105 new RefCountedObject<NotifyingAsyncClosure<ReturnT, FunctorT, HostT> >( | 103 new RefCountedObject<NotifyingAsyncClosure<ReturnT, FunctorT, HostT> >( |
106 this, Thread::Current(), functor, callback, callback_host)); | 104 this, Thread::Current(), functor, callback, callback_host)); |
107 DoInvoke(thread, closure, id); | 105 DoInvoke(thread, closure, id); |
108 } | 106 } |
109 | 107 |
110 // Call |functor| asynchronously on |thread|, calling |callback| when done. | 108 // Call |functor| asynchronously on |thread|, calling |callback| when done. |
111 // Overloaded for void return. | 109 // Overloaded for void return. |
112 template <class ReturnT, class FunctorT, class HostT> | 110 template <class ReturnT, class FunctorT, class HostT> |
113 void AsyncInvoke(Thread* thread, | 111 void AsyncInvoke(Thread* thread, |
114 const FunctorT& functor, | 112 const FunctorT& functor, |
115 void (HostT::*callback)(), | 113 void (HostT::*callback)(), |
116 HostT* callback_host, | 114 HostT* callback_host, |
117 uint32 id = 0) { | 115 uint32_t id = 0) { |
118 scoped_refptr<AsyncClosure> closure( | 116 scoped_refptr<AsyncClosure> closure( |
119 new RefCountedObject<NotifyingAsyncClosure<void, FunctorT, HostT> >( | 117 new RefCountedObject<NotifyingAsyncClosure<void, FunctorT, HostT> >( |
120 this, Thread::Current(), functor, callback, callback_host)); | 118 this, Thread::Current(), functor, callback, callback_host)); |
121 DoInvoke(thread, closure, id); | 119 DoInvoke(thread, closure, id); |
122 } | 120 } |
123 | 121 |
124 // Synchronously execute on |thread| all outstanding calls we own | 122 // Synchronously execute on |thread| all outstanding calls we own |
125 // that are pending on |thread|, and wait for calls to complete | 123 // that are pending on |thread|, and wait for calls to complete |
126 // before returning. Optionally filter by message id. | 124 // before returning. Optionally filter by message id. |
127 // The destructor will not wait for outstanding calls, so if that | 125 // The destructor will not wait for outstanding calls, so if that |
128 // behavior is desired, call Flush() before destroying this object. | 126 // behavior is desired, call Flush() before destroying this object. |
129 void Flush(Thread* thread, uint32 id = MQID_ANY); | 127 void Flush(Thread* thread, uint32_t id = MQID_ANY); |
130 | 128 |
131 // Signaled when this object is destructed. | 129 // Signaled when this object is destructed. |
132 sigslot::signal0<> SignalInvokerDestroyed; | 130 sigslot::signal0<> SignalInvokerDestroyed; |
133 | 131 |
134 private: | 132 private: |
135 void OnMessage(Message* msg) override; | 133 void OnMessage(Message* msg) override; |
136 void DoInvoke(Thread* thread, const scoped_refptr<AsyncClosure>& closure, | 134 void DoInvoke(Thread* thread, |
137 uint32 id); | 135 const scoped_refptr<AsyncClosure>& closure, |
| 136 uint32_t id); |
138 void DoInvokeDelayed(Thread* thread, | 137 void DoInvokeDelayed(Thread* thread, |
139 const scoped_refptr<AsyncClosure>& closure, | 138 const scoped_refptr<AsyncClosure>& closure, |
140 uint32 delay_ms, | 139 uint32_t delay_ms, |
141 uint32 id); | 140 uint32_t id); |
142 bool destroying_; | 141 bool destroying_; |
143 | 142 |
144 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); | 143 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); |
145 }; | 144 }; |
146 | 145 |
147 // Similar to AsyncInvoker, but guards against the Thread being destroyed while | 146 // Similar to AsyncInvoker, but guards against the Thread being destroyed while |
148 // there are outstanding dangling pointers to it. It will connect to the current | 147 // there are outstanding dangling pointers to it. It will connect to the current |
149 // thread in the constructor, and will get notified when that thread is | 148 // thread in the constructor, and will get notified when that thread is |
150 // destroyed. After GuardedAsyncInvoker is constructed, it can be used from | 149 // destroyed. After GuardedAsyncInvoker is constructed, it can be used from |
151 // other threads to post functors to the thread it was constructed on. If that | 150 // other threads to post functors to the thread it was constructed on. If that |
152 // thread dies, any further calls to AsyncInvoke() will be safely ignored. | 151 // thread dies, any further calls to AsyncInvoke() will be safely ignored. |
153 class GuardedAsyncInvoker : public sigslot::has_slots<> { | 152 class GuardedAsyncInvoker : public sigslot::has_slots<> { |
154 public: | 153 public: |
155 GuardedAsyncInvoker(); | 154 GuardedAsyncInvoker(); |
156 ~GuardedAsyncInvoker() override; | 155 ~GuardedAsyncInvoker() override; |
157 | 156 |
158 // Synchronously execute all outstanding calls we own, and wait for calls to | 157 // Synchronously execute all outstanding calls we own, and wait for calls to |
159 // complete before returning. Optionally filter by message id. The destructor | 158 // complete before returning. Optionally filter by message id. The destructor |
160 // will not wait for outstanding calls, so if that behavior is desired, call | 159 // will not wait for outstanding calls, so if that behavior is desired, call |
161 // Flush() first. Returns false if the thread has died. | 160 // Flush() first. Returns false if the thread has died. |
162 bool Flush(uint32 id = MQID_ANY); | 161 bool Flush(uint32_t id = MQID_ANY); |
163 | 162 |
164 // Call |functor| asynchronously with no callback upon completion. Returns | 163 // Call |functor| asynchronously with no callback upon completion. Returns |
165 // immediately. Returns false if the thread has died. | 164 // immediately. Returns false if the thread has died. |
166 template <class ReturnT, class FunctorT> | 165 template <class ReturnT, class FunctorT> |
167 bool AsyncInvoke(const FunctorT& functor, uint32 id = 0) { | 166 bool AsyncInvoke(const FunctorT& functor, uint32_t id = 0) { |
168 rtc::CritScope cs(&crit_); | 167 rtc::CritScope cs(&crit_); |
169 if (thread_ == nullptr) | 168 if (thread_ == nullptr) |
170 return false; | 169 return false; |
171 invoker_.AsyncInvoke<ReturnT, FunctorT>(thread_, functor, id); | 170 invoker_.AsyncInvoke<ReturnT, FunctorT>(thread_, functor, id); |
172 return true; | 171 return true; |
173 } | 172 } |
174 | 173 |
175 // Call |functor| asynchronously with |delay_ms|, with no callback upon | 174 // Call |functor| asynchronously with |delay_ms|, with no callback upon |
176 // completion. Returns immediately. Returns false if the thread has died. | 175 // completion. Returns immediately. Returns false if the thread has died. |
177 template <class ReturnT, class FunctorT> | 176 template <class ReturnT, class FunctorT> |
178 bool AsyncInvokeDelayed(const FunctorT& functor, | 177 bool AsyncInvokeDelayed(const FunctorT& functor, |
179 uint32 delay_ms, | 178 uint32_t delay_ms, |
180 uint32 id = 0) { | 179 uint32_t id = 0) { |
181 rtc::CritScope cs(&crit_); | 180 rtc::CritScope cs(&crit_); |
182 if (thread_ == nullptr) | 181 if (thread_ == nullptr) |
183 return false; | 182 return false; |
184 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(thread_, functor, delay_ms, | 183 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(thread_, functor, delay_ms, |
185 id); | 184 id); |
186 return true; | 185 return true; |
187 } | 186 } |
188 | 187 |
189 // Call |functor| asynchronously, calling |callback| when done. Returns false | 188 // Call |functor| asynchronously, calling |callback| when done. Returns false |
190 // if the thread has died. | 189 // if the thread has died. |
191 template <class ReturnT, class FunctorT, class HostT> | 190 template <class ReturnT, class FunctorT, class HostT> |
192 bool AsyncInvoke(const FunctorT& functor, | 191 bool AsyncInvoke(const FunctorT& functor, |
193 void (HostT::*callback)(ReturnT), | 192 void (HostT::*callback)(ReturnT), |
194 HostT* callback_host, | 193 HostT* callback_host, |
195 uint32 id = 0) { | 194 uint32_t id = 0) { |
196 rtc::CritScope cs(&crit_); | 195 rtc::CritScope cs(&crit_); |
197 if (thread_ == nullptr) | 196 if (thread_ == nullptr) |
198 return false; | 197 return false; |
199 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(thread_, functor, callback, | 198 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(thread_, functor, callback, |
200 callback_host, id); | 199 callback_host, id); |
201 return true; | 200 return true; |
202 } | 201 } |
203 | 202 |
204 // Call |functor| asynchronously calling |callback| when done. Overloaded for | 203 // Call |functor| asynchronously calling |callback| when done. Overloaded for |
205 // void return. Returns false if the thread has died. | 204 // void return. Returns false if the thread has died. |
206 template <class ReturnT, class FunctorT, class HostT> | 205 template <class ReturnT, class FunctorT, class HostT> |
207 bool AsyncInvoke(const FunctorT& functor, | 206 bool AsyncInvoke(const FunctorT& functor, |
208 void (HostT::*callback)(), | 207 void (HostT::*callback)(), |
209 HostT* callback_host, | 208 HostT* callback_host, |
210 uint32 id = 0) { | 209 uint32_t id = 0) { |
211 rtc::CritScope cs(&crit_); | 210 rtc::CritScope cs(&crit_); |
212 if (thread_ == nullptr) | 211 if (thread_ == nullptr) |
213 return false; | 212 return false; |
214 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(thread_, functor, callback, | 213 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(thread_, functor, callback, |
215 callback_host, id); | 214 callback_host, id); |
216 return true; | 215 return true; |
217 } | 216 } |
218 | 217 |
219 private: | 218 private: |
220 // Callback when |thread_| is destroyed. | 219 // Callback when |thread_| is destroyed. |
221 void ThreadDestroyed(); | 220 void ThreadDestroyed(); |
222 | 221 |
223 CriticalSection crit_; | 222 CriticalSection crit_; |
224 Thread* thread_ GUARDED_BY(crit_); | 223 Thread* thread_ GUARDED_BY(crit_); |
225 AsyncInvoker invoker_ GUARDED_BY(crit_); | 224 AsyncInvoker invoker_ GUARDED_BY(crit_); |
226 }; | 225 }; |
227 | 226 |
228 } // namespace rtc | 227 } // namespace rtc |
229 | 228 |
230 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ | 229 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ |
OLD | NEW |