2
Simple DirectMedia Layer
3
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21
#include "SDL_internal.h"
23
// An implementation of rwlocks using mutexes, condition variables, and atomics.
25
#include "SDL_systhread_c.h"
27
#include "../generic/SDL_sysrwlock_c.h"
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
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
44
#ifdef SDL_THREADS_DISABLED
48
SDL_Condition *condition;
49
SDL_ThreadID writer_thread;
50
SDL_AtomicInt reader_count;
51
SDL_AtomicInt writer_count;
55
SDL_RWLock *SDL_CreateRWLock_generic(void)
57
SDL_RWLock *rwlock = (SDL_RWLock *) SDL_calloc(1, sizeof (*rwlock));
63
#ifndef SDL_THREADS_DISABLED
64
rwlock->lock = SDL_CreateMutex();
70
rwlock->condition = SDL_CreateCondition();
71
if (!rwlock->condition) {
72
SDL_DestroyMutex(rwlock->lock);
77
SDL_AtomicSet(&rwlock->reader_count, 0);
78
SDL_AtomicSet(&rwlock->writer_count, 0);
84
void SDL_DestroyRWLock_generic(SDL_RWLock *rwlock)
87
#ifndef SDL_THREADS_DISABLED
88
SDL_DestroyMutex(rwlock->lock);
89
SDL_DestroyCondition(rwlock->condition);
95
void SDL_LockRWLockForReading_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
97
#ifndef SDL_THREADS_DISABLED
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_AtomicGet(&rwlock->writer_count) == 0); // shouldn't be able to grab lock if there's a writer!
102
SDL_AtomicAdd(&rwlock->reader_count, 1);
103
SDL_UnlockMutex(rwlock->lock); // other readers can attempt to share the lock.
108
void SDL_LockRWLockForWriting_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
110
#ifndef SDL_THREADS_DISABLED
112
SDL_LockMutex(rwlock->lock);
113
while (SDL_AtomicGet(&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.
118
SDL_AtomicAdd(&rwlock->writer_count, 1); // we let these be recursive, but the API doesn't require this. It _does_ trust you unlock correctly!
123
bool SDL_TryLockRWLockForReading_generic(SDL_RWLock *rwlock)
125
#ifndef SDL_THREADS_DISABLED
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.
132
SDL_assert(SDL_AtomicGet(&rwlock->writer_count) == 0); // shouldn't be able to grab lock if there's a writer!
133
SDL_AtomicAdd(&rwlock->reader_count, 1);
134
SDL_UnlockMutex(rwlock->lock); // other readers can attempt to share the lock.
141
#ifndef SDL_THREAD_GENERIC_RWLOCK_SUFFIX
142
SDL_bool SDL_TryLockRWLockForReading(SDL_RWLock *rwlock)
144
return SDL_TryLockRWLockForReading_generic(rwlock);
148
bool SDL_TryLockRWLockForWriting_generic(SDL_RWLock *rwlock)
150
#ifndef SDL_THREADS_DISABLED
152
if (!SDL_TryLockMutex(rwlock->lock)) {
156
if (SDL_AtomicGet(&rwlock->reader_count) > 0) { // a reader is using the shared lock, treat it as unavailable.
157
SDL_UnlockMutex(rwlock->lock);
162
SDL_AtomicAdd(&rwlock->writer_count, 1); // we let these be recursive, but the API doesn't require this. It _does_ trust you unlock correctly!
169
#ifndef SDL_THREAD_GENERIC_RWLOCK_SUFFIX
170
SDL_bool SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock)
172
return SDL_TryLockRWLockForWriting_generic(rwlock);
176
void SDL_UnlockRWLock_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
178
#ifndef SDL_THREADS_DISABLED
180
SDL_LockMutex(rwlock->lock); // recursive lock for writers, readers grab lock to make sure things are sane.
182
if (SDL_AtomicGet(&rwlock->reader_count) > 0) { // we're a reader
183
SDL_AtomicAdd(&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_AtomicGet(&rwlock->writer_count) > 0) { // we're a writer
186
SDL_AtomicAdd(&rwlock->writer_count, -1);
187
SDL_UnlockMutex(rwlock->lock); // recursive unlock.
190
SDL_UnlockMutex(rwlock->lock);