blob: 56f1a19d543bfeecc54b4ef7ffb7abbcb4400847 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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
Per33544192015-04-02 10:30:5113#include "webrtc/base/logging.h"
14
henrike@webrtc.orgf0488722014-05-13 18:00:2615namespace rtc {
16
17AsyncInvoker::AsyncInvoker() : destroying_(false) {}
18
19AsyncInvoker::~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
26void 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
38void 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.org827d7e82015-01-29 08:53:4557void AsyncInvoker::DoInvoke(Thread* thread,
58 const scoped_refptr<AsyncClosure>& closure,
henrike@webrtc.orgf0488722014-05-13 18:00:2659 uint32 id) {
60 if (destroying_) {
61 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
henrike@webrtc.orgf0488722014-05-13 18:00:2662 return;
63 }
64 thread->Post(this, id, new ScopedRefMessageData<AsyncClosure>(closure));
65}
66
Guo-wei Shiehdc13abc2015-06-18 21:44:4167void 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.orgf0488722014-05-13 18:00:2679NotifyingAsyncClosureBase::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.org67186fe2015-03-09 22:21:5388NotifyingAsyncClosureBase::~NotifyingAsyncClosureBase() {
89 disconnect_all();
90}
91
henrike@webrtc.orgf0488722014-05-13 18:00:2692void NotifyingAsyncClosureBase::TriggerCallback() {
93 CritScope cs(&crit_);
94 if (!CallbackCanceled() && !callback_.empty()) {
95 invoker_->AsyncInvoke<void>(calling_thread_, callback_);
96 }
97}
98
99void 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