DDNet documentation
Loading...
Searching...
No Matches
sphore.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#ifndef BASE_SPHORE_H
5#define BASE_SPHORE_H
6
7#include "detect.h"
8
9#include <atomic>
10
16
17#if defined(CONF_FAMILY_WINDOWS)
21typedef void *SEMAPHORE;
22#elif defined(CONF_PLATFORM_MACOS)
23#include <semaphore.h>
27typedef sem_t *SEMAPHORE;
28#elif defined(CONF_FAMILY_UNIX)
29#include <semaphore.h>
33typedef sem_t SEMAPHORE;
34#else
35#error not implemented on this platform
36#endif
37
41void sphore_init(SEMAPHORE *sem);
42
46void sphore_wait(SEMAPHORE *sem);
47
51void sphore_signal(SEMAPHORE *sem);
52
56void sphore_destroy(SEMAPHORE *sem);
57
62{
64 // implement the counter separately, because the `sem_getvalue`-API is
65 // deprecated on macOS: https://stackoverflow.com/a/16655541
66 std::atomic_int m_Count{0};
67
68public:
71 CSemaphore(const CSemaphore &) = delete;
72 int GetApproximateValue() { return m_Count.load(); }
73 void Wait()
74 {
76 m_Count.fetch_sub(1);
77 }
78 void Signal()
79 {
80 m_Count.fetch_add(1);
82 }
83};
84
85#endif
~CSemaphore()
Definition sphore.h:70
std::atomic_int m_Count
Definition sphore.h:66
int GetApproximateValue()
Definition sphore.h:72
CSemaphore()
Definition sphore.h:69
void Wait()
Definition sphore.h:73
SEMAPHORE m_Sem
Definition sphore.h:63
CSemaphore(const CSemaphore &)=delete
void Signal()
Definition sphore.h:78
void sphore_signal(SEMAPHORE *sem)
Definition sphore.cpp:84
void sphore_destroy(SEMAPHORE *sem)
Definition sphore.cpp:88
void sphore_init(SEMAPHORE *sem)
Definition sphore.cpp:71
sem_t SEMAPHORE
Definition sphore.h:33
void sphore_wait(SEMAPHORE *sem)
Definition sphore.cpp:75