blob: d2583091ccdcfca9d8140b9db0f3b7c4c411ece3 [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
deadbeef162cb532017-02-23 17:10:07 -080013#include "webrtc/base/atomicops.h"
Magnus Jedverta1f590f2015-08-20 16:42:42 +020014#include "webrtc/base/checks.h"
Per33544192015-04-02 12:30:51 +020015#include "webrtc/base/logging.h"
16
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017namespace rtc {
18
deadbeef162cb532017-02-23 17:10:07 -080019AsyncInvoker::AsyncInvoker() : invocation_complete_(false, false) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
21AsyncInvoker::~AsyncInvoker() {
deadbeefef37ca52017-05-22 15:32:51 -070022 AtomicOps::Increment(&destroying_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023 // Messages for this need to be cleared *before* our destructor is complete.
24 MessageQueueManager::Clear(this);
deadbeef162cb532017-02-23 17:10:07 -080025 // And we need to wait for any invocations that are still in progress on
26 // other threads.
27 while (AtomicOps::AcquireLoad(&pending_invocations_)) {
28 // If the destructor was called while AsyncInvoke was being called by
29 // another thread, WITHIN an AsyncInvoked functor, it may do another
30 // Thread::Post even after we called MessageQueueManager::Clear(this). So
31 // we need to keep calling Clear to discard these posts.
32 MessageQueueManager::Clear(this);
33 invocation_complete_.Wait(Event::kForever);
34 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035}
36
37void AsyncInvoker::OnMessage(Message* msg) {
38 // Get the AsyncClosure shared ptr from this message's data.
deadbeefa8bc1a12017-02-17 18:06:26 -080039 ScopedMessageData<AsyncClosure>* data =
40 static_cast<ScopedMessageData<AsyncClosure>*>(msg->pdata);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041 // Execute the closure and trigger the return message if needed.
deadbeefa8bc1a12017-02-17 18:06:26 -080042 data->inner_data().Execute();
43 delete data;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044}
45
Peter Boström0c4e06b2015-10-07 12:23:21 +020046void AsyncInvoker::Flush(Thread* thread, uint32_t id /*= MQID_ANY*/) {
deadbeefef37ca52017-05-22 15:32:51 -070047 if (AtomicOps::AcquireLoad(&destroying_))
48 return;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049
50 // Run this on |thread| to reduce the number of context switches.
51 if (Thread::Current() != thread) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070052 thread->Invoke<void>(RTC_FROM_HERE,
53 Bind(&AsyncInvoker::Flush, this, thread, id));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054 return;
55 }
56
57 MessageList removed;
58 thread->Clear(this, id, &removed);
59 for (MessageList::iterator it = removed.begin(); it != removed.end(); ++it) {
60 // This message was pending on this thread, so run it now.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070061 thread->Send(it->posted_from, it->phandler, it->message_id, it->pdata);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 }
63}
64
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070065void AsyncInvoker::DoInvoke(const Location& posted_from,
66 Thread* thread,
deadbeefa8bc1a12017-02-17 18:06:26 -080067 std::unique_ptr<AsyncClosure> closure,
Peter Boström0c4e06b2015-10-07 12:23:21 +020068 uint32_t id) {
deadbeefef37ca52017-05-22 15:32:51 -070069 if (AtomicOps::AcquireLoad(&destroying_)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071 return;
72 }
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070073 thread->Post(posted_from, this, id,
deadbeefa8bc1a12017-02-17 18:06:26 -080074 new ScopedMessageData<AsyncClosure>(std::move(closure)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075}
76
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070077void AsyncInvoker::DoInvokeDelayed(const Location& posted_from,
78 Thread* thread,
deadbeefa8bc1a12017-02-17 18:06:26 -080079 std::unique_ptr<AsyncClosure> closure,
Peter Boström0c4e06b2015-10-07 12:23:21 +020080 uint32_t delay_ms,
81 uint32_t id) {
deadbeefef37ca52017-05-22 15:32:51 -070082 if (AtomicOps::AcquireLoad(&destroying_)) {
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070083 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
84 return;
85 }
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070086 thread->PostDelayed(posted_from, delay_ms, this, id,
deadbeefa8bc1a12017-02-17 18:06:26 -080087 new ScopedMessageData<AsyncClosure>(std::move(closure)));
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070088}
89
Magnus Jedverta1f590f2015-08-20 16:42:42 +020090GuardedAsyncInvoker::GuardedAsyncInvoker() : thread_(Thread::Current()) {
91 thread_->SignalQueueDestroyed.connect(this,
92 &GuardedAsyncInvoker::ThreadDestroyed);
93}
94
95GuardedAsyncInvoker::~GuardedAsyncInvoker() {
96}
97
Peter Boström0c4e06b2015-10-07 12:23:21 +020098bool GuardedAsyncInvoker::Flush(uint32_t id) {
Magnus Jedverta1f590f2015-08-20 16:42:42 +020099 rtc::CritScope cs(&crit_);
100 if (thread_ == nullptr)
101 return false;
102 invoker_.Flush(thread_, id);
103 return true;
104}
105
106void GuardedAsyncInvoker::ThreadDestroyed() {
107 rtc::CritScope cs(&crit_);
108 // We should never get more than one notification about the thread dying.
henrikg91d6ede2015-09-17 00:24:34 -0700109 RTC_DCHECK(thread_ != nullptr);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200110 thread_ = nullptr;
111}
112
deadbeefef37ca52017-05-22 15:32:51 -0700113AsyncClosure::AsyncClosure(AsyncInvoker* invoker) : invoker_(invoker) {
114 AtomicOps::Increment(&invoker_->pending_invocations_);
115}
116
deadbeef162cb532017-02-23 17:10:07 -0800117AsyncClosure::~AsyncClosure() {
118 AtomicOps::Decrement(&invoker_->pending_invocations_);
119 invoker_->invocation_complete_.Set();
120}
121
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122} // namespace rtc