You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/async_invoker.h"
  11. #include "rtc_base/checks.h"
  12. #include "rtc_base/logging.h"
  13. namespace rtc {
  14. AsyncInvoker::AsyncInvoker()
  15. : pending_invocations_(0),
  16. invocation_complete_(new RefCountedObject<Event>()),
  17. destroying_(false) {}
  18. AsyncInvoker::~AsyncInvoker() {
  19. destroying_.store(true, std::memory_order_relaxed);
  20. // Messages for this need to be cleared *before* our destructor is complete.
  21. MessageQueueManager::Clear(this);
  22. // And we need to wait for any invocations that are still in progress on
  23. // other threads. Using memory_order_acquire for synchronization with
  24. // AsyncClosure destructors.
  25. while (pending_invocations_.load(std::memory_order_acquire) > 0) {
  26. // If the destructor was called while AsyncInvoke was being called by
  27. // another thread, WITHIN an AsyncInvoked functor, it may do another
  28. // Thread::Post even after we called MessageQueueManager::Clear(this). So
  29. // we need to keep calling Clear to discard these posts.
  30. Thread::Current()->Clear(this);
  31. invocation_complete_->Wait(Event::kForever);
  32. }
  33. }
  34. void AsyncInvoker::OnMessage(Message* msg) {
  35. // Get the AsyncClosure shared ptr from this message's data.
  36. ScopedMessageData<AsyncClosure>* data =
  37. static_cast<ScopedMessageData<AsyncClosure>*>(msg->pdata);
  38. // Execute the closure and trigger the return message if needed.
  39. data->inner_data().Execute();
  40. delete data;
  41. }
  42. void AsyncInvoker::Flush(Thread* thread, uint32_t id /*= MQID_ANY*/) {
  43. // If the destructor is waiting for invocations to finish, don't start
  44. // running even more tasks.
  45. if (destroying_.load(std::memory_order_relaxed))
  46. return;
  47. // Run this on |thread| to reduce the number of context switches.
  48. if (Thread::Current() != thread) {
  49. thread->Invoke<void>(RTC_FROM_HERE,
  50. Bind(&AsyncInvoker::Flush, this, thread, id));
  51. return;
  52. }
  53. MessageList removed;
  54. thread->Clear(this, id, &removed);
  55. for (MessageList::iterator it = removed.begin(); it != removed.end(); ++it) {
  56. // This message was pending on this thread, so run it now.
  57. thread->Send(it->posted_from, it->phandler, it->message_id, it->pdata);
  58. }
  59. }
  60. void AsyncInvoker::Clear() {
  61. MessageQueueManager::Clear(this);
  62. }
  63. void AsyncInvoker::DoInvoke(const Location& posted_from,
  64. Thread* thread,
  65. std::unique_ptr<AsyncClosure> closure,
  66. uint32_t id) {
  67. if (destroying_.load(std::memory_order_relaxed)) {
  68. // Note that this may be expected, if the application is AsyncInvoking
  69. // tasks that AsyncInvoke other tasks. But otherwise it indicates a race
  70. // between a thread destroying the AsyncInvoker and a thread still trying
  71. // to use it.
  72. RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
  73. return;
  74. }
  75. thread->Post(posted_from, this, id,
  76. new ScopedMessageData<AsyncClosure>(std::move(closure)));
  77. }
  78. void AsyncInvoker::DoInvokeDelayed(const Location& posted_from,
  79. Thread* thread,
  80. std::unique_ptr<AsyncClosure> closure,
  81. uint32_t delay_ms,
  82. uint32_t id) {
  83. if (destroying_.load(std::memory_order_relaxed)) {
  84. // See above comment.
  85. RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
  86. return;
  87. }
  88. thread->PostDelayed(posted_from, delay_ms, this, id,
  89. new ScopedMessageData<AsyncClosure>(std::move(closure)));
  90. }
  91. GuardedAsyncInvoker::GuardedAsyncInvoker() : thread_(Thread::Current()) {
  92. thread_->SignalQueueDestroyed.connect(this,
  93. &GuardedAsyncInvoker::ThreadDestroyed);
  94. }
  95. GuardedAsyncInvoker::~GuardedAsyncInvoker() {}
  96. bool GuardedAsyncInvoker::Flush(uint32_t id) {
  97. CritScope cs(&crit_);
  98. if (thread_ == nullptr)
  99. return false;
  100. invoker_.Flush(thread_, id);
  101. return true;
  102. }
  103. void GuardedAsyncInvoker::ThreadDestroyed() {
  104. CritScope cs(&crit_);
  105. // We should never get more than one notification about the thread dying.
  106. RTC_DCHECK(thread_ != nullptr);
  107. thread_ = nullptr;
  108. }
  109. AsyncClosure::AsyncClosure(AsyncInvoker* invoker)
  110. : invoker_(invoker), invocation_complete_(invoker_->invocation_complete_) {
  111. invoker_->pending_invocations_.fetch_add(1, std::memory_order_relaxed);
  112. }
  113. AsyncClosure::~AsyncClosure() {
  114. // Using memory_order_release for synchronization with the AsyncInvoker
  115. // destructor.
  116. invoker_->pending_invocations_.fetch_sub(1, std::memory_order_release);
  117. // After |pending_invocations_| is decremented, we may need to signal
  118. // |invocation_complete_| in case the AsyncInvoker is being destroyed and
  119. // waiting for pending tasks to complete.
  120. //
  121. // It's also possible that the destructor finishes before "Set()" is called,
  122. // which is safe because the event is reference counted (and in a thread-safe
  123. // way).
  124. invocation_complete_->Set();
  125. }
  126. } // namespace rtc