From 017631e51b1977ac69e905613cbae0385fd9ede0 Mon Sep 17 00:00:00 2001 From: Tobias Date: Sat, 19 Sep 2020 18:42:21 +0200 Subject: [PATCH] Port yuzu-emu/yuzu#4587 and yuzu-emu/yuzu#4588: Fix data races (#5545) Co-authored-by: ReinUsesLisp --- externals/microprofile/microprofile.h | 2 +- src/common/thread.h | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/externals/microprofile/microprofile.h b/externals/microprofile/microprofile.h index b5035443a4..cf16661f88 100644 --- a/externals/microprofile/microprofile.h +++ b/externals/microprofile/microprofile.h @@ -1018,7 +1018,7 @@ static void MicroProfileCreateThreadLogKey() #else MP_THREAD_LOCAL MicroProfileThreadLog* g_MicroProfileThreadLog = 0; #endif -static bool g_bUseLock = false; /// This is used because windows does not support using mutexes under dll init(which is where global initialization is handled) +static std::atomic g_bUseLock{false}; /// This is used because windows does not support using mutexes under dll init(which is where global initialization is handled) MICROPROFILE_DEFINE(g_MicroProfileFlip, "MicroProfile", "MicroProfileFlip", 0x3355ee); diff --git a/src/common/thread.h b/src/common/thread.h index 618236b590..b4881fd444 100644 --- a/src/common/thread.h +++ b/src/common/thread.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -24,14 +25,14 @@ public: void Wait() { std::unique_lock lk{mutex}; - condvar.wait(lk, [&] { return is_set; }); + condvar.wait(lk, [&] { return is_set.load(); }); is_set = false; } template bool WaitFor(const std::chrono::duration& time) { std::unique_lock lk{mutex}; - if (!condvar.wait_for(lk, time, [this] { return is_set; })) + if (!condvar.wait_for(lk, time, [this] { return is_set.load(); })) return false; is_set = false; return true; @@ -40,7 +41,7 @@ public: template bool WaitUntil(const std::chrono::time_point& time) { std::unique_lock lk{mutex}; - if (!condvar.wait_until(lk, time, [this] { return is_set; })) + if (!condvar.wait_until(lk, time, [this] { return is_set.load(); })) return false; is_set = false; return true; @@ -54,9 +55,9 @@ public: } private: - bool is_set = false; std::condition_variable condvar; std::mutex mutex; + std::atomic_bool is_set{false}; }; class Barrier {