SDL_syssem.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include <errno.h>
  20. #include <pthread.h>
  21. #include <semaphore.h>
  22. #include <sys/time.h>
  23. #include "SDL_thread.h"
  24. #include "SDL_timer.h"
  25. /* Wrapper around POSIX 1003.1b semaphores */
  26. #if defined(__MACOSX__) || defined(__IPHONEOS__)
  27. /* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
  28. #include "../generic/SDL_syssem.c"
  29. #else
  30. struct SDL_semaphore
  31. {
  32. sem_t sem;
  33. };
  34. /* Create a semaphore, initialized with value */
  35. SDL_sem *
  36. 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
  51. SDL_DestroySemaphore(SDL_sem * sem)
  52. {
  53. if (sem) {
  54. sem_destroy(&sem->sem);
  55. SDL_free(sem);
  56. }
  57. }
  58. int
  59. SDL_SemTryWait(SDL_sem * sem)
  60. {
  61. int retval;
  62. if (!sem) {
  63. return SDL_SetError("Passed a NULL semaphore");
  64. }
  65. retval = SDL_MUTEX_TIMEDOUT;
  66. if (sem_trywait(&sem->sem) == 0) {
  67. retval = 0;
  68. }
  69. return retval;
  70. }
  71. int
  72. SDL_SemWait(SDL_sem * sem)
  73. {
  74. int retval;
  75. if (!sem) {
  76. return SDL_SetError("Passed a NULL semaphore");
  77. }
  78. retval = sem_wait(&sem->sem);
  79. if (retval < 0) {
  80. retval = SDL_SetError("sem_wait() failed");
  81. }
  82. return retval;
  83. }
  84. int
  85. SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
  86. {
  87. int retval;
  88. #ifdef HAVE_SEM_TIMEDWAIT
  89. struct timeval now;
  90. struct timespec ts_timeout;
  91. #else
  92. Uint32 end;
  93. #endif
  94. if (!sem) {
  95. return SDL_SetError("Passed a NULL semaphore");
  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. gettimeofday(&now, NULL);
  110. /* Add our timeout to current time */
  111. now.tv_usec += (timeout % 1000) * 1000;
  112. now.tv_sec += timeout / 1000;
  113. /* Wrap the second if needed */
  114. if ( now.tv_usec >= 1000000 ) {
  115. now.tv_usec -= 1000000;
  116. now.tv_sec ++;
  117. }
  118. /* Convert to timespec */
  119. ts_timeout.tv_sec = now.tv_sec;
  120. ts_timeout.tv_nsec = now.tv_usec * 1000;
  121. /* Wait. */
  122. do {
  123. retval = sem_timedwait(&sem->sem, &ts_timeout);
  124. } while (retval < 0 && errno == EINTR);
  125. if (retval < 0) {
  126. if (errno == ETIMEDOUT) {
  127. retval = SDL_MUTEX_TIMEDOUT;
  128. } else {
  129. SDL_SetError(strerror(errno));
  130. }
  131. }
  132. #else
  133. end = SDL_GetTicks() + timeout;
  134. while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
  135. if (SDL_TICKS_PASSED(SDL_GetTicks(), end)) {
  136. break;
  137. }
  138. SDL_Delay(1);
  139. }
  140. #endif /* HAVE_SEM_TIMEDWAIT */
  141. return retval;
  142. }
  143. Uint32
  144. SDL_SemValue(SDL_sem * sem)
  145. {
  146. int ret = 0;
  147. if (sem) {
  148. sem_getvalue(&sem->sem, &ret);
  149. if (ret < 0) {
  150. ret = 0;
  151. }
  152. }
  153. return (Uint32) ret;
  154. }
  155. int
  156. SDL_SemPost(SDL_sem * sem)
  157. {
  158. int retval;
  159. if (!sem) {
  160. return SDL_SetError("Passed a NULL semaphore");
  161. }
  162. retval = sem_post(&sem->sem);
  163. if (retval < 0) {
  164. SDL_SetError("sem_post() failed");
  165. }
  166. return retval;
  167. }
  168. #endif /* __MACOSX__ */
  169. /* vi: set ts=4 sw=4 expandtab: */