1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
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#include <errno.h>
24#include <pthread.h>
25
26struct SDL_RWLock
27{
28 pthread_rwlock_t id;
29};
30
31
32SDL_RWLock *SDL_CreateRWLock(void)
33{
34 SDL_RWLock *rwlock;
35
36 // Allocate the structure
37 rwlock = (SDL_RWLock *)SDL_calloc(1, sizeof(*rwlock));
38 if (rwlock) {
39 if (pthread_rwlock_init(&rwlock->id, NULL) != 0) {
40 SDL_SetError("pthread_rwlock_init() failed");
41 SDL_free(rwlock);
42 rwlock = NULL;
43 }
44 }
45 return rwlock;
46}
47
48void SDL_DestroyRWLock(SDL_RWLock *rwlock)
49{
50 if (rwlock) {
51 pthread_rwlock_destroy(&rwlock->id);
52 SDL_free(rwlock);
53 }
54}
55
56void SDL_LockRWLockForReading(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
57{
58 if (rwlock) {
59 const int rc = pthread_rwlock_rdlock(&rwlock->id);
60 SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.
61 }
62}
63
64void SDL_LockRWLockForWriting(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
65{
66 if (rwlock) {
67 const int rc = pthread_rwlock_wrlock(&rwlock->id);
68 SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.
69 }
70}
71
72bool SDL_TryLockRWLockForReading(SDL_RWLock *rwlock)
73{
74 bool result = true;
75
76 if (rwlock) {
77 const int rc = pthread_rwlock_tryrdlock(&rwlock->id);
78 if (rc != 0) {
79 result = false;
80 if (rc != EBUSY) {
81 SDL_assert(!"Error trying to lock rwlock for reading"); // assume we're in a lot of trouble if this assert fails.
82 }
83 }
84 }
85
86 return result;
87}
88
89bool SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock)
90{
91 bool result = true;
92
93 if (rwlock) {
94 const int rc = pthread_rwlock_trywrlock(&rwlock->id);
95 if (rc != 0) {
96 result = false;
97 if (rc != EBUSY) {
98 SDL_assert(!"Error trying to lock rwlock for writing"); // assume we're in a lot of trouble if this assert fails.
99 }
100 }
101 }
102
103 return result;
104}
105
106void SDL_UnlockRWLock(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
107{
108 if (rwlock) {
109 const int rc = pthread_rwlock_unlock(&rwlock->id);
110 SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.
111 }
112}
113
114