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> | |
15 #include <utility> | |
16 | |
17 #include "webrtc/base/asyncinvoker-inl.h" | 14 #include "webrtc/base/asyncinvoker-inl.h" |
18 #include "webrtc/base/bind.h" | 15 #include "webrtc/base/bind.h" |
19 #include "webrtc/base/constructormagic.h" | 16 #include "webrtc/base/constructormagic.h" |
20 #include "webrtc/base/sigslot.h" | 17 #include "webrtc/base/sigslot.h" |
| 18 #include "webrtc/base/scopedptrcollection.h" |
21 #include "webrtc/base/thread.h" | 19 #include "webrtc/base/thread.h" |
22 | 20 |
23 namespace rtc { | 21 namespace rtc { |
24 | 22 |
25 // Invokes function objects (aka functors) asynchronously on a Thread, and | 23 // Invokes function objects (aka functors) asynchronously on a Thread, and |
26 // owns the lifetime of calls (ie, when this object is destroyed, calls in | 24 // owns the lifetime of calls (ie, when this object is destroyed, calls in |
27 // flight are cancelled). AsyncInvoker can optionally execute a user-specified | 25 // flight are cancelled). AsyncInvoker can optionally execute a user-specified |
28 // function when the asynchronous call is complete, or operates in | 26 // function when the asynchronous call is complete, or operates in |
29 // fire-and-forget mode otherwise. | 27 // fire-and-forget mode otherwise. |
30 // | 28 // |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 AsyncInvoker(); | 72 AsyncInvoker(); |
75 ~AsyncInvoker() override; | 73 ~AsyncInvoker() override; |
76 | 74 |
77 // Call |functor| asynchronously on |thread|, with no callback upon | 75 // Call |functor| asynchronously on |thread|, with no callback upon |
78 // completion. Returns immediately. | 76 // completion. Returns immediately. |
79 template <class ReturnT, class FunctorT> | 77 template <class ReturnT, class FunctorT> |
80 void AsyncInvoke(const Location& posted_from, | 78 void AsyncInvoke(const Location& posted_from, |
81 Thread* thread, | 79 Thread* thread, |
82 const FunctorT& functor, | 80 const FunctorT& functor, |
83 uint32_t id = 0) { | 81 uint32_t id = 0) { |
84 std::unique_ptr<AsyncClosure> closure( | 82 scoped_refptr<AsyncClosure> closure( |
85 new FireAndForgetAsyncClosure<FunctorT>(functor)); | 83 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); |
86 DoInvoke(posted_from, thread, std::move(closure), id); | 84 DoInvoke(posted_from, thread, closure, id); |
87 } | 85 } |
88 | 86 |
89 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback | 87 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback |
90 // upon completion. Returns immediately. | 88 // upon completion. Returns immediately. |
91 template <class ReturnT, class FunctorT> | 89 template <class ReturnT, class FunctorT> |
92 void AsyncInvokeDelayed(const Location& posted_from, | 90 void AsyncInvokeDelayed(const Location& posted_from, |
93 Thread* thread, | 91 Thread* thread, |
94 const FunctorT& functor, | 92 const FunctorT& functor, |
95 uint32_t delay_ms, | 93 uint32_t delay_ms, |
96 uint32_t id = 0) { | 94 uint32_t id = 0) { |
97 std::unique_ptr<AsyncClosure> closure( | 95 scoped_refptr<AsyncClosure> closure( |
98 new FireAndForgetAsyncClosure<FunctorT>(functor)); | 96 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); |
99 DoInvokeDelayed(posted_from, thread, std::move(closure), delay_ms, id); | 97 DoInvokeDelayed(posted_from, thread, closure, delay_ms, id); |
100 } | 98 } |
101 | 99 |
102 // Call |functor| asynchronously on |thread|, calling |callback| when done. | 100 // Call |functor| asynchronously on |thread|, calling |callback| when done. |
103 // Uses a separate Location for |callback_posted_from| so that the functor | 101 // Uses a separate Location for |callback_posted_from| so that the functor |
104 // invoke and the callback invoke can be differentiated. | 102 // invoke and the callback invoke can be differentiated. |
105 template <class ReturnT, class FunctorT, class HostT> | 103 template <class ReturnT, class FunctorT, class HostT> |
106 void AsyncInvoke(const Location& posted_from, | 104 void AsyncInvoke(const Location& posted_from, |
107 const Location& callback_posted_from, | 105 const Location& callback_posted_from, |
108 Thread* thread, | 106 Thread* thread, |
109 const FunctorT& functor, | 107 const FunctorT& functor, |
110 void (HostT::*callback)(ReturnT), | 108 void (HostT::*callback)(ReturnT), |
111 HostT* callback_host, | 109 HostT* callback_host, |
112 uint32_t id = 0) { | 110 uint32_t id = 0) { |
113 std::unique_ptr<AsyncClosure> closure( | 111 scoped_refptr<AsyncClosure> closure( |
114 new NotifyingAsyncClosure<ReturnT, FunctorT, HostT>( | 112 new RefCountedObject<NotifyingAsyncClosure<ReturnT, FunctorT, HostT> >( |
115 this, callback_posted_from, Thread::Current(), functor, callback, | 113 this, callback_posted_from, Thread::Current(), functor, callback, |
116 callback_host)); | 114 callback_host)); |
117 DoInvoke(posted_from, thread, std::move(closure), id); | 115 DoInvoke(posted_from, thread, closure, id); |
118 } | 116 } |
119 | 117 |
120 // Call |functor| asynchronously on |thread|, calling |callback| when done. | 118 // Call |functor| asynchronously on |thread|, calling |callback| when done. |
121 // Uses a separate Location for |callback_posted_from| so that the functor | 119 // Uses a separate Location for |callback_posted_from| so that the functor |
122 // invoke and the callback invoke can be differentiated. | 120 // invoke and the callback invoke can be differentiated. |
123 // Overloaded for void return. | 121 // Overloaded for void return. |
124 template <class ReturnT, class FunctorT, class HostT> | 122 template <class ReturnT, class FunctorT, class HostT> |
125 void AsyncInvoke(const Location& posted_from, | 123 void AsyncInvoke(const Location& posted_from, |
126 const Location& callback_posted_from, | 124 const Location& callback_posted_from, |
127 Thread* thread, | 125 Thread* thread, |
128 const FunctorT& functor, | 126 const FunctorT& functor, |
129 void (HostT::*callback)(), | 127 void (HostT::*callback)(), |
130 HostT* callback_host, | 128 HostT* callback_host, |
131 uint32_t id = 0) { | 129 uint32_t id = 0) { |
132 std::unique_ptr<AsyncClosure> closure( | 130 scoped_refptr<AsyncClosure> closure( |
133 new NotifyingAsyncClosure<void, FunctorT, HostT>( | 131 new RefCountedObject<NotifyingAsyncClosure<void, FunctorT, HostT> >( |
134 this, callback_posted_from, Thread::Current(), functor, callback, | 132 this, callback_posted_from, Thread::Current(), functor, callback, |
135 callback_host)); | 133 callback_host)); |
136 DoInvoke(posted_from, thread, std::move(closure), id); | 134 DoInvoke(posted_from, thread, closure, id); |
137 } | 135 } |
138 | 136 |
139 // Synchronously execute on |thread| all outstanding calls we own | 137 // Synchronously execute on |thread| all outstanding calls we own |
140 // that are pending on |thread|, and wait for calls to complete | 138 // that are pending on |thread|, and wait for calls to complete |
141 // before returning. Optionally filter by message id. | 139 // before returning. Optionally filter by message id. |
142 // The destructor will not wait for outstanding calls, so if that | 140 // The destructor will not wait for outstanding calls, so if that |
143 // behavior is desired, call Flush() before destroying this object. | 141 // behavior is desired, call Flush() before destroying this object. |
144 void Flush(Thread* thread, uint32_t id = MQID_ANY); | 142 void Flush(Thread* thread, uint32_t id = MQID_ANY); |
145 | 143 |
146 // Signaled when this object is destructed. | 144 // Signaled when this object is destructed. |
147 sigslot::signal0<> SignalInvokerDestroyed; | 145 sigslot::signal0<> SignalInvokerDestroyed; |
148 | 146 |
149 private: | 147 private: |
150 void OnMessage(Message* msg) override; | 148 void OnMessage(Message* msg) override; |
151 void DoInvoke(const Location& posted_from, | 149 void DoInvoke(const Location& posted_from, |
152 Thread* thread, | 150 Thread* thread, |
153 std::unique_ptr<AsyncClosure> closure, | 151 const scoped_refptr<AsyncClosure>& closure, |
154 uint32_t id); | 152 uint32_t id); |
155 void DoInvokeDelayed(const Location& posted_from, | 153 void DoInvokeDelayed(const Location& posted_from, |
156 Thread* thread, | 154 Thread* thread, |
157 std::unique_ptr<AsyncClosure> closure, | 155 const scoped_refptr<AsyncClosure>& closure, |
158 uint32_t delay_ms, | 156 uint32_t delay_ms, |
159 uint32_t id); | 157 uint32_t id); |
160 bool destroying_; | 158 bool destroying_; |
161 | 159 |
162 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); | 160 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); |
163 }; | 161 }; |
164 | 162 |
165 // Similar to AsyncInvoker, but guards against the Thread being destroyed while | 163 // 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 | 164 // 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 | 165 // thread in the constructor, and will get notified when that thread is |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 void ThreadDestroyed(); | 246 void ThreadDestroyed(); |
249 | 247 |
250 CriticalSection crit_; | 248 CriticalSection crit_; |
251 Thread* thread_ GUARDED_BY(crit_); | 249 Thread* thread_ GUARDED_BY(crit_); |
252 AsyncInvoker invoker_ GUARDED_BY(crit_); | 250 AsyncInvoker invoker_ GUARDED_BY(crit_); |
253 }; | 251 }; |
254 | 252 |
255 } // namespace rtc | 253 } // namespace rtc |
256 | 254 |
257 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ | 255 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ |
OLD | NEW |