Skip to content

Commit

Permalink
Fixing some of the TSAN errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouwe committed Sep 25, 2024
1 parent 31c1117 commit fc720ed
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Jolt/Core/Semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

#pragma once

#include <Jolt/Core/Atomics.h>

JPH_SUPPRESS_WARNINGS_STD_BEGIN
#include <atomic>
#include <mutex>
#include <condition_variable>
JPH_SUPPRESS_WARNINGS_STD_END
Expand Down Expand Up @@ -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<mutex> lock(mLock);
#endif
return mCount;
}

private:
#ifdef JPH_PLATFORM_WINDOWS
Expand All @@ -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
Expand Down

0 comments on commit fc720ed

Please sign in to comment.