SDL_syscond.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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 <sys/time.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <errno.h>
  23. #include <pthread.h>
  24. #include "SDL_thread.h"
  25. #include "SDL_sysmutex_c.h"
  26. struct SDL_cond
  27. {
  28. pthread_cond_t cond;
  29. };
  30. /* Create a condition variable */
  31. SDL_cond *SDL_CreateCond(void)
  32. {
  33. SDL_cond *cond;
  34. cond = (SDL_cond *)SDL_malloc(sizeof(SDL_cond));
  35. if (cond) {
  36. if (pthread_cond_init(&cond->cond, NULL) != 0) {
  37. SDL_SetError("pthread_cond_init() failed");
  38. SDL_free(cond);
  39. cond = NULL;
  40. }
  41. }
  42. return cond;
  43. }
  44. /* Destroy a condition variable */
  45. void SDL_DestroyCond(SDL_cond *cond)
  46. {
  47. if (cond) {
  48. pthread_cond_destroy(&cond->cond);
  49. SDL_free(cond);
  50. }
  51. }
  52. /* Restart one of the threads that are waiting on the condition variable */
  53. int SDL_CondSignal(SDL_cond *cond)
  54. {
  55. int retval;
  56. if (!cond) {
  57. return SDL_InvalidParamError("cond");
  58. }
  59. retval = 0;
  60. if (pthread_cond_signal(&cond->cond) != 0) {
  61. return SDL_SetError("pthread_cond_signal() failed");
  62. }
  63. return retval;
  64. }
  65. /* Restart all threads that are waiting on the condition variable */
  66. int SDL_CondBroadcast(SDL_cond *cond)
  67. {
  68. int retval;
  69. if (!cond) {
  70. return SDL_InvalidParamError("cond");
  71. }
  72. retval = 0;
  73. if (pthread_cond_broadcast(&cond->cond) != 0) {
  74. return SDL_SetError("pthread_cond_broadcast() failed");
  75. }
  76. return retval;
  77. }
  78. int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
  79. {
  80. int retval;
  81. #ifndef HAVE_CLOCK_GETTIME
  82. struct timeval delta;
  83. #endif
  84. struct timespec abstime;
  85. if (!cond) {
  86. return SDL_InvalidParamError("cond");
  87. }
  88. #ifdef HAVE_CLOCK_GETTIME
  89. clock_gettime(CLOCK_REALTIME, &abstime);
  90. abstime.tv_nsec += (ms % 1000) * 1000000;
  91. abstime.tv_sec += ms / 1000;
  92. #else
  93. gettimeofday(&delta, NULL);
  94. abstime.tv_sec = delta.tv_sec + (ms / 1000);
  95. abstime.tv_nsec = (long)(delta.tv_usec + (ms % 1000) * 1000) * 1000;
  96. #endif
  97. if (abstime.tv_nsec >= 1000000000) {
  98. abstime.tv_sec += 1;
  99. abstime.tv_nsec -= 1000000000;
  100. }
  101. tryagain:
  102. retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
  103. switch (retval) {
  104. case EINTR:
  105. goto tryagain;
  106. /* break; -Wunreachable-code-break */
  107. case ETIMEDOUT:
  108. retval = SDL_MUTEX_TIMEDOUT;
  109. break;
  110. case 0:
  111. break;
  112. default:
  113. retval = SDL_SetError("pthread_cond_timedwait() failed");
  114. }
  115. return retval;
  116. }
  117. /* Wait on the condition variable, unlocking the provided mutex.
  118. The mutex must be locked before entering this function!
  119. */
  120. int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
  121. {
  122. if (!cond) {
  123. return SDL_InvalidParamError("cond");
  124. } else if (pthread_cond_wait(&cond->cond, &mutex->id) != 0) {
  125. return SDL_SetError("pthread_cond_wait() failed");
  126. }
  127. return 0;
  128. }
  129. /* vi: set ts=4 sw=4 expandtab: */