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_H_ | 11 #ifndef WEBRTC_BASE_ASYNCINVOKER_H_ |
12 #define WEBRTC_BASE_ASYNCINVOKER_H_ | 12 #define WEBRTC_BASE_ASYNCINVOKER_H_ |
13 | 13 |
14 #include <memory> | 14 #include <memory> |
15 #include <utility> | 15 #include <utility> |
16 | 16 |
17 #include "webrtc/base/asyncinvoker-inl.h" | 17 #include "webrtc/base/asyncinvoker-inl.h" |
18 #include "webrtc/base/bind.h" | 18 #include "webrtc/base/bind.h" |
19 #include "webrtc/base/constructormagic.h" | 19 #include "webrtc/base/constructormagic.h" |
| 20 #include "webrtc/base/event.h" |
20 #include "webrtc/base/sigslot.h" | 21 #include "webrtc/base/sigslot.h" |
21 #include "webrtc/base/thread.h" | 22 #include "webrtc/base/thread.h" |
22 | 23 |
23 namespace rtc { | 24 namespace rtc { |
24 | 25 |
25 // Invokes function objects (aka functors) asynchronously on a Thread, and | 26 // Invokes function objects (aka functors) asynchronously on a Thread, and |
26 // owns the lifetime of calls (ie, when this object is destroyed, calls in | 27 // owns the lifetime of calls (ie, when this object is destroyed, calls in |
27 // flight are cancelled). AsyncInvoker can optionally execute a user-specified | 28 // flight are cancelled). AsyncInvoker can optionally execute a user-specified |
28 // function when the asynchronous call is complete, or operates in | 29 // function when the asynchronous call is complete, or operates in |
29 // fire-and-forget mode otherwise. | 30 // fire-and-forget mode otherwise. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 ~AsyncInvoker() override; | 76 ~AsyncInvoker() override; |
76 | 77 |
77 // Call |functor| asynchronously on |thread|, with no callback upon | 78 // Call |functor| asynchronously on |thread|, with no callback upon |
78 // completion. Returns immediately. | 79 // completion. Returns immediately. |
79 template <class ReturnT, class FunctorT> | 80 template <class ReturnT, class FunctorT> |
80 void AsyncInvoke(const Location& posted_from, | 81 void AsyncInvoke(const Location& posted_from, |
81 Thread* thread, | 82 Thread* thread, |
82 const FunctorT& functor, | 83 const FunctorT& functor, |
83 uint32_t id = 0) { | 84 uint32_t id = 0) { |
84 std::unique_ptr<AsyncClosure> closure( | 85 std::unique_ptr<AsyncClosure> closure( |
85 new FireAndForgetAsyncClosure<FunctorT>(functor)); | 86 new FireAndForgetAsyncClosure<FunctorT>(this, functor)); |
86 DoInvoke(posted_from, thread, std::move(closure), id); | 87 DoInvoke(posted_from, thread, std::move(closure), id); |
87 } | 88 } |
88 | 89 |
89 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback | 90 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback |
90 // upon completion. Returns immediately. | 91 // upon completion. Returns immediately. |
91 template <class ReturnT, class FunctorT> | 92 template <class ReturnT, class FunctorT> |
92 void AsyncInvokeDelayed(const Location& posted_from, | 93 void AsyncInvokeDelayed(const Location& posted_from, |
93 Thread* thread, | 94 Thread* thread, |
94 const FunctorT& functor, | 95 const FunctorT& functor, |
95 uint32_t delay_ms, | 96 uint32_t delay_ms, |
96 uint32_t id = 0) { | 97 uint32_t id = 0) { |
97 std::unique_ptr<AsyncClosure> closure( | 98 std::unique_ptr<AsyncClosure> closure( |
98 new FireAndForgetAsyncClosure<FunctorT>(functor)); | 99 new FireAndForgetAsyncClosure<FunctorT>(this, functor)); |
99 DoInvokeDelayed(posted_from, thread, std::move(closure), delay_ms, id); | 100 DoInvokeDelayed(posted_from, thread, std::move(closure), delay_ms, id); |
100 } | 101 } |
101 | 102 |
102 // Call |functor| asynchronously on |thread|, calling |callback| when done. | 103 // Call |functor| asynchronously on |thread|, calling |callback| when done. |
103 // Uses a separate Location for |callback_posted_from| so that the functor | 104 // Uses a separate Location for |callback_posted_from| so that the functor |
104 // invoke and the callback invoke can be differentiated. | 105 // invoke and the callback invoke can be differentiated. |
105 template <class ReturnT, class FunctorT, class HostT> | 106 template <class ReturnT, class FunctorT, class HostT> |
106 void AsyncInvoke(const Location& posted_from, | 107 void AsyncInvoke(const Location& posted_from, |
107 const Location& callback_posted_from, | 108 const Location& callback_posted_from, |
108 Thread* thread, | 109 Thread* thread, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 void OnMessage(Message* msg) override; | 151 void OnMessage(Message* msg) override; |
151 void DoInvoke(const Location& posted_from, | 152 void DoInvoke(const Location& posted_from, |
152 Thread* thread, | 153 Thread* thread, |
153 std::unique_ptr<AsyncClosure> closure, | 154 std::unique_ptr<AsyncClosure> closure, |
154 uint32_t id); | 155 uint32_t id); |
155 void DoInvokeDelayed(const Location& posted_from, | 156 void DoInvokeDelayed(const Location& posted_from, |
156 Thread* thread, | 157 Thread* thread, |
157 std::unique_ptr<AsyncClosure> closure, | 158 std::unique_ptr<AsyncClosure> closure, |
158 uint32_t delay_ms, | 159 uint32_t delay_ms, |
159 uint32_t id); | 160 uint32_t id); |
160 bool destroying_; | 161 volatile int pending_invocations_ = 0; |
| 162 Event invocation_complete_; |
| 163 bool destroying_ = false; |
| 164 friend class AsyncClosure; |
161 | 165 |
162 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); | 166 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); |
163 }; | 167 }; |
164 | 168 |
165 // Similar to AsyncInvoker, but guards against the Thread being destroyed while | 169 // Similar to AsyncInvoker, but guards against the Thread being destroyed while |
166 // there are outstanding dangling pointers to it. It will connect to the current | 170 // there are outstanding dangling pointers to it. It will connect to the current |
167 // thread in the constructor, and will get notified when that thread is | 171 // thread in the constructor, and will get notified when that thread is |
168 // destroyed. After GuardedAsyncInvoker is constructed, it can be used from | 172 // destroyed. After GuardedAsyncInvoker is constructed, it can be used from |
169 // other threads to post functors to the thread it was constructed on. If that | 173 // other threads to post functors to the thread it was constructed on. If that |
170 // thread dies, any further calls to AsyncInvoke() will be safely ignored. | 174 // thread dies, any further calls to AsyncInvoke() will be safely ignored. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 void ThreadDestroyed(); | 252 void ThreadDestroyed(); |
249 | 253 |
250 CriticalSection crit_; | 254 CriticalSection crit_; |
251 Thread* thread_ GUARDED_BY(crit_); | 255 Thread* thread_ GUARDED_BY(crit_); |
252 AsyncInvoker invoker_ GUARDED_BY(crit_); | 256 AsyncInvoker invoker_ GUARDED_BY(crit_); |
253 }; | 257 }; |
254 | 258 |
255 } // namespace rtc | 259 } // namespace rtc |
256 | 260 |
257 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ | 261 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ |
OLD | NEW |