blob: 3ccdc4bb4d5829b881037bd09425d6a0206497dc [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
74void NotifyingAsyncClosureBase::TriggerCallback() {
75 CritScope cs(&crit_);
76 if (!CallbackCanceled() && !callback_.empty()) {
77 invoker_->AsyncInvoke<void>(calling_thread_, callback_);
78 }
79}
80
81void NotifyingAsyncClosureBase::CancelCallback() {
82 // If the callback is triggering when this is called, block the
83 // destructor of the dying object here by waiting until the callback
84 // is done triggering.
85 CritScope cs(&crit_);
86 // calling_thread_ == NULL means do not trigger the callback.
87 calling_thread_ = NULL;
88}
89
90} // namespace rtc