2
Simple DirectMedia Layer
3
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
5
This software is provided 'as-is', without any express or implied
6
warranty. In no event will the authors be held liable for any damages
7
arising from the use of this software.
9
Permission is granted to anyone to use this software for any purpose,
10
including commercial applications, and to alter it and redistribute it
11
freely, subject to the following restrictions:
13
1. The origin of this software must not be misrepresented; you must not
14
claim that you wrote the original software. If you use this software
15
in a product, an acknowledgment in the product documentation would be
16
appreciated but is not required.
17
2. Altered source versions must be plainly marked as such, and must not be
18
misrepresented as being the original software.
19
3. This notice may not be removed or altered from any source distribution.
21
#include "SDL_internal.h"
23
// An implementation of semaphores using mutexes and condition variables
25
#include "SDL_systhread_c.h"
27
#ifdef SDL_THREADS_DISABLED
29
SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
31
SDL_SetError("SDL not built with thread support");
35
void SDL_DestroySemaphore(SDL_Semaphore *sem)
39
SDL_bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
44
Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)
49
void SDL_SignalSemaphore(SDL_Semaphore *sem)
60
SDL_Mutex *count_lock;
61
SDL_Condition *count_nonzero;
64
SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
68
sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem));
72
sem->count = initial_value;
73
sem->waiters_count = 0;
75
sem->count_lock = SDL_CreateMutex();
76
sem->count_nonzero = SDL_CreateCondition();
77
if (!sem->count_lock || !sem->count_nonzero) {
78
SDL_DestroySemaphore(sem);
86
You cannot call this function when another thread is using the semaphore.
88
void SDL_DestroySemaphore(SDL_Semaphore *sem)
91
sem->count = 0xFFFFFFFF;
92
while (sem->waiters_count > 0) {
93
SDL_SignalCondition(sem->count_nonzero);
96
SDL_DestroyCondition(sem->count_nonzero);
97
if (sem->count_lock) {
98
SDL_LockMutex(sem->count_lock);
99
SDL_UnlockMutex(sem->count_lock);
100
SDL_DestroyMutex(sem->count_lock);
106
SDL_bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
114
if (timeoutNS == 0) {
115
SDL_LockMutex(sem->count_lock);
116
if (sem->count > 0) {
120
SDL_UnlockMutex(sem->count_lock);
121
} else if (timeoutNS < 0) {
122
SDL_LockMutex(sem->count_lock);
123
++sem->waiters_count;
124
while (sem->count == 0) {
125
SDL_WaitConditionTimeoutNS(sem->count_nonzero, sem->count_lock, -1);
127
--sem->waiters_count;
130
SDL_UnlockMutex(sem->count_lock);
132
Uint64 stop_time = SDL_GetTicksNS() + timeoutNS;
134
SDL_LockMutex(sem->count_lock);
135
++sem->waiters_count;
136
while (sem->count == 0) {
137
Sint64 waitNS = (Sint64)(stop_time - SDL_GetTicksNS());
139
SDL_WaitConditionTimeoutNS(sem->count_nonzero, sem->count_lock, waitNS);
144
--sem->waiters_count;
146
if (sem->count > 0) {
150
SDL_UnlockMutex(sem->count_lock);
156
Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)
162
SDL_LockMutex(sem->count_lock);
164
SDL_UnlockMutex(sem->count_lock);
169
void SDL_SignalSemaphore(SDL_Semaphore *sem)
175
SDL_LockMutex(sem->count_lock);
176
if (sem->waiters_count > 0) {
177
SDL_SignalCondition(sem->count_nonzero);
180
SDL_UnlockMutex(sem->count_lock);
183
#endif // SDL_THREADS_DISABLED