SDL_sysmutex.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #if SDL_THREAD_WINDOWS
  20. /**
  21. * Mutex functions using the Win32 API
  22. * There are two implementations available based on:
  23. * - Critical Sections. Available on all OS versions since Windows XP.
  24. * - Slim Reader/Writer Locks. Requires Windows 7 or newer.
  25. * which are chosen at runtime.
  26. */
  27. #include "SDL_hints.h"
  28. #include "SDL_sysmutex_c.h"
  29. /* Implementation will be chosen at runtime based on available Kernel features */
  30. SDL_mutex_impl_t SDL_mutex_impl_active = {0};
  31. /**
  32. * Implementation based on Slim Reader/Writer (SRW) Locks for Win 7 and newer.
  33. */
  34. #if __WINRT__
  35. /* Functions are guaranteed to be available */
  36. #define pReleaseSRWLockExclusive ReleaseSRWLockExclusive
  37. #define pAcquireSRWLockExclusive AcquireSRWLockExclusive
  38. #define pTryAcquireSRWLockExclusive TryAcquireSRWLockExclusive
  39. #else
  40. typedef VOID(WINAPI *pfnReleaseSRWLockExclusive)(PSRWLOCK);
  41. typedef VOID(WINAPI *pfnAcquireSRWLockExclusive)(PSRWLOCK);
  42. typedef BOOLEAN(WINAPI *pfnTryAcquireSRWLockExclusive)(PSRWLOCK);
  43. static pfnReleaseSRWLockExclusive pReleaseSRWLockExclusive = NULL;
  44. static pfnAcquireSRWLockExclusive pAcquireSRWLockExclusive = NULL;
  45. static pfnTryAcquireSRWLockExclusive pTryAcquireSRWLockExclusive = NULL;
  46. #endif
  47. static SDL_mutex *
  48. SDL_CreateMutex_srw(void)
  49. {
  50. SDL_mutex_srw *mutex;
  51. /* Relies on SRWLOCK_INIT == 0. */
  52. mutex = (SDL_mutex_srw *) SDL_calloc(1, sizeof(*mutex));
  53. if (!mutex) {
  54. SDL_OutOfMemory();
  55. }
  56. return (SDL_mutex *)mutex;
  57. }
  58. static void
  59. SDL_DestroyMutex_srw(SDL_mutex * mutex)
  60. {
  61. if (mutex) {
  62. /* There are no kernel allocated resources */
  63. SDL_free(mutex);
  64. }
  65. }
  66. static int
  67. SDL_LockMutex_srw(SDL_mutex * _mutex)
  68. {
  69. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  70. DWORD this_thread;
  71. if (mutex == NULL) {
  72. return SDL_InvalidParamError("mutex");
  73. }
  74. this_thread = GetCurrentThreadId();
  75. if (mutex->owner == this_thread) {
  76. ++mutex->count;
  77. } else {
  78. /* The order of operations is important.
  79. We set the locking thread id after we obtain the lock
  80. so unlocks from other threads will fail.
  81. */
  82. pAcquireSRWLockExclusive(&mutex->srw);
  83. SDL_assert(mutex->count == 0 && mutex->owner == 0);
  84. mutex->owner = this_thread;
  85. mutex->count = 1;
  86. }
  87. return 0;
  88. }
  89. static int
  90. SDL_TryLockMutex_srw(SDL_mutex * _mutex)
  91. {
  92. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  93. DWORD this_thread;
  94. int retval = 0;
  95. if (mutex == NULL) {
  96. return SDL_InvalidParamError("mutex");
  97. }
  98. this_thread = GetCurrentThreadId();
  99. if (mutex->owner == this_thread) {
  100. ++mutex->count;
  101. } else {
  102. if (pTryAcquireSRWLockExclusive(&mutex->srw) != 0) {
  103. SDL_assert(mutex->count == 0 && mutex->owner == 0);
  104. mutex->owner = this_thread;
  105. mutex->count = 1;
  106. } else {
  107. retval = SDL_MUTEX_TIMEDOUT;
  108. }
  109. }
  110. return retval;
  111. }
  112. static int
  113. SDL_UnlockMutex_srw(SDL_mutex * _mutex)
  114. {
  115. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  116. if (mutex == NULL) {
  117. return SDL_InvalidParamError("mutex");
  118. }
  119. if (mutex->owner == GetCurrentThreadId()) {
  120. if (--mutex->count == 0) {
  121. mutex->owner = 0;
  122. pReleaseSRWLockExclusive(&mutex->srw);
  123. }
  124. } else {
  125. return SDL_SetError("mutex not owned by this thread");
  126. }
  127. return 0;
  128. }
  129. static const SDL_mutex_impl_t SDL_mutex_impl_srw =
  130. {
  131. &SDL_CreateMutex_srw,
  132. &SDL_DestroyMutex_srw,
  133. &SDL_LockMutex_srw,
  134. &SDL_TryLockMutex_srw,
  135. &SDL_UnlockMutex_srw,
  136. SDL_MUTEX_SRW,
  137. };
  138. /**
  139. * Fallback Mutex implementation using Critical Sections (before Win 7)
  140. */
  141. /* Create a mutex */
  142. static SDL_mutex *
  143. SDL_CreateMutex_cs(void)
  144. {
  145. SDL_mutex_cs *mutex;
  146. /* Allocate mutex memory */
  147. mutex = (SDL_mutex_cs *) SDL_malloc(sizeof(*mutex));
  148. if (mutex) {
  149. /* Initialize */
  150. /* On SMP systems, a non-zero spin count generally helps performance */
  151. #if __WINRT__
  152. InitializeCriticalSectionEx(&mutex->cs, 2000, 0);
  153. #else
  154. InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
  155. #endif
  156. } else {
  157. SDL_OutOfMemory();
  158. }
  159. return (SDL_mutex *)mutex;
  160. }
  161. /* Free the mutex */
  162. static void
  163. SDL_DestroyMutex_cs(SDL_mutex * mutex_)
  164. {
  165. SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
  166. if (mutex) {
  167. DeleteCriticalSection(&mutex->cs);
  168. SDL_free(mutex);
  169. }
  170. }
  171. /* Lock the mutex */
  172. static int
  173. SDL_LockMutex_cs(SDL_mutex * mutex_)
  174. {
  175. SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
  176. if (mutex == NULL) {
  177. return SDL_InvalidParamError("mutex");
  178. }
  179. EnterCriticalSection(&mutex->cs);
  180. return 0;
  181. }
  182. /* TryLock the mutex */
  183. static int
  184. SDL_TryLockMutex_cs(SDL_mutex * mutex_)
  185. {
  186. SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
  187. int retval = 0;
  188. if (mutex == NULL) {
  189. return SDL_InvalidParamError("mutex");
  190. }
  191. if (TryEnterCriticalSection(&mutex->cs) == 0) {
  192. retval = SDL_MUTEX_TIMEDOUT;
  193. }
  194. return retval;
  195. }
  196. /* Unlock the mutex */
  197. static int
  198. SDL_UnlockMutex_cs(SDL_mutex * mutex_)
  199. {
  200. SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
  201. if (mutex == NULL) {
  202. return SDL_InvalidParamError("mutex");
  203. }
  204. LeaveCriticalSection(&mutex->cs);
  205. return 0;
  206. }
  207. static const SDL_mutex_impl_t SDL_mutex_impl_cs =
  208. {
  209. &SDL_CreateMutex_cs,
  210. &SDL_DestroyMutex_cs,
  211. &SDL_LockMutex_cs,
  212. &SDL_TryLockMutex_cs,
  213. &SDL_UnlockMutex_cs,
  214. SDL_MUTEX_CS,
  215. };
  216. /**
  217. * Runtime selection and redirection
  218. */
  219. SDL_mutex *
  220. SDL_CreateMutex(void)
  221. {
  222. if (SDL_mutex_impl_active.Create == NULL) {
  223. /* Default to fallback implementation */
  224. const SDL_mutex_impl_t * impl = &SDL_mutex_impl_cs;
  225. if (!SDL_GetHintBoolean(SDL_HINT_WINDOWS_FORCE_MUTEX_CRITICAL_SECTIONS, SDL_FALSE)) {
  226. #if __WINRT__
  227. /* Link statically on this platform */
  228. impl = &SDL_mutex_impl_srw;
  229. #else
  230. /* Try faster implementation for Windows 7 and newer */
  231. HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  232. if (kernel32) {
  233. /* Requires Vista: */
  234. pReleaseSRWLockExclusive = (pfnReleaseSRWLockExclusive) GetProcAddress(kernel32, "ReleaseSRWLockExclusive");
  235. pAcquireSRWLockExclusive = (pfnAcquireSRWLockExclusive) GetProcAddress(kernel32, "AcquireSRWLockExclusive");
  236. /* Requires 7: */
  237. pTryAcquireSRWLockExclusive = (pfnTryAcquireSRWLockExclusive) GetProcAddress(kernel32, "TryAcquireSRWLockExclusive");
  238. if (pReleaseSRWLockExclusive && pAcquireSRWLockExclusive && pTryAcquireSRWLockExclusive) {
  239. impl = &SDL_mutex_impl_srw;
  240. }
  241. }
  242. #endif
  243. }
  244. /* Copy instead of using pointer to save one level of indirection */
  245. SDL_copyp(&SDL_mutex_impl_active, impl);
  246. }
  247. return SDL_mutex_impl_active.Create();
  248. }
  249. void
  250. SDL_DestroyMutex(SDL_mutex * mutex) {
  251. SDL_mutex_impl_active.Destroy(mutex);
  252. }
  253. int
  254. SDL_LockMutex(SDL_mutex * mutex) {
  255. return SDL_mutex_impl_active.Lock(mutex);
  256. }
  257. int
  258. SDL_TryLockMutex(SDL_mutex * mutex) {
  259. return SDL_mutex_impl_active.TryLock(mutex);
  260. }
  261. int
  262. SDL_UnlockMutex(SDL_mutex * mutex) {
  263. return SDL_mutex_impl_active.Unlock(mutex);
  264. }
  265. #endif /* SDL_THREAD_WINDOWS */
  266. /* vi: set ts=4 sw=4 expandtab: */