blob: 35ce2fb12814a60a2e5e976d750ea1fb30f95c07 [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
Per33544192015-04-02 12:30:51 +020013#include "webrtc/base/logging.h"
14
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015namespace 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:45 +000057void AsyncInvoker::DoInvoke(Thread* thread,
58 const scoped_refptr<AsyncClosure>& closure,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059 uint32 id) {
60 if (destroying_) {
61 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 return;
63 }
64 thread->Post(this, id, new ScopedRefMessageData<AsyncClosure>(closure));
65}
66
67NotifyingAsyncClosureBase::NotifyingAsyncClosureBase(AsyncInvoker* invoker,
68 Thread* calling_thread)
69 : invoker_(invoker), calling_thread_(calling_thread) {
70 calling_thread->SignalQueueDestroyed.connect(
71 this, &NotifyingAsyncClosureBase::CancelCallback);
72 invoker->SignalInvokerDestroyed.connect(
73 this, &NotifyingAsyncClosureBase::CancelCallback);
74}
75
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000076NotifyingAsyncClosureBase::~NotifyingAsyncClosureBase() {
77 disconnect_all();
78}
79
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080void NotifyingAsyncClosureBase::TriggerCallback() {
81 CritScope cs(&crit_);
82 if (!CallbackCanceled() && !callback_.empty()) {
83 invoker_->AsyncInvoke<void>(calling_thread_, callback_);
84 }
85}
86
87void NotifyingAsyncClosureBase::CancelCallback() {
88 // If the callback is triggering when this is called, block the
89 // destructor of the dying object here by waiting until the callback
90 // is done triggering.
91 CritScope cs(&crit_);
92 // calling_thread_ == NULL means do not trigger the callback.
93 calling_thread_ = NULL;
94}
95
96} // namespace rtc