blob: 048873412eb994e1e4e6d47f9885233b5962e219 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
13namespace rtc {
14
15AsyncInvoker::AsyncInvoker() : destroying_(false) {}
16
17AsyncInvoker::~AsyncInvoker() {
18 destroying_ = true;
19 SignalInvokerDestroyed();
20 // Messages for this need to be cleared *before* our destructor is complete.
21 MessageQueueManager::Clear(this);
22}
23
24void AsyncInvoker::OnMessage(Message* msg) {
25 // Get the AsyncClosure shared ptr from this message's data.
26 ScopedRefMessageData<AsyncClosure>* data =
27 static_cast<ScopedRefMessageData<AsyncClosure>*>(msg->pdata);
28 scoped_refptr<AsyncClosure> closure = data->data();
29 delete msg->pdata;
30 msg->pdata = NULL;
31
32 // Execute the closure and trigger the return message if needed.
33 closure->Execute();
34}
35
36void AsyncInvoker::Flush(Thread* thread, uint32 id /*= MQID_ANY*/) {
37 if (destroying_) return;
38
39 // Run this on |thread| to reduce the number of context switches.
40 if (Thread::Current() != thread) {
41 thread->Invoke<void>(Bind(&AsyncInvoker::Flush, this, thread, id));
42 return;
43 }
44
45 MessageList removed;
46 thread->Clear(this, id, &removed);
47 for (MessageList::iterator it = removed.begin(); it != removed.end(); ++it) {
48 // This message was pending on this thread, so run it now.
49 thread->Send(it->phandler,
50 it->message_id,
51 it->pdata);
52 }
53}
54
perkj@webrtc.org827d7e82015-01-29 08:53:45 +000055void AsyncInvoker::DoInvoke(Thread* thread,
56 const scoped_refptr<AsyncClosure>& closure,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057 uint32 id) {
58 if (destroying_) {
59 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 return;
61 }
62 thread->Post(this, id, new ScopedRefMessageData<AsyncClosure>(closure));
63}
64
65NotifyingAsyncClosureBase::NotifyingAsyncClosureBase(AsyncInvoker* invoker,
66 Thread* calling_thread)
67 : invoker_(invoker), calling_thread_(calling_thread) {
68 calling_thread->SignalQueueDestroyed.connect(
69 this, &NotifyingAsyncClosureBase::CancelCallback);
70 invoker->SignalInvokerDestroyed.connect(
71 this, &NotifyingAsyncClosureBase::CancelCallback);
72}
73
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000074NotifyingAsyncClosureBase::~NotifyingAsyncClosureBase() {
75 disconnect_all();
76}
77
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078void NotifyingAsyncClosureBase::TriggerCallback() {
79 CritScope cs(&crit_);
80 if (!CallbackCanceled() && !callback_.empty()) {
81 invoker_->AsyncInvoke<void>(calling_thread_, callback_);
82 }
83}
84
85void NotifyingAsyncClosureBase::CancelCallback() {
86 // If the callback is triggering when this is called, block the
87 // destructor of the dying object here by waiting until the callback
88 // is done triggering.
89 CritScope cs(&crit_);
90 // calling_thread_ == NULL means do not trigger the callback.
91 calling_thread_ = NULL;
92}
93
94} // namespace rtc