blob: 541486741841b82b09b936cf6aceb65d56f4872a [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 // Synchronously execute on |thread| all outstanding calls we own
104 // that are pending on |thread|, and wait for calls to complete
105 // before returning. Optionally filter by message id.
106 // The destructor will not wait for outstanding calls, so if that
107 // behavior is desired, call Flush() before destroying this object.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200108 void Flush(Thread* thread, uint32_t id = MQID_ANY);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110 private:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000111 void OnMessage(Message* msg) override;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700112 void DoInvoke(const Location& posted_from,
113 Thread* thread,
deadbeefa8bc1a12017-02-17 18:06:26 -0800114 std::unique_ptr<AsyncClosure> closure,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200115 uint32_t id);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700116 void DoInvokeDelayed(const Location& posted_from,
117 Thread* thread,
deadbeefa8bc1a12017-02-17 18:06:26 -0800118 std::unique_ptr<AsyncClosure> closure,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200119 uint32_t delay_ms,
120 uint32_t id);
deadbeef162cb532017-02-23 17:10:07 -0800121 volatile int pending_invocations_ = 0;
122 Event invocation_complete_;
deadbeefaea92932017-05-23 12:55:03 -0700123 bool destroying_ = false;
deadbeef162cb532017-02-23 17:10:07 -0800124 friend class AsyncClosure;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125
henrikg3c089d72015-09-16 05:37:44 -0700126 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127};
128
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200129// Similar to AsyncInvoker, but guards against the Thread being destroyed while
130// there are outstanding dangling pointers to it. It will connect to the current
131// thread in the constructor, and will get notified when that thread is
132// destroyed. After GuardedAsyncInvoker is constructed, it can be used from
133// other threads to post functors to the thread it was constructed on. If that
134// thread dies, any further calls to AsyncInvoke() will be safely ignored.
135class GuardedAsyncInvoker : public sigslot::has_slots<> {
136 public:
137 GuardedAsyncInvoker();
138 ~GuardedAsyncInvoker() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200140 // Synchronously execute all outstanding calls we own, and wait for calls to
141 // complete before returning. Optionally filter by message id. The destructor
142 // will not wait for outstanding calls, so if that behavior is desired, call
143 // Flush() first. Returns false if the thread has died.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200144 bool Flush(uint32_t id = MQID_ANY);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200145
146 // Call |functor| asynchronously with no callback upon completion. Returns
147 // immediately. Returns false if the thread has died.
148 template <class ReturnT, class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700149 bool AsyncInvoke(const Location& posted_from,
150 const FunctorT& functor,
151 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200152 rtc::CritScope cs(&crit_);
153 if (thread_ == nullptr)
154 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700155 invoker_.AsyncInvoke<ReturnT, FunctorT>(posted_from, thread_, functor, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200156 return true;
157 }
158
159 // Call |functor| asynchronously with |delay_ms|, with no callback upon
160 // completion. Returns immediately. Returns false if the thread has died.
161 template <class ReturnT, class FunctorT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700162 bool AsyncInvokeDelayed(const Location& posted_from,
163 const FunctorT& functor,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200164 uint32_t delay_ms,
165 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200166 rtc::CritScope cs(&crit_);
167 if (thread_ == nullptr)
168 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700169 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(posted_from, thread_,
170 functor, delay_ms, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200171 return true;
172 }
173
174 // Call |functor| asynchronously, calling |callback| when done. Returns false
175 // if the thread has died.
176 template <class ReturnT, class FunctorT, class HostT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700177 bool AsyncInvoke(const Location& posted_from,
178 const Location& callback_posted_from,
179 const FunctorT& functor,
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200180 void (HostT::*callback)(ReturnT),
181 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200182 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200183 rtc::CritScope cs(&crit_);
184 if (thread_ == nullptr)
185 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700186 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
187 posted_from, callback_posted_from, thread_, functor, callback,
188 callback_host, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200189 return true;
190 }
191
192 // Call |functor| asynchronously calling |callback| when done. Overloaded for
193 // void return. Returns false if the thread has died.
194 template <class ReturnT, class FunctorT, class HostT>
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700195 bool AsyncInvoke(const Location& posted_from,
196 const Location& callback_posted_from,
197 const FunctorT& functor,
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200198 void (HostT::*callback)(),
199 HostT* callback_host,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200200 uint32_t id = 0) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200201 rtc::CritScope cs(&crit_);
202 if (thread_ == nullptr)
203 return false;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700204 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
205 posted_from, callback_posted_from, thread_, functor, callback,
206 callback_host, id);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200207 return true;
208 }
209
210 private:
211 // Callback when |thread_| is destroyed.
212 void ThreadDestroyed();
213
214 CriticalSection crit_;
215 Thread* thread_ GUARDED_BY(crit_);
216 AsyncInvoker invoker_ GUARDED_BY(crit_);
217};
218
219} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220
221#endif // WEBRTC_BASE_ASYNCINVOKER_H_