Path: blob/master/thirdparty/sdl/thread/generic/SDL_syssem.c
10279 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122// An implementation of semaphores using mutexes and condition variables2324#include "SDL_systhread_c.h"2526#ifdef SDL_THREADS_DISABLED2728SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)29{30SDL_SetError("SDL not built with thread support");31return NULL;32}3334void SDL_DestroySemaphore(SDL_Semaphore *sem)35{36}3738bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)39{40return true;41}4243Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)44{45return 0;46}4748void SDL_SignalSemaphore(SDL_Semaphore *sem)49{50return;51}5253#else5455struct SDL_Semaphore56{57Uint32 count;58Uint32 waiters_count;59SDL_Mutex *count_lock;60SDL_Condition *count_nonzero;61};6263SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)64{65SDL_Semaphore *sem;6667sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem));68if (!sem) {69return NULL;70}71sem->count = initial_value;72sem->waiters_count = 0;7374sem->count_lock = SDL_CreateMutex();75sem->count_nonzero = SDL_CreateCondition();76if (!sem->count_lock || !sem->count_nonzero) {77SDL_DestroySemaphore(sem);78return NULL;79}8081return sem;82}8384/* WARNING:85You cannot call this function when another thread is using the semaphore.86*/87void SDL_DestroySemaphore(SDL_Semaphore *sem)88{89if (sem) {90sem->count = 0xFFFFFFFF;91while (sem->waiters_count > 0) {92SDL_SignalCondition(sem->count_nonzero);93SDL_Delay(10);94}95SDL_DestroyCondition(sem->count_nonzero);96if (sem->count_lock) {97SDL_LockMutex(sem->count_lock);98SDL_UnlockMutex(sem->count_lock);99SDL_DestroyMutex(sem->count_lock);100}101SDL_free(sem);102}103}104105bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)106{107bool result = false;108109if (!sem) {110return true;111}112113if (timeoutNS == 0) {114SDL_LockMutex(sem->count_lock);115if (sem->count > 0) {116--sem->count;117result = true;118}119SDL_UnlockMutex(sem->count_lock);120} else if (timeoutNS < 0) {121SDL_LockMutex(sem->count_lock);122++sem->waiters_count;123while (sem->count == 0) {124SDL_WaitConditionTimeoutNS(sem->count_nonzero, sem->count_lock, -1);125}126--sem->waiters_count;127--sem->count;128result = true;129SDL_UnlockMutex(sem->count_lock);130} else {131Uint64 stop_time = SDL_GetTicksNS() + timeoutNS;132133SDL_LockMutex(sem->count_lock);134++sem->waiters_count;135while (sem->count == 0) {136Sint64 waitNS = (Sint64)(stop_time - SDL_GetTicksNS());137if (waitNS > 0) {138SDL_WaitConditionTimeoutNS(sem->count_nonzero, sem->count_lock, waitNS);139} else {140break;141}142}143--sem->waiters_count;144145if (sem->count > 0) {146--sem->count;147result = true;148}149SDL_UnlockMutex(sem->count_lock);150}151152return result;153}154155Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)156{157Uint32 value;158159value = 0;160if (sem) {161SDL_LockMutex(sem->count_lock);162value = sem->count;163SDL_UnlockMutex(sem->count_lock);164}165return value;166}167168void SDL_SignalSemaphore(SDL_Semaphore *sem)169{170if (!sem) {171return;172}173174SDL_LockMutex(sem->count_lock);175if (sem->waiters_count > 0) {176SDL_SignalCondition(sem->count_nonzero);177}178++sem->count;179SDL_UnlockMutex(sem->count_lock);180}181182#endif // SDL_THREADS_DISABLED183184185