blob: 9fb328782c4d9fc55c12c49a0d5bff735fa6ec91 [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
Niels Möller0694ce72021-05-03 16:03:22 +020024class DEPRECATED_AsyncInvoker;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025
26// Helper class for AsyncInvoker. Runs a task and triggers a callback
27// on the calling thread if necessary.
28class AsyncClosure {
29 public:
Niels Möller0694ce72021-05-03 16:03:22 +020030 explicit AsyncClosure(DEPRECATED_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:
Niels Möller0694ce72021-05-03 16:03:22 +020037 DEPRECATED_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.
Tomas Gunnarssone249d192021-04-26 11:46:54 +020042 rtc::Ref<Event>::Ptr 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:
Niels Möller0694ce72021-05-03 16:03:22 +020049 explicit FireAndForgetAsyncClosure(DEPRECATED_AsyncInvoker* invoker,
50 FunctorT&& functor)
Cameron Pickettd132ce12018-03-12 16:07:37 -070051 : AsyncClosure(invoker), functor_(std::forward<FunctorT>(functor)) {}
Yves Gerey665174f2018-06-19 15:03:05 +020052 virtual void Execute() { functor_(); }
53
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054 private:
Cameron Pickettd132ce12018-03-12 16:07:37 -070055 typename std::decay<FunctorT>::type functor_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056};
57
58} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
Steve Anton10542f22019-01-11 09:11:00 -080060#endif // RTC_BASE_ASYNC_INVOKER_INL_H_