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 | |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 18 | AsyncInvoker::AsyncInvoker() |
| 19 | : pending_invocations_(0), |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 20 | invocation_complete_(new RefCountedObject<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 | |
| 23 | AsyncInvoker::~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. |
| 26 | MessageQueueManager::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 |
| 33 | // Thread::Post even after we called MessageQueueManager::Clear(this). So |
| 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 | |
| 40 | void AsyncInvoker::OnMessage(Message* msg) { |
| 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 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 49 | void AsyncInvoker::Flush(Thread* thread, uint32_t id /*= MQID_ANY*/) { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 50 | // If the destructor is waiting for invocations to finish, don't start |
| 51 | // running even more tasks. |
| 52 | if (destroying_.load(std::memory_order_relaxed)) |
| 53 | return; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 54 | |
| 55 | // Run this on |thread| to reduce the number of context switches. |
| 56 | if (Thread::Current() != thread) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 57 | thread->Invoke<void>(RTC_FROM_HERE, |
| 58 | Bind(&AsyncInvoker::Flush, this, thread, id)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | |
| 62 | MessageList removed; |
| 63 | thread->Clear(this, id, &removed); |
| 64 | for (MessageList::iterator it = removed.begin(); it != removed.end(); ++it) { |
| 65 | // This message was pending on this thread, so run it now. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 66 | 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] | 67 | } |
| 68 | } |
| 69 | |
Chris Dziemborowicz | c38d320 | 2018-01-31 12:52:24 -0800 | [diff] [blame] | 70 | void AsyncInvoker::Clear() { |
| 71 | MessageQueueManager::Clear(this); |
| 72 | } |
| 73 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 74 | void AsyncInvoker::DoInvoke(const Location& posted_from, |
| 75 | Thread* thread, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 76 | std::unique_ptr<AsyncClosure> closure, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 77 | uint32_t id) { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 78 | if (destroying_.load(std::memory_order_relaxed)) { |
| 79 | // Note that this may be expected, if the application is AsyncInvoking |
| 80 | // tasks that AsyncInvoke other tasks. But otherwise it indicates a race |
| 81 | // between a thread destroying the AsyncInvoker and a thread still trying |
| 82 | // to use it. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 83 | RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | return; |
| 85 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 86 | thread->Post(posted_from, this, id, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 87 | new ScopedMessageData<AsyncClosure>(std::move(closure))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 90 | void AsyncInvoker::DoInvokeDelayed(const Location& posted_from, |
| 91 | Thread* thread, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 92 | std::unique_ptr<AsyncClosure> closure, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 93 | uint32_t delay_ms, |
| 94 | uint32_t id) { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 95 | if (destroying_.load(std::memory_order_relaxed)) { |
| 96 | // See above comment. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 97 | RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 98 | return; |
| 99 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 100 | thread->PostDelayed(posted_from, delay_ms, this, id, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 101 | new ScopedMessageData<AsyncClosure>(std::move(closure))); |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 104 | GuardedAsyncInvoker::GuardedAsyncInvoker() : thread_(Thread::Current()) { |
| 105 | thread_->SignalQueueDestroyed.connect(this, |
| 106 | &GuardedAsyncInvoker::ThreadDestroyed); |
| 107 | } |
| 108 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 109 | GuardedAsyncInvoker::~GuardedAsyncInvoker() {} |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 110 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 111 | bool GuardedAsyncInvoker::Flush(uint32_t id) { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 112 | CritScope cs(&crit_); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 113 | if (thread_ == nullptr) |
| 114 | return false; |
| 115 | invoker_.Flush(thread_, id); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | void GuardedAsyncInvoker::ThreadDestroyed() { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 120 | CritScope cs(&crit_); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 121 | // We should never get more than one notification about the thread dying. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 122 | RTC_DCHECK(thread_ != nullptr); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 123 | thread_ = nullptr; |
| 124 | } |
| 125 | |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 126 | AsyncClosure::AsyncClosure(AsyncInvoker* invoker) |
| 127 | : invoker_(invoker), invocation_complete_(invoker_->invocation_complete_) { |
| 128 | invoker_->pending_invocations_.fetch_add(1, std::memory_order_relaxed); |
| 129 | } |
| 130 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 131 | AsyncClosure::~AsyncClosure() { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 132 | // Using memory_order_release for synchronization with the AsyncInvoker |
| 133 | // destructor. |
| 134 | invoker_->pending_invocations_.fetch_sub(1, std::memory_order_release); |
| 135 | |
| 136 | // After |pending_invocations_| is decremented, we may need to signal |
| 137 | // |invocation_complete_| in case the AsyncInvoker is being destroyed and |
| 138 | // waiting for pending tasks to complete. |
| 139 | // |
| 140 | // It's also possible that the destructor finishes before "Set()" is called, |
| 141 | // which is safe because the event is reference counted (and in a thread-safe |
| 142 | // way). |
| 143 | invocation_complete_->Set(); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 144 | } |
| 145 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 146 | } // namespace rtc |