blob: e0264be20803c113e57ad0c8cb7a32289816c21c [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"
16#include "webrtc/base/sigslot.h"
17#include "webrtc/base/scopedptrcollection.h"
18#include "webrtc/base/thread.h"
19
20namespace rtc {
21
22// Invokes function objects (aka functors) asynchronously on a Thread, and
23// owns the lifetime of calls (ie, when this object is destroyed, calls in
24// flight are cancelled). AsyncInvoker can optionally execute a user-specified
25// function when the asynchronous call is complete, or operates in
26// fire-and-forget mode otherwise.
27//
28// AsyncInvoker does not own the thread it calls functors on.
29//
30// A note about async calls and object lifetimes: users should
31// be mindful of object lifetimes when calling functions asynchronously and
32// ensure objects used by the function _cannot_ be deleted between the
33// invocation and execution of the functor. AsyncInvoker is designed to
34// help: any calls in flight will be cancelled when the AsyncInvoker used to
35// make the call is destructed, and any calls executing will be allowed to
36// complete before AsyncInvoker destructs.
37//
38// The easiest way to ensure lifetimes are handled correctly is to create a
39// class that owns the Thread and AsyncInvoker objects, and then call its
40// methods asynchronously as needed.
41//
42// Example:
43// class MyClass {
44// public:
45// void FireAsyncTaskWithResult(Thread* thread, int x) {
46// // Specify a callback to get the result upon completion.
47// invoker_.AsyncInvoke<int>(
48// thread, Bind(&MyClass::AsyncTaskWithResult, this, x),
49// &MyClass::OnTaskComplete, this);
50// }
51// void FireAnotherAsyncTask(Thread* thread) {
52// // No callback specified means fire-and-forget.
53// invoker_.AsyncInvoke<void>(
54// thread, Bind(&MyClass::AnotherAsyncTask, this));
55//
56// private:
57// int AsyncTaskWithResult(int x) {
58// // Some long running process...
59// return x * x;
60// }
61// void AnotherAsyncTask() {
62// // Some other long running process...
63// }
64// void OnTaskComplete(int result) { result_ = result; }
65//
66// AsyncInvoker invoker_;
67// int result_;
68// };
69class AsyncInvoker : public MessageHandler {
70 public:
71 AsyncInvoker();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000072 ~AsyncInvoker() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073
74 // Call |functor| asynchronously on |thread|, with no callback upon
75 // completion. Returns immediately.
76 template <class ReturnT, class FunctorT>
77 void AsyncInvoke(Thread* thread,
78 const FunctorT& functor,
79 uint32 id = 0) {
perkj@webrtc.org827d7e82015-01-29 08:53:45 +000080 scoped_refptr<AsyncClosure> closure(
81 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082 DoInvoke(thread, closure, id);
83 }
84
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070085 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback
86 // upon completion. Returns immediately.
87 template <class ReturnT, class FunctorT>
88 void AsyncInvokeDelayed(Thread* thread,
89 const FunctorT& functor,
90 uint32 delay_ms,
91 uint32 id = 0) {
92 scoped_refptr<AsyncClosure> closure(
93 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor));
94 DoInvokeDelayed(thread, closure, delay_ms, id);
95 }
96
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097 // Call |functor| asynchronously on |thread|, calling |callback| when done.
98 template <class ReturnT, class FunctorT, class HostT>
99 void AsyncInvoke(Thread* thread,
100 const FunctorT& functor,
101 void (HostT::*callback)(ReturnT),
102 HostT* callback_host,
103 uint32 id = 0) {
perkj@webrtc.org827d7e82015-01-29 08:53:45 +0000104 scoped_refptr<AsyncClosure> closure(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105 new RefCountedObject<NotifyingAsyncClosure<ReturnT, FunctorT, HostT> >(
perkj@webrtc.org827d7e82015-01-29 08:53:45 +0000106 this, Thread::Current(), functor, callback, callback_host));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107 DoInvoke(thread, closure, id);
108 }
109
110 // Call |functor| asynchronously on |thread|, calling |callback| when done.
111 // Overloaded for void return.
112 template <class ReturnT, class FunctorT, class HostT>
113 void AsyncInvoke(Thread* thread,
114 const FunctorT& functor,
115 void (HostT::*callback)(),
116 HostT* callback_host,
117 uint32 id = 0) {
perkj@webrtc.org827d7e82015-01-29 08:53:45 +0000118 scoped_refptr<AsyncClosure> closure(
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119 new RefCountedObject<NotifyingAsyncClosure<void, FunctorT, HostT> >(
perkj@webrtc.org827d7e82015-01-29 08:53:45 +0000120 this, Thread::Current(), functor, callback, callback_host));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121 DoInvoke(thread, closure, id);
122 }
123
124 // Synchronously execute on |thread| all outstanding calls we own
125 // that are pending on |thread|, and wait for calls to complete
126 // before returning. Optionally filter by message id.
127 // The destructor will not wait for outstanding calls, so if that
128 // behavior is desired, call Flush() before destroying this object.
129 void Flush(Thread* thread, uint32 id = MQID_ANY);
130
131 // Signaled when this object is destructed.
132 sigslot::signal0<> SignalInvokerDestroyed;
133
134 private:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000135 void OnMessage(Message* msg) override;
perkj@webrtc.org827d7e82015-01-29 08:53:45 +0000136 void DoInvoke(Thread* thread, const scoped_refptr<AsyncClosure>& closure,
137 uint32 id);
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -0700138 void DoInvokeDelayed(Thread* thread,
139 const scoped_refptr<AsyncClosure>& closure,
140 uint32 delay_ms,
141 uint32 id);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142 bool destroying_;
143
144 DISALLOW_COPY_AND_ASSIGN(AsyncInvoker);
145};
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.
162 bool Flush(uint32 id = MQID_ANY);
163
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>
167 bool AsyncInvoke(const FunctorT& functor, uint32 id = 0) {
168 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,
179 uint32 delay_ms,
180 uint32 id = 0) {
181 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,
195 uint32 id = 0) {
196 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,
210 uint32 id = 0) {
211 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_