| 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 "rtc_base/async_invoker.h" |
| 12 |
| 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/logging.h" |
| 15 |
| 16 | namespace rtc { |
| 17 |
| 18 | DEPRECATED_AsyncInvoker::DEPRECATED_AsyncInvoker() |
| 19 | : pending_invocations_(0), |
| 20 | invocation_complete_(make_ref_counted<Event>()), |
| 21 | destroying_(false) {} |
| 22 |
| 23 | DEPRECATED_AsyncInvoker::~DEPRECATED_AsyncInvoker() { |
| 24 | destroying_.store(true, std::memory_order_relaxed); |
| 25 | // Messages for this need to be cleared *before* our destructor is complete. |
| 26 | ThreadManager::Clear(this); |
| 27 | // And we need to wait for any invocations that are still in progress on |
| 28 | // other threads. Using memory_order_acquire for synchronization with |
| 29 | // AsyncClosure destructors. |
| 30 | while (pending_invocations_.load(std::memory_order_acquire) > 0) { |
| 31 | // 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 ThreadManager::Clear(this). So |
| 34 | // we need to keep calling Clear to discard these posts. |
| 35 | Thread::Current()->Clear(this); |
| 36 | invocation_complete_->Wait(Event::kForever); |
| 37 | } |
| 38 | } |
| 39 |
| 40 | void DEPRECATED_AsyncInvoker::OnMessage(Message* msg) { |
| 41 | // Get the AsyncClosure shared ptr from this message's data. |
| 42 | ScopedMessageData<AsyncClosure>* data = |
| 43 | static_cast<ScopedMessageData<AsyncClosure>*>(msg->pdata); |
| 44 | // Execute the closure and trigger the return message if needed. |
| 45 | data->data().Execute(); |
| 46 | delete data; |
| 47 | } |
| 48 |
| 49 | void DEPRECATED_AsyncInvoker::Clear() { |
| 50 | ThreadManager::Clear(this); |
| 51 | } |
| 52 |
| 53 | void DEPRECATED_AsyncInvoker::DoInvoke(const Location& posted_from, |
| 54 | Thread* thread, |
| 55 | std::unique_ptr<AsyncClosure> closure, |
| 56 | uint32_t id) { |
| 57 | if (destroying_.load(std::memory_order_relaxed)) { |
| 58 | // Note that this may be expected, if the application is AsyncInvoking |
| 59 | // tasks that AsyncInvoke other tasks. But otherwise it indicates a race |
| 60 | // between a thread destroying the AsyncInvoker and a thread still trying |
| 61 | // to use it. |
| 62 | RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
| 63 | return; |
| 64 | } |
| 65 | thread->Post(posted_from, this, id, |
| 66 | new ScopedMessageData<AsyncClosure>(std::move(closure))); |
| 67 | } |
| 68 |
| 69 | void DEPRECATED_AsyncInvoker::DoInvokeDelayed( |
| 70 | const Location& posted_from, |
| 71 | Thread* thread, |
| 72 | std::unique_ptr<AsyncClosure> closure, |
| 73 | uint32_t delay_ms, |
| 74 | uint32_t id) { |
| 75 | if (destroying_.load(std::memory_order_relaxed)) { |
| 76 | // See above comment. |
| 77 | RTC_LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
| 78 | return; |
| 79 | } |
| 80 | thread->PostDelayed(posted_from, delay_ms, this, id, |
| 81 | new ScopedMessageData<AsyncClosure>(std::move(closure))); |
| 82 | } |
| 83 |
| 84 | AsyncClosure::AsyncClosure(DEPRECATED_AsyncInvoker* invoker) |
| 85 | : invoker_(invoker), invocation_complete_(invoker_->invocation_complete_) { |
| 86 | invoker_->pending_invocations_.fetch_add(1, std::memory_order_relaxed); |
| 87 | } |
| 88 |
| 89 | AsyncClosure::~AsyncClosure() { |
| 90 | // Using memory_order_release for synchronization with the AsyncInvoker |
| 91 | // destructor. |
| 92 | invoker_->pending_invocations_.fetch_sub(1, std::memory_order_release); |
| 93 |
| 94 | // After `pending_invocations_` is decremented, we may need to signal |
| 95 | // `invocation_complete_` in case the AsyncInvoker is being destroyed and |
| 96 | // waiting for pending tasks to complete. |
| 97 | // |
| 98 | // It's also possible that the destructor finishes before "Set()" is called, |
| 99 | // which is safe because the event is reference counted (and in a thread-safe |
| 100 | // way). |
| 101 | invocation_complete_->Set(); |
| 102 | } |
| 103 |
| 104 | } // namespace rtc |
| 105 |