Atlas - SDL_sysrwlock.c

Home / ext / SDL / src / thread / generic Lines: 1 | Size: 6523 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 rwlocks using mutexes, condition variables, and atomics. 24 25#include "SDL_systhread_c.h" 26 27#include "../generic/SDL_sysrwlock_c.h" 28 29/* If two implementations are to be compiled into SDL (the active one 30 * will be chosen at runtime), the function names need to be 31 * suffixed 32 */ 33// !!! FIXME: this is quite a tapdance with macros and the build system, maybe we can simplify how we do this. --ryan. 34#ifndef SDL_THREAD_GENERIC_RWLOCK_SUFFIX 35#define SDL_CreateRWLock_generic SDL_CreateRWLock 36#define SDL_DestroyRWLock_generic SDL_DestroyRWLock 37#define SDL_LockRWLockForReading_generic SDL_LockRWLockForReading 38#define SDL_LockRWLockForWriting_generic SDL_LockRWLockForWriting 39#define SDL_UnlockRWLock_generic SDL_UnlockRWLock 40#endif 41 42struct SDL_RWLock 43{ 44#ifdef SDL_THREADS_DISABLED 45 int unused; 46#else 47 SDL_Mutex *lock; 48 SDL_Condition *condition; 49 SDL_ThreadID writer_thread; 50 SDL_AtomicInt reader_count; 51 SDL_AtomicInt writer_count; 52#endif 53}; 54 55SDL_RWLock *SDL_CreateRWLock_generic(void) 56{ 57 SDL_RWLock *rwlock = (SDL_RWLock *) SDL_calloc(1, sizeof (*rwlock)); 58 59 if (!rwlock) { 60 return NULL; 61 } 62 63#ifndef SDL_THREADS_DISABLED 64 rwlock->lock = SDL_CreateMutex(); 65 if (!rwlock->lock) { 66 SDL_free(rwlock); 67 return NULL; 68 } 69 70 rwlock->condition = SDL_CreateCondition(); 71 if (!rwlock->condition) { 72 SDL_DestroyMutex(rwlock->lock); 73 SDL_free(rwlock); 74 return NULL; 75 } 76 77 SDL_SetAtomicInt(&rwlock->reader_count, 0); 78 SDL_SetAtomicInt(&rwlock->writer_count, 0); 79#endif 80 81 return rwlock; 82} 83 84void SDL_DestroyRWLock_generic(SDL_RWLock *rwlock) 85{ 86 if (rwlock) { 87#ifndef SDL_THREADS_DISABLED 88 SDL_DestroyMutex(rwlock->lock); 89 SDL_DestroyCondition(rwlock->condition); 90#endif 91 SDL_free(rwlock); 92 } 93} 94 95void SDL_LockRWLockForReading_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes 96{ 97#ifndef SDL_THREADS_DISABLED 98 if (rwlock) { 99 // !!! FIXME: these don't have to be atomic, we always gate them behind a mutex. 100 SDL_LockMutex(rwlock->lock); 101 SDL_assert(SDL_GetAtomicInt(&rwlock->writer_count) == 0); // shouldn't be able to grab lock if there's a writer! 102 SDL_AddAtomicInt(&rwlock->reader_count, 1); 103 SDL_UnlockMutex(rwlock->lock); // other readers can attempt to share the lock. 104 } 105#endif 106} 107 108void SDL_LockRWLockForWriting_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes 109{ 110#ifndef SDL_THREADS_DISABLED 111 if (rwlock) { 112 SDL_LockMutex(rwlock->lock); 113 while (SDL_GetAtomicInt(&rwlock->reader_count) > 0) { // while something is holding the shared lock, keep waiting. 114 SDL_WaitCondition(rwlock->condition, rwlock->lock); // release the lock and wait for readers holding the shared lock to release it, regrab the lock. 115 } 116 117 // we hold the lock! 118 SDL_AddAtomicInt(&rwlock->writer_count, 1); // we let these be recursive, but the API doesn't require this. It _does_ trust you unlock correctly! 119 } 120#endif 121} 122 123bool SDL_TryLockRWLockForReading_generic(SDL_RWLock *rwlock) 124{ 125#ifndef SDL_THREADS_DISABLED 126 if (rwlock) { 127 if (!SDL_TryLockMutex(rwlock->lock)) { 128 // !!! FIXME: there is a small window where a reader has to lock the mutex, and if we hit that, we will return SDL_RWLOCK_TIMEDOUT even though we could have shared the lock. 129 return false; 130 } 131 132 SDL_assert(SDL_GetAtomicInt(&rwlock->writer_count) == 0); // shouldn't be able to grab lock if there's a writer! 133 SDL_AddAtomicInt(&rwlock->reader_count, 1); 134 SDL_UnlockMutex(rwlock->lock); // other readers can attempt to share the lock. 135 } 136#endif 137 138 return true; 139} 140 141#ifndef SDL_THREAD_GENERIC_RWLOCK_SUFFIX 142bool SDL_TryLockRWLockForReading(SDL_RWLock *rwlock) 143{ 144 return SDL_TryLockRWLockForReading_generic(rwlock); 145} 146#endif 147 148bool SDL_TryLockRWLockForWriting_generic(SDL_RWLock *rwlock) 149{ 150#ifndef SDL_THREADS_DISABLED 151 if (rwlock) { 152 if (!SDL_TryLockMutex(rwlock->lock)) { 153 return false; 154 } 155 156 if (SDL_GetAtomicInt(&rwlock->reader_count) > 0) { // a reader is using the shared lock, treat it as unavailable. 157 SDL_UnlockMutex(rwlock->lock); 158 return false; 159 } 160 161 // we hold the lock! 162 SDL_AddAtomicInt(&rwlock->writer_count, 1); // we let these be recursive, but the API doesn't require this. It _does_ trust you unlock correctly! 163 } 164#endif 165 166 return true; 167} 168 169#ifndef SDL_THREAD_GENERIC_RWLOCK_SUFFIX 170bool SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock) 171{ 172 return SDL_TryLockRWLockForWriting_generic(rwlock); 173} 174#endif 175 176void SDL_UnlockRWLock_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes 177{ 178#ifndef SDL_THREADS_DISABLED 179 if (rwlock) { 180 SDL_LockMutex(rwlock->lock); // recursive lock for writers, readers grab lock to make sure things are sane. 181 182 if (SDL_GetAtomicInt(&rwlock->reader_count) > 0) { // we're a reader 183 SDL_AddAtomicInt(&rwlock->reader_count, -1); 184 SDL_BroadcastCondition(rwlock->condition); // alert any pending writers to attempt to try to grab the lock again. 185 } else if (SDL_GetAtomicInt(&rwlock->writer_count) > 0) { // we're a writer 186 SDL_AddAtomicInt(&rwlock->writer_count, -1); 187 SDL_UnlockMutex(rwlock->lock); // recursive unlock. 188 } 189 190 SDL_UnlockMutex(rwlock->lock); 191 } 192#endif 193} 194 195
[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.