blob: ed6124397460e35113e9093cc6fa76580ee33a7d [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
deadbeefa8bc1a12017-02-17 18:06:26 -080014#include <memory>
15#include <utility>
16
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include "webrtc/base/asyncinvoker-inl.h"
18#include "webrtc/base/bind.h"
kwiberg4485ffb2016-04-26 08:14:39 -070019#include "webrtc/base/constructormagic.h"
deadbeef162cb532017-02-23 17:10:07 -080020#include "webrtc/base/event.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021#include "webrtc/base/sigslot.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022#include "webrtc/base/thread.h"
23
24namespace rtc {
25
26// Invokes function objects (aka functors) asynchronously on a Thread, and
27// owns the lifetime of calls (ie, when this object is destroyed, calls in
28// flight are cancelled). AsyncInvoker can optionally execute a user-specified
29// function when the asynchronous call is complete, or operates in
30// fire-and-forget mode otherwise.
31//
32// AsyncInvoker does not own the thread it calls functors on.
33//
34// A note about async calls and object lifetimes: users should
35// be mindful of object lifetimes when calling functions asynchronously and
36// ensure objects used by the function _cannot_ be deleted between the
37// invocation and execution of the functor. AsyncInvoker is designed to
38// help: any calls in flight will be cancelled when the AsyncInvoker used to
39// make the call is destructed, and any calls executing will be allowed to
40// complete before AsyncInvoker destructs.
41//
42// The easiest way to ensure lifetimes are handled correctly is to create a
43// class that owns the Thread and AsyncInvoker objects, and then call its
44// methods asynchronously as needed.
45//
46// Example:
47// class MyClass {
48// public:
49// void FireAsyncTaskWithResult(Thread* thread, int x) {
50// // Specify a callback to get the result upon completion.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070051// invoker_.AsyncInvoke<int>(RTC_FROM_HERE,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052// thread, Bind(&MyClass::AsyncTaskWithResult, this, x),
53// &MyClass::OnTaskComplete, this);
54// }
55// void FireAnotherAsyncTask(Thread* thread) {
56// // No callback specified means fire-and-forget.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070057// invoker_.AsyncInvoke<void>(RTC_FROM_HERE,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058// thread, Bind(&MyClass::AnotherAsyncTask, this));
59//
60// private:
61// int AsyncTaskWithResult(int x) {
62// // Some long running process...
63// return x * x;
64// }
65// void AnotherAsyncTask() {
66// // Some other long running process...
67// }
68// void OnTaskComplete(int result) { result_ = result; }
69//
70// AsyncInvoker invoker_;
71// int result_;
72// };
73class AsyncInvoker : public MessageHandler {
74 public:
75 AsyncInvoker();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000076 ~AsyncInvoker() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077
78 // Call |functor| asynchronously on |thread|, with no callback upon
79 // completion. Returns immediately.
80 template <class ReturnT, class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070081 void AsyncInvoke(const Location& posted_from,
82 Thread* thread,
83 const FunctorT& functor,
84 uint32_t id = 0) {
deadbeefa8bc1a12017-02-17 18:06:26 -080085 std::unique_ptr<AsyncClosure> closure(
deadbeef162cb532017-02-23 17:10:07 -080086 new FireAndForgetAsyncClosure<FunctorT>(this, functor));
deadbeefa8bc1a12017-02-17 18:06:26 -080087 DoInvoke(posted_from, thread, std::move(closure), id);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088 }
89
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070090 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback
91 // upon completion. Returns immediately.
92 template <class ReturnT, class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070093 void AsyncInvokeDelayed(const Location& posted_from,
94 Thread* thread,
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070095 const FunctorT& functor,
Peter Boström0c4e06b2015-10-07 12:23:21 +020096 uint32_t delay_ms,
97 uint32_t id = 0) {
deadbeefa8bc1a12017-02-17 18:06:26 -080098 std::unique_ptr<AsyncClosure> closure(
deadbeef162cb532017-02-23 17:10:07 -080099 new FireAndForgetAsyncClosure<FunctorT>(this, functor));
deadbeefa8bc1a12017-02-17 18:06:26 -0800100 DoInvokeDelayed(posted_from, thread, std::move(closure), delay_ms, id);
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -0700101 }
102
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103 // Call |functor| asynchronously on |thread|, calling |callback| when done.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700104 // Uses a separate Location for |callback_posted_from| so that the functor
105 // invoke and the callback invoke can be differentiated.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106 template <class ReturnT, class FunctorT, class HostT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700107 void AsyncInvoke(const Location& posted_from,
108 const Location& callback_posted_from,
109 Thread* thread,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110 const FunctorT& functor,
111 void (HostT::*callback)(ReturnT),
112 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200113 uint32_t id = 0) {
deadbeefa8bc1a12017-02-17 18:06:26 -0800114 std::unique_ptr<AsyncClosure> closure(
115 new NotifyingAsyncClosure<ReturnT, FunctorT, HostT>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700116 this, callback_posted_from, Thread::Current(), functor, callback,
117 callback_host));
deadbeefa8bc1a12017-02-17 18:06:26 -0800118 DoInvoke(posted_from, thread, std::move(closure), id);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119 }
120
121 // Call |functor| asynchronously on |thread|, calling |callback| when done.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700122 // Uses a separate Location for |callback_posted_from| so that the functor
123 // invoke and the callback invoke can be differentiated.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124 // Overloaded for void return.
125 template <class ReturnT, class FunctorT, class HostT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700126 void AsyncInvoke(const Location& posted_from,
127 const Location& callback_posted_from,
128 Thread* thread,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129 const FunctorT& functor,
130 void (HostT::*callback)(),
131 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200132 uint32_t id = 0) {
deadbeefa8bc1a12017-02-17 18:06:26 -0800133 std::unique_ptr<AsyncClosure> closure(
134 new NotifyingAsyncClosure<void, FunctorT, HostT>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700135 this, callback_posted_from, Thread::Current(), functor, callback,
136 callback_host));
deadbeefa8bc1a12017-02-17 18:06:26 -0800137 DoInvoke(posted_from, thread, std::move(closure), id);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138 }
139
140 // Synchronously execute on |thread| all outstanding calls we own
141 // that are pending on |thread|, and wait for calls to complete
142 // before returning. Optionally filter by message id.
143 // The destructor will not wait for outstanding calls, so if that
144 // behavior is desired, call Flush() before destroying this object.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200145 void Flush(Thread* thread, uint32_t id = MQID_ANY);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146
147 // Signaled when this object is destructed.
148 sigslot::signal0<> SignalInvokerDestroyed;
149
150 private:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000151 void OnMessage(Message* msg) override;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700152 void DoInvoke(const Location& posted_from,
153 Thread* thread,
deadbeefa8bc1a12017-02-17 18:06:26 -0800154 std::unique_ptr<AsyncClosure> closure,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200155 uint32_t id);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700156 void DoInvokeDelayed(const Location& posted_from,
157 Thread* thread,
deadbeefa8bc1a12017-02-17 18:06:26 -0800158 std::unique_ptr<AsyncClosure> closure,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200159 uint32_t delay_ms,
160 uint32_t id);
deadbeef162cb532017-02-23 17:10:07 -0800161 volatile int pending_invocations_ = 0;
162 Event invocation_complete_;
163 bool destroying_ = false;
164 friend class AsyncClosure;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000165
henrikg3c089d72015-09-16 05:37:44 -0700166 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167};
168
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200169// Similar to AsyncInvoker, but guards against the Thread being destroyed while
170// there are outstanding dangling pointers to it. It will connect to the current
171// thread in the constructor, and will get notified when that thread is
172// destroyed. After GuardedAsyncInvoker is constructed, it can be used from
173// other threads to post functors to the thread it was constructed on. If that
174// thread dies, any further calls to AsyncInvoke() will be safely ignored.
175class GuardedAsyncInvoker : public sigslot::has_slots<> {
176 public:
177 GuardedAsyncInvoker();
178 ~GuardedAsyncInvoker() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000179
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200180 // Synchronously execute all outstanding calls we own, and wait for calls to
181 // complete before returning. Optionally filter by message id. The destructor
182 // will not wait for outstanding calls, so if that behavior is desired, call
183 // Flush() first. Returns false if the thread has died.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200184 bool Flush(uint32_t id = MQID_ANY);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200185
186 // Call |functor| asynchronously with no callback upon completion. Returns
187 // immediately. Returns false if the thread has died.
188 template <class ReturnT, class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700189 bool AsyncInvoke(const Location& posted_from,
190 const FunctorT& functor,
191 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200192 rtc::CritScope cs(&crit_);
193 if (thread_ == nullptr)
194 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700195 invoker_.AsyncInvoke<ReturnT, FunctorT>(posted_from, thread_, functor, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200196 return true;
197 }
198
199 // Call |functor| asynchronously with |delay_ms|, with no callback upon
200 // completion. Returns immediately. Returns false if the thread has died.
201 template <class ReturnT, class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700202 bool AsyncInvokeDelayed(const Location& posted_from,
203 const FunctorT& functor,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200204 uint32_t delay_ms,
205 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200206 rtc::CritScope cs(&crit_);
207 if (thread_ == nullptr)
208 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700209 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(posted_from, thread_,
210 functor, delay_ms, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200211 return true;
212 }
213
214 // Call |functor| asynchronously, calling |callback| when done. Returns false
215 // if the thread has died.
216 template <class ReturnT, class FunctorT, class HostT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700217 bool AsyncInvoke(const Location& posted_from,
218 const Location& callback_posted_from,
219 const FunctorT& functor,
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200220 void (HostT::*callback)(ReturnT),
221 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200222 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200223 rtc::CritScope cs(&crit_);
224 if (thread_ == nullptr)
225 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700226 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
227 posted_from, callback_posted_from, thread_, functor, callback,
228 callback_host, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200229 return true;
230 }
231
232 // Call |functor| asynchronously calling |callback| when done. Overloaded for
233 // void return. Returns false if the thread has died.
234 template <class ReturnT, class FunctorT, class HostT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700235 bool AsyncInvoke(const Location& posted_from,
236 const Location& callback_posted_from,
237 const FunctorT& functor,
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200238 void (HostT::*callback)(),
239 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200240 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200241 rtc::CritScope cs(&crit_);
242 if (thread_ == nullptr)
243 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700244 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
245 posted_from, callback_posted_from, thread_, functor, callback,
246 callback_host, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200247 return true;
248 }
249
250 private:
251 // Callback when |thread_| is destroyed.
252 void ThreadDestroyed();
253
254 CriticalSection crit_;
255 Thread* thread_ GUARDED_BY(crit_);
256 AsyncInvoker invoker_ GUARDED_BY(crit_);
257};
258
259} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000260
261#endif // WEBRTC_BASE_ASYNCINVOKER_H_