| 1 | #ifndef BASE_TL_THREADING_H |
|---|---|
| 2 | #define BASE_TL_THREADING_H |
| 3 | |
| 4 | #include "../system.h" |
| 5 | |
| 6 | #include <atomic> |
| 7 | |
| 8 | class CSemaphore |
| 9 | { |
| 10 | SEMAPHORE m_Sem; |
| 11 | // implement the counter separately, because the `sem_getvalue`-API is |
| 12 | // deprecated on macOS: https://stackoverflow.com/a/16655541 |
| 13 | std::atomic_int m_Count{0}; |
| 14 | |
| 15 | public: |
| 16 | CSemaphore() { sphore_init(sem: &m_Sem); } |
| 17 | ~CSemaphore() { sphore_destroy(sem: &m_Sem); } |
| 18 | CSemaphore(const CSemaphore &) = delete; |
| 19 | int GetApproximateValue() { return m_Count.load(); } |
| 20 | void Wait() |
| 21 | { |
| 22 | sphore_wait(sem: &m_Sem); |
| 23 | m_Count.fetch_sub(i: 1); |
| 24 | } |
| 25 | void Signal() |
| 26 | { |
| 27 | m_Count.fetch_add(i: 1); |
| 28 | sphore_signal(sem: &m_Sem); |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | #endif // BASE_TL_THREADING_H |
| 33 |