blob: 6151059ab5f55b26c1179d65e158919aa668f5ee [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_ASYNC_INVOKER_INL_H_
12#define RTC_BASE_ASYNC_INVOKER_INL_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Mirko Bonadeid9708072019-01-25 20:26:48 +010014#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/event.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/message_handler.h"
17#include "rtc_base/ref_counted_object.h"
Artem Titove41c4332018-07-25 15:04:28 +020018#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/thread.h"
20#include "rtc_base/thread_annotations.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022namespace rtc {
23
24class AsyncInvoker;
25
26// Helper class for AsyncInvoker. Runs a task and triggers a callback
27// on the calling thread if necessary.
28class AsyncClosure {
29 public:
deadbeef3af63b02017-08-08 17:59:47 -070030 explicit AsyncClosure(AsyncInvoker* invoker);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031 virtual ~AsyncClosure();
32 // Runs the asynchronous task, and triggers a callback to the calling
33 // thread if needed. Should be called from the target thread.
34 virtual void Execute() = 0;
35
36 protected:
37 AsyncInvoker* invoker_;
deadbeef3af63b02017-08-08 17:59:47 -070038 // Reference counted so that if the AsyncInvoker destructor finishes before
39 // an AsyncClosure's destructor that's about to call
40 // "invocation_complete_->Set()", it's not dereferenced after being
41 // destroyed.
42 scoped_refptr<RefCountedObject<Event>> invocation_complete_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043};
44
45// Simple closure that doesn't trigger a callback for the calling thread.
46template <class FunctorT>
47class FireAndForgetAsyncClosure : public AsyncClosure {
48 public:
Yves Gerey665174f2018-06-19 15:03:05 +020049 explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker, FunctorT&& functor)
Cameron Pickettd132ce12018-03-12 16:07:37 -070050 : AsyncClosure(invoker), functor_(std::forward<FunctorT>(functor)) {}
Yves Gerey665174f2018-06-19 15:03:05 +020051 virtual void Execute() { functor_(); }
52
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053 private:
Cameron Pickettd132ce12018-03-12 16:07:37 -070054 typename std::decay<FunctorT>::type functor_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055};
56
57} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058
Steve Anton10542f22019-01-11 09:11:00 -080059#endif // RTC_BASE_ASYNC_INVOKER_INL_H_