From fc720ed52dfc9482ae117df4b2167c7d37f6a8be Mon Sep 17 00:00:00 2001 From: Jorrit Rouwe Date: Wed, 25 Sep 2024 23:06:49 +0200 Subject: [PATCH] Fixing some of the TSAN errors --- Jolt/Core/Semaphore.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Jolt/Core/Semaphore.h b/Jolt/Core/Semaphore.h index 498b1b8d4..c4e8f221e 100644 --- a/Jolt/Core/Semaphore.h +++ b/Jolt/Core/Semaphore.h @@ -4,8 +4,9 @@ #pragma once +#include + JPH_SUPPRESS_WARNINGS_STD_BEGIN -#include #include #include JPH_SUPPRESS_WARNINGS_STD_END @@ -33,7 +34,14 @@ class JPH_EXPORT Semaphore void Acquire(uint inNumber = 1); /// Get the current value of the semaphore - inline int GetValue() const { return mCount; } + inline int GetValue() const + { +#if defined(JPH_TSAN_ENABLED) && !defined(JPH_PLATFORM_WINDOWS) + // TSAN complains that we're reading mCount without locking. We don't care if we read a stale value. Inserting a lock to keep TSAN happy. + std::unique_lock lock(mLock); +#endif + return mCount; + } private: #ifdef JPH_PLATFORM_WINDOWS @@ -42,7 +50,7 @@ class JPH_EXPORT Semaphore void * mSemaphore; ///< The semaphore is an expensive construct so we only acquire/release it if we know that we need to wait/have waiting threads #else // Other platforms: Emulate a semaphore using a mutex, condition variable and count - mutex mLock; + mutable mutex mLock; condition_variable mWaitVariable; int mCount = 0; #endif