blob: d91e30601eff620aaec65537ee47632187a705c5 [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.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070048// invoker_.AsyncInvoke<int>(RTC_FROM_HERE,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049// thread, Bind(&MyClass::AsyncTaskWithResult, this, x),
50// &MyClass::OnTaskComplete, this);
51// }
52// void FireAnotherAsyncTask(Thread* thread) {
53// // No callback specified means fire-and-forget.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070054// invoker_.AsyncInvoke<void>(RTC_FROM_HERE,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055// 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>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070078 void AsyncInvoke(const Location& posted_from,
79 Thread* thread,
80 const FunctorT& functor,
81 uint32_t id = 0) {
perkj@webrtc.org827d7e82015-01-29 08:53:45 +000082 scoped_refptr<AsyncClosure> closure(
83 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor));
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070084 DoInvoke(posted_from, thread, closure, id);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085 }
86
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070087 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback
88 // upon completion. Returns immediately.
89 template <class ReturnT, class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070090 void AsyncInvokeDelayed(const Location& posted_from,
91 Thread* thread,
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070092 const FunctorT& functor,
Peter Boström0c4e06b2015-10-07 12:23:21 +020093 uint32_t delay_ms,
94 uint32_t id = 0) {
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070095 scoped_refptr<AsyncClosure> closure(
96 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor));
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070097 DoInvokeDelayed(posted_from, thread, closure, delay_ms, id);
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070098 }
99
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100 // Call |functor| asynchronously on |thread|, calling |callback| when done.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700101 // Uses a separate Location for |callback_posted_from| so that the functor
102 // invoke and the callback invoke can be differentiated.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103 template <class ReturnT, class FunctorT, class HostT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700104 void AsyncInvoke(const Location& posted_from,
105 const Location& callback_posted_from,
106 Thread* thread,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107 const FunctorT& functor,
108 void (HostT::*callback)(ReturnT),
109 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200110 uint32_t id = 0) {
perkj@webrtc.org827d7e82015-01-29 08:53:45 +0000111 scoped_refptr<AsyncClosure> closure(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 new RefCountedObject<NotifyingAsyncClosure<ReturnT, FunctorT, HostT> >(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700113 this, callback_posted_from, Thread::Current(), functor, callback,
114 callback_host));
115 DoInvoke(posted_from, thread, closure, id);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 }
117
118 // Call |functor| asynchronously on |thread|, calling |callback| when done.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700119 // Uses a separate Location for |callback_posted_from| so that the functor
120 // invoke and the callback invoke can be differentiated.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121 // Overloaded for void return.
122 template <class ReturnT, class FunctorT, class HostT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700123 void AsyncInvoke(const Location& posted_from,
124 const Location& callback_posted_from,
125 Thread* thread,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126 const FunctorT& functor,
127 void (HostT::*callback)(),
128 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200129 uint32_t id = 0) {
perkj@webrtc.org827d7e82015-01-29 08:53:45 +0000130 scoped_refptr<AsyncClosure> closure(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131 new RefCountedObject<NotifyingAsyncClosure<void, FunctorT, HostT> >(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700132 this, callback_posted_from, Thread::Current(), functor, callback,
133 callback_host));
134 DoInvoke(posted_from, thread, closure, id);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 }
136
137 // Synchronously execute on |thread| all outstanding calls we own
138 // that are pending on |thread|, and wait for calls to complete
139 // before returning. Optionally filter by message id.
140 // The destructor will not wait for outstanding calls, so if that
141 // behavior is desired, call Flush() before destroying this object.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200142 void Flush(Thread* thread, uint32_t id = MQID_ANY);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143
144 // Signaled when this object is destructed.
145 sigslot::signal0<> SignalInvokerDestroyed;
146
147 private:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000148 void OnMessage(Message* msg) override;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700149 void DoInvoke(const Location& posted_from,
150 Thread* thread,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200151 const scoped_refptr<AsyncClosure>& closure,
152 uint32_t id);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700153 void DoInvokeDelayed(const Location& posted_from,
154 Thread* thread,
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -0700155 const scoped_refptr<AsyncClosure>& closure,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200156 uint32_t delay_ms,
157 uint32_t id);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158 bool destroying_;
159
henrikg3c089d72015-09-16 05:37:44 -0700160 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161};
162
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200163// 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
165// thread in the constructor, and will get notified when that thread is
166// 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
168// thread dies, any further calls to AsyncInvoke() will be safely ignored.
169class GuardedAsyncInvoker : public sigslot::has_slots<> {
170 public:
171 GuardedAsyncInvoker();
172 ~GuardedAsyncInvoker() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200174 // Synchronously execute all outstanding calls we own, and wait for calls to
175 // complete before returning. Optionally filter by message id. The destructor
176 // will not wait for outstanding calls, so if that behavior is desired, call
177 // Flush() first. Returns false if the thread has died.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200178 bool Flush(uint32_t id = MQID_ANY);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200179
180 // Call |functor| asynchronously with no callback upon completion. Returns
181 // immediately. Returns false if the thread has died.
182 template <class ReturnT, class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700183 bool AsyncInvoke(const Location& posted_from,
184 const FunctorT& functor,
185 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200186 rtc::CritScope cs(&crit_);
187 if (thread_ == nullptr)
188 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700189 invoker_.AsyncInvoke<ReturnT, FunctorT>(posted_from, thread_, functor, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200190 return true;
191 }
192
193 // Call |functor| asynchronously with |delay_ms|, with no callback upon
194 // completion. Returns immediately. Returns false if the thread has died.
195 template <class ReturnT, class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700196 bool AsyncInvokeDelayed(const Location& posted_from,
197 const FunctorT& functor,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200198 uint32_t delay_ms,
199 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200200 rtc::CritScope cs(&crit_);
201 if (thread_ == nullptr)
202 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700203 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(posted_from, thread_,
204 functor, delay_ms, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200205 return true;
206 }
207
208 // Call |functor| asynchronously, calling |callback| when done. Returns false
209 // if the thread has died.
210 template <class ReturnT, class FunctorT, class HostT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700211 bool AsyncInvoke(const Location& posted_from,
212 const Location& callback_posted_from,
213 const FunctorT& functor,
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200214 void (HostT::*callback)(ReturnT),
215 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200216 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200217 rtc::CritScope cs(&crit_);
218 if (thread_ == nullptr)
219 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700220 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
221 posted_from, callback_posted_from, thread_, functor, callback,
222 callback_host, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200223 return true;
224 }
225
226 // Call |functor| asynchronously calling |callback| when done. Overloaded for
227 // void return. Returns false if the thread has died.
228 template <class ReturnT, class FunctorT, class HostT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700229 bool AsyncInvoke(const Location& posted_from,
230 const Location& callback_posted_from,
231 const FunctorT& functor,
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200232 void (HostT::*callback)(),
233 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200234 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200235 rtc::CritScope cs(&crit_);
236 if (thread_ == nullptr)
237 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700238 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
239 posted_from, callback_posted_from, thread_, functor, callback,
240 callback_host, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200241 return true;
242 }
243
244 private:
245 // Callback when |thread_| is destroyed.
246 void ThreadDestroyed();
247
248 CriticalSection crit_;
249 Thread* thread_ GUARDED_BY(crit_);
250 AsyncInvoker invoker_ GUARDED_BY(crit_);
251};
252
253} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254
255#endif // WEBRTC_BASE_ASYNCINVOKER_H_