| 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 "webrtc/base/asyncinvoker-inl.h" | 14 #include "webrtc/base/asyncinvoker-inl.h" |
| 15 #include "webrtc/base/bind.h" | 15 #include "webrtc/base/bind.h" |
| 16 #include "webrtc/base/constructormagic.h" | 16 #include "webrtc/base/constructormagic.h" |
| 17 #include "webrtc/base/event.h" |
| 17 #include "webrtc/base/sigslot.h" | 18 #include "webrtc/base/sigslot.h" |
| 18 #include "webrtc/base/scopedptrcollection.h" | |
| 19 #include "webrtc/base/thread.h" | 19 #include "webrtc/base/thread.h" |
| 20 | 20 |
| 21 namespace rtc { | 21 namespace rtc { |
| 22 | 22 |
| 23 // Invokes function objects (aka functors) asynchronously on a Thread, and | 23 // Invokes function objects (aka functors) asynchronously on a Thread, and |
| 24 // 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 |
| 25 // flight are cancelled). AsyncInvoker can optionally execute a user-specified | 25 // flight are cancelled). AsyncInvoker can optionally execute a user-specified |
| 26 // function when the asynchronous call is complete, or operates in | 26 // function when the asynchronous call is complete, or operates in |
| 27 // fire-and-forget mode otherwise. | 27 // fire-and-forget mode otherwise. |
| 28 // | 28 // |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 ~AsyncInvoker() override; | 73 ~AsyncInvoker() override; |
| 74 | 74 |
| 75 // Call |functor| asynchronously on |thread|, with no callback upon | 75 // Call |functor| asynchronously on |thread|, with no callback upon |
| 76 // completion. Returns immediately. | 76 // completion. Returns immediately. |
| 77 template <class ReturnT, class FunctorT> | 77 template <class ReturnT, class FunctorT> |
| 78 void AsyncInvoke(const Location& posted_from, | 78 void AsyncInvoke(const Location& posted_from, |
| 79 Thread* thread, | 79 Thread* thread, |
| 80 const FunctorT& functor, | 80 const FunctorT& functor, |
| 81 uint32_t id = 0) { | 81 uint32_t id = 0) { |
| 82 scoped_refptr<AsyncClosure> closure( | 82 scoped_refptr<AsyncClosure> closure( |
| 83 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); | 83 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(this, |
| 84 functor)); |
| 84 DoInvoke(posted_from, thread, closure, id); | 85 DoInvoke(posted_from, thread, closure, id); |
| 85 } | 86 } |
| 86 | 87 |
| 87 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback | 88 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback |
| 88 // upon completion. Returns immediately. | 89 // upon completion. Returns immediately. |
| 89 template <class ReturnT, class FunctorT> | 90 template <class ReturnT, class FunctorT> |
| 90 void AsyncInvokeDelayed(const Location& posted_from, | 91 void AsyncInvokeDelayed(const Location& posted_from, |
| 91 Thread* thread, | 92 Thread* thread, |
| 92 const FunctorT& functor, | 93 const FunctorT& functor, |
| 93 uint32_t delay_ms, | 94 uint32_t delay_ms, |
| 94 uint32_t id = 0) { | 95 uint32_t id = 0) { |
| 95 scoped_refptr<AsyncClosure> closure( | 96 scoped_refptr<AsyncClosure> closure( |
| 96 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); | 97 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(this, |
| 98 functor)); |
| 97 DoInvokeDelayed(posted_from, thread, closure, delay_ms, id); | 99 DoInvokeDelayed(posted_from, thread, 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, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 const scoped_refptr<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 const scoped_refptr<AsyncClosure>& closure, |
| 156 uint32_t delay_ms, | 158 uint32_t delay_ms, |
| 157 uint32_t id); | 159 uint32_t id); |
| 158 bool destroying_; | 160 volatile int pending_invocations_ = 0; |
| 161 Event invocation_complete_; |
| 162 bool destroying_ = false; |
| 163 friend class AsyncClosure; |
| 159 | 164 |
| 160 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); | 165 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); |
| 161 }; | 166 }; |
| 162 | 167 |
| 163 // Similar to AsyncInvoker, but guards against the Thread being destroyed while | 168 // 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 | 169 // 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 | 170 // thread in the constructor, and will get notified when that thread is |
| 166 // destroyed. After GuardedAsyncInvoker is constructed, it can be used from | 171 // destroyed. After GuardedAsyncInvoker is constructed, it can be used from |
| 167 // other threads to post functors to the thread it was constructed on. If that | 172 // other threads to post functors to the thread it was constructed on. If that |
| 168 // thread dies, any further calls to AsyncInvoke() will be safely ignored. | 173 // thread dies, any further calls to AsyncInvoke() will be safely ignored. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 void ThreadDestroyed(); | 251 void ThreadDestroyed(); |
| 247 | 252 |
| 248 CriticalSection crit_; | 253 CriticalSection crit_; |
| 249 Thread* thread_ GUARDED_BY(crit_); | 254 Thread* thread_ GUARDED_BY(crit_); |
| 250 AsyncInvoker invoker_ GUARDED_BY(crit_); | 255 AsyncInvoker invoker_ GUARDED_BY(crit_); |
| 251 }; | 256 }; |
| 252 | 257 |
| 253 } // namespace rtc | 258 } // namespace rtc |
| 254 | 259 |
| 255 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ | 260 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ |
| OLD | NEW |