blob: 0dadc0f3b34ecb2a9dba314cb2f8ac58270d92d8 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 04:47:3111#ifndef RTC_BASE_ASYNCINVOKER_INL_H_
12#define RTC_BASE_ASYNCINVOKER_INL_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Mirko Bonadei92ea95e2017-09-15 04:47:3114#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"
Artem Titove41c4332018-07-25 13:04:2820#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "rtc_base/thread.h"
22#include "rtc_base/thread_annotations.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2623
Henrik Kjellanderec78f1c2017-06-29 05:52:5024namespace 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-09 00:59:4732 explicit AsyncClosure(AsyncInvoker* invoker);
Henrik Kjellanderec78f1c2017-06-29 05:52:5033 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-09 00:59:4740 // 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 05:52:5045};
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 13:03:0551 explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker, FunctorT&& functor)
Cameron Pickettd132ce12018-03-12 23:07:3752 : AsyncClosure(invoker), functor_(std::forward<FunctorT>(functor)) {}
Yves Gerey665174f2018-06-19 13:03:0553 virtual void Execute() { functor_(); }
54
Henrik Kjellanderec78f1c2017-06-29 05:52:5055 private:
Cameron Pickettd132ce12018-03-12 23:07:3756 typename std::decay<FunctorT>::type functor_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5057};
58
59} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:2660
Mirko Bonadei92ea95e2017-09-15 04:47:3161#endif // RTC_BASE_ASYNCINVOKER_INL_H_