SDL_syssem.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <[email protected]>
  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 semaphores using mutexes and condition variables */
  20. #include "SDL_timer.h"
  21. #include "SDL_thread.h"
  22. #include "SDL_systhread_c.h"
  23. #if SDL_THREADS_DISABLED
  24. SDL_sem *
  25. SDL_CreateSemaphore(Uint32 initial_value)
  26. {
  27. SDL_SetError("SDL not built with thread support");
  28. return (SDL_sem *) 0;
  29. }
  30. void
  31. SDL_DestroySemaphore(SDL_sem * sem)
  32. {
  33. }
  34. int
  35. SDL_SemTryWait(SDL_sem * sem)
  36. {
  37. return SDL_SetError("SDL not built with thread support");
  38. }
  39. int
  40. SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
  41. {
  42. return SDL_SetError("SDL not built with thread support");
  43. }
  44. int
  45. SDL_SemWait(SDL_sem * sem)
  46. {
  47. return SDL_SetError("SDL not built with thread support");
  48. }
  49. Uint32
  50. SDL_SemValue(SDL_sem * sem)
  51. {
  52. return 0;
  53. }
  54. int
  55. SDL_SemPost(SDL_sem * sem)
  56. {
  57. return SDL_SetError("SDL not built with thread support");
  58. }
  59. #else
  60. struct SDL_semaphore
  61. {
  62. Uint32 count;
  63. Uint32 waiters_count;
  64. SDL_mutex *count_lock;
  65. SDL_cond *count_nonzero;
  66. };
  67. SDL_sem *
  68. SDL_CreateSemaphore(Uint32 initial_value)
  69. {
  70. SDL_sem *sem;
  71. sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
  72. if (!sem) {
  73. SDL_OutOfMemory();
  74. return NULL;
  75. }
  76. sem->count = initial_value;
  77. sem->waiters_count = 0;
  78. sem->count_lock = SDL_CreateMutex();
  79. sem->count_nonzero = SDL_CreateCond();
  80. if (!sem->count_lock || !sem->count_nonzero) {
  81. SDL_DestroySemaphore(sem);
  82. return NULL;
  83. }
  84. return sem;
  85. }
  86. /* WARNING:
  87. You cannot call this function when another thread is using the semaphore.
  88. */
  89. void
  90. SDL_DestroySemaphore(SDL_sem * sem)
  91. {
  92. if (sem) {
  93. sem->count = 0xFFFFFFFF;
  94. while (sem->waiters_count > 0) {
  95. SDL_CondSignal(sem->count_nonzero);
  96. SDL_Delay(10);
  97. }
  98. SDL_DestroyCond(sem->count_nonzero);
  99. if (sem->count_lock) {
  100. SDL_LockMutex(sem->count_lock);
  101. SDL_UnlockMutex(sem->count_lock);
  102. SDL_DestroyMutex(sem->count_lock);
  103. }
  104. SDL_free(sem);
  105. }
  106. }
  107. int
  108. SDL_SemTryWait(SDL_sem * sem)
  109. {
  110. int retval;
  111. if (!sem) {
  112. return SDL_SetError("Passed a NULL semaphore");
  113. }
  114. retval = SDL_MUTEX_TIMEDOUT;
  115. SDL_LockMutex(sem->count_lock);
  116. if (sem->count > 0) {
  117. --sem->count;
  118. retval = 0;
  119. }
  120. SDL_UnlockMutex(sem->count_lock);
  121. return retval;
  122. }
  123. int
  124. SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
  125. {
  126. int retval;
  127. if (!sem) {
  128. return SDL_SetError("Passed a NULL semaphore");
  129. }
  130. /* A timeout of 0 is an easy case */
  131. if (timeout == 0) {
  132. return SDL_SemTryWait(sem);
  133. }
  134. SDL_LockMutex(sem->count_lock);
  135. ++sem->waiters_count;
  136. retval = 0;
  137. while ((sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT)) {
  138. retval = SDL_CondWaitTimeout(sem->count_nonzero,
  139. sem->count_lock, timeout);
  140. }
  141. --sem->waiters_count;
  142. if (retval == 0) {
  143. --sem->count;
  144. }
  145. SDL_UnlockMutex(sem->count_lock);
  146. return retval;
  147. }
  148. int
  149. SDL_SemWait(SDL_sem * sem)
  150. {
  151. return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
  152. }
  153. Uint32
  154. SDL_SemValue(SDL_sem * sem)
  155. {
  156. Uint32 value;
  157. value = 0;
  158. if (sem) {
  159. SDL_LockMutex(sem->count_lock);
  160. value = sem->count;
  161. SDL_UnlockMutex(sem->count_lock);
  162. }
  163. return value;
  164. }
  165. int
  166. SDL_SemPost(SDL_sem * sem)
  167. {
  168. if (!sem) {
  169. return SDL_SetError("Passed a NULL semaphore");
  170. }
  171. SDL_LockMutex(sem->count_lock);
  172. if (sem->waiters_count > 0) {
  173. SDL_CondSignal(sem->count_nonzero);
  174. }
  175. ++sem->count;
  176. SDL_UnlockMutex(sem->count_lock);
  177. return 0;
  178. }
  179. #endif /* SDL_THREADS_DISABLED */
  180. /* vi: set ts=4 sw=4 expandtab: */