SDL_syssem.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /* Wrapper around POSIX 1003.1b semaphores */
  25. #if defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS)
  26. /* macOS doesn't support sem_getvalue() as of version 10.4 */
  27. #include "../generic/SDL_syssem.c"
  28. #else
  29. struct SDL_Semaphore
  30. {
  31. sem_t sem;
  32. };
  33. /* Create a semaphore, initialized with value */
  34. SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
  35. {
  36. SDL_Semaphore *sem = (SDL_Semaphore *)SDL_malloc(sizeof(SDL_Semaphore));
  37. if (sem) {
  38. if (sem_init(&sem->sem, 0, initial_value) < 0) {
  39. SDL_SetError("sem_init() failed");
  40. SDL_free(sem);
  41. sem = NULL;
  42. }
  43. }
  44. return sem;
  45. }
  46. void SDL_DestroySemaphore(SDL_Semaphore *sem)
  47. {
  48. if (sem) {
  49. sem_destroy(&sem->sem);
  50. SDL_free(sem);
  51. }
  52. }
  53. int SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
  54. {
  55. int retval = 0;
  56. #ifdef HAVE_SEM_TIMEDWAIT
  57. #ifndef HAVE_CLOCK_GETTIME
  58. struct timeval now;
  59. #endif
  60. struct timespec ts_timeout;
  61. #else
  62. Uint64 end;
  63. #endif
  64. if (!sem) {
  65. return SDL_InvalidParamError("sem");
  66. }
  67. /* Try the easy cases first */
  68. if (timeoutNS == 0) {
  69. retval = SDL_MUTEX_TIMEDOUT;
  70. if (sem_trywait(&sem->sem) == 0) {
  71. retval = 0;
  72. }
  73. return retval;
  74. }
  75. if (timeoutNS < 0) {
  76. do {
  77. retval = sem_wait(&sem->sem);
  78. } while (retval < 0 && errno == EINTR);
  79. if (retval < 0) {
  80. retval = SDL_SetError("sem_wait() failed");
  81. }
  82. return retval;
  83. }
  84. #ifdef HAVE_SEM_TIMEDWAIT
  85. /* Setup the timeout. sem_timedwait doesn't wait for
  86. * a lapse of time, but until we reach a certain time.
  87. * This time is now plus the timeout.
  88. */
  89. #ifdef HAVE_CLOCK_GETTIME
  90. clock_gettime(CLOCK_REALTIME, &ts_timeout);
  91. /* Add our timeout to current time */
  92. ts_timeout.tv_sec += (timeoutNS / SDL_NS_PER_SECOND);
  93. ts_timeout.tv_nsec += (timeoutNS % SDL_NS_PER_SECOND);
  94. #else
  95. gettimeofday(&now, NULL);
  96. /* Add our timeout to current time */
  97. ts_timeout.tv_sec = now.tv_sec + (timeoutNS / SDL_NS_PER_SECOND);
  98. ts_timeout.tv_nsec = SDL_US_TO_NS(now.tv_usec) + (timeoutNS % SDL_NS_PER_SECOND);
  99. #endif
  100. /* Wrap the second if needed */
  101. while (ts_timeout.tv_nsec >= 1000000000) {
  102. ts_timeout.tv_sec += 1;
  103. ts_timeout.tv_nsec -= 1000000000;
  104. }
  105. /* Wait. */
  106. do {
  107. retval = sem_timedwait(&sem->sem, &ts_timeout);
  108. } while (retval < 0 && errno == EINTR);
  109. if (retval < 0) {
  110. if (errno == ETIMEDOUT) {
  111. retval = SDL_MUTEX_TIMEDOUT;
  112. } else {
  113. SDL_SetError("sem_timedwait returned an error: %s", strerror(errno));
  114. }
  115. }
  116. #else
  117. end = SDL_GetTicksNS() + timeoutNS;
  118. while (sem_trywait(&sem->sem) != 0) {
  119. if (SDL_GetTicksNS() >= end) {
  120. retval = SDL_MUTEX_TIMEDOUT;
  121. break;
  122. }
  123. SDL_DelayNS(100);
  124. }
  125. #endif /* HAVE_SEM_TIMEDWAIT */
  126. return retval;
  127. }
  128. Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)
  129. {
  130. int ret = 0;
  131. if (!sem) {
  132. SDL_InvalidParamError("sem");
  133. return 0;
  134. }
  135. sem_getvalue(&sem->sem, &ret);
  136. if (ret < 0) {
  137. ret = 0;
  138. }
  139. return (Uint32)ret;
  140. }
  141. int SDL_PostSemaphore(SDL_Semaphore *sem)
  142. {
  143. int retval;
  144. if (!sem) {
  145. return SDL_InvalidParamError("sem");
  146. }
  147. retval = sem_post(&sem->sem);
  148. if (retval < 0) {
  149. SDL_SetError("sem_post() failed");
  150. }
  151. return retval;
  152. }
  153. #endif /* SDL_PLATFORM_MACOS */