Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Side by Side Diff: webrtc/base/asyncinvoker.cc

Issue 2885143006: Fixing potential AsyncInvoker deadlock that occurs for "reentrant" invocations. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | webrtc/base/asyncinvoker-inl.h » ('j') | webrtc/base/thread_unittest.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 11 matching lines...) Expand all
22 destroying_ = true; 22 destroying_ = true;
23 // Messages for this need to be cleared *before* our destructor is complete. 23 // Messages for this need to be cleared *before* our destructor is complete.
24 MessageQueueManager::Clear(this); 24 MessageQueueManager::Clear(this);
25 // And we need to wait for any invocations that are still in progress on 25 // And we need to wait for any invocations that are still in progress on
26 // other threads. 26 // other threads.
27 while (AtomicOps::AcquireLoad(&pending_invocations_)) { 27 while (AtomicOps::AcquireLoad(&pending_invocations_)) {
28 // If the destructor was called while AsyncInvoke was being called by 28 // If the destructor was called while AsyncInvoke was being called by
29 // another thread, WITHIN an AsyncInvoked functor, it may do another 29 // another thread, WITHIN an AsyncInvoked functor, it may do another
30 // Thread::Post even after we called MessageQueueManager::Clear(this). So 30 // Thread::Post even after we called MessageQueueManager::Clear(this). So
31 // we need to keep calling Clear to discard these posts. 31 // we need to keep calling Clear to discard these posts.
32 invocation_complete_.Wait(Event::kForever);
32 MessageQueueManager::Clear(this); 33 MessageQueueManager::Clear(this);
nisse-webrtc 2017/05/18 08:04:55 This is subtle (maybe too subtle...). So at the e
Taylor Brandstetter 2017/05/18 20:54:52 I must have had a brain lapse and forgot that if "
33 invocation_complete_.Wait(Event::kForever);
34 } 34 }
35 } 35 }
36 36
37 void AsyncInvoker::OnMessage(Message* msg) { 37 void AsyncInvoker::OnMessage(Message* msg) {
38 // Get the AsyncClosure shared ptr from this message's data. 38 // Get the AsyncClosure shared ptr from this message's data.
39 ScopedMessageData<AsyncClosure>* data = 39 ScopedMessageData<AsyncClosure>* data =
40 static_cast<ScopedMessageData<AsyncClosure>*>(msg->pdata); 40 static_cast<ScopedMessageData<AsyncClosure>*>(msg->pdata);
41 // Execute the closure and trigger the return message if needed. 41 // Execute the closure and trigger the return message if needed.
42 data->inner_data().Execute(); 42 data->inner_data().Execute();
43 delete data; 43 delete data;
(...skipping 18 matching lines...) Expand all
62 } 62 }
63 63
64 void AsyncInvoker::DoInvoke(const Location& posted_from, 64 void AsyncInvoker::DoInvoke(const Location& posted_from,
65 Thread* thread, 65 Thread* thread,
66 std::unique_ptr<AsyncClosure> closure, 66 std::unique_ptr<AsyncClosure> closure,
67 uint32_t id) { 67 uint32_t id) {
68 if (destroying_) { 68 if (destroying_) {
69 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; 69 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
70 return; 70 return;
71 } 71 }
72 AtomicOps::Increment(&pending_invocations_);
73 thread->Post(posted_from, this, id, 72 thread->Post(posted_from, this, id,
74 new ScopedMessageData<AsyncClosure>(std::move(closure))); 73 new ScopedMessageData<AsyncClosure>(std::move(closure)));
75 } 74 }
76 75
77 void AsyncInvoker::DoInvokeDelayed(const Location& posted_from, 76 void AsyncInvoker::DoInvokeDelayed(const Location& posted_from,
78 Thread* thread, 77 Thread* thread,
79 std::unique_ptr<AsyncClosure> closure, 78 std::unique_ptr<AsyncClosure> closure,
80 uint32_t delay_ms, 79 uint32_t delay_ms,
81 uint32_t id) { 80 uint32_t id) {
82 if (destroying_) { 81 if (destroying_) {
83 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; 82 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
84 return; 83 return;
85 } 84 }
86 AtomicOps::Increment(&pending_invocations_);
87 thread->PostDelayed(posted_from, delay_ms, this, id, 85 thread->PostDelayed(posted_from, delay_ms, this, id,
88 new ScopedMessageData<AsyncClosure>(std::move(closure))); 86 new ScopedMessageData<AsyncClosure>(std::move(closure)));
89 } 87 }
90 88
91 GuardedAsyncInvoker::GuardedAsyncInvoker() : thread_(Thread::Current()) { 89 GuardedAsyncInvoker::GuardedAsyncInvoker() : thread_(Thread::Current()) {
92 thread_->SignalQueueDestroyed.connect(this, 90 thread_->SignalQueueDestroyed.connect(this,
93 &GuardedAsyncInvoker::ThreadDestroyed); 91 &GuardedAsyncInvoker::ThreadDestroyed);
94 } 92 }
95 93
96 GuardedAsyncInvoker::~GuardedAsyncInvoker() { 94 GuardedAsyncInvoker::~GuardedAsyncInvoker() {
97 } 95 }
98 96
99 bool GuardedAsyncInvoker::Flush(uint32_t id) { 97 bool GuardedAsyncInvoker::Flush(uint32_t id) {
100 rtc::CritScope cs(&crit_); 98 rtc::CritScope cs(&crit_);
101 if (thread_ == nullptr) 99 if (thread_ == nullptr)
102 return false; 100 return false;
103 invoker_.Flush(thread_, id); 101 invoker_.Flush(thread_, id);
104 return true; 102 return true;
105 } 103 }
106 104
107 void GuardedAsyncInvoker::ThreadDestroyed() { 105 void GuardedAsyncInvoker::ThreadDestroyed() {
108 rtc::CritScope cs(&crit_); 106 rtc::CritScope cs(&crit_);
109 // We should never get more than one notification about the thread dying. 107 // We should never get more than one notification about the thread dying.
110 RTC_DCHECK(thread_ != nullptr); 108 RTC_DCHECK(thread_ != nullptr);
111 thread_ = nullptr; 109 thread_ = nullptr;
112 } 110 }
113 111
112 AsyncClosure::AsyncClosure(AsyncInvoker* invoker) : invoker_(invoker) {
113 AtomicOps::Increment(&invoker_->pending_invocations_);
nisse-webrtc 2017/05/18 08:04:55 I take it moving the increment here from DoInvoke*
Taylor Brandstetter 2017/05/18 20:54:52 It's still done before anything gets added to the
nisse-webrtc 2017/05/19 06:43:21 I see, I was under the impression that the closure
114 }
115
114 AsyncClosure::~AsyncClosure() { 116 AsyncClosure::~AsyncClosure() {
115 AtomicOps::Decrement(&invoker_->pending_invocations_); 117 AtomicOps::Decrement(&invoker_->pending_invocations_);
nisse-webrtc 2017/05/18 11:12:39 Thinking a bit more, I'm afraids this approach is
Taylor Brandstetter 2017/05/18 20:54:52 That's exactly the issue I was trying to fix in th
nisse-webrtc 2017/05/19 06:43:21 Makes sense. lgtm, then.
nisse-webrtc 2017/05/19 06:48:16 But please update cl title accordingly...
116 invoker_->invocation_complete_.Set(); 118 invoker_->invocation_complete_.Set();
117 } 119 }
118 120
119 } // namespace rtc 121 } // namespace rtc
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/asyncinvoker-inl.h » ('j') | webrtc/base/thread_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698