SDL_syssem.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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_SetError("Passed a NULL semaphore");
  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_SetError("Passed a NULL semaphore");
  78. }
  79. retval = sem_wait(&sem->sem);
  80. if (retval < 0) {
  81. retval = SDL_SetError("sem_wait() failed");
  82. }
  83. return retval;
  84. }
  85. int
  86. SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
  87. {
  88. int retval;
  89. #ifdef HAVE_SEM_TIMEDWAIT
  90. #ifndef HAVE_CLOCK_GETTIME
  91. struct timeval now;
  92. #endif
  93. struct timespec ts_timeout;
  94. #else
  95. Uint32 end;
  96. #endif
  97. if (!sem) {
  98. return SDL_SetError("Passed a NULL semaphore");
  99. }
  100. /* Try the easy cases first */
  101. if (timeout == 0) {
  102. return SDL_SemTryWait(sem);
  103. }
  104. if (timeout == SDL_MUTEX_MAXWAIT) {
  105. return SDL_SemWait(sem);
  106. }
  107. #ifdef HAVE_SEM_TIMEDWAIT
  108. /* Setup the timeout. sem_timedwait doesn't wait for
  109. * a lapse of time, but until we reach a certain time.
  110. * This time is now plus the timeout.
  111. */
  112. #ifdef HAVE_CLOCK_GETTIME
  113. clock_gettime(CLOCK_REALTIME, &ts_timeout);
  114. /* Add our timeout to current time */
  115. ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
  116. ts_timeout.tv_sec += timeout / 1000;
  117. #else
  118. gettimeofday(&now, NULL);
  119. /* Add our timeout to current time */
  120. ts_timeout.tv_sec = now.tv_sec + (timeout / 1000);
  121. ts_timeout.tv_nsec = (now.tv_usec + (timeout % 1000) * 1000) * 1000;
  122. #endif
  123. /* Wrap the second if needed */
  124. if (ts_timeout.tv_nsec > 1000000000) {
  125. ts_timeout.tv_sec += 1;
  126. ts_timeout.tv_nsec -= 1000000000;
  127. }
  128. /* Wait. */
  129. do {
  130. retval = sem_timedwait(&sem->sem, &ts_timeout);
  131. } while (retval < 0 && errno == EINTR);
  132. if (retval < 0) {
  133. if (errno == ETIMEDOUT) {
  134. retval = SDL_MUTEX_TIMEDOUT;
  135. } else {
  136. SDL_SetError("sem_timedwait returned an error: %s", strerror(errno));
  137. }
  138. }
  139. #else
  140. end = SDL_GetTicks() + timeout;
  141. while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
  142. if (SDL_TICKS_PASSED(SDL_GetTicks(), end)) {
  143. break;
  144. }
  145. SDL_Delay(1);
  146. }
  147. #endif /* HAVE_SEM_TIMEDWAIT */
  148. return retval;
  149. }
  150. Uint32
  151. SDL_SemValue(SDL_sem * sem)
  152. {
  153. int ret = 0;
  154. if (sem) {
  155. sem_getvalue(&sem->sem, &ret);
  156. if (ret < 0) {
  157. ret = 0;
  158. }
  159. }
  160. return (Uint32) ret;
  161. }
  162. int
  163. SDL_SemPost(SDL_sem * sem)
  164. {
  165. int retval;
  166. if (!sem) {
  167. return SDL_SetError("Passed a NULL semaphore");
  168. }
  169. retval = sem_post(&sem->sem);
  170. if (retval < 0) {
  171. SDL_SetError("sem_post() failed");
  172. }
  173. return retval;
  174. }
  175. #endif /* __MACOSX__ */
  176. /* vi: set ts=4 sw=4 expandtab: */