blob: 93e7671a9531034e34727a08df593543fbdebbfb [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_INL_H_
12#define WEBRTC_BASE_ASYNCINVOKER_INL_H_
13
deadbeef162cb532017-02-23 17:10:07 -080014#include "webrtc/base/atomicops.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include "webrtc/base/bind.h"
16#include "webrtc/base/callback.h"
17#include "webrtc/base/criticalsection.h"
18#include "webrtc/base/messagehandler.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019#include "webrtc/base/sigslot.h"
20#include "webrtc/base/thread.h"
deadbeefb85a8882017-03-03 10:33:18 -080021#include "webrtc/base/thread_annotations.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
23namespace rtc {
24
25class AsyncInvoker;
26
27// Helper class for AsyncInvoker. Runs a task and triggers a callback
deadbeefa8bc1a12017-02-17 18:06:26 -080028// on the calling thread if necessary.
29class AsyncClosure {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030 public:
deadbeef162cb532017-02-23 17:10:07 -080031 explicit AsyncClosure(AsyncInvoker* invoker) : invoker_(invoker) {}
32 virtual ~AsyncClosure();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033 // Runs the asynchronous task, and triggers a callback to the calling
34 // thread if needed. Should be called from the target thread.
35 virtual void Execute() = 0;
deadbeef162cb532017-02-23 17:10:07 -080036
37 protected:
38 AsyncInvoker* invoker_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039};
40
41// Simple closure that doesn't trigger a callback for the calling thread.
42template <class FunctorT>
43class FireAndForgetAsyncClosure : public AsyncClosure {
44 public:
deadbeef162cb532017-02-23 17:10:07 -080045 explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker,
46 const FunctorT& functor)
47 : AsyncClosure(invoker), functor_(functor) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 virtual void Execute() {
49 functor_();
50 }
51 private:
52 FunctorT functor_;
53};
54
55// Base class for closures that may trigger a callback for the calling thread.
56// Listens for the "destroyed" signals from the calling thread and the invoker,
57// and cancels the callback to the calling thread if either is destroyed.
58class NotifyingAsyncClosureBase : public AsyncClosure,
59 public sigslot::has_slots<> {
60 public:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000061 ~NotifyingAsyncClosureBase() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062
63 protected:
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070064 NotifyingAsyncClosureBase(AsyncInvoker* invoker,
65 const Location& callback_posted_from,
66 Thread* calling_thread);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067 void TriggerCallback();
68 void SetCallback(const Callback0<void>& callback) {
69 CritScope cs(&crit_);
70 callback_ = callback;
71 }
deadbeefb85a8882017-03-03 10:33:18 -080072 bool CallbackCanceled() const {
73 CritScope cs(&crit_);
74 return calling_thread_ == nullptr;
75 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076
77 private:
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070078 Location callback_posted_from_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 CriticalSection crit_;
deadbeefb85a8882017-03-03 10:33:18 -080080 Callback0<void> callback_ GUARDED_BY(crit_);
81 Thread* calling_thread_ GUARDED_BY(crit_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082
83 void CancelCallback();
84};
85
86// Closures that have a non-void return value and require a callback.
87template <class ReturnT, class FunctorT, class HostT>
88class NotifyingAsyncClosure : public NotifyingAsyncClosureBase {
89 public:
90 NotifyingAsyncClosure(AsyncInvoker* invoker,
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070091 const Location& callback_posted_from,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092 Thread* calling_thread,
93 const FunctorT& functor,
94 void (HostT::*callback)(ReturnT),
95 HostT* callback_host)
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070096 : NotifyingAsyncClosureBase(invoker,
97 callback_posted_from,
98 calling_thread),
99 functor_(functor),
100 callback_(callback),
101 callback_host_(callback_host) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102 virtual void Execute() {
103 ReturnT result = functor_();
104 if (!CallbackCanceled()) {
105 SetCallback(Callback0<void>(Bind(callback_, callback_host_, result)));
106 TriggerCallback();
107 }
108 }
109
110 private:
111 FunctorT functor_;
112 void (HostT::*callback_)(ReturnT);
113 HostT* callback_host_;
114};
115
116// Closures that have a void return value and require a callback.
117template <class FunctorT, class HostT>
118class NotifyingAsyncClosure<void, FunctorT, HostT>
119 : public NotifyingAsyncClosureBase {
120 public:
121 NotifyingAsyncClosure(AsyncInvoker* invoker,
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700122 const Location& callback_posted_from,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 Thread* calling_thread,
124 const FunctorT& functor,
125 void (HostT::*callback)(),
126 HostT* callback_host)
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700127 : NotifyingAsyncClosureBase(invoker,
128 callback_posted_from,
129 calling_thread),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 functor_(functor) {
131 SetCallback(Callback0<void>(Bind(callback, callback_host)));
132 }
133 virtual void Execute() {
134 functor_();
135 TriggerCallback();
136 }
137
138 private:
139 FunctorT functor_;
140};
141
142} // namespace rtc
143
144#endif // WEBRTC_BASE_ASYNCINVOKER_INL_H_