Skip to content

Commit

Permalink
Implemented Semaphore using pthreads
Browse files Browse the repository at this point in the history
This generates a lot less stalls than the mutex/condition_variable construct
  • Loading branch information
jrouwe committed Jan 17, 2025
1 parent 2c1864d commit 71b662f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
27 changes: 24 additions & 3 deletions Jolt/Core/Semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#else
#include <windows.h>
#endif

JPH_SUPPRESS_WARNING_POP
#endif

Expand All @@ -27,13 +26,17 @@ Semaphore::Semaphore()
{
#ifdef JPH_PLATFORM_WINDOWS
mSemaphore = CreateSemaphore(nullptr, 0, INT_MAX, nullptr);
#elif defined(JPH_USE_PTHREADS)
sem_init(&mSemaphore, 0, 0);
#endif
}

Semaphore::~Semaphore()
{
#ifdef JPH_PLATFORM_WINDOWS
CloseHandle(mSemaphore);
#elif defined(JPH_USE_PTHREADS)
sem_destroy(&mSemaphore);
#endif
}

Expand All @@ -42,13 +45,22 @@ void Semaphore::Release(uint inNumber)
JPH_ASSERT(inNumber > 0);

#ifdef JPH_PLATFORM_WINDOWS
int old_value = mCount.fetch_add(inNumber);
int old_value = mCount.fetch_add(inNumber, std::memory_order_release);
if (old_value < 0)
{
int new_value = old_value + (int)inNumber;
int num_to_release = min(new_value, 0) - old_value;
::ReleaseSemaphore(mSemaphore, num_to_release, nullptr);
}
#elif defined(JPH_USE_PTHREADS)
int old_value = mCount.fetch_add(inNumber, std::memory_order_release);
if (old_value < 0)
{
int new_value = old_value + (int)inNumber;
int num_to_release = min(new_value, 0) - old_value;
for (int i = 0; i < num_to_release; ++i)
sem_post(&mSemaphore);
}
#else
std::lock_guard lock(mLock);
mCount.fetch_add(inNumber, std::memory_order_relaxed);
Expand All @@ -64,14 +76,23 @@ void Semaphore::Acquire(uint inNumber)
JPH_ASSERT(inNumber > 0);

#ifdef JPH_PLATFORM_WINDOWS
int old_value = mCount.fetch_sub(inNumber);
int old_value = mCount.fetch_sub(inNumber, std::memory_order_acquire);
int new_value = old_value - (int)inNumber;
if (new_value < 0)
{
int num_to_acquire = min(old_value, 0) - new_value;
for (int i = 0; i < num_to_acquire; ++i)
WaitForSingleObject(mSemaphore, INFINITE);
}
#elif defined(JPH_USE_PTHREADS)
int old_value = mCount.fetch_sub(inNumber, std::memory_order_acquire);
int new_value = old_value - (int)inNumber;
if (new_value < 0)
{
int num_to_acquire = min(old_value, 0) - new_value;
for (int i = 0; i < num_to_acquire; ++i)
sem_wait(&mSemaphore);
}
#else
std::unique_lock lock(mLock);
mWaitVariable.wait(lock, [this, inNumber]() {
Expand Down
41 changes: 23 additions & 18 deletions Jolt/Core/Semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,52 @@

#pragma once

#include <Jolt/Core/Atomics.h>

// Determine if we will use pthreads or not
JPH_SUPPRESS_WARNINGS_STD_BEGIN
#include <atomic>
#include <mutex>
#include <condition_variable>
#if defined(JPH_PLATFORM_LINUX) || defined(JPH_PLATFORM_ANDROID) || defined(JPH_PLATFORM_BSD) || defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS) || defined(JPH_PLATFORM_WASM)
#include <semaphore.h>
#define JPH_USE_PTHREADS
#elif !defined(JPH_PLATFORM_WINDOWS)
#include <mutex>
#include <condition_variable>
#endif
JPH_SUPPRESS_WARNINGS_STD_END

JPH_NAMESPACE_BEGIN

// Things we're using from STL
using std::atomic;
using std::mutex;
using std::condition_variable;

/// Implements a semaphore
/// When we switch to C++20 we can use counting_semaphore to unify this
class JPH_EXPORT Semaphore
{
public:
/// Constructor
Semaphore();
~Semaphore();
Semaphore();
~Semaphore();

/// Release the semaphore, signaling the thread waiting on the barrier that there may be work
void Release(uint inNumber = 1);
void Release(uint inNumber = 1);

/// Acquire the semaphore inNumber times
void Acquire(uint inNumber = 1);
void Acquire(uint inNumber = 1);

/// Get the current value of the semaphore
inline int GetValue() const { return mCount.load(std::memory_order_relaxed); }
inline int GetValue() const { return mCount.load(std::memory_order_relaxed); }

private:
#ifdef JPH_PLATFORM_WINDOWS
// On windows we use a semaphore object since it is more efficient than a lock and a condition variable
alignas(JPH_CACHE_LINE_SIZE) atomic<int> mCount { 0 }; ///< We increment mCount for every release, to acquire we decrement the count. If the count is negative we know that we are waiting on the actual 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
alignas(JPH_CACHE_LINE_SIZE) atomic<int> mCount { 0 }; ///< We increment mCount for every release, to acquire we decrement the count. If the count is negative we know that we are waiting on the actual 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
#elif defined(JPH_USE_PTHREADS)
alignas(JPH_CACHE_LINE_SIZE) atomic<int> mCount { 0 }; ///< We increment mCount for every release, to acquire we decrement the count. If the count is negative we know that we are waiting on the actual semaphore.
sem_t 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;
condition_variable mWaitVariable;
atomic<int> mCount { 0 };
std::mutex mLock;
std::condition_variable mWaitVariable;
atomic<int> mCount { 0 };
#endif
};

Expand Down

0 comments on commit 71b662f

Please sign in to comment.