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 | |
| 11 | #include "webrtc/base/asyncinvoker.h" |
| 12 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 13 | #include "webrtc/base/atomicops.h" |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 14 | #include "webrtc/base/checks.h" |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 15 | #include "webrtc/base/logging.h" |
| 16 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | namespace rtc { |
| 18 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 19 | AsyncInvoker::AsyncInvoker() : invocation_complete_(false, false) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 20 | |
| 21 | AsyncInvoker::~AsyncInvoker() { |
| 22 | destroying_ = true; |
| 23 | SignalInvokerDestroyed(); |
| 24 | // Messages for this need to be cleared *before* our destructor is complete. |
| 25 | MessageQueueManager::Clear(this); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 26 | // And we need to wait for any invocations that are still in progress on |
| 27 | // other threads. |
| 28 | while (AtomicOps::AcquireLoad(&pending_invocations_)) { |
| 29 | // If the destructor was called while AsyncInvoke was being called by |
| 30 | // another thread, WITHIN an AsyncInvoked functor, it may do another |
| 31 | // Thread::Post even after we called MessageQueueManager::Clear(this). So |
| 32 | // we need to keep calling Clear to discard these posts. |
| 33 | MessageQueueManager::Clear(this); |
| 34 | invocation_complete_.Wait(Event::kForever); |
| 35 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void AsyncInvoker::OnMessage(Message* msg) { |
| 39 | // Get the AsyncClosure shared ptr from this message's data. |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 40 | ScopedMessageData<AsyncClosure>* data = |
| 41 | static_cast<ScopedMessageData<AsyncClosure>*>(msg->pdata); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 42 | // Execute the closure and trigger the return message if needed. |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 43 | data->inner_data().Execute(); |
| 44 | delete data; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 47 | void AsyncInvoker::Flush(Thread* thread, uint32_t id /*= MQID_ANY*/) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 48 | if (destroying_) return; |
| 49 | |
| 50 | // Run this on |thread| to reduce the number of context switches. |
| 51 | if (Thread::Current() != thread) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 52 | thread->Invoke<void>(RTC_FROM_HERE, |
| 53 | Bind(&AsyncInvoker::Flush, this, thread, id)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | |
| 57 | MessageList removed; |
| 58 | thread->Clear(this, id, &removed); |
| 59 | for (MessageList::iterator it = removed.begin(); it != removed.end(); ++it) { |
| 60 | // This message was pending on this thread, so run it now. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 61 | 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] | 62 | } |
| 63 | } |
| 64 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 65 | void AsyncInvoker::DoInvoke(const Location& posted_from, |
| 66 | Thread* thread, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 67 | std::unique_ptr<AsyncClosure> closure, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 68 | uint32_t id) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | if (destroying_) { |
| 70 | LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | return; |
| 72 | } |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 73 | AtomicOps::Increment(&pending_invocations_); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 74 | thread->Post(posted_from, this, id, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 75 | new ScopedMessageData<AsyncClosure>(std::move(closure))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 78 | void AsyncInvoker::DoInvokeDelayed(const Location& posted_from, |
| 79 | Thread* thread, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 80 | std::unique_ptr<AsyncClosure> closure, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 81 | uint32_t delay_ms, |
| 82 | uint32_t id) { |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 83 | if (destroying_) { |
| 84 | LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
| 85 | return; |
| 86 | } |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 87 | AtomicOps::Increment(&pending_invocations_); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 88 | thread->PostDelayed(posted_from, delay_ms, this, id, |
deadbeef | a8bc1a1 | 2017-02-17 18:06:26 -0800 | [diff] [blame] | 89 | new ScopedMessageData<AsyncClosure>(std::move(closure))); |
Guo-wei Shieh | dc13abc | 2015-06-18 14:44:41 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 92 | GuardedAsyncInvoker::GuardedAsyncInvoker() : thread_(Thread::Current()) { |
| 93 | thread_->SignalQueueDestroyed.connect(this, |
| 94 | &GuardedAsyncInvoker::ThreadDestroyed); |
| 95 | } |
| 96 | |
| 97 | GuardedAsyncInvoker::~GuardedAsyncInvoker() { |
| 98 | } |
| 99 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 100 | bool GuardedAsyncInvoker::Flush(uint32_t id) { |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 101 | rtc::CritScope cs(&crit_); |
| 102 | if (thread_ == nullptr) |
| 103 | return false; |
| 104 | invoker_.Flush(thread_, id); |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | void GuardedAsyncInvoker::ThreadDestroyed() { |
| 109 | rtc::CritScope cs(&crit_); |
| 110 | // We should never get more than one notification about the thread dying. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 111 | RTC_DCHECK(thread_ != nullptr); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 112 | thread_ = nullptr; |
| 113 | } |
| 114 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 115 | AsyncClosure::~AsyncClosure() { |
| 116 | AtomicOps::Decrement(&invoker_->pending_invocations_); |
| 117 | invoker_->invocation_complete_.Set(); |
| 118 | } |
| 119 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 120 | NotifyingAsyncClosureBase::NotifyingAsyncClosureBase( |
| 121 | AsyncInvoker* invoker, |
| 122 | const Location& callback_posted_from, |
| 123 | Thread* calling_thread) |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 124 | : AsyncClosure(invoker), |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 125 | callback_posted_from_(callback_posted_from), |
| 126 | calling_thread_(calling_thread) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | calling_thread->SignalQueueDestroyed.connect( |
| 128 | this, &NotifyingAsyncClosureBase::CancelCallback); |
| 129 | invoker->SignalInvokerDestroyed.connect( |
| 130 | this, &NotifyingAsyncClosureBase::CancelCallback); |
| 131 | } |
| 132 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 133 | NotifyingAsyncClosureBase::~NotifyingAsyncClosureBase() { |
| 134 | disconnect_all(); |
| 135 | } |
| 136 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 137 | void NotifyingAsyncClosureBase::TriggerCallback() { |
| 138 | CritScope cs(&crit_); |
| 139 | if (!CallbackCanceled() && !callback_.empty()) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 140 | invoker_->AsyncInvoke<void>(callback_posted_from_, calling_thread_, |
| 141 | callback_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | void NotifyingAsyncClosureBase::CancelCallback() { |
| 146 | // If the callback is triggering when this is called, block the |
| 147 | // destructor of the dying object here by waiting until the callback |
| 148 | // is done triggering. |
| 149 | CritScope cs(&crit_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame^] | 150 | // calling_thread_ == nullptr means do not trigger the callback. |
| 151 | calling_thread_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | } // namespace rtc |