SDL_sysrwlock.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. // An implementation of rwlocks using mutexes, condition variables, and atomics.
  20. #include "SDL_systhread_c.h"
  21. #include "../generic/SDL_sysrwlock_c.h"
  22. /* If two implementations are to be compiled into SDL (the active one
  23. * will be chosen at runtime), the function names need to be
  24. * suffixed
  25. */
  26. // !!! FIXME: this is quite a tapdance with macros and the build system, maybe we can simplify how we do this. --ryan.
  27. #ifndef SDL_THREAD_GENERIC_RWLOCK_SUFFIX
  28. #define SDL_CreateRWLock_generic SDL_CreateRWLock
  29. #define SDL_DestroyRWLock_generic SDL_DestroyRWLock
  30. #define SDL_LockRWLockForReading_generic SDL_LockRWLockForReading
  31. #define SDL_LockRWLockForWriting_generic SDL_LockRWLockForWriting
  32. #define SDL_UnlockRWLock_generic SDL_UnlockRWLock
  33. #endif
  34. struct SDL_RWLock
  35. {
  36. #ifdef SDL_THREADS_DISABLED
  37. int unused;
  38. #else
  39. SDL_Mutex *lock;
  40. SDL_Condition *condition;
  41. SDL_ThreadID writer_thread;
  42. SDL_AtomicInt reader_count;
  43. SDL_AtomicInt writer_count;
  44. #endif
  45. };
  46. SDL_RWLock *SDL_CreateRWLock_generic(void)
  47. {
  48. SDL_RWLock *rwlock = (SDL_RWLock *) SDL_calloc(1, sizeof (*rwlock));
  49. if (!rwlock) {
  50. return NULL;
  51. }
  52. #ifndef SDL_THREADS_DISABLED
  53. rwlock->lock = SDL_CreateMutex();
  54. if (!rwlock->lock) {
  55. SDL_free(rwlock);
  56. return NULL;
  57. }
  58. rwlock->condition = SDL_CreateCondition();
  59. if (!rwlock->condition) {
  60. SDL_DestroyMutex(rwlock->lock);
  61. SDL_free(rwlock);
  62. return NULL;
  63. }
  64. SDL_SetAtomicInt(&rwlock->reader_count, 0);
  65. SDL_SetAtomicInt(&rwlock->writer_count, 0);
  66. #endif
  67. return rwlock;
  68. }
  69. void SDL_DestroyRWLock_generic(SDL_RWLock *rwlock)
  70. {
  71. if (rwlock) {
  72. #ifndef SDL_THREADS_DISABLED
  73. SDL_DestroyMutex(rwlock->lock);
  74. SDL_DestroyCondition(rwlock->condition);
  75. #endif
  76. SDL_free(rwlock);
  77. }
  78. }
  79. void SDL_LockRWLockForReading_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  80. {
  81. #ifndef SDL_THREADS_DISABLED
  82. if (rwlock) {
  83. // !!! FIXME: these don't have to be atomic, we always gate them behind a mutex.
  84. SDL_LockMutex(rwlock->lock);
  85. SDL_assert(SDL_GetAtomicInt(&rwlock->writer_count) == 0); // shouldn't be able to grab lock if there's a writer!
  86. SDL_AddAtomicInt(&rwlock->reader_count, 1);
  87. SDL_UnlockMutex(rwlock->lock); // other readers can attempt to share the lock.
  88. }
  89. #endif
  90. }
  91. void SDL_LockRWLockForWriting_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  92. {
  93. #ifndef SDL_THREADS_DISABLED
  94. if (rwlock) {
  95. SDL_LockMutex(rwlock->lock);
  96. while (SDL_GetAtomicInt(&rwlock->reader_count) > 0) { // while something is holding the shared lock, keep waiting.
  97. SDL_WaitCondition(rwlock->condition, rwlock->lock); // release the lock and wait for readers holding the shared lock to release it, regrab the lock.
  98. }
  99. // we hold the lock!
  100. SDL_AddAtomicInt(&rwlock->writer_count, 1); // we let these be recursive, but the API doesn't require this. It _does_ trust you unlock correctly!
  101. }
  102. #endif
  103. }
  104. bool SDL_TryLockRWLockForReading_generic(SDL_RWLock *rwlock)
  105. {
  106. #ifndef SDL_THREADS_DISABLED
  107. if (rwlock) {
  108. if (!SDL_TryLockMutex(rwlock->lock)) {
  109. // !!! 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.
  110. return false;
  111. }
  112. SDL_assert(SDL_GetAtomicInt(&rwlock->writer_count) == 0); // shouldn't be able to grab lock if there's a writer!
  113. SDL_AddAtomicInt(&rwlock->reader_count, 1);
  114. SDL_UnlockMutex(rwlock->lock); // other readers can attempt to share the lock.
  115. }
  116. #endif
  117. return true;
  118. }
  119. #ifndef SDL_THREAD_GENERIC_RWLOCK_SUFFIX
  120. bool SDL_TryLockRWLockForReading(SDL_RWLock *rwlock)
  121. {
  122. return SDL_TryLockRWLockForReading_generic(rwlock);
  123. }
  124. #endif
  125. bool SDL_TryLockRWLockForWriting_generic(SDL_RWLock *rwlock)
  126. {
  127. #ifndef SDL_THREADS_DISABLED
  128. if (rwlock) {
  129. if (!SDL_TryLockMutex(rwlock->lock)) {
  130. return false;
  131. }
  132. if (SDL_GetAtomicInt(&rwlock->reader_count) > 0) { // a reader is using the shared lock, treat it as unavailable.
  133. SDL_UnlockMutex(rwlock->lock);
  134. return false;
  135. }
  136. // we hold the lock!
  137. SDL_AddAtomicInt(&rwlock->writer_count, 1); // we let these be recursive, but the API doesn't require this. It _does_ trust you unlock correctly!
  138. }
  139. #endif
  140. return true;
  141. }
  142. #ifndef SDL_THREAD_GENERIC_RWLOCK_SUFFIX
  143. bool SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock)
  144. {
  145. return SDL_TryLockRWLockForWriting_generic(rwlock);
  146. }
  147. #endif
  148. void SDL_UnlockRWLock_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  149. {
  150. #ifndef SDL_THREADS_DISABLED
  151. if (rwlock) {
  152. SDL_LockMutex(rwlock->lock); // recursive lock for writers, readers grab lock to make sure things are sane.
  153. if (SDL_GetAtomicInt(&rwlock->reader_count) > 0) { // we're a reader
  154. SDL_AddAtomicInt(&rwlock->reader_count, -1);
  155. SDL_BroadcastCondition(rwlock->condition); // alert any pending writers to attempt to try to grab the lock again.
  156. } else if (SDL_GetAtomicInt(&rwlock->writer_count) > 0) { // we're a writer
  157. SDL_AddAtomicInt(&rwlock->writer_count, -1);
  158. SDL_UnlockMutex(rwlock->lock); // recursive unlock.
  159. }
  160. SDL_UnlockMutex(rwlock->lock);
  161. }
  162. #endif
  163. }