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
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
55void AsyncInvoker::DoInvoke(Thread* thread, AsyncClosure* closure,
56                            uint32 id) {
57  if (destroying_) {
58    LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
59    // Since this call transwers ownership of |closure|, we clean it up here.
60    delete closure;
61    return;
62  }
63  thread->Post(this, id, new ScopedRefMessageData<AsyncClosure>(closure));
64}
65
66NotifyingAsyncClosureBase::NotifyingAsyncClosureBase(AsyncInvoker* invoker,
67                                                     Thread* calling_thread)
68    : invoker_(invoker), calling_thread_(calling_thread) {
69  calling_thread->SignalQueueDestroyed.connect(
70      this, &NotifyingAsyncClosureBase::CancelCallback);
71  invoker->SignalInvokerDestroyed.connect(
72      this, &NotifyingAsyncClosureBase::CancelCallback);
73}
74
75void NotifyingAsyncClosureBase::TriggerCallback() {
76  CritScope cs(&crit_);
77  if (!CallbackCanceled() && !callback_.empty()) {
78    invoker_->AsyncInvoke<void>(calling_thread_, callback_);
79  }
80}
81
82void NotifyingAsyncClosureBase::CancelCallback() {
83  // If the callback is triggering when this is called, block the
84  // destructor of the dying object here by waiting until the callback
85  // is done triggering.
86  CritScope cs(&crit_);
87  // calling_thread_ == NULL means do not trigger the callback.
88  calling_thread_ = NULL;
89}
90
91}  // namespace rtc
92