From 49cb4599560d6005d5df0dadfca2db04b288f216 Mon Sep 17 00:00:00 2001 From: Markus Handell Date: Tue, 22 Jun 2021 10:02:26 +0200 Subject: [PATCH] TaskQueueStdlib: initialize the thread last. TaskQueueStdlib initialized it's thread too early which permitted it to access uninitialized attributes. Also remove the |stopped_| event which isn't needed because of the platform thread being joinable. Fixed: webrtc:12876 Change-Id: Ibd27ce915e0e3ac92ebafca535c5a3fd72f9165e Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/223340 Commit-Queue: Markus Handell Reviewed-by: Harald Alvestrand Cr-Commit-Position: refs/heads/master@{#34355} --- rtc_base/task_queue_stdlib.cc | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/rtc_base/task_queue_stdlib.cc b/rtc_base/task_queue_stdlib.cc index 548f7ef69a..41da285ee7 100644 --- a/rtc_base/task_queue_stdlib.cc +++ b/rtc_base/task_queue_stdlib.cc @@ -82,16 +82,9 @@ class TaskQueueStdlib final : public TaskQueueBase { // Indicates if the thread has started. rtc::Event started_; - // Indicates if the thread has stopped. - rtc::Event stopped_; - // Signaled whenever a new task is pending. rtc::Event flag_notify_; - // Contains the active worker thread assigned to processing - // tasks (including delayed tasks). - rtc::PlatformThread thread_; - Mutex pending_lock_; // Indicates if the worker thread needs to shutdown now. @@ -114,12 +107,17 @@ class TaskQueueStdlib final : public TaskQueueBase { // std::unique_ptr out of the queue without the presence of a hack. std::map> delayed_queue_ RTC_GUARDED_BY(pending_lock_); + + // Contains the active worker thread assigned to processing + // tasks (including delayed tasks). + // Placing this last ensures the thread doesn't touch uninitialized attributes + // throughout it's lifetime. + rtc::PlatformThread thread_; }; TaskQueueStdlib::TaskQueueStdlib(absl::string_view queue_name, rtc::ThreadPriority priority) : started_(/*manual_reset=*/false, /*initially_signaled=*/false), - stopped_(/*manual_reset=*/false, /*initially_signaled=*/false), flag_notify_(/*manual_reset=*/false, /*initially_signaled=*/false), thread_(rtc::PlatformThread::SpawnJoinable( [this] { @@ -141,8 +139,6 @@ void TaskQueueStdlib::Delete() { NotifyWake(); - stopped_.Wait(rtc::Event::kForever); - thread_.Finalize(); delete this; } @@ -243,8 +239,6 @@ void TaskQueueStdlib::ProcessTasks() { else flag_notify_.Wait(task.sleep_time_ms_); } - - stopped_.Set(); } void TaskQueueStdlib::NotifyWake() {