blob: 76e5d922e60ae214c867f1dd191a438b415dd879 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_BASE_ASYNCINVOKER_H_
12#define WEBRTC_BASE_ASYNCINVOKER_H_
13
14#include "webrtc/base/asyncinvoker-inl.h"
15#include "webrtc/base/bind.h"
kwiberg4485ffb2016-04-26 08:14:39 -070016#include "webrtc/base/constructormagic.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include "webrtc/base/sigslot.h"
18#include "webrtc/base/scopedptrcollection.h"
19#include "webrtc/base/thread.h"
20
21namespace rtc {
22
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
25// flight are cancelled). AsyncInvoker can optionally execute a user-specified
26// function when the asynchronous call is complete, or operates in
27// fire-and-forget mode otherwise.
28//
29// AsyncInvoker does not own the thread it calls functors on.
30//
31// A note about async calls and object lifetimes: users should
32// be mindful of object lifetimes when calling functions asynchronously and
33// ensure objects used by the function _cannot_ be deleted between the
34// invocation and execution of the functor. AsyncInvoker is designed to
35// help: any calls in flight will be cancelled when the AsyncInvoker used to
36// make the call is destructed, and any calls executing will be allowed to
37// complete before AsyncInvoker destructs.
38//
39// The easiest way to ensure lifetimes are handled correctly is to create a
40// class that owns the Thread and AsyncInvoker objects, and then call its
41// methods asynchronously as needed.
42//
43// Example:
44// class MyClass {
45// public:
46// void FireAsyncTaskWithResult(Thread* thread, int x) {
47// // Specify a callback to get the result upon completion.
48// invoker_.AsyncInvoke<int>(
49// thread, Bind(&MyClass::AsyncTaskWithResult, this, x),
50// &MyClass::OnTaskComplete, this);
51// }
52// void FireAnotherAsyncTask(Thread* thread) {
53// // No callback specified means fire-and-forget.
54// invoker_.AsyncInvoke<void>(
55// thread, Bind(&MyClass::AnotherAsyncTask, this));
56//
57// private:
58// int AsyncTaskWithResult(int x) {
59// // Some long running process...
60// return x * x;
61// }
62// void AnotherAsyncTask() {
63// // Some other long running process...
64// }
65// void OnTaskComplete(int result) { result_ = result; }
66//
67// AsyncInvoker invoker_;
68// int result_;
69// };
70class AsyncInvoker : public MessageHandler {
71 public:
72 AsyncInvoker();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000073 ~AsyncInvoker() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074
75 // Call |functor| asynchronously on |thread|, with no callback upon
76 // completion. Returns immediately.
77 template <class ReturnT, class FunctorT>
Peter Boström0c4e06b2015-10-07 12:23:21 +020078 void AsyncInvoke(Thread* thread, const FunctorT& functor, uint32_t id = 0) {
perkj@webrtc.org827d7e82015-01-29 08:53:45 +000079 scoped_refptr<AsyncClosure> closure(
80 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 DoInvoke(thread, closure, id);
82 }
83
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070084 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback
85 // upon completion. Returns immediately.
86 template <class ReturnT, class FunctorT>
87 void AsyncInvokeDelayed(Thread* thread,
88 const FunctorT& functor,
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 uint32_t delay_ms,
90 uint32_t id = 0) {
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070091 scoped_refptr<AsyncClosure> closure(
92 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor));
93 DoInvokeDelayed(thread, closure, delay_ms, id);
94 }
95
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 // Call |functor| asynchronously on |thread|, calling |callback| when done.
97 template <class ReturnT, class FunctorT, class HostT>
98 void AsyncInvoke(Thread* thread,
99 const FunctorT& functor,
100 void (HostT::*callback)(ReturnT),
101 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 uint32_t id = 0) {
perkj@webrtc.org827d7e82015-01-29 08:53:45 +0000103 scoped_refptr<AsyncClosure> closure(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104 new RefCountedObject<NotifyingAsyncClosure<ReturnT, FunctorT, HostT> >(
perkj@webrtc.org827d7e82015-01-29 08:53:45 +0000105 this, Thread::Current(), functor, callback, callback_host));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106 DoInvoke(thread, closure, id);
107 }
108
109 // Call |functor| asynchronously on |thread|, calling |callback| when done.
110 // Overloaded for void return.
111 template <class ReturnT, class FunctorT, class HostT>
112 void AsyncInvoke(Thread* thread,
113 const FunctorT& functor,
114 void (HostT::*callback)(),
115 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200116 uint32_t id = 0) {
perkj@webrtc.org827d7e82015-01-29 08:53:45 +0000117 scoped_refptr<AsyncClosure> closure(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118 new RefCountedObject<NotifyingAsyncClosure<void, FunctorT, HostT> >(
perkj@webrtc.org827d7e82015-01-29 08:53:45 +0000119 this, Thread::Current(), functor, callback, callback_host));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120 DoInvoke(thread, closure, id);
121 }
122
123 // Synchronously execute on |thread| all outstanding calls we own
124 // that are pending on |thread|, and wait for calls to complete
125 // before returning. Optionally filter by message id.
126 // The destructor will not wait for outstanding calls, so if that
127 // behavior is desired, call Flush() before destroying this object.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200128 void Flush(Thread* thread, uint32_t id = MQID_ANY);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129
130 // Signaled when this object is destructed.
131 sigslot::signal0<> SignalInvokerDestroyed;
132
133 private:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000134 void OnMessage(Message* msg) override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200135 void DoInvoke(Thread* thread,
136 const scoped_refptr<AsyncClosure>& closure,
137 uint32_t id);
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -0700138 void DoInvokeDelayed(Thread* thread,
139 const scoped_refptr<AsyncClosure>& closure,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200140 uint32_t delay_ms,
141 uint32_t id);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142 bool destroying_;
143
henrikg3c089d72015-09-16 05:37:44 -0700144 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000145};
146
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200147// 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
149// thread in the constructor, and will get notified when that thread is
150// 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
152// thread dies, any further calls to AsyncInvoke() will be safely ignored.
153class GuardedAsyncInvoker : public sigslot::has_slots<> {
154 public:
155 GuardedAsyncInvoker();
156 ~GuardedAsyncInvoker() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200158 // Synchronously execute all outstanding calls we own, and wait for calls to
159 // complete before returning. Optionally filter by message id. The destructor
160 // will not wait for outstanding calls, so if that behavior is desired, call
161 // Flush() first. Returns false if the thread has died.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200162 bool Flush(uint32_t id = MQID_ANY);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200163
164 // Call |functor| asynchronously with no callback upon completion. Returns
165 // immediately. Returns false if the thread has died.
166 template <class ReturnT, class FunctorT>
Peter Boström0c4e06b2015-10-07 12:23:21 +0200167 bool AsyncInvoke(const FunctorT& functor, uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200168 rtc::CritScope cs(&crit_);
169 if (thread_ == nullptr)
170 return false;
171 invoker_.AsyncInvoke<ReturnT, FunctorT>(thread_, functor, id);
172 return true;
173 }
174
175 // Call |functor| asynchronously with |delay_ms|, with no callback upon
176 // completion. Returns immediately. Returns false if the thread has died.
177 template <class ReturnT, class FunctorT>
178 bool AsyncInvokeDelayed(const FunctorT& functor,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200179 uint32_t delay_ms,
180 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200181 rtc::CritScope cs(&crit_);
182 if (thread_ == nullptr)
183 return false;
184 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(thread_, functor, delay_ms,
185 id);
186 return true;
187 }
188
189 // Call |functor| asynchronously, calling |callback| when done. Returns false
190 // if the thread has died.
191 template <class ReturnT, class FunctorT, class HostT>
192 bool AsyncInvoke(const FunctorT& functor,
193 void (HostT::*callback)(ReturnT),
194 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200195 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200196 rtc::CritScope cs(&crit_);
197 if (thread_ == nullptr)
198 return false;
199 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(thread_, functor, callback,
200 callback_host, id);
201 return true;
202 }
203
204 // Call |functor| asynchronously calling |callback| when done. Overloaded for
205 // void return. Returns false if the thread has died.
206 template <class ReturnT, class FunctorT, class HostT>
207 bool AsyncInvoke(const FunctorT& functor,
208 void (HostT::*callback)(),
209 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200210 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200211 rtc::CritScope cs(&crit_);
212 if (thread_ == nullptr)
213 return false;
214 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(thread_, functor, callback,
215 callback_host, id);
216 return true;
217 }
218
219 private:
220 // Callback when |thread_| is destroyed.
221 void ThreadDestroyed();
222
223 CriticalSection crit_;
224 Thread* thread_ GUARDED_BY(crit_);
225 AsyncInvoker invoker_ GUARDED_BY(crit_);
226};
227
228} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229
230#endif // WEBRTC_BASE_ASYNCINVOKER_H_