SDL_syscond_cv.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #if __WINRT__
  50. #define pWakeConditionVariable WakeConditionVariable
  51. #define pWakeAllConditionVariable WakeAllConditionVariable
  52. #define pSleepConditionVariableSRW SleepConditionVariableSRW
  53. #define pSleepConditionVariableCS SleepConditionVariableCS
  54. #else
  55. typedef VOID(WINAPI *pfnWakeConditionVariable)(PCONDITION_VARIABLE);
  56. typedef VOID(WINAPI *pfnWakeAllConditionVariable)(PCONDITION_VARIABLE);
  57. typedef BOOL(WINAPI *pfnSleepConditionVariableSRW)(PCONDITION_VARIABLE, PSRWLOCK, DWORD, ULONG);
  58. typedef BOOL(WINAPI* pfnSleepConditionVariableCS)(PCONDITION_VARIABLE, PCRITICAL_SECTION, DWORD);
  59. static pfnWakeConditionVariable pWakeConditionVariable = NULL;
  60. static pfnWakeAllConditionVariable pWakeAllConditionVariable = NULL;
  61. static pfnSleepConditionVariableSRW pSleepConditionVariableSRW = NULL;
  62. static pfnSleepConditionVariableCS pSleepConditionVariableCS = NULL;
  63. #endif
  64. typedef struct SDL_cond_cv
  65. {
  66. CONDITION_VARIABLE cond;
  67. } SDL_cond_cv;
  68. static SDL_cond *
  69. SDL_CreateCond_cv(void)
  70. {
  71. SDL_cond_cv *cond;
  72. /* Relies on CONDITION_VARIABLE_INIT == 0. */
  73. cond = (SDL_cond_cv *) SDL_calloc(1, sizeof(*cond));
  74. if (!cond) {
  75. SDL_OutOfMemory();
  76. }
  77. return (SDL_cond *)cond;
  78. }
  79. static void
  80. SDL_DestroyCond_cv(SDL_cond * cond)
  81. {
  82. if (cond) {
  83. /* There are no kernel allocated resources */
  84. SDL_free(cond);
  85. }
  86. }
  87. static int
  88. SDL_CondSignal_cv(SDL_cond * _cond)
  89. {
  90. SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
  91. if (!cond) {
  92. return SDL_InvalidParamError("cond");
  93. }
  94. pWakeConditionVariable(&cond->cond);
  95. return 0;
  96. }
  97. static int
  98. SDL_CondBroadcast_cv(SDL_cond * _cond)
  99. {
  100. SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
  101. if (!cond) {
  102. return SDL_InvalidParamError("cond");
  103. }
  104. pWakeAllConditionVariable(&cond->cond);
  105. return 0;
  106. }
  107. static int
  108. SDL_CondWaitTimeout_cv(SDL_cond * _cond, SDL_mutex * _mutex, Uint32 ms)
  109. {
  110. SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
  111. DWORD timeout;
  112. int ret;
  113. if (!cond) {
  114. return SDL_InvalidParamError("cond");
  115. }
  116. if (!_mutex) {
  117. return SDL_InvalidParamError("mutex");
  118. }
  119. if (ms == SDL_MUTEX_MAXWAIT) {
  120. timeout = INFINITE;
  121. } else {
  122. timeout = (DWORD) ms;
  123. }
  124. if (SDL_mutex_impl_active.Type == SDL_MUTEX_SRW) {
  125. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  126. if (mutex->count != 1 || mutex->owner != GetCurrentThreadId()) {
  127. return SDL_SetError("Passed mutex is not locked or locked recursively");
  128. }
  129. /* The mutex must be updated to the released state */
  130. mutex->count = 0;
  131. mutex->owner = 0;
  132. if (pSleepConditionVariableSRW(&cond->cond, &mutex->srw, timeout, 0) == FALSE) {
  133. if (GetLastError() == ERROR_TIMEOUT) {
  134. ret = SDL_MUTEX_TIMEDOUT;
  135. } else {
  136. ret = SDL_SetError("SleepConditionVariableSRW() failed");
  137. }
  138. } else {
  139. ret = 0;
  140. }
  141. /* The mutex is owned by us again, regardless of status of the wait */
  142. SDL_assert(mutex->count == 0 && mutex->owner == 0);
  143. mutex->count = 1;
  144. mutex->owner = GetCurrentThreadId();
  145. } else {
  146. SDL_mutex_cs *mutex = (SDL_mutex_cs *)_mutex;
  147. SDL_assert(SDL_mutex_impl_active.Type == SDL_MUTEX_CS);
  148. if (pSleepConditionVariableCS(&cond->cond, &mutex->cs, timeout) == FALSE) {
  149. if (GetLastError() == ERROR_TIMEOUT) {
  150. ret = SDL_MUTEX_TIMEDOUT;
  151. } else {
  152. ret = SDL_SetError("SleepConditionVariableCS() failed");
  153. }
  154. } else {
  155. ret = 0;
  156. }
  157. }
  158. return ret;
  159. }
  160. static int
  161. SDL_CondWait_cv(SDL_cond * cond, SDL_mutex * mutex) {
  162. return SDL_CondWaitTimeout_cv(cond, mutex, SDL_MUTEX_MAXWAIT);
  163. }
  164. static const SDL_cond_impl_t SDL_cond_impl_cv =
  165. {
  166. &SDL_CreateCond_cv,
  167. &SDL_DestroyCond_cv,
  168. &SDL_CondSignal_cv,
  169. &SDL_CondBroadcast_cv,
  170. &SDL_CondWait_cv,
  171. &SDL_CondWaitTimeout_cv,
  172. };
  173. /**
  174. * Generic Condition Variable implementation using SDL_mutex and SDL_sem
  175. */
  176. static const SDL_cond_impl_t SDL_cond_impl_generic =
  177. {
  178. &SDL_CreateCond_generic,
  179. &SDL_DestroyCond_generic,
  180. &SDL_CondSignal_generic,
  181. &SDL_CondBroadcast_generic,
  182. &SDL_CondWait_generic,
  183. &SDL_CondWaitTimeout_generic,
  184. };
  185. SDL_cond *
  186. SDL_CreateCond(void)
  187. {
  188. if (SDL_cond_impl_active.Create == NULL) {
  189. /* Default to generic implementation, works with all mutex implementations */
  190. const SDL_cond_impl_t * impl = &SDL_cond_impl_generic;
  191. if (SDL_mutex_impl_active.Type == SDL_MUTEX_INVALID) {
  192. /* The mutex implementation isn't decided yet, trigger it */
  193. SDL_mutex *mutex = SDL_CreateMutex();
  194. if (!mutex) {
  195. return NULL;
  196. }
  197. SDL_DestroyMutex(mutex);
  198. SDL_assert(SDL_mutex_impl_active.Type != SDL_MUTEX_INVALID);
  199. }
  200. #if __WINRT__
  201. /* Link statically on this platform */
  202. impl = &SDL_cond_impl_cv;
  203. #else
  204. {
  205. HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  206. if (kernel32) {
  207. pWakeConditionVariable = (pfnWakeConditionVariable) GetProcAddress(kernel32, "WakeConditionVariable");
  208. pWakeAllConditionVariable = (pfnWakeAllConditionVariable) GetProcAddress(kernel32, "WakeAllConditionVariable");
  209. pSleepConditionVariableSRW = (pfnSleepConditionVariableSRW) GetProcAddress(kernel32, "SleepConditionVariableSRW");
  210. pSleepConditionVariableCS = (pfnSleepConditionVariableCS) GetProcAddress(kernel32, "SleepConditionVariableCS");
  211. if (pWakeConditionVariable && pWakeAllConditionVariable && pSleepConditionVariableSRW && pSleepConditionVariableCS) {
  212. /* Use the Windows provided API */
  213. impl = &SDL_cond_impl_cv;
  214. }
  215. }
  216. }
  217. #endif
  218. SDL_copyp(&SDL_cond_impl_active, impl);
  219. }
  220. return SDL_cond_impl_active.Create();
  221. }
  222. void
  223. SDL_DestroyCond(SDL_cond * cond)
  224. {
  225. SDL_cond_impl_active.Destroy(cond);
  226. }
  227. int
  228. SDL_CondSignal(SDL_cond * cond)
  229. {
  230. return SDL_cond_impl_active.Signal(cond);
  231. }
  232. int
  233. SDL_CondBroadcast(SDL_cond * cond)
  234. {
  235. return SDL_cond_impl_active.Broadcast(cond);
  236. }
  237. int
  238. SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
  239. {
  240. return SDL_cond_impl_active.WaitTimeout(cond, mutex, ms);
  241. }
  242. int
  243. SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
  244. {
  245. return SDL_cond_impl_active.Wait(cond, mutex);
  246. }
  247. /* vi: set ts=4 sw=4 expandtab: */