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