blob: 073d5bffac9b0f8d94eed3a852f11e62792d0b23 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_ASYNCINVOKER_INL_H_
12#define RTC_BASE_ASYNCINVOKER_INL_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/bind.h"
15#include "rtc_base/criticalsection.h"
16#include "rtc_base/event.h"
17#include "rtc_base/messagehandler.h"
18#include "rtc_base/refcountedobject.h"
19#include "rtc_base/scoped_ref_ptr.h"
20#include "rtc_base/sigslot.h"
21#include "rtc_base/thread.h"
22#include "rtc_base/thread_annotations.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024namespace rtc {
25
26class AsyncInvoker;
27
28// Helper class for AsyncInvoker. Runs a task and triggers a callback
29// on the calling thread if necessary.
30class AsyncClosure {
31 public:
deadbeef3af63b02017-08-08 17:59:47 -070032 explicit AsyncClosure(AsyncInvoker* invoker);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033 virtual ~AsyncClosure();
34 // Runs the asynchronous task, and triggers a callback to the calling
35 // thread if needed. Should be called from the target thread.
36 virtual void Execute() = 0;
37
38 protected:
39 AsyncInvoker* invoker_;
deadbeef3af63b02017-08-08 17:59:47 -070040 // Reference counted so that if the AsyncInvoker destructor finishes before
41 // an AsyncClosure's destructor that's about to call
42 // "invocation_complete_->Set()", it's not dereferenced after being
43 // destroyed.
44 scoped_refptr<RefCountedObject<Event>> invocation_complete_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045};
46
47// Simple closure that doesn't trigger a callback for the calling thread.
48template <class FunctorT>
49class FireAndForgetAsyncClosure : public AsyncClosure {
50 public:
51 explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker,
52 const FunctorT& functor)
53 : AsyncClosure(invoker), functor_(functor) {}
54 virtual void Execute() {
55 functor_();
56 }
57 private:
58 FunctorT functor_;
59};
60
61} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#endif // RTC_BASE_ASYNCINVOKER_INL_H_