blob: 87d039373d9def1cef322b24a286b76ddacc4544 [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#include "rtc_base/async_invoker.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
14#include "rtc_base/logging.h"
Per33544192015-04-02 12:30:51 +020015
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016namespace rtc {
17
Niels Möller0694ce72021-05-03 16:03:22 +020018DEPRECATED_AsyncInvoker::DEPRECATED_AsyncInvoker()
deadbeef3af63b02017-08-08 17:59:47 -070019 : pending_invocations_(0),
Tomas Gunnarssone249d192021-04-26 11:46:54 +020020 invocation_complete_(make_ref_counted<Event>()),
deadbeef3af63b02017-08-08 17:59:47 -070021 destroying_(false) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
Niels Möller0694ce72021-05-03 16:03:22 +020023DEPRECATED_AsyncInvoker::~DEPRECATED_AsyncInvoker() {
deadbeef3af63b02017-08-08 17:59:47 -070024 destroying_.store(true, std::memory_order_relaxed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025 // Messages for this need to be cleared *before* our destructor is complete.
Sebastian Jansson6ea2c6a2020-01-13 14:07:22 +010026 ThreadManager::Clear(this);
deadbeef162cb532017-02-23 17:10:07 -080027 // And we need to wait for any invocations that are still in progress on
deadbeef3af63b02017-08-08 17:59:47 -070028 // other threads. Using memory_order_acquire for synchronization with
29 // AsyncClosure destructors.
30 while (pending_invocations_.load(std::memory_order_acquire) > 0) {
deadbeef162cb532017-02-23 17:10:07 -080031 // If the destructor was called while AsyncInvoke was being called by
32 // another thread, WITHIN an AsyncInvoked functor, it may do another
Sebastian Jansson6ea2c6a2020-01-13 14:07:22 +010033 // Thread::Post even after we called ThreadManager::Clear(this). So
deadbeef162cb532017-02-23 17:10:07 -080034 // we need to keep calling Clear to discard these posts.
deadbeef3af63b02017-08-08 17:59:47 -070035 Thread::Current()->Clear(this);
36 invocation_complete_->Wait(Event::kForever);
deadbeef162cb532017-02-23 17:10:07 -080037 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038}
39
Niels Möller0694ce72021-05-03 16:03:22 +020040void DEPRECATED_AsyncInvoker::OnMessage(Message* msg) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041 // Get the AsyncClosure shared ptr from this message's data.
deadbeefa8bc1a12017-02-17 18:06:26 -080042 ScopedMessageData<AsyncClosure>* data =
43 static_cast<ScopedMessageData<AsyncClosure>*>(msg->pdata);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044 // Execute the closure and trigger the return message if needed.
deadbeefa8bc1a12017-02-17 18:06:26 -080045 data->inner_data().Execute();
46 delete data;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047}
48
Niels Möller0694ce72021-05-03 16:03:22 +020049void DEPRECATED_AsyncInvoker::Flush(Thread* thread,
50 uint32_t id /*= MQID_ANY*/) {
deadbeef3af63b02017-08-08 17:59:47 -070051 // If the destructor is waiting for invocations to finish, don't start
52 // running even more tasks.
53 if (destroying_.load(std::memory_order_relaxed))
54 return;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055
56 // Run this on |thread| to reduce the number of context switches.
57 if (Thread::Current() != thread) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070058 thread->Invoke<void>(RTC_FROM_HERE,
Niels Möller1a29a5d2021-01-18 11:35:23 +010059 [this, thread, id] { Flush(thread, id); });
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 return;
61 }
62
63 MessageList removed;
64 thread->Clear(this, id, &removed);
65 for (MessageList::iterator it = removed.begin(); it != removed.end(); ++it) {
66 // This message was pending on this thread, so run it now.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070067 thread->Send(it->posted_from, it->phandler, it->message_id, it->pdata);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068 }
69}
70
Niels Möller0694ce72021-05-03 16:03:22 +020071void DEPRECATED_AsyncInvoker::Clear() {
Sebastian Jansson6ea2c6a2020-01-13 14:07:22 +010072 ThreadManager::Clear(this);
Chris Dziemborowiczc38d3202018-01-31 12:52:24 -080073}
74
Niels Möller0694ce72021-05-03 16:03:22 +020075void DEPRECATED_AsyncInvoker::DoInvoke(const Location& posted_from,
76 Thread* thread,
77 std::unique_ptr<AsyncClosure> closure,
78 uint32_t id) {
deadbeef3af63b02017-08-08 17:59:47 -070079 if (destroying_.load(std::memory_order_relaxed)) {
80 // Note that this may be expected, if the application is AsyncInvoking
81 // tasks that AsyncInvoke other tasks. But otherwise it indicates a race
82 // between a thread destroying the AsyncInvoker and a thread still trying
83 // to use it.
Mirko Bonadei675513b2017-11-09 11:09:25 +010084 RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085 return;
86 }
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070087 thread->Post(posted_from, this, id,
deadbeefa8bc1a12017-02-17 18:06:26 -080088 new ScopedMessageData<AsyncClosure>(std::move(closure)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089}
90
Niels Möller0694ce72021-05-03 16:03:22 +020091void DEPRECATED_AsyncInvoker::DoInvokeDelayed(
92 const Location& posted_from,
93 Thread* thread,
94 std::unique_ptr<AsyncClosure> closure,
95 uint32_t delay_ms,
96 uint32_t id) {
deadbeef3af63b02017-08-08 17:59:47 -070097 if (destroying_.load(std::memory_order_relaxed)) {
98 // See above comment.
Mirko Bonadei675513b2017-11-09 11:09:25 +010099 RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -0700100 return;
101 }
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700102 thread->PostDelayed(posted_from, delay_ms, this, id,
deadbeefa8bc1a12017-02-17 18:06:26 -0800103 new ScopedMessageData<AsyncClosure>(std::move(closure)));
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -0700104}
105
Niels Möller0694ce72021-05-03 16:03:22 +0200106AsyncClosure::AsyncClosure(DEPRECATED_AsyncInvoker* invoker)
deadbeef3af63b02017-08-08 17:59:47 -0700107 : invoker_(invoker), invocation_complete_(invoker_->invocation_complete_) {
108 invoker_->pending_invocations_.fetch_add(1, std::memory_order_relaxed);
109}
110
deadbeef162cb532017-02-23 17:10:07 -0800111AsyncClosure::~AsyncClosure() {
deadbeef3af63b02017-08-08 17:59:47 -0700112 // Using memory_order_release for synchronization with the AsyncInvoker
113 // destructor.
114 invoker_->pending_invocations_.fetch_sub(1, std::memory_order_release);
115
116 // After |pending_invocations_| is decremented, we may need to signal
117 // |invocation_complete_| in case the AsyncInvoker is being destroyed and
118 // waiting for pending tasks to complete.
119 //
120 // It's also possible that the destructor finishes before "Set()" is called,
121 // which is safe because the event is reference counted (and in a thread-safe
122 // way).
123 invocation_complete_->Set();
deadbeef162cb532017-02-23 17:10:07 -0800124}
125
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126} // namespace rtc