DDraceNetwork 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)
18typedef void *SEMAPHORE;
19#elif defined(CONF_PLATFORM_MACOS)
20#include <semaphore.h>
21typedef sem_t *SEMAPHORE;
22#elif defined(CONF_FAMILY_UNIX)
23#include <semaphore.h>
24typedef sem_t SEMAPHORE;
25#else
26#error not implemented on this platform
27#endif
28
32void sphore_init(SEMAPHORE *sem);
33
37void sphore_wait(SEMAPHORE *sem);
38
42void sphore_signal(SEMAPHORE *sem);
43
47void sphore_destroy(SEMAPHORE *sem);
48
50{
52 // implement the counter separately, because the `sem_getvalue`-API is
53 // deprecated on macOS: https://stackoverflow.com/a/16655541
54 std::atomic_int m_Count{0};
55
56public:
59 CSemaphore(const CSemaphore &) = delete;
60 int GetApproximateValue() { return m_Count.load(); }
61 void Wait()
62 {
64 m_Count.fetch_sub(1);
65 }
66 void Signal()
67 {
68 m_Count.fetch_add(1);
70 }
71};
72
73#endif
~CSemaphore()
Definition sphore.h:58
std::atomic_int m_Count
Definition sphore.h:54
int GetApproximateValue()
Definition sphore.h:60
CSemaphore()
Definition sphore.h:57
void Wait()
Definition sphore.h:61
SEMAPHORE m_Sem
Definition sphore.h:51
CSemaphore(const CSemaphore &)=delete
void Signal()
Definition sphore.h:66
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
void sphore_wait(SEMAPHORE *sem)
Definition sphore.cpp:75
sem_t SEMAPHORE
Definition sphore.h:24