SDL_syssem.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include <errno.h>
  20. #include <pthread.h>
  21. #include <semaphore.h>
  22. #include <sys/time.h>
  23. #include <time.h>
  24. #include "SDL_thread.h"
  25. #include "SDL_timer.h"
  26. /* Wrapper around POSIX 1003.1b semaphores */
  27. #if defined(__MACOSX__) || defined(__IPHONEOS__)
  28. /* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
  29. #include "../generic/SDL_syssem.c"
  30. #else
  31. struct SDL_semaphore
  32. {
  33. sem_t sem;
  34. };
  35. /* Create a semaphore, initialized with value */
  36. SDL_sem *
  37. SDL_CreateSemaphore(Uint32 initial_value)
  38. {
  39. SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
  40. if (sem) {
  41. if (sem_init(&sem->sem, 0, initial_value) < 0) {
  42. SDL_SetError("sem_init() failed");
  43. SDL_free(sem);
  44. sem = NULL;
  45. }
  46. } else {
  47. SDL_OutOfMemory();
  48. }
  49. return sem;
  50. }
  51. void
  52. SDL_DestroySemaphore(SDL_sem * sem)
  53. {
  54. if (sem) {
  55. sem_destroy(&sem->sem);
  56. SDL_free(sem);
  57. }
  58. }
  59. int
  60. SDL_SemTryWait(SDL_sem * sem)
  61. {
  62. int retval;
  63. if (!sem) {
  64. return SDL_InvalidParamError("sem");
  65. }
  66. retval = SDL_MUTEX_TIMEDOUT;
  67. if (sem_trywait(&sem->sem) == 0) {
  68. retval = 0;
  69. }
  70. return retval;
  71. }
  72. int
  73. SDL_SemWait(SDL_sem * sem)
  74. {
  75. int retval;
  76. if (!sem) {
  77. return SDL_InvalidParamError("sem");
  78. }
  79. do {
  80. retval = sem_wait(&sem->sem);
  81. } while (retval < 0 && errno == EINTR);
  82. if (retval < 0) {
  83. retval = SDL_SetError("sem_wait() failed");
  84. }
  85. return retval;
  86. }
  87. int
  88. SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
  89. {
  90. int retval;
  91. #ifdef HAVE_SEM_TIMEDWAIT
  92. #ifndef HAVE_CLOCK_GETTIME
  93. struct timeval now;
  94. #endif
  95. struct timespec ts_timeout;
  96. #else
  97. Uint32 end;
  98. #endif
  99. if (!sem) {
  100. return SDL_InvalidParamError("sem");
  101. }
  102. /* Try the easy cases first */
  103. if (timeout == 0) {
  104. return SDL_SemTryWait(sem);
  105. }
  106. if (timeout == SDL_MUTEX_MAXWAIT) {
  107. return SDL_SemWait(sem);
  108. }
  109. #ifdef HAVE_SEM_TIMEDWAIT
  110. /* Setup the timeout. sem_timedwait doesn't wait for
  111. * a lapse of time, but until we reach a certain time.
  112. * This time is now plus the timeout.
  113. */
  114. #ifdef HAVE_CLOCK_GETTIME
  115. clock_gettime(CLOCK_REALTIME, &ts_timeout);
  116. /* Add our timeout to current time */
  117. ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
  118. ts_timeout.tv_sec += timeout / 1000;
  119. #else
  120. gettimeofday(&now, NULL);
  121. /* Add our timeout to current time */
  122. ts_timeout.tv_sec = now.tv_sec + (timeout / 1000);
  123. ts_timeout.tv_nsec = (now.tv_usec + (timeout % 1000) * 1000) * 1000;
  124. #endif
  125. /* Wrap the second if needed */
  126. if (ts_timeout.tv_nsec > 1000000000) {
  127. ts_timeout.tv_sec += 1;
  128. ts_timeout.tv_nsec -= 1000000000;
  129. }
  130. /* Wait. */
  131. do {
  132. retval = sem_timedwait(&sem->sem, &ts_timeout);
  133. } while (retval < 0 && errno == EINTR);
  134. if (retval < 0) {
  135. if (errno == ETIMEDOUT) {
  136. retval = SDL_MUTEX_TIMEDOUT;
  137. } else {
  138. SDL_SetError("sem_timedwait returned an error: %s", strerror(errno));
  139. }
  140. }
  141. #else
  142. end = SDL_GetTicks() + timeout;
  143. while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
  144. if (SDL_TICKS_PASSED(SDL_GetTicks(), end)) {
  145. break;
  146. }
  147. SDL_Delay(1);
  148. }
  149. #endif /* HAVE_SEM_TIMEDWAIT */
  150. return retval;
  151. }
  152. Uint32
  153. SDL_SemValue(SDL_sem * sem)
  154. {
  155. int ret = 0;
  156. if (sem) {
  157. sem_getvalue(&sem->sem, &ret);
  158. if (ret < 0) {
  159. ret = 0;
  160. }
  161. }
  162. return (Uint32) ret;
  163. }
  164. int
  165. SDL_SemPost(SDL_sem * sem)
  166. {
  167. int retval;
  168. if (!sem) {
  169. return SDL_InvalidParamError("sem");
  170. }
  171. retval = sem_post(&sem->sem);
  172. if (retval < 0) {
  173. SDL_SetError("sem_post() failed");
  174. }
  175. return retval;
  176. }
  177. #endif /* __MACOSX__ */
  178. /* vi: set ts=4 sw=4 expandtab: */