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