• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 RTC_BASE_ASYNC_INVOKER_INL_H_
12 #define RTC_BASE_ASYNC_INVOKER_INL_H_
13 
14 #include "api/scoped_refptr.h"
15 #include "rtc_base/bind.h"
16 #include "rtc_base/event.h"
17 #include "rtc_base/message_handler.h"
18 #include "rtc_base/ref_counted_object.h"
19 #include "rtc_base/third_party/sigslot/sigslot.h"
20 #include "rtc_base/thread.h"
21 #include "rtc_base/thread_annotations.h"
22 
23 namespace rtc {
24 
25 class AsyncInvoker;
26 
27 // Helper class for AsyncInvoker. Runs a task and triggers a callback
28 // on the calling thread if necessary.
29 class AsyncClosure {
30  public:
31   explicit AsyncClosure(AsyncInvoker* invoker);
32   virtual ~AsyncClosure();
33   // 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;
36 
37  protected:
38   AsyncInvoker* invoker_;
39   // Reference counted so that if the AsyncInvoker destructor finishes before
40   // an AsyncClosure's destructor that's about to call
41   // "invocation_complete_->Set()", it's not dereferenced after being
42   // destroyed.
43   scoped_refptr<RefCountedObject<Event>> invocation_complete_;
44 };
45 
46 // Simple closure that doesn't trigger a callback for the calling thread.
47 template <class FunctorT>
48 class FireAndForgetAsyncClosure : public AsyncClosure {
49  public:
FireAndForgetAsyncClosure(AsyncInvoker * invoker,FunctorT && functor)50   explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker, FunctorT&& functor)
51       : AsyncClosure(invoker), functor_(std::forward<FunctorT>(functor)) {}
Execute()52   virtual void Execute() { functor_(); }
53 
54  private:
55   typename std::decay<FunctorT>::type functor_;
56 };
57 
58 }  // namespace rtc
59 
60 #endif  // RTC_BASE_ASYNC_INVOKER_INL_H_
61