SDL_syscond_cv.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "../generic/SDL_syscond_c.h"
  20. #include "SDL_sysmutex_c.h"
  21. typedef SDL_Condition *(*pfnSDL_CreateCondition)(void);
  22. typedef void (*pfnSDL_DestroyCondition)(SDL_Condition *);
  23. typedef void (*pfnSDL_SignalCondition)(SDL_Condition *);
  24. typedef void (*pfnSDL_BroadcastCondition)(SDL_Condition *);
  25. typedef bool (*pfnSDL_WaitConditionTimeoutNS)(SDL_Condition *, SDL_Mutex *, Sint64);
  26. typedef struct SDL_cond_impl_t
  27. {
  28. pfnSDL_CreateCondition Create;
  29. pfnSDL_DestroyCondition Destroy;
  30. pfnSDL_SignalCondition Signal;
  31. pfnSDL_BroadcastCondition Broadcast;
  32. pfnSDL_WaitConditionTimeoutNS WaitTimeoutNS;
  33. } SDL_cond_impl_t;
  34. // Implementation will be chosen at runtime based on available Kernel features
  35. static SDL_cond_impl_t SDL_cond_impl_active = { 0 };
  36. /**
  37. * Native Windows Condition Variable (SRW Locks)
  38. */
  39. #ifndef CONDITION_VARIABLE_INIT
  40. #define CONDITION_VARIABLE_INIT \
  41. { \
  42. 0 \
  43. }
  44. typedef struct CONDITION_VARIABLE
  45. {
  46. PVOID Ptr;
  47. } CONDITION_VARIABLE, *PCONDITION_VARIABLE;
  48. #endif
  49. typedef VOID(WINAPI *pfnWakeConditionVariable)(PCONDITION_VARIABLE);
  50. typedef VOID(WINAPI *pfnWakeAllConditionVariable)(PCONDITION_VARIABLE);
  51. typedef BOOL(WINAPI *pfnSleepConditionVariableSRW)(PCONDITION_VARIABLE, PSRWLOCK, DWORD, ULONG);
  52. typedef BOOL(WINAPI *pfnSleepConditionVariableCS)(PCONDITION_VARIABLE, PCRITICAL_SECTION, DWORD);
  53. static pfnWakeConditionVariable pWakeConditionVariable = NULL;
  54. static pfnWakeAllConditionVariable pWakeAllConditionVariable = NULL;
  55. static pfnSleepConditionVariableSRW pSleepConditionVariableSRW = NULL;
  56. static pfnSleepConditionVariableCS pSleepConditionVariableCS = NULL;
  57. typedef struct SDL_cond_cv
  58. {
  59. CONDITION_VARIABLE cond;
  60. } SDL_cond_cv;
  61. static SDL_Condition *SDL_CreateCondition_cv(void)
  62. {
  63. // Relies on CONDITION_VARIABLE_INIT == 0.
  64. return (SDL_Condition *)SDL_calloc(1, sizeof(SDL_cond_cv));
  65. }
  66. static void SDL_DestroyCondition_cv(SDL_Condition *cond)
  67. {
  68. // There are no kernel allocated resources
  69. SDL_free(cond);
  70. }
  71. static void SDL_SignalCondition_cv(SDL_Condition *_cond)
  72. {
  73. SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
  74. pWakeConditionVariable(&cond->cond);
  75. }
  76. static void SDL_BroadcastCondition_cv(SDL_Condition *_cond)
  77. {
  78. SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
  79. pWakeAllConditionVariable(&cond->cond);
  80. }
  81. static bool SDL_WaitConditionTimeoutNS_cv(SDL_Condition *_cond, SDL_Mutex *_mutex, Sint64 timeoutNS)
  82. {
  83. SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
  84. DWORD timeout;
  85. bool result;
  86. if (timeoutNS < 0) {
  87. timeout = INFINITE;
  88. } else {
  89. timeout = (DWORD)SDL_NS_TO_MS(timeoutNS);
  90. }
  91. if (SDL_mutex_impl_active.Type == SDL_MUTEX_SRW) {
  92. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  93. if (mutex->count != 1 || mutex->owner != GetCurrentThreadId()) {
  94. // Passed mutex is not locked or locked recursively"
  95. return false;
  96. }
  97. // The mutex must be updated to the released state
  98. mutex->count = 0;
  99. mutex->owner = 0;
  100. result = (pSleepConditionVariableSRW(&cond->cond, &mutex->srw, timeout, 0) == TRUE);
  101. // The mutex is owned by us again, regardless of status of the wait
  102. SDL_assert(mutex->count == 0 && mutex->owner == 0);
  103. mutex->count = 1;
  104. mutex->owner = GetCurrentThreadId();
  105. } else {
  106. SDL_mutex_cs *mutex = (SDL_mutex_cs *)_mutex;
  107. SDL_assert(SDL_mutex_impl_active.Type == SDL_MUTEX_CS);
  108. result = (pSleepConditionVariableCS(&cond->cond, &mutex->cs, timeout) == TRUE);
  109. }
  110. return result;
  111. }
  112. static const SDL_cond_impl_t SDL_cond_impl_cv = {
  113. &SDL_CreateCondition_cv,
  114. &SDL_DestroyCondition_cv,
  115. &SDL_SignalCondition_cv,
  116. &SDL_BroadcastCondition_cv,
  117. &SDL_WaitConditionTimeoutNS_cv,
  118. };
  119. // Generic Condition Variable implementation using SDL_Mutex and SDL_Semaphore
  120. static const SDL_cond_impl_t SDL_cond_impl_generic = {
  121. &SDL_CreateCondition_generic,
  122. &SDL_DestroyCondition_generic,
  123. &SDL_SignalCondition_generic,
  124. &SDL_BroadcastCondition_generic,
  125. &SDL_WaitConditionTimeoutNS_generic,
  126. };
  127. SDL_Condition *SDL_CreateCondition(void)
  128. {
  129. if (!SDL_cond_impl_active.Create) {
  130. const SDL_cond_impl_t *impl = NULL;
  131. if (SDL_mutex_impl_active.Type == SDL_MUTEX_INVALID) {
  132. // The mutex implementation isn't decided yet, trigger it
  133. SDL_Mutex *mutex = SDL_CreateMutex();
  134. if (!mutex) {
  135. return NULL;
  136. }
  137. SDL_DestroyMutex(mutex);
  138. SDL_assert(SDL_mutex_impl_active.Type != SDL_MUTEX_INVALID);
  139. }
  140. // Default to generic implementation, works with all mutex implementations
  141. impl = &SDL_cond_impl_generic;
  142. {
  143. HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  144. if (kernel32) {
  145. pWakeConditionVariable = (pfnWakeConditionVariable)GetProcAddress(kernel32, "WakeConditionVariable");
  146. pWakeAllConditionVariable = (pfnWakeAllConditionVariable)GetProcAddress(kernel32, "WakeAllConditionVariable");
  147. pSleepConditionVariableSRW = (pfnSleepConditionVariableSRW)GetProcAddress(kernel32, "SleepConditionVariableSRW");
  148. pSleepConditionVariableCS = (pfnSleepConditionVariableCS)GetProcAddress(kernel32, "SleepConditionVariableCS");
  149. if (pWakeConditionVariable && pWakeAllConditionVariable && pSleepConditionVariableSRW && pSleepConditionVariableCS) {
  150. // Use the Windows provided API
  151. impl = &SDL_cond_impl_cv;
  152. }
  153. }
  154. }
  155. SDL_copyp(&SDL_cond_impl_active, impl);
  156. }
  157. return SDL_cond_impl_active.Create();
  158. }
  159. void SDL_DestroyCondition(SDL_Condition *cond)
  160. {
  161. if (cond) {
  162. SDL_cond_impl_active.Destroy(cond);
  163. }
  164. }
  165. void SDL_SignalCondition(SDL_Condition *cond)
  166. {
  167. if (!cond) {
  168. return;
  169. }
  170. SDL_cond_impl_active.Signal(cond);
  171. }
  172. void SDL_BroadcastCondition(SDL_Condition *cond)
  173. {
  174. if (!cond) {
  175. return;
  176. }
  177. SDL_cond_impl_active.Broadcast(cond);
  178. }
  179. bool SDL_WaitConditionTimeoutNS(SDL_Condition *cond, SDL_Mutex *mutex, Sint64 timeoutNS)
  180. {
  181. if (!cond || !mutex) {
  182. return true;
  183. }
  184. return SDL_cond_impl_active.WaitTimeoutNS(cond, mutex, timeoutNS);
  185. }