SDL_syscond.c 6.1 KB

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