| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/Atomics.h> |
| 8 |
|
| 9 |
|
| 10 |
JPH_SUPPRESS_WARNINGS_STD_BEGIN |
| 11 |
#ifdef JPH_PLATFORM_WINDOWS |
| 12 |
|
| 13 |
#elif defined(JPH_PLATFORM_LINUX) || defined(JPH_PLATFORM_ANDROID) || defined(JPH_PLATFORM_BSD) || defined(JPH_PLATFORM_WASM) |
| 14 |
#include <semaphore.h> |
| 15 |
#define JPH_USE_PTHREADS |
| 16 |
#elif defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS) |
| 17 |
#include <dispatch/dispatch.h> |
| 18 |
#define JPH_USE_GRAND_CENTRAL_DISPATCH |
| 19 |
#elif defined(JPH_PLATFORM_BLUE) |
| 20 |
|
| 21 |
#else |
| 22 |
#include <mutex> |
| 23 |
#include <condition_variable> |
| 24 |
#endif |
| 25 |
JPH_SUPPRESS_WARNINGS_STD_END |
| 26 |
|
| 27 |
JPH_NAMESPACE_BEGIN |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class JPH_EXPORT Semaphore |
| 32 |
{ |
| 33 |
public: |
| 34 |
|
| 35 |
Semaphore(); |
| 36 |
~Semaphore(); |
| 37 |
|
| 38 |
|
| 39 |
void Release(uint inNumber = 1); |
| 40 |
|
| 41 |
|
| 42 |
void Acquire(uint inNumber = 1); |
| 43 |
|
| 44 |
|
| 45 |
inline int GetValue() const { return mCount.load(std::memory_order_relaxed); } |
| 46 |
|
| 47 |
private: |
| 48 |
#if defined(JPH_PLATFORM_WINDOWS) || defined(JPH_USE_PTHREADS) || defined(JPH_USE_GRAND_CENTRAL_DISPATCH) || defined(JPH_PLATFORM_BLUE) |
| 49 |
#ifdef JPH_PLATFORM_WINDOWS |
| 50 |
using SemaphoreType = void *; |
| 51 |
#elif defined(JPH_USE_PTHREADS) |
| 52 |
using SemaphoreType = sem_t; |
| 53 |
#elif defined(JPH_USE_GRAND_CENTRAL_DISPATCH) |
| 54 |
using SemaphoreType = dispatch_semaphore_t; |
| 55 |
#elif defined(JPH_PLATFORM_BLUE) |
| 56 |
using SemaphoreType = JPH_PLATFORM_BLUE_SEMAPHORE; |
| 57 |
#endif |
| 58 |
alignas(JPH_CACHE_LINE_SIZE) atomic<int> mCount { 0 }; |
| 59 |
SemaphoreType mSemaphore { }; |
| 60 |
#else |
| 61 |
|
| 62 |
std::mutex mLock; |
| 63 |
std::condition_variable mWaitVariable; |
| 64 |
atomic<int> mCount { 0 }; |
| 65 |
#endif |
| 66 |
}; |
| 67 |
|
| 68 |
JPH_NAMESPACE_END |