1
0

SDL_syscond_cv.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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 "SDL_hints.h"
  20. #include "SDL_thread.h"
  21. #include "../generic/SDL_syscond_c.h"
  22. #include "SDL_sysmutex_c.h"
  23. typedef SDL_cond * (*pfnSDL_CreateCond)(void);
  24. typedef void (*pfnSDL_DestroyCond)(SDL_cond *);
  25. typedef int (*pfnSDL_CondSignal)(SDL_cond *);
  26. typedef int (*pfnSDL_CondBroadcast)(SDL_cond *);
  27. typedef int (*pfnSDL_CondWait)(SDL_cond *, SDL_mutex *);
  28. typedef int (*pfnSDL_CondWaitTimeout)(SDL_cond *, SDL_mutex *, Uint32);
  29. typedef struct SDL_cond_impl_t
  30. {
  31. pfnSDL_CreateCond Create;
  32. pfnSDL_DestroyCond Destroy;
  33. pfnSDL_CondSignal Signal;
  34. pfnSDL_CondBroadcast Broadcast;
  35. pfnSDL_CondWait Wait;
  36. pfnSDL_CondWaitTimeout WaitTimeout;
  37. } SDL_cond_impl_t;
  38. /* Implementation will be chosen at runtime based on available Kernel features */
  39. static SDL_cond_impl_t SDL_cond_impl_active = {0};
  40. /**
  41. * Native Windows Condition Variable (SRW Locks)
  42. */
  43. #ifndef CONDITION_VARIABLE_INIT
  44. #define CONDITION_VARIABLE_INIT {0}
  45. typedef struct CONDITION_VARIABLE {
  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_cond *
  62. SDL_CreateCond_cv(void)
  63. {
  64. SDL_cond_cv *cond;
  65. /* Relies on CONDITION_VARIABLE_INIT == 0. */
  66. cond = (SDL_cond_cv *) SDL_calloc(1, sizeof(*cond));
  67. if (!cond) {
  68. SDL_OutOfMemory();
  69. }
  70. return (SDL_cond *)cond;
  71. }
  72. static void
  73. SDL_DestroyCond_cv(SDL_cond * cond)
  74. {
  75. if (cond) {
  76. /* There are no kernel allocated resources */
  77. SDL_free(cond);
  78. }
  79. }
  80. static int
  81. SDL_CondSignal_cv(SDL_cond * _cond)
  82. {
  83. SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
  84. if (!cond) {
  85. return SDL_InvalidParamError("cond");
  86. }
  87. pWakeConditionVariable(&cond->cond);
  88. return 0;
  89. }
  90. static int
  91. SDL_CondBroadcast_cv(SDL_cond * _cond)
  92. {
  93. SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
  94. if (!cond) {
  95. return SDL_InvalidParamError("cond");
  96. }
  97. pWakeAllConditionVariable(&cond->cond);
  98. return 0;
  99. }
  100. static int
  101. SDL_CondWaitTimeout_cv(SDL_cond * _cond, SDL_mutex * _mutex, Uint32 ms)
  102. {
  103. SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
  104. DWORD timeout;
  105. int ret;
  106. if (!cond) {
  107. return SDL_InvalidParamError("cond");
  108. }
  109. if (!_mutex) {
  110. return SDL_InvalidParamError("mutex");
  111. }
  112. if (ms == SDL_MUTEX_MAXWAIT) {
  113. timeout = INFINITE;
  114. } else {
  115. timeout = (DWORD) ms;
  116. }
  117. if (SDL_mutex_impl_active.Type == SDL_MUTEX_SRW) {
  118. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  119. if (mutex->count != 1 || mutex->owner != GetCurrentThreadId()) {
  120. return SDL_SetError("Passed mutex is not locked or locked recursively");
  121. }
  122. /* The mutex must be updated to the released state */
  123. mutex->count = 0;
  124. mutex->owner = 0;
  125. if (pSleepConditionVariableSRW(&cond->cond, &mutex->srw, timeout, 0) == FALSE) {
  126. if (GetLastError() == ERROR_TIMEOUT) {
  127. ret = SDL_MUTEX_TIMEDOUT;
  128. } else {
  129. ret = SDL_SetError("SleepConditionVariableSRW() failed");
  130. }
  131. } else {
  132. ret = 0;
  133. }
  134. /* The mutex is owned by us again, regardless of status of the wait */
  135. SDL_assert(mutex->count == 0 && mutex->owner == 0);
  136. mutex->count = 1;
  137. mutex->owner = GetCurrentThreadId();
  138. } else {
  139. SDL_mutex_cs *mutex = (SDL_mutex_cs *)_mutex;
  140. SDL_assert(SDL_mutex_impl_active.Type == SDL_MUTEX_CS);
  141. if (pSleepConditionVariableCS(&cond->cond, &mutex->cs, timeout) == FALSE) {
  142. if (GetLastError() == ERROR_TIMEOUT) {
  143. ret = SDL_MUTEX_TIMEDOUT;
  144. } else {
  145. ret = SDL_SetError("SleepConditionVariableCS() failed");
  146. }
  147. } else {
  148. ret = 0;
  149. }
  150. }
  151. return ret;
  152. }
  153. static int
  154. SDL_CondWait_cv(SDL_cond * cond, SDL_mutex * mutex) {
  155. return SDL_CondWaitTimeout_cv(cond, mutex, SDL_MUTEX_MAXWAIT);
  156. }
  157. static const SDL_cond_impl_t SDL_cond_impl_cv =
  158. {
  159. &SDL_CreateCond_cv,
  160. &SDL_DestroyCond_cv,
  161. &SDL_CondSignal_cv,
  162. &SDL_CondBroadcast_cv,
  163. &SDL_CondWait_cv,
  164. &SDL_CondWaitTimeout_cv,
  165. };
  166. /**
  167. * Generic Condition Variable implementation using SDL_mutex and SDL_sem
  168. */
  169. static const SDL_cond_impl_t SDL_cond_impl_generic =
  170. {
  171. &SDL_CreateCond_generic,
  172. &SDL_DestroyCond_generic,
  173. &SDL_CondSignal_generic,
  174. &SDL_CondBroadcast_generic,
  175. &SDL_CondWait_generic,
  176. &SDL_CondWaitTimeout_generic,
  177. };
  178. SDL_cond *
  179. SDL_CreateCond(void)
  180. {
  181. if (SDL_cond_impl_active.Create == NULL) {
  182. /* Default to generic implementation, works with all mutex implementations */
  183. const SDL_cond_impl_t * impl = &SDL_cond_impl_generic;
  184. if (SDL_mutex_impl_active.Type == SDL_MUTEX_INVALID) {
  185. /* The mutex implementation isn't decided yet, trigger it */
  186. SDL_mutex *mutex = SDL_CreateMutex();
  187. if (!mutex) {
  188. return NULL;
  189. }
  190. SDL_DestroyMutex(mutex);
  191. SDL_assert(SDL_mutex_impl_active.Type != SDL_MUTEX_INVALID);
  192. }
  193. {
  194. HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  195. if (kernel32) {
  196. pWakeConditionVariable = (pfnWakeConditionVariable) GetProcAddress(kernel32, "WakeConditionVariable");
  197. pWakeAllConditionVariable = (pfnWakeAllConditionVariable) GetProcAddress(kernel32, "WakeAllConditionVariable");
  198. pSleepConditionVariableSRW = (pfnSleepConditionVariableSRW) GetProcAddress(kernel32, "SleepConditionVariableSRW");
  199. pSleepConditionVariableCS = (pfnSleepConditionVariableCS) GetProcAddress(kernel32, "SleepConditionVariableCS");
  200. if (pWakeConditionVariable && pWakeAllConditionVariable && pSleepConditionVariableSRW && pSleepConditionVariableCS) {
  201. /* Use the Windows provided API */
  202. impl = &SDL_cond_impl_cv;
  203. }
  204. }
  205. }
  206. SDL_copyp(&SDL_cond_impl_active, impl);
  207. }
  208. return SDL_cond_impl_active.Create();
  209. }
  210. void
  211. SDL_DestroyCond(SDL_cond * cond)
  212. {
  213. SDL_cond_impl_active.Destroy(cond);
  214. }
  215. int
  216. SDL_CondSignal(SDL_cond * cond)
  217. {
  218. return SDL_cond_impl_active.Signal(cond);
  219. }
  220. int
  221. SDL_CondBroadcast(SDL_cond * cond)
  222. {
  223. return SDL_cond_impl_active.Broadcast(cond);
  224. }
  225. int
  226. SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
  227. {
  228. return SDL_cond_impl_active.WaitTimeout(cond, mutex, ms);
  229. }
  230. int
  231. SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
  232. {
  233. return SDL_cond_impl_active.Wait(cond, mutex);
  234. }
  235. /* vi: set ts=4 sw=4 expandtab: */