SDL_syscond_srw.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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. #else
  54. typedef VOID(WINAPI *pfnWakeConditionVariable)(PCONDITION_VARIABLE);
  55. typedef VOID(WINAPI *pfnWakeAllConditionVariable)(PCONDITION_VARIABLE);
  56. typedef BOOL(WINAPI *pfnSleepConditionVariableSRW)(PCONDITION_VARIABLE, PSRWLOCK, DWORD, ULONG);
  57. static pfnWakeConditionVariable pWakeConditionVariable = NULL;
  58. static pfnWakeAllConditionVariable pWakeAllConditionVariable = NULL;
  59. static pfnSleepConditionVariableSRW pSleepConditionVariableSRW = NULL;
  60. #endif
  61. typedef struct SDL_cond_srw
  62. {
  63. CONDITION_VARIABLE cond;
  64. } SDL_cond_srw;
  65. static SDL_cond *
  66. SDL_CreateCond_srw(void)
  67. {
  68. SDL_cond_srw *cond;
  69. /* Relies on CONDITION_VARIABLE_INIT == 0. */
  70. cond = (SDL_cond_srw *) SDL_calloc(1, sizeof(*cond));
  71. if (!cond) {
  72. SDL_OutOfMemory();
  73. }
  74. return (SDL_cond *)cond;
  75. }
  76. static void
  77. SDL_DestroyCond_srw(SDL_cond * cond)
  78. {
  79. if (cond) {
  80. /* There are no kernel allocated resources */
  81. SDL_free(cond);
  82. }
  83. }
  84. static int
  85. SDL_CondSignal_srw(SDL_cond * _cond)
  86. {
  87. SDL_cond_srw *cond = (SDL_cond_srw *)_cond;
  88. if (!cond) {
  89. return SDL_SetError("Passed a NULL condition variable");
  90. }
  91. pWakeConditionVariable(&cond->cond);
  92. return 0;
  93. }
  94. static int
  95. SDL_CondBroadcast_srw(SDL_cond * _cond)
  96. {
  97. SDL_cond_srw *cond = (SDL_cond_srw *)_cond;
  98. if (!cond) {
  99. return SDL_SetError("Passed a NULL condition variable");
  100. }
  101. pWakeAllConditionVariable(&cond->cond);
  102. return 0;
  103. }
  104. static int
  105. SDL_CondWaitTimeout_srw(SDL_cond * _cond, SDL_mutex * _mutex, Uint32 ms)
  106. {
  107. SDL_cond_srw *cond = (SDL_cond_srw *)_cond;
  108. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  109. DWORD timeout;
  110. if (!cond) {
  111. return SDL_SetError("Passed a NULL condition variable");
  112. }
  113. if (!mutex) {
  114. return SDL_SetError("Passed a NULL mutex");
  115. }
  116. if (mutex->count != 1) {
  117. return SDL_SetError("Passed mutex is not locked or locked recursively");
  118. }
  119. if (ms == SDL_MUTEX_MAXWAIT) {
  120. timeout = INFINITE;
  121. } else {
  122. timeout = (DWORD) ms;
  123. }
  124. if (pSleepConditionVariableSRW(&cond->cond, &mutex->srw, timeout, 0) == FALSE) {
  125. if (GetLastError() == ERROR_TIMEOUT) {
  126. return SDL_MUTEX_TIMEDOUT;
  127. }
  128. return SDL_SetError("SleepConditionVariableSRW() failed");
  129. }
  130. return 0;
  131. }
  132. static int
  133. SDL_CondWait_srw(SDL_cond * cond, SDL_mutex * mutex) {
  134. return SDL_CondWaitTimeout_srw(cond, mutex, SDL_MUTEX_MAXWAIT);
  135. }
  136. static const SDL_cond_impl_t SDL_cond_impl_srw =
  137. {
  138. &SDL_CreateCond_srw,
  139. &SDL_DestroyCond_srw,
  140. &SDL_CondSignal_srw,
  141. &SDL_CondBroadcast_srw,
  142. &SDL_CondWait_srw,
  143. &SDL_CondWaitTimeout_srw,
  144. };
  145. /**
  146. * Generic Condition Variable implementation using SDL_mutex and SDL_sem
  147. */
  148. static const SDL_cond_impl_t SDL_cond_impl_generic =
  149. {
  150. &SDL_CreateCond_generic,
  151. &SDL_DestroyCond_generic,
  152. &SDL_CondSignal_generic,
  153. &SDL_CondBroadcast_generic,
  154. &SDL_CondWait_generic,
  155. &SDL_CondWaitTimeout_generic,
  156. };
  157. SDL_cond *
  158. SDL_CreateCond(void)
  159. {
  160. if (SDL_cond_impl_active.Create == NULL) {
  161. /* Default to generic implementation, works with all mutex implementations */
  162. const SDL_cond_impl_t * impl = &SDL_cond_impl_generic;
  163. if (SDL_mutex_impl_active.Type == SDL_MUTEX_INVALID) {
  164. /* The mutex implementation isn't decided yet, trigger it */
  165. SDL_mutex *mutex = SDL_CreateMutex();
  166. if (!mutex) {
  167. return NULL;
  168. }
  169. SDL_DestroyMutex(mutex);
  170. SDL_assert(SDL_mutex_impl_active.Type != SDL_MUTEX_INVALID);
  171. }
  172. /* It is required SRW Locks are used */
  173. if (SDL_mutex_impl_active.Type == SDL_MUTEX_SRW) {
  174. #if __WINRT__
  175. /* Link statically on this platform */
  176. impl = &SDL_cond_impl_srw;
  177. #else
  178. HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
  179. if (kernel32) {
  180. pWakeConditionVariable = (pfnWakeConditionVariable) GetProcAddress(kernel32, "WakeConditionVariable");
  181. pWakeAllConditionVariable = (pfnWakeAllConditionVariable) GetProcAddress(kernel32, "WakeAllConditionVariable");
  182. pSleepConditionVariableSRW = (pfnSleepConditionVariableSRW) GetProcAddress(kernel32, "SleepConditionVariableSRW");
  183. if (pWakeConditionVariable && pWakeAllConditionVariable && pSleepConditionVariableSRW) {
  184. /* Use the Windows provided API */
  185. impl = &SDL_cond_impl_srw;
  186. }
  187. }
  188. if (!(kernel32 && pWakeConditionVariable && pWakeAllConditionVariable && pSleepConditionVariableSRW)) {
  189. SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM, "Could not load required imports for SRW Condition Variables although SRW Locks are used!");
  190. }
  191. #endif
  192. }
  193. SDL_memcpy(&SDL_cond_impl_active, impl, sizeof(SDL_cond_impl_active));
  194. }
  195. return SDL_cond_impl_active.Create();
  196. }
  197. void
  198. SDL_DestroyCond(SDL_cond * cond)
  199. {
  200. SDL_cond_impl_active.Destroy(cond);
  201. }
  202. int
  203. SDL_CondSignal(SDL_cond * cond)
  204. {
  205. return SDL_cond_impl_active.Signal(cond);
  206. }
  207. int
  208. SDL_CondBroadcast(SDL_cond * cond)
  209. {
  210. return SDL_cond_impl_active.Broadcast(cond);
  211. }
  212. int
  213. SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
  214. {
  215. return SDL_cond_impl_active.WaitTimeout(cond, mutex, ms);
  216. }
  217. int
  218. SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
  219. {
  220. return SDL_cond_impl_active.Wait(cond, mutex);
  221. }
  222. /* vi: set ts=4 sw=4 expandtab: */