henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 | |
Per | 3354419 | 2015-04-02 10:30:51 | [diff] [blame] | 13 | #include "webrtc/base/logging.h" |
| 14 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 15 | namespace rtc { |
| 16 | |
| 17 | AsyncInvoker::AsyncInvoker() : destroying_(false) {} |
| 18 | |
| 19 | AsyncInvoker::~AsyncInvoker() { |
| 20 | destroying_ = true; |
| 21 | SignalInvokerDestroyed(); |
| 22 | // Messages for this need to be cleared *before* our destructor is complete. |
| 23 | MessageQueueManager::Clear(this); |
| 24 | } |
| 25 | |
| 26 | void AsyncInvoker::OnMessage(Message* msg) { |
| 27 | // Get the AsyncClosure shared ptr from this message's data. |
| 28 | ScopedRefMessageData<AsyncClosure>* data = |
| 29 | static_cast<ScopedRefMessageData<AsyncClosure>*>(msg->pdata); |
| 30 | scoped_refptr<AsyncClosure> closure = data->data(); |
| 31 | delete msg->pdata; |
| 32 | msg->pdata = NULL; |
| 33 | |
| 34 | // Execute the closure and trigger the return message if needed. |
| 35 | closure->Execute(); |
| 36 | } |
| 37 | |
| 38 | void AsyncInvoker::Flush(Thread* thread, uint32 id /*= MQID_ANY*/) { |
| 39 | if (destroying_) return; |
| 40 | |
| 41 | // Run this on |thread| to reduce the number of context switches. |
| 42 | if (Thread::Current() != thread) { |
| 43 | thread->Invoke<void>(Bind(&AsyncInvoker::Flush, this, thread, id)); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | MessageList removed; |
| 48 | thread->Clear(this, id, &removed); |
| 49 | for (MessageList::iterator it = removed.begin(); it != removed.end(); ++it) { |
| 50 | // This message was pending on this thread, so run it now. |
| 51 | thread->Send(it->phandler, |
| 52 | it->message_id, |
| 53 | it->pdata); |
| 54 | } |
| 55 | } |
| 56 | |
perkj@webrtc.org | 827d7e8 | 2015-01-29 08:53:45 | [diff] [blame] | 57 | void AsyncInvoker::DoInvoke(Thread* thread, |
| 58 | const scoped_refptr<AsyncClosure>& closure, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 59 | uint32 id) { |
| 60 | if (destroying_) { |
| 61 | LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | thread->Post(this, id, new ScopedRefMessageData<AsyncClosure>(closure)); |
| 65 | } |
| 66 | |
Guo-wei Shieh | dc13abc | 2015-06-18 21:44:41 | [diff] [blame] | 67 | void AsyncInvoker::DoInvokeDelayed(Thread* thread, |
| 68 | const scoped_refptr<AsyncClosure>& closure, |
| 69 | uint32 delay_ms, |
| 70 | uint32 id) { |
| 71 | if (destroying_) { |
| 72 | LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
| 73 | return; |
| 74 | } |
| 75 | thread->PostDelayed(delay_ms, this, id, |
| 76 | new ScopedRefMessageData<AsyncClosure>(closure)); |
| 77 | } |
| 78 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 79 | NotifyingAsyncClosureBase::NotifyingAsyncClosureBase(AsyncInvoker* invoker, |
| 80 | Thread* calling_thread) |
| 81 | : invoker_(invoker), calling_thread_(calling_thread) { |
| 82 | calling_thread->SignalQueueDestroyed.connect( |
| 83 | this, &NotifyingAsyncClosureBase::CancelCallback); |
| 84 | invoker->SignalInvokerDestroyed.connect( |
| 85 | this, &NotifyingAsyncClosureBase::CancelCallback); |
| 86 | } |
| 87 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 88 | NotifyingAsyncClosureBase::~NotifyingAsyncClosureBase() { |
| 89 | disconnect_all(); |
| 90 | } |
| 91 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 92 | void NotifyingAsyncClosureBase::TriggerCallback() { |
| 93 | CritScope cs(&crit_); |
| 94 | if (!CallbackCanceled() && !callback_.empty()) { |
| 95 | invoker_->AsyncInvoke<void>(calling_thread_, callback_); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void NotifyingAsyncClosureBase::CancelCallback() { |
| 100 | // If the callback is triggering when this is called, block the |
| 101 | // destructor of the dying object here by waiting until the callback |
| 102 | // is done triggering. |
| 103 | CritScope cs(&crit_); |
| 104 | // calling_thread_ == NULL means do not trigger the callback. |
| 105 | calling_thread_ = NULL; |
| 106 | } |
| 107 | |
| 108 | } // namespace rtc |