DDNet documentation
Toggle main menu visibility
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)
21
typedef
void
*
SEMAPHORE
;
22
#elif defined(CONF_PLATFORM_MACOS) || defined(CONF_PLATFORM_IOS)
23
#include <semaphore.h>
27
typedef
sem_t *
SEMAPHORE
;
28
#elif defined(CONF_FAMILY_UNIX)
29
#include <semaphore.h>
33
typedef
sem_t
SEMAPHORE
;
34
#else
35
#error not implemented on this platform
36
#endif
37
41
void
sphore_init
(
SEMAPHORE
*sem);
42
46
void
sphore_wait
(
SEMAPHORE
*sem);
47
51
void
sphore_signal
(
SEMAPHORE
*sem);
52
56
void
sphore_destroy
(
SEMAPHORE
*sem);
57
61
class
CSemaphore
62
{
63
SEMAPHORE
m_Sem
;
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
68
public
:
69
CSemaphore
() {
sphore_init
(&
m_Sem
); }
70
~CSemaphore
() {
sphore_destroy
(&
m_Sem
); }
71
CSemaphore
(
const
CSemaphore
&) =
delete
;
72
int
GetApproximateValue
() {
return
m_Count
.load(); }
73
void
Wait
()
74
{
75
sphore_wait
(&
m_Sem
);
76
m_Count
.fetch_sub(1);
77
}
78
void
Signal
()
79
{
80
m_Count
.fetch_add(1);
81
sphore_signal
(&
m_Sem
);
82
}
83
};
84
85
#endif
CSemaphore::~CSemaphore
~CSemaphore()
Definition
sphore.h:70
CSemaphore::m_Count
std::atomic_int m_Count
Definition
sphore.h:66
CSemaphore::GetApproximateValue
int GetApproximateValue()
Definition
sphore.h:72
CSemaphore::CSemaphore
CSemaphore()
Definition
sphore.h:69
CSemaphore::Wait
void Wait()
Definition
sphore.h:73
CSemaphore::m_Sem
SEMAPHORE m_Sem
Definition
sphore.h:63
CSemaphore::CSemaphore
CSemaphore(const CSemaphore &)=delete
CSemaphore::Signal
void Signal()
Definition
sphore.h:78
detect.h
sphore_signal
void sphore_signal(SEMAPHORE *sem)
Definition
sphore.cpp:85
sphore_destroy
void sphore_destroy(SEMAPHORE *sem)
Definition
sphore.cpp:89
sphore_init
void sphore_init(SEMAPHORE *sem)
Definition
sphore.cpp:72
SEMAPHORE
sem_t SEMAPHORE
Definition
sphore.h:33
sphore_wait
void sphore_wait(SEMAPHORE *sem)
Definition
sphore.cpp:76
src
base
sphore.h
Generated by
1.18.0