SDL_sysrwlock.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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_TryLockRWLockForReading_generic SDL_TryLockRWLockForReading
  33. #define SDL_TryLockRWLockForWriting_generic SDL_TryLockRWLockForWriting
  34. #define SDL_UnlockRWLock_generic SDL_UnlockRWLock
  35. #endif
  36. struct SDL_RWLock
  37. {
  38. #ifdef SDL_THREADS_DISABLED
  39. int unused;
  40. #else
  41. SDL_Mutex *lock;
  42. SDL_Condition *condition;
  43. SDL_threadID writer_thread;
  44. SDL_AtomicInt reader_count;
  45. SDL_AtomicInt writer_count;
  46. #endif
  47. };
  48. SDL_RWLock *SDL_CreateRWLock_generic(void)
  49. {
  50. SDL_RWLock *rwlock = (SDL_RWLock *) SDL_calloc(1, sizeof (*rwlock));
  51. if (!rwlock) {
  52. SDL_OutOfMemory();
  53. return NULL;
  54. }
  55. #ifndef SDL_THREADS_DISABLED
  56. rwlock->lock = SDL_CreateMutex();
  57. if (!rwlock->lock) {
  58. SDL_free(rwlock);
  59. return NULL;
  60. }
  61. rwlock->condition = SDL_CreateCondition();
  62. if (!rwlock->condition) {
  63. SDL_DestroyMutex(rwlock->lock);
  64. SDL_free(rwlock);
  65. return NULL;
  66. }
  67. SDL_AtomicSet(&rwlock->reader_count, 0);
  68. SDL_AtomicSet(&rwlock->writer_count, 0);
  69. #endif
  70. return rwlock;
  71. }
  72. void SDL_DestroyRWLock_generic(SDL_RWLock *rwlock)
  73. {
  74. if (rwlock) {
  75. #ifndef SDL_THREADS_DISABLED
  76. SDL_DestroyMutex(rwlock->lock);
  77. SDL_DestroyCondition(rwlock->condition);
  78. #endif
  79. SDL_free(rwlock);
  80. }
  81. }
  82. int SDL_LockRWLockForReading_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  83. {
  84. #ifndef SDL_THREADS_DISABLED
  85. if (!rwlock) {
  86. return SDL_InvalidParamError("rwlock");
  87. } else if (SDL_LockMutex(rwlock->lock) == -1) {
  88. return -1;
  89. }
  90. SDL_assert(SDL_AtomicGet(&rwlock->writer_count) == 0); /* shouldn't be able to grab lock if there's a writer! */
  91. SDL_AtomicAdd(&rwlock->reader_count, 1);
  92. SDL_UnlockMutex(rwlock->lock); /* other readers can attempt to share the lock. */
  93. #endif
  94. return 0;
  95. }
  96. int SDL_LockRWLockForWriting_generic(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  97. {
  98. #ifndef SDL_THREADS_DISABLED
  99. if (!rwlock) {
  100. return SDL_InvalidParamError("rwlock");
  101. } else if (SDL_LockMutex(rwlock->lock) == -1) {
  102. return -1;
  103. }
  104. while (SDL_AtomicGet(&rwlock->reader_count) > 0) { /* while something is holding the shared lock, keep waiting. */
  105. SDL_WaitCondition(rwlock->condition, rwlock->lock); /* release the lock and wait for readers holding the shared lock to release it, regrab the lock. */
  106. }
  107. /* we hold the lock! */
  108. SDL_AtomicAdd(&rwlock->writer_count, 1); /* we let these be recursive, but the API doesn't require this. It _does_ trust you unlock correctly! */
  109. #endif
  110. return 0;
  111. }
  112. int SDL_TryLockRWLockForReading_generic(SDL_RWLock *rwlock)
  113. {
  114. #ifndef SDL_THREADS_DISABLED
  115. int rc;
  116. if (!rwlock) {
  117. return SDL_InvalidParamError("rwlock");
  118. }
  119. rc = SDL_TryLockMutex(rwlock->lock);
  120. if (rc != 0) {
  121. /* !!! 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. */
  122. return rc;
  123. }
  124. SDL_assert(SDL_AtomicGet(&rwlock->writer_count) == 0); /* shouldn't be able to grab lock if there's a writer! */
  125. SDL_AtomicAdd(&rwlock->reader_count, 1);
  126. SDL_UnlockMutex(rwlock->lock); /* other readers can attempt to share the lock. */
  127. #endif
  128. return 0;
  129. }
  130. int SDL_TryLockRWLockForWriting_generic(SDL_RWLock *rwlock)
  131. {
  132. #ifndef SDL_THREADS_DISABLED
  133. int rc;
  134. if (!rwlock) {
  135. return SDL_InvalidParamError("rwlock");
  136. } else if ((rc = SDL_TryLockMutex(rwlock->lock)) != 0) {
  137. return rc;
  138. }
  139. if (SDL_AtomicGet(&rwlock->reader_count) > 0) { /* a reader is using the shared lock, treat it as unavailable. */
  140. SDL_UnlockMutex(rwlock->lock);
  141. return SDL_RWLOCK_TIMEDOUT;
  142. }
  143. /* we hold the lock! */
  144. SDL_AtomicAdd(&rwlock->writer_count, 1); /* we let these be recursive, but the API doesn't require this. It _does_ trust you unlock correctly! */
  145. #endif
  146. return 0;
  147. }
  148. int 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. return SDL_InvalidParamError("rwlock");
  153. }
  154. SDL_LockMutex(rwlock->lock); /* recursive lock for writers, readers grab lock to make sure things are sane. */
  155. if (SDL_AtomicGet(&rwlock->reader_count) > 0) { /* we're a reader */
  156. SDL_AtomicAdd(&rwlock->reader_count, -1);
  157. SDL_BroadcastCondition(rwlock->condition); /* alert any pending writers to attempt to try to grab the lock again. */
  158. } else if (SDL_AtomicGet(&rwlock->writer_count) > 0) { /* we're a writer */
  159. SDL_AtomicAdd(&rwlock->writer_count, -1);
  160. SDL_UnlockMutex(rwlock->lock); /* recursive unlock. */
  161. }
  162. SDL_UnlockMutex(rwlock->lock);
  163. #endif
  164. return 0;
  165. }