Atlas - SDL_sysmutex.c
Home / ext / SDL / src / thread / generic Lines: 1 | Size: 4065 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 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. 8 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: 12 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. 20*/ 21#include "SDL_internal.h" 22 23// An implementation of mutexes using semaphores 24 25#include "SDL_systhread_c.h" 26 27struct SDL_Mutex 28{ 29 int recursive; 30 SDL_ThreadID owner; 31 SDL_Semaphore *sem; 32}; 33 34SDL_Mutex *SDL_CreateMutex(void) 35{ 36 SDL_Mutex *mutex = (SDL_Mutex *)SDL_calloc(1, sizeof(*mutex)); 37 38#ifndef SDL_THREADS_DISABLED 39 if (mutex) { 40 // Create the mutex semaphore, with initial value 1 41 mutex->sem = SDL_CreateSemaphore(1); 42 mutex->recursive = 0; 43 mutex->owner = 0; 44 if (!mutex->sem) { 45 SDL_free(mutex); 46 mutex = NULL; 47 } 48 } 49#endif // !SDL_THREADS_DISABLED 50 51 return mutex; 52} 53 54void SDL_DestroyMutex(SDL_Mutex *mutex) 55{ 56 if (mutex) { 57 if (mutex->sem) { 58 SDL_DestroySemaphore(mutex->sem); 59 } 60 SDL_free(mutex); 61 } 62} 63 64void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes 65{ 66#ifndef SDL_THREADS_DISABLED 67 if (mutex != NULL) { 68 SDL_ThreadID this_thread = SDL_GetCurrentThreadID(); 69 if (mutex->owner == this_thread) { 70 ++mutex->recursive; 71 } else { 72 /* The order of operations is important. 73 We set the locking thread id after we obtain the lock 74 so unlocks from other threads will fail. 75 */ 76 SDL_WaitSemaphore(mutex->sem); 77 mutex->owner = this_thread; 78 mutex->recursive = 0; 79 } 80 } 81#endif // SDL_THREADS_DISABLED 82} 83 84bool SDL_TryLockMutex(SDL_Mutex *mutex) 85{ 86 bool result = true; 87#ifndef SDL_THREADS_DISABLED 88 if (mutex) { 89 SDL_ThreadID this_thread = SDL_GetCurrentThreadID(); 90 if (mutex->owner == this_thread) { 91 ++mutex->recursive; 92 } else { 93 /* The order of operations is important. 94 We set the locking thread id after we obtain the lock 95 so unlocks from other threads will fail. 96 */ 97 result = SDL_TryWaitSemaphore(mutex->sem); 98 if (result) { 99 mutex->owner = this_thread; 100 mutex->recursive = 0; 101 } 102 } 103 } 104#endif // SDL_THREADS_DISABLED 105 return result; 106} 107 108void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes 109{ 110#ifndef SDL_THREADS_DISABLED 111 if (mutex != NULL) { 112 // If we don't own the mutex, we can't unlock it 113 if (SDL_GetCurrentThreadID() != mutex->owner) { 114 SDL_assert(!"Tried to unlock a mutex we don't own!"); 115 return; // (undefined behavior!) SDL_SetError("mutex not owned by this thread"); 116 } 117 118 if (mutex->recursive) { 119 --mutex->recursive; 120 } else { 121 /* The order of operations is important. 122 First reset the owner so another thread doesn't lock 123 the mutex and set the ownership before we reset it, 124 then release the lock semaphore. 125 */ 126 mutex->owner = 0; 127 SDL_SignalSemaphore(mutex->sem); 128 } 129 } 130#endif // SDL_THREADS_DISABLED 131} 132 133[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.