blob: 7f65dfc35fc0f500d6bc49dc7c063576e716dab0 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/asyncinvoker.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
14#include "rtc_base/logging.h"
Per33544192015-04-02 12:30:51 +020015
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016namespace rtc {
17
deadbeef3af63b02017-08-08 17:59:47 -070018AsyncInvoker::AsyncInvoker()
19 : pending_invocations_(0),
20 invocation_complete_(new RefCountedObject<Event>(false, false)),
21 destroying_(false) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
23AsyncInvoker::~AsyncInvoker() {
deadbeef3af63b02017-08-08 17:59:47 -070024 destroying_.store(true, std::memory_order_relaxed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025 // Messages for this need to be cleared *before* our destructor is complete.
26 MessageQueueManager::Clear(this);
deadbeef162cb532017-02-23 17:10:07 -080027 // And we need to wait for any invocations that are still in progress on
deadbeef3af63b02017-08-08 17:59:47 -070028 // other threads. Using memory_order_acquire for synchronization with
29 // AsyncClosure destructors.
30 while (pending_invocations_.load(std::memory_order_acquire) > 0) {
deadbeef162cb532017-02-23 17:10:07 -080031 // If the destructor was called while AsyncInvoke was being called by
32 // another thread, WITHIN an AsyncInvoked functor, it may do another
33 // Thread::Post even after we called MessageQueueManager::Clear(this). So
34 // we need to keep calling Clear to discard these posts.
deadbeef3af63b02017-08-08 17:59:47 -070035 Thread::Current()->Clear(this);
36 invocation_complete_->Wait(Event::kForever);
deadbeef162cb532017-02-23 17:10:07 -080037 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038}
39
40void AsyncInvoker::OnMessage(Message* msg) {
41 // Get the AsyncClosure shared ptr from this message's data.
deadbeefa8bc1a12017-02-17 18:06:26 -080042 ScopedMessageData<AsyncClosure>* data =
43 static_cast<ScopedMessageData<AsyncClosure>*>(msg->pdata);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044 // Execute the closure and trigger the return message if needed.
deadbeefa8bc1a12017-02-17 18:06:26 -080045 data->inner_data().Execute();
46 delete data;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047}
48
Peter Boström0c4e06b2015-10-07 12:23:21 +020049void AsyncInvoker::Flush(Thread* thread, uint32_t id /*= MQID_ANY*/) {
deadbeef3af63b02017-08-08 17:59:47 -070050 // If the destructor is waiting for invocations to finish, don't start
51 // running even more tasks.
52 if (destroying_.load(std::memory_order_relaxed))
53 return;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054
55 // Run this on |thread| to reduce the number of context switches.
56 if (Thread::Current() != thread) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070057 thread->Invoke<void>(RTC_FROM_HERE,
58 Bind(&AsyncInvoker::Flush, this, thread, id));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059 return;
60 }
61
62 MessageList removed;
63 thread->Clear(this, id, &removed);
64 for (MessageList::iterator it = removed.begin(); it != removed.end(); ++it) {
65 // This message was pending on this thread, so run it now.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070066 thread->Send(it->posted_from, it->phandler, it->message_id, it->pdata);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067 }
68}
69
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070070void AsyncInvoker::DoInvoke(const Location& posted_from,
71 Thread* thread,
deadbeefa8bc1a12017-02-17 18:06:26 -080072 std::unique_ptr<AsyncClosure> closure,
Peter Boström0c4e06b2015-10-07 12:23:21 +020073 uint32_t id) {
deadbeef3af63b02017-08-08 17:59:47 -070074 if (destroying_.load(std::memory_order_relaxed)) {
75 // Note that this may be expected, if the application is AsyncInvoking
76 // tasks that AsyncInvoke other tasks. But otherwise it indicates a race
77 // between a thread destroying the AsyncInvoker and a thread still trying
78 // to use it.
Mirko Bonadei675513b2017-11-09 11:09:25 +010079 RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 return;
81 }
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070082 thread->Post(posted_from, this, id,
deadbeefa8bc1a12017-02-17 18:06:26 -080083 new ScopedMessageData<AsyncClosure>(std::move(closure)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084}
85
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070086void AsyncInvoker::DoInvokeDelayed(const Location& posted_from,
87 Thread* thread,
deadbeefa8bc1a12017-02-17 18:06:26 -080088 std::unique_ptr<AsyncClosure> closure,
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 uint32_t delay_ms,
90 uint32_t id) {
deadbeef3af63b02017-08-08 17:59:47 -070091 if (destroying_.load(std::memory_order_relaxed)) {
92 // See above comment.
Mirko Bonadei675513b2017-11-09 11:09:25 +010093 RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070094 return;
95 }
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070096 thread->PostDelayed(posted_from, delay_ms, this, id,
deadbeefa8bc1a12017-02-17 18:06:26 -080097 new ScopedMessageData<AsyncClosure>(std::move(closure)));
Guo-wei Shiehdc13abc2015-06-18 14:44:41 -070098}
99
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200100GuardedAsyncInvoker::GuardedAsyncInvoker() : thread_(Thread::Current()) {
101 thread_->SignalQueueDestroyed.connect(this,
102 &GuardedAsyncInvoker::ThreadDestroyed);
103}
104
105GuardedAsyncInvoker::~GuardedAsyncInvoker() {
106}
107
Peter Boström0c4e06b2015-10-07 12:23:21 +0200108bool GuardedAsyncInvoker::Flush(uint32_t id) {
deadbeef3af63b02017-08-08 17:59:47 -0700109 CritScope cs(&crit_);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200110 if (thread_ == nullptr)
111 return false;
112 invoker_.Flush(thread_, id);
113 return true;
114}
115
116void GuardedAsyncInvoker::ThreadDestroyed() {
deadbeef3af63b02017-08-08 17:59:47 -0700117 CritScope cs(&crit_);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200118 // We should never get more than one notification about the thread dying.
henrikg91d6ede2015-09-17 00:24:34 -0700119 RTC_DCHECK(thread_ != nullptr);
Magnus Jedverta1f590f2015-08-20 16:42:42 +0200120 thread_ = nullptr;
121}
122
deadbeef3af63b02017-08-08 17:59:47 -0700123AsyncClosure::AsyncClosure(AsyncInvoker* invoker)
124 : invoker_(invoker), invocation_complete_(invoker_->invocation_complete_) {
125 invoker_->pending_invocations_.fetch_add(1, std::memory_order_relaxed);
126}
127
deadbeef162cb532017-02-23 17:10:07 -0800128AsyncClosure::~AsyncClosure() {
deadbeef3af63b02017-08-08 17:59:47 -0700129 // Using memory_order_release for synchronization with the AsyncInvoker
130 // destructor.
131 invoker_->pending_invocations_.fetch_sub(1, std::memory_order_release);
132
133 // After |pending_invocations_| is decremented, we may need to signal
134 // |invocation_complete_| in case the AsyncInvoker is being destroyed and
135 // waiting for pending tasks to complete.
136 //
137 // It's also possible that the destructor finishes before "Set()" is called,
138 // which is safe because the event is reference counted (and in a thread-safe
139 // way).
140 invocation_complete_->Set();
deadbeef162cb532017-02-23 17:10:07 -0800141}
142
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143} // namespace rtc