Path: blob/master/thirdparty/sdl/thread/generic/SDL_sysrwlock.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 rwlocks using mutexes, condition variables, and atomics.2324#include "SDL_systhread_c.h"2526#include "../generic/SDL_sysrwlock_c.h"2728/* If two implementations are to be compiled into SDL (the active one29* will be chosen at runtime), the function names need to be30* suffixed31*/32// !!! FIXME: this is quite a tapdance with macros and the build system, maybe we can simplify how we do this. --ryan.33#ifndef SDL_THREAD_GENERIC_RWLOCK_SUFFIX34#define SDL_CreateRWLock_generic SDL_CreateRWLock35#define SDL_DestroyRWLock_generic SDL_DestroyRWLock36#define SDL_LockRWLockForReading_generic SDL_LockRWLockForReading37#define SDL_LockRWLockForWriting_generic SDL_LockRWLockForWriting38#define SDL_UnlockRWLock_generic SDL_UnlockRWLock39#endif4041struct SDL_RWLock42{43#ifdef SDL_THREADS_DISABLED44int unused;45#else46SDL_Mutex *lock;47SDL_Condition *condition;48SDL_ThreadID writer_thread;49SDL_AtomicInt reader_count;50SDL_AtomicInt writer_count;51#endif52};5354SDL_RWLock *SDL_CreateRWLock_generic(void)55{56SDL_RWLock *rwlock = (SDL_RWLock *) SDL_calloc(1, sizeof (*rwlock));5758if (!rwlock) {59return NULL;60}6162#ifndef SDL_THREADS_DISABLED63rwlock->lock = SDL_CreateMutex();64if (!rwlock->lock) {65SDL_free(rwlock);66return NULL;67}6869rwlock->condition = SDL_CreateCondition();70if (!rwlock->condition) {71SDL_DestroyMutex(rwlock->lock);72SDL_free(rwlock);73return NULL;74}7576SDL_SetAtomicInt(&rwlock->reader_count, 0);77SDL_SetAtomicInt(&rwlock->writer_count, 0);78#endif7980return rwlock;81}8283void SDL_DestroyRWLock_generic(SDL_RWLock *rwlock)84{85if (rwlock) {86#ifndef SDL_THREADS_DISABLED87SDL_DestroyMutex(rwlock->lock);88SDL_DestroyCondition(rwlock->condition);89#endif90SDL_free(rwlock);91}92}9394void SDL_LockRWLockForReading_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes95{96#ifndef SDL_THREADS_DISABLED97if (rwlock) {98// !!! FIXME: these don't have to be atomic, we always gate them behind a mutex.99SDL_LockMutex(rwlock->lock);100SDL_assert(SDL_GetAtomicInt(&rwlock->writer_count) == 0); // shouldn't be able to grab lock if there's a writer!101SDL_AddAtomicInt(&rwlock->reader_count, 1);102SDL_UnlockMutex(rwlock->lock); // other readers can attempt to share the lock.103}104#endif105}106107void SDL_LockRWLockForWriting_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes108{109#ifndef SDL_THREADS_DISABLED110if (rwlock) {111SDL_LockMutex(rwlock->lock);112while (SDL_GetAtomicInt(&rwlock->reader_count) > 0) { // while something is holding the shared lock, keep waiting.113SDL_WaitCondition(rwlock->condition, rwlock->lock); // release the lock and wait for readers holding the shared lock to release it, regrab the lock.114}115116// we hold the lock!117SDL_AddAtomicInt(&rwlock->writer_count, 1); // we let these be recursive, but the API doesn't require this. It _does_ trust you unlock correctly!118}119#endif120}121122bool SDL_TryLockRWLockForReading_generic(SDL_RWLock *rwlock)123{124#ifndef SDL_THREADS_DISABLED125if (rwlock) {126if (!SDL_TryLockMutex(rwlock->lock)) {127// !!! 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.128return false;129}130131SDL_assert(SDL_GetAtomicInt(&rwlock->writer_count) == 0); // shouldn't be able to grab lock if there's a writer!132SDL_AddAtomicInt(&rwlock->reader_count, 1);133SDL_UnlockMutex(rwlock->lock); // other readers can attempt to share the lock.134}135#endif136137return true;138}139140#ifndef SDL_THREAD_GENERIC_RWLOCK_SUFFIX141bool SDL_TryLockRWLockForReading(SDL_RWLock *rwlock)142{143return SDL_TryLockRWLockForReading_generic(rwlock);144}145#endif146147bool SDL_TryLockRWLockForWriting_generic(SDL_RWLock *rwlock)148{149#ifndef SDL_THREADS_DISABLED150if (rwlock) {151if (!SDL_TryLockMutex(rwlock->lock)) {152return false;153}154155if (SDL_GetAtomicInt(&rwlock->reader_count) > 0) { // a reader is using the shared lock, treat it as unavailable.156SDL_UnlockMutex(rwlock->lock);157return false;158}159160// we hold the lock!161SDL_AddAtomicInt(&rwlock->writer_count, 1); // we let these be recursive, but the API doesn't require this. It _does_ trust you unlock correctly!162}163#endif164165return true;166}167168#ifndef SDL_THREAD_GENERIC_RWLOCK_SUFFIX169bool SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock)170{171return SDL_TryLockRWLockForWriting_generic(rwlock);172}173#endif174175void SDL_UnlockRWLock_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes176{177#ifndef SDL_THREADS_DISABLED178if (rwlock) {179SDL_LockMutex(rwlock->lock); // recursive lock for writers, readers grab lock to make sure things are sane.180181if (SDL_GetAtomicInt(&rwlock->reader_count) > 0) { // we're a reader182SDL_AddAtomicInt(&rwlock->reader_count, -1);183SDL_BroadcastCondition(rwlock->condition); // alert any pending writers to attempt to try to grab the lock again.184} else if (SDL_GetAtomicInt(&rwlock->writer_count) > 0) { // we're a writer185SDL_AddAtomicInt(&rwlock->writer_count, -1);186SDL_UnlockMutex(rwlock->lock); // recursive unlock.187}188189SDL_UnlockMutex(rwlock->lock);190}191#endif192}193194195196