Chromium Code Reviews| Index: webrtc/base/asyncinvoker.cc |
| diff --git a/webrtc/base/asyncinvoker.cc b/webrtc/base/asyncinvoker.cc |
| index 83a873811e7258a16a982c39609598114ca78055..8f141e05ec36a5bed9d2b3d49dce861c27c31bd6 100644 |
| --- a/webrtc/base/asyncinvoker.cc |
| +++ b/webrtc/base/asyncinvoker.cc |
| @@ -10,18 +10,29 @@ |
| #include "webrtc/base/asyncinvoker.h" |
| +#include "webrtc/base/atomicops.h" |
| #include "webrtc/base/checks.h" |
| #include "webrtc/base/logging.h" |
| namespace rtc { |
| -AsyncInvoker::AsyncInvoker() : destroying_(false) {} |
| +AsyncInvoker::AsyncInvoker() : invocation_complete_(false, false) {} |
| AsyncInvoker::~AsyncInvoker() { |
| destroying_ = true; |
| SignalInvokerDestroyed(); |
| // Messages for this need to be cleared *before* our destructor is complete. |
| MessageQueueManager::Clear(this); |
| + // And we need to wait for any invocations that are still in progress on |
| + // other threads. |
| + while (AtomicOps::AcquireLoad(&pending_invocations_)) { |
|
tommi
2017/02/22 11:19:53
is there a chance that we could get 0 returned her
Taylor Brandstetter
2017/02/23 01:19:00
That would mean that the invoker is being destroye
|
| + // If the destructor was called while AsyncInvoke was being called by |
| + // another thread, WITHIN an AsyncInvoked functor, it may do another |
| + // Thread::Post even after we called MessageQueueManager::Clear(this). So |
| + // we need to keep calling Clear to discard these posts. |
| + MessageQueueManager::Clear(this); |
| + invocation_complete_.Wait(Event::kForever); |
| + } |
| } |
| void AsyncInvoker::OnMessage(Message* msg) { |
| @@ -62,6 +73,7 @@ void AsyncInvoker::DoInvoke(const Location& posted_from, |
| LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
| return; |
| } |
| + AtomicOps::Increment(&pending_invocations_); |
| thread->Post(posted_from, this, id, |
| new ScopedRefMessageData<AsyncClosure>(closure)); |
| } |
| @@ -75,6 +87,7 @@ void AsyncInvoker::DoInvokeDelayed(const Location& posted_from, |
| LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
| return; |
| } |
| + AtomicOps::Increment(&pending_invocations_); |
|
pthatcher1
2017/02/17 18:19:38
Do we have any sense of what perf impact these add
Taylor Brandstetter
2017/02/17 19:00:46
Assuming the CPU architecture supports atomic oper
|
| thread->PostDelayed(posted_from, delay_ms, this, id, |
| new ScopedRefMessageData<AsyncClosure>(closure)); |
| } |
| @@ -102,11 +115,16 @@ void GuardedAsyncInvoker::ThreadDestroyed() { |
| thread_ = nullptr; |
| } |
| +AsyncClosure::~AsyncClosure() { |
| + AtomicOps::Decrement(&invoker_->pending_invocations_); |
| + invoker_->invocation_complete_.Set(); |
|
pthatcher1
2017/02/17 18:19:38
Why does it need the friend class to touch a priva
Taylor Brandstetter
2017/02/17 19:00:46
Because only AsyncClosure should be able to do thi
|
| +} |
| + |
| NotifyingAsyncClosureBase::NotifyingAsyncClosureBase( |
| AsyncInvoker* invoker, |
| const Location& callback_posted_from, |
| Thread* calling_thread) |
| - : invoker_(invoker), |
| + : AsyncClosure(invoker), |
| callback_posted_from_(callback_posted_from), |
| calling_thread_(calling_thread) { |
| calling_thread->SignalQueueDestroyed.connect( |