瀏覽代碼

k_thread: Fix data race

TSan reports a data race between writing at cpp:1162 and reading at
h:262. Make the thread_state atomic to prevent this.
lat9nq 4 年之前
父節點
當前提交
6bcbbb29e7
共有 2 個文件被更改,包括 4 次插入3 次删除
  1. 2 2
      src/core/hle/kernel/k_thread.cpp
  2. 2 1
      src/core/hle/kernel/k_thread.h

+ 2 - 2
src/core/hle/kernel/k_thread.cpp

@@ -723,7 +723,7 @@ void KThread::UpdateState() {
     ASSERT(kernel.GlobalSchedulerContext().IsLocked());
     ASSERT(kernel.GlobalSchedulerContext().IsLocked());
 
 
     // Set our suspend flags in state.
     // Set our suspend flags in state.
-    const auto old_state = thread_state;
+    const ThreadState old_state = thread_state;
     const auto new_state =
     const auto new_state =
         static_cast<ThreadState>(this->GetSuspendFlags()) | (old_state & ThreadState::Mask);
         static_cast<ThreadState>(this->GetSuspendFlags()) | (old_state & ThreadState::Mask);
     thread_state = new_state;
     thread_state = new_state;
@@ -738,7 +738,7 @@ void KThread::Continue() {
     ASSERT(kernel.GlobalSchedulerContext().IsLocked());
     ASSERT(kernel.GlobalSchedulerContext().IsLocked());
 
 
     // Clear our suspend flags in state.
     // Clear our suspend flags in state.
-    const auto old_state = thread_state;
+    const ThreadState old_state = thread_state;
     thread_state = old_state & ThreadState::Mask;
     thread_state = old_state & ThreadState::Mask;
 
 
     // Note the state change in scheduler.
     // Note the state change in scheduler.

+ 2 - 1
src/core/hle/kernel/k_thread.h

@@ -5,6 +5,7 @@
 #pragma once
 #pragma once
 
 
 #include <array>
 #include <array>
+#include <atomic>
 #include <span>
 #include <span>
 #include <string>
 #include <string>
 #include <utility>
 #include <utility>
@@ -751,7 +752,7 @@ private:
     KAffinityMask original_physical_affinity_mask{};
     KAffinityMask original_physical_affinity_mask{};
     s32 original_physical_ideal_core_id{};
     s32 original_physical_ideal_core_id{};
     s32 num_core_migration_disables{};
     s32 num_core_migration_disables{};
-    ThreadState thread_state{};
+    std::atomic<ThreadState> thread_state{};
     std::atomic<bool> termination_requested{};
     std::atomic<bool> termination_requested{};
     bool wait_cancelled{};
     bool wait_cancelled{};
     bool cancellable{};
     bool cancellable{};