1
0

SDL_syscond.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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 condition variables using semaphores and mutexes */
  20. /*
  21. This implementation borrows heavily from the BeOS condition variable
  22. implementation, written by Christopher Tate and Owen Smith. Thanks!
  23. */
  24. #include "../generic/SDL_syscond_c.h"
  25. /* If two implementations are to be compiled into SDL (the active one
  26. * will be chosen at runtime), the function names need to be
  27. * suffixed
  28. */
  29. #if !SDL_THREAD_GENERIC_COND_SUFFIX
  30. #define SDL_CreateCond_generic SDL_CreateCond
  31. #define SDL_DestroyCond_generic SDL_DestroyCond
  32. #define SDL_CondSignal_generic SDL_CondSignal
  33. #define SDL_CondBroadcast_generic SDL_CondBroadcast
  34. #define SDL_CondWaitTimeoutNS_generic SDL_CondWaitTimeoutNS
  35. #endif
  36. typedef struct SDL_cond_generic
  37. {
  38. SDL_mutex *lock;
  39. int waiting;
  40. int signals;
  41. SDL_sem *wait_sem;
  42. SDL_sem *wait_done;
  43. } SDL_cond_generic;
  44. /* Create a condition variable */
  45. SDL_cond *
  46. SDL_CreateCond_generic(void)
  47. {
  48. SDL_cond_generic *cond;
  49. cond = (SDL_cond_generic *)SDL_malloc(sizeof(SDL_cond_generic));
  50. if (cond) {
  51. cond->lock = SDL_CreateMutex();
  52. cond->wait_sem = SDL_CreateSemaphore(0);
  53. cond->wait_done = SDL_CreateSemaphore(0);
  54. cond->waiting = cond->signals = 0;
  55. if (!cond->lock || !cond->wait_sem || !cond->wait_done) {
  56. SDL_DestroyCond_generic((SDL_cond *)cond);
  57. cond = NULL;
  58. }
  59. } else {
  60. SDL_OutOfMemory();
  61. }
  62. return (SDL_cond *)cond;
  63. }
  64. /* Destroy a condition variable */
  65. void SDL_DestroyCond_generic(SDL_cond *_cond)
  66. {
  67. SDL_cond_generic *cond = (SDL_cond_generic *)_cond;
  68. if (cond) {
  69. if (cond->wait_sem) {
  70. SDL_DestroySemaphore(cond->wait_sem);
  71. }
  72. if (cond->wait_done) {
  73. SDL_DestroySemaphore(cond->wait_done);
  74. }
  75. if (cond->lock) {
  76. SDL_DestroyMutex(cond->lock);
  77. }
  78. SDL_free(cond);
  79. }
  80. }
  81. /* Restart one of the threads that are waiting on the condition variable */
  82. int SDL_CondSignal_generic(SDL_cond *_cond)
  83. {
  84. SDL_cond_generic *cond = (SDL_cond_generic *)_cond;
  85. if (cond == NULL) {
  86. return SDL_InvalidParamError("cond");
  87. }
  88. /* If there are waiting threads not already signalled, then
  89. signal the condition and wait for the thread to respond.
  90. */
  91. SDL_LockMutex(cond->lock);
  92. if (cond->waiting > cond->signals) {
  93. ++cond->signals;
  94. SDL_SemPost(cond->wait_sem);
  95. SDL_UnlockMutex(cond->lock);
  96. SDL_SemWait(cond->wait_done);
  97. } else {
  98. SDL_UnlockMutex(cond->lock);
  99. }
  100. return 0;
  101. }
  102. /* Restart all threads that are waiting on the condition variable */
  103. int SDL_CondBroadcast_generic(SDL_cond *_cond)
  104. {
  105. SDL_cond_generic *cond = (SDL_cond_generic *)_cond;
  106. if (cond == NULL) {
  107. return SDL_InvalidParamError("cond");
  108. }
  109. /* If there are waiting threads not already signalled, then
  110. signal the condition and wait for the thread to respond.
  111. */
  112. SDL_LockMutex(cond->lock);
  113. if (cond->waiting > cond->signals) {
  114. int i, num_waiting;
  115. num_waiting = (cond->waiting - cond->signals);
  116. cond->signals = cond->waiting;
  117. for (i = 0; i < num_waiting; ++i) {
  118. SDL_SemPost(cond->wait_sem);
  119. }
  120. /* Now all released threads are blocked here, waiting for us.
  121. Collect them all (and win fabulous prizes!) :-)
  122. */
  123. SDL_UnlockMutex(cond->lock);
  124. for (i = 0; i < num_waiting; ++i) {
  125. SDL_SemWait(cond->wait_done);
  126. }
  127. } else {
  128. SDL_UnlockMutex(cond->lock);
  129. }
  130. return 0;
  131. }
  132. /* Wait on the condition variable for at most 'timeoutNS' nanoseconds.
  133. The mutex must be locked before entering this function!
  134. The mutex is unlocked during the wait, and locked again after the wait.
  135. Typical use:
  136. Thread A:
  137. SDL_LockMutex(lock);
  138. while ( ! condition ) {
  139. SDL_CondWait(cond, lock);
  140. }
  141. SDL_UnlockMutex(lock);
  142. Thread B:
  143. SDL_LockMutex(lock);
  144. ...
  145. condition = true;
  146. ...
  147. SDL_CondSignal(cond);
  148. SDL_UnlockMutex(lock);
  149. */
  150. int SDL_CondWaitTimeoutNS_generic(SDL_cond *_cond, SDL_mutex *mutex, Sint64 timeoutNS)
  151. {
  152. SDL_cond_generic *cond = (SDL_cond_generic *)_cond;
  153. int retval;
  154. if (cond == NULL) {
  155. return SDL_InvalidParamError("cond");
  156. }
  157. /* Obtain the protection mutex, and increment the number of waiters.
  158. This allows the signal mechanism to only perform a signal if there
  159. are waiting threads.
  160. */
  161. SDL_LockMutex(cond->lock);
  162. ++cond->waiting;
  163. SDL_UnlockMutex(cond->lock);
  164. /* Unlock the mutex, as is required by condition variable semantics */
  165. SDL_UnlockMutex(mutex);
  166. /* Wait for a signal */
  167. retval = SDL_SemWaitTimeoutNS(cond->wait_sem, timeoutNS);
  168. /* Let the signaler know we have completed the wait, otherwise
  169. the signaler can race ahead and get the condition semaphore
  170. if we are stopped between the mutex unlock and semaphore wait,
  171. giving a deadlock. See the following URL for details:
  172. http://web.archive.org/web/20010914175514/http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html#Workshop
  173. */
  174. SDL_LockMutex(cond->lock);
  175. if (cond->signals > 0) {
  176. /* If we timed out, we need to eat a condition signal */
  177. if (retval > 0) {
  178. SDL_SemWait(cond->wait_sem);
  179. }
  180. /* We always notify the signal thread that we are done */
  181. SDL_SemPost(cond->wait_done);
  182. /* Signal handshake complete */
  183. --cond->signals;
  184. }
  185. --cond->waiting;
  186. SDL_UnlockMutex(cond->lock);
  187. /* Lock the mutex, as is required by condition variable semantics */
  188. SDL_LockMutex(mutex);
  189. return retval;
  190. }
  191. /* vi: set ts=4 sw=4 expandtab: */