henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef RTC_BASE_ASYNCINVOKER_INL_H_ |
| 12 | #define RTC_BASE_ASYNCINVOKER_INL_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 14 | #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 Titov | e41c433 | 2018-07-25 13:04:28 | [diff] [blame] | 20 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 21 | #include "rtc_base/thread.h" |
| 22 | #include "rtc_base/thread_annotations.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 23 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 24 | namespace rtc { |
| 25 | |
| 26 | class AsyncInvoker; |
| 27 | |
| 28 | // Helper class for AsyncInvoker. Runs a task and triggers a callback |
| 29 | // on the calling thread if necessary. |
| 30 | class AsyncClosure { |
| 31 | public: |
deadbeef | 3af63b0 | 2017-08-09 00:59:47 | [diff] [blame] | 32 | explicit AsyncClosure(AsyncInvoker* invoker); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 33 | 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_; |
deadbeef | 3af63b0 | 2017-08-09 00:59:47 | [diff] [blame] | 40 | // 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 Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | // Simple closure that doesn't trigger a callback for the calling thread. |
| 48 | template <class FunctorT> |
| 49 | class FireAndForgetAsyncClosure : public AsyncClosure { |
| 50 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 51 | explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker, FunctorT&& functor) |
Cameron Pickett | d132ce1 | 2018-03-12 23:07:37 | [diff] [blame] | 52 | : AsyncClosure(invoker), functor_(std::forward<FunctorT>(functor)) {} |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 53 | virtual void Execute() { functor_(); } |
| 54 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 55 | private: |
Cameron Pickett | d132ce1 | 2018-03-12 23:07:37 | [diff] [blame] | 56 | typename std::decay<FunctorT>::type functor_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 60 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 61 | #endif // RTC_BASE_ASYNCINVOKER_INL_H_ |