SDL_syssem.c 4.7 KB

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