3 * Copyright 2014 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_ASYNCINVOKER_INL_H_
29 #define TALK_BASE_ASYNCINVOKER_INL_H_
31 #include "talk/base/bind.h"
32 #include "talk/base/callback.h"
33 #include "talk/base/criticalsection.h"
34 #include "talk/base/messagehandler.h"
35 #include "talk/base/refcount.h"
36 #include "talk/base/scoped_ref_ptr.h"
37 #include "talk/base/sigslot.h"
38 #include "talk/base/thread.h"
44 // Helper class for AsyncInvoker. Runs a task and triggers a callback
45 // on the calling thread if necessary. Instances are ref-counted so their
46 // lifetime can be independent of AsyncInvoker.
47 class AsyncClosure : public RefCountInterface {
49 virtual ~AsyncClosure() {}
50 // Runs the asynchronous task, and triggers a callback to the calling
51 // thread if needed. Should be called from the target thread.
52 virtual void Execute() = 0;
55 // Simple closure that doesn't trigger a callback for the calling thread.
56 template <class FunctorT>
57 class FireAndForgetAsyncClosure : public AsyncClosure {
59 explicit FireAndForgetAsyncClosure(const FunctorT& functor)
60 : functor_(functor) {}
61 virtual void Execute() {
68 // Base class for closures that may trigger a callback for the calling thread.
69 // Listens for the "destroyed" signals from the calling thread and the invoker,
70 // and cancels the callback to the calling thread if either is destroyed.
71 class NotifyingAsyncClosureBase : public AsyncClosure,
72 public sigslot::has_slots<> {
74 virtual ~NotifyingAsyncClosureBase() { disconnect_all(); }
77 NotifyingAsyncClosureBase(AsyncInvoker* invoker, Thread* calling_thread);
78 void TriggerCallback();
79 void SetCallback(const Callback0<void>& callback) {
83 bool CallbackCanceled() const { return calling_thread_ == NULL; }
86 Callback0<void> callback_;
87 CriticalSection crit_;
88 AsyncInvoker* invoker_;
89 Thread* calling_thread_;
91 void CancelCallback();
94 // Closures that have a non-void return value and require a callback.
95 template <class ReturnT, class FunctorT, class HostT>
96 class NotifyingAsyncClosure : public NotifyingAsyncClosureBase {
98 NotifyingAsyncClosure(AsyncInvoker* invoker,
99 Thread* calling_thread,
100 const FunctorT& functor,
101 void (HostT::*callback)(ReturnT),
102 HostT* callback_host)
103 : NotifyingAsyncClosureBase(invoker, calling_thread),
106 callback_host_(callback_host) {}
107 virtual void Execute() {
108 ReturnT result = functor_();
109 if (!CallbackCanceled()) {
110 SetCallback(Callback0<void>(Bind(callback_, callback_host_, result)));
117 void (HostT::*callback_)(ReturnT);
118 HostT* callback_host_;
121 // Closures that have a void return value and require a callback.
122 template <class FunctorT, class HostT>
123 class NotifyingAsyncClosure<void, FunctorT, HostT>
124 : public NotifyingAsyncClosureBase {
126 NotifyingAsyncClosure(AsyncInvoker* invoker,
127 Thread* calling_thread,
128 const FunctorT& functor,
129 void (HostT::*callback)(),
130 HostT* callback_host)
131 : NotifyingAsyncClosureBase(invoker, calling_thread),
133 SetCallback(Callback0<void>(Bind(callback, callback_host)));
135 virtual void Execute() {
144 } // namespace talk_base
146 #endif // TALK_BASE_ASYNCINVOKER_INL_H_