blob: 3ae2430bee0af437ecb67b3a11787e1f41be133e [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:
Yves Gerey665174f2018-06-19 15:03:05 +020051 explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker, FunctorT&& functor)
Cameron Pickettd132ce12018-03-12 16:07:37 -070052 : AsyncClosure(invoker), functor_(std::forward<FunctorT>(functor)) {}
Yves Gerey665174f2018-06-19 15:03:05 +020053 virtual void Execute() { functor_(); }
54
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 private:
Cameron Pickettd132ce12018-03-12 16:07:37 -070056 typename std::decay<FunctorT>::type functor_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057};
58
59} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020061#endif // RTC_BASE_ASYNCINVOKER_INL_H_