Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Semaphore.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2023 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8#include <atomic>
9#include <mutex>
10#include <condition_variable>
12
14
15// Things we're using from STL
16using std::atomic;
17using std::mutex;
18using std::condition_variable;
19
23{
24public:
26 Semaphore();
27 ~Semaphore();
28
30 void Release(uint inNumber = 1);
31
33 void Acquire(uint inNumber = 1);
34
36 inline int GetValue() const { return mCount; }
37
38private:
39#ifdef JPH_PLATFORM_WINDOWS
40 // On windows we use a semaphore object since it is more efficient than a lock and a condition variable
41 alignas(JPH_CACHE_LINE_SIZE) atomic<int> mCount { 0 };
42 void * mSemaphore;
43#else
44 // Other platforms: Emulate a semaphore using a mutex, condition variable and count
45 mutex mLock;
46 condition_variable mWaitVariable;
47 int mCount = 0;
48#endif
49};
50
#define JPH_EXPORT
Definition: Core.h:227
#define JPH_CACHE_LINE_SIZE
Definition: Core.h:466
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition: Core.h:372
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition: Core.h:384
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
Definition: Semaphore.h:23
int GetValue() const
Get the current value of the semaphore.
Definition: Semaphore.h:36