SDL
1/*
2Simple DirectMedia Layer
3Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5This software is provided 'as-is', without any express or implied
6warranty. In no event will the authors be held liable for any damages
7arising from the use of this software.
8
9Permission is granted to anyone to use this software for any purpose,
10including commercial applications, and to alter it and redistribute it
11freely, subject to the following restrictions:
12
131. The origin of this software must not be misrepresented; you must not
14claim that you wrote the original software. If you use this software
15in a product, an acknowledgment in the product documentation would be
16appreciated but is not required.
172. Altered source versions must be plainly marked as such, and must not be
18misrepresented as being the original software.
193. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"22
23// An implementation of mutexes using semaphores
24
25#include "SDL_systhread_c.h"26
27struct SDL_Mutex28{
29int recursive;30SDL_ThreadID owner;31SDL_Semaphore *sem;32};33
34SDL_Mutex *SDL_CreateMutex(void)35{
36SDL_Mutex *mutex = (SDL_Mutex *)SDL_calloc(1, sizeof(*mutex));37
38#ifndef SDL_THREADS_DISABLED39if (mutex) {40// Create the mutex semaphore, with initial value 141mutex->sem = SDL_CreateSemaphore(1);42mutex->recursive = 0;43mutex->owner = 0;44if (!mutex->sem) {45SDL_free(mutex);46mutex = NULL;47}48}49#endif // !SDL_THREADS_DISABLED50
51return mutex;52}
53
54void SDL_DestroyMutex(SDL_Mutex *mutex)55{
56if (mutex) {57if (mutex->sem) {58SDL_DestroySemaphore(mutex->sem);59}60SDL_free(mutex);61}62}
63
64void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes65{
66#ifndef SDL_THREADS_DISABLED67if (mutex != NULL) {68SDL_ThreadID this_thread = SDL_GetCurrentThreadID();69if (mutex->owner == this_thread) {70++mutex->recursive;71} else {72/* The order of operations is important.73We set the locking thread id after we obtain the lock
74so unlocks from other threads will fail.
75*/
76SDL_WaitSemaphore(mutex->sem);77mutex->owner = this_thread;78mutex->recursive = 0;79}80}81#endif // SDL_THREADS_DISABLED82}
83
84SDL_bool SDL_TryLockMutex(SDL_Mutex *mutex)85{
86bool result = true;87#ifndef SDL_THREADS_DISABLED88if (mutex) {89SDL_ThreadID this_thread = SDL_GetCurrentThreadID();90if (mutex->owner == this_thread) {91++mutex->recursive;92} else {93/* The order of operations is important.94We set the locking thread id after we obtain the lock
95so unlocks from other threads will fail.
96*/
97result = SDL_TryWaitSemaphore(mutex->sem);98if (result) {99mutex->owner = this_thread;100mutex->recursive = 0;101}102}103}104#endif // SDL_THREADS_DISABLED105return result;106}
107
108void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes109{
110#ifndef SDL_THREADS_DISABLED111if (mutex != NULL) {112// If we don't own the mutex, we can't unlock it113if (SDL_GetCurrentThreadID() != mutex->owner) {114SDL_assert(!"Tried to unlock a mutex we don't own!");115return; // (undefined behavior!) SDL_SetError("mutex not owned by this thread");116}117
118if (mutex->recursive) {119--mutex->recursive;120} else {121/* The order of operations is important.122First reset the owner so another thread doesn't lock
123the mutex and set the ownership before we reset it,
124then release the lock semaphore.
125*/
126mutex->owner = 0;127SDL_SignalSemaphore(mutex->sem);128}129}130#endif // SDL_THREADS_DISABLED131}
132
133