Make rtc::Thread a TaskQueue
in support of converging on single way to run asynchronous tasks in webrtc
Bug: b/144982320
Change-Id: I200ad298136d11764a3f5c0547ebcba51aceafa0
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/158782
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29896}
diff --git a/rtc_base/thread.cc b/rtc_base/thread.cc
index 20f58b2..0b8905e 100644
--- a/rtc_base/thread.cc
+++ b/rtc_base/thread.cc
@@ -335,6 +335,7 @@
Thread* thread = static_cast<Thread*>(pv);
ThreadManager::Instance()->SetCurrentThread(thread);
rtc::SetCurrentThreadName(thread->name_.c_str());
+ CurrentTaskQueueSetter set_current_task_queue(thread);
#if defined(WEBRTC_MAC)
ScopedAutoReleasePool pool;
#endif
@@ -475,6 +476,41 @@
Send(posted_from, handler);
}
+void Thread::QueuedTaskHandler::OnMessage(Message* msg) {
+ RTC_DCHECK(msg);
+ auto* data = static_cast<ScopedMessageData<webrtc::QueuedTask>*>(msg->pdata);
+ std::unique_ptr<webrtc::QueuedTask> task = std::move(data->data());
+ // MessageQueue expects handler to own Message::pdata when OnMessage is called
+ // Since MessageData is no longer needed, delete it.
+ delete data;
+
+ // QueuedTask interface uses Run return value to communicate who owns the
+ // task. false means QueuedTask took the ownership.
+ if (!task->Run())
+ task.release();
+}
+
+void Thread::PostTask(std::unique_ptr<webrtc::QueuedTask> task) {
+ // Though Post takes MessageData by raw pointer (last parameter), it still
+ // takes it with ownership.
+ Post(RTC_FROM_HERE, &queued_task_handler_,
+ /*id=*/0, new ScopedMessageData<webrtc::QueuedTask>(std::move(task)));
+}
+
+void Thread::PostDelayedTask(std::unique_ptr<webrtc::QueuedTask> task,
+ uint32_t milliseconds) {
+ // Though PostDelayed takes MessageData by raw pointer (last parameter),
+ // it still takes it with ownership.
+ PostDelayed(RTC_FROM_HERE, milliseconds, &queued_task_handler_,
+ /*id=*/0,
+ new ScopedMessageData<webrtc::QueuedTask>(std::move(task)));
+}
+
+void Thread::Delete() {
+ Stop();
+ delete this;
+}
+
bool Thread::IsProcessingMessagesForTesting() {
return (owned_ || IsCurrent()) &&
MessageQueue::IsProcessingMessagesForTesting();