henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/async_invoker.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/logging.h" |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 15 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | namespace rtc { |
| 17 | |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame^] | 18 | DEPRECATED_AsyncInvoker::DEPRECATED_AsyncInvoker() |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 19 | : pending_invocations_(0), |
Tomas Gunnarsson | e249d19 | 2021-04-26 11:46:54 +0200 | [diff] [blame] | 20 | invocation_complete_(make_ref_counted<Event>()), |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 21 | destroying_(false) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame^] | 23 | DEPRECATED_AsyncInvoker::~DEPRECATED_AsyncInvoker() { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 24 | destroying_.store(true, std::memory_order_relaxed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | // Messages for this need to be cleared *before* our destructor is complete. |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 26 | ThreadManager::Clear(this); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 27 | // And we need to wait for any invocations that are still in progress on |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 28 | // other threads. Using memory_order_acquire for synchronization with |
| 29 | // AsyncClosure destructors. |
| 30 | while (pending_invocations_.load(std::memory_order_acquire) > 0) { |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 31 | // If the destructor was called while AsyncInvoke was being called by |
| 32 | // another thread, WITHIN an AsyncInvoked functor, it may do another |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 33 | // Thread::Post even after we called ThreadManager::Clear(this). So |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 34 | // we need to keep calling Clear to discard these posts. |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 35 | Thread::Current()->Clear(this); |
| 36 | invocation_complete_->Wait(Event::kForever); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 37 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame^] | 40 | void DEPRECATED_AsyncInvoker::OnMessage(Message* msg) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | // Get the AsyncClosure shared ptr from this message's data. |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 42 | ScopedMessageData<AsyncClosure>* data = |
| 43 | static_cast<ScopedMessageData<AsyncClosure>*>(msg->pdata); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | // Execute the closure and trigger the return message if needed. |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 45 | data->inner_data().Execute(); |
| 46 | delete data; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame^] | 49 | void DEPRECATED_AsyncInvoker::Flush(Thread* thread, |
| 50 | uint32_t id /*= MQID_ANY*/) { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 51 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | |
| 56 | // Run this on |thread| to reduce the number of context switches. |
| 57 | if (Thread::Current() != thread) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 58 | thread->Invoke<void>(RTC_FROM_HERE, |
Niels Möller | 1a29a5d | 2021-01-18 11:35:23 +0100 | [diff] [blame] | 59 | [this, thread, id] { Flush(thread, id); }); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | 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 Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 67 | thread->Send(it->posted_from, it->phandler, it->message_id, it->pdata); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame^] | 71 | void DEPRECATED_AsyncInvoker::Clear() { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 72 | ThreadManager::Clear(this); |
Chris Dziemborowicz | c38d320 | 2018-01-31 12:52:24 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame^] | 75 | void DEPRECATED_AsyncInvoker::DoInvoke(const Location& posted_from, |
| 76 | Thread* thread, |
| 77 | std::unique_ptr<AsyncClosure> closure, |
| 78 | uint32_t id) { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 79 | 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 84 | RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | return; |
| 86 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 87 | thread->Post(posted_from, this, id, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 88 | new ScopedMessageData<AsyncClosure>(std::move(closure))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame^] | 91 | void 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) { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 97 | if (destroying_.load(std::memory_order_relaxed)) { |
| 98 | // See above comment. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 99 | RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 100 | return; |
| 101 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 102 | thread->PostDelayed(posted_from, delay_ms, this, id, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 103 | new ScopedMessageData<AsyncClosure>(std::move(closure))); |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Niels Möller | 0694ce7 | 2021-05-03 16:03:22 +0200 | [diff] [blame^] | 106 | AsyncClosure::AsyncClosure(DEPRECATED_AsyncInvoker* invoker) |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 107 | : invoker_(invoker), invocation_complete_(invoker_->invocation_complete_) { |
| 108 | invoker_->pending_invocations_.fetch_add(1, std::memory_order_relaxed); |
| 109 | } |
| 110 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 111 | AsyncClosure::~AsyncClosure() { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 112 | // 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(); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 124 | } |
| 125 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | } // namespace rtc |