blob: c3691b5659b78935fe6381fd744622cbc11030ef [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
14#include "webrtc/base/bind.h"
15#include "webrtc/base/callback.h"
16#include "webrtc/base/criticalsection.h"
17#include "webrtc/base/messagehandler.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include "webrtc/base/sigslot.h"
19#include "webrtc/base/thread.h"
20
21namespace rtc {
22
23class AsyncInvoker;
24
25// Helper class for AsyncInvoker. Runs a task and triggers a callback
deadbeefa5a47292017-02-17 15:19:19 -080026// on the calling thread if necessary.
27class AsyncClosure {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028 public:
deadbeefa5a47292017-02-17 15:19:19 -080029 virtual ~AsyncClosure() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030 // Runs the asynchronous task, and triggers a callback to the calling
31 // thread if needed. Should be called from the target thread.
32 virtual void Execute() = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033};
34
35// Simple closure that doesn't trigger a callback for the calling thread.
36template <class FunctorT>
37class FireAndForgetAsyncClosure : public AsyncClosure {
38 public:
39 explicit FireAndForgetAsyncClosure(const FunctorT& functor)
40 : functor_(functor) {}
41 virtual void Execute() {
42 functor_();
43 }
44 private:
45 FunctorT functor_;
46};
47
48// Base class for closures that may trigger a callback for the calling thread.
49// Listens for the "destroyed" signals from the calling thread and the invoker,
50// and cancels the callback to the calling thread if either is destroyed.
51class NotifyingAsyncClosureBase : public AsyncClosure,
52 public sigslot::has_slots<> {
53 public:
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000054 ~NotifyingAsyncClosureBase() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055
56 protected:
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070057 NotifyingAsyncClosureBase(AsyncInvoker* invoker,
58 const Location& callback_posted_from,
59 Thread* calling_thread);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 void TriggerCallback();
61 void SetCallback(const Callback0<void>& callback) {
62 CritScope cs(&crit_);
63 callback_ = callback;
64 }
65 bool CallbackCanceled() const { return calling_thread_ == NULL; }
66
67 private:
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070068 AsyncInvoker* invoker_;
69 Location callback_posted_from_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 Callback0<void> callback_;
71 CriticalSection crit_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072 Thread* calling_thread_;
73
74 void CancelCallback();
75};
76
77// Closures that have a non-void return value and require a callback.
78template <class ReturnT, class FunctorT, class HostT>
79class NotifyingAsyncClosure : public NotifyingAsyncClosureBase {
80 public:
81 NotifyingAsyncClosure(AsyncInvoker* invoker,
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070082 const Location& callback_posted_from,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083 Thread* calling_thread,
84 const FunctorT& functor,
85 void (HostT::*callback)(ReturnT),
86 HostT* callback_host)
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070087 : NotifyingAsyncClosureBase(invoker,
88 callback_posted_from,
89 calling_thread),
90 functor_(functor),
91 callback_(callback),
92 callback_host_(callback_host) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093 virtual void Execute() {
94 ReturnT result = functor_();
95 if (!CallbackCanceled()) {
96 SetCallback(Callback0<void>(Bind(callback_, callback_host_, result)));
97 TriggerCallback();
98 }
99 }
100
101 private:
102 FunctorT functor_;
103 void (HostT::*callback_)(ReturnT);
104 HostT* callback_host_;
105};
106
107// Closures that have a void return value and require a callback.
108template <class FunctorT, class HostT>
109class NotifyingAsyncClosure<void, FunctorT, HostT>
110 : public NotifyingAsyncClosureBase {
111 public:
112 NotifyingAsyncClosure(AsyncInvoker* invoker,
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700113 const Location& callback_posted_from,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 Thread* calling_thread,
115 const FunctorT& functor,
116 void (HostT::*callback)(),
117 HostT* callback_host)
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700118 : NotifyingAsyncClosureBase(invoker,
119 callback_posted_from,
120 calling_thread),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121 functor_(functor) {
122 SetCallback(Callback0<void>(Bind(callback, callback_host)));
123 }
124 virtual void Execute() {
125 functor_();
126 TriggerCallback();
127 }
128
129 private:
130 FunctorT functor_;
131};
132
133} // namespace rtc
134
135#endif // WEBRTC_BASE_ASYNCINVOKER_INL_H_