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