SDL_sysmutex.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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 *SDL_CreateMutex_srw(void)
  48. {
  49. SDL_mutex_srw *mutex;
  50. /* Relies on SRWLOCK_INIT == 0. */
  51. mutex = (SDL_mutex_srw *)SDL_calloc(1, sizeof(*mutex));
  52. if (mutex == NULL) {
  53. SDL_OutOfMemory();
  54. }
  55. return (SDL_mutex *)mutex;
  56. }
  57. static void SDL_DestroyMutex_srw(SDL_mutex *mutex)
  58. {
  59. /* There are no kernel allocated resources */
  60. SDL_free(mutex);
  61. }
  62. static int SDL_LockMutex_srw(SDL_mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  63. {
  64. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  65. DWORD this_thread;
  66. this_thread = GetCurrentThreadId();
  67. if (mutex->owner == this_thread) {
  68. ++mutex->count;
  69. } else {
  70. /* The order of operations is important.
  71. We set the locking thread id after we obtain the lock
  72. so unlocks from other threads will fail.
  73. */
  74. pAcquireSRWLockExclusive(&mutex->srw);
  75. SDL_assert(mutex->count == 0 && mutex->owner == 0);
  76. mutex->owner = this_thread;
  77. mutex->count = 1;
  78. }
  79. return 0;
  80. }
  81. static int SDL_TryLockMutex_srw(SDL_mutex *_mutex)
  82. {
  83. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  84. DWORD this_thread;
  85. int retval = 0;
  86. this_thread = GetCurrentThreadId();
  87. if (mutex->owner == this_thread) {
  88. ++mutex->count;
  89. } else {
  90. if (pTryAcquireSRWLockExclusive(&mutex->srw) != 0) {
  91. SDL_assert(mutex->count == 0 && mutex->owner == 0);
  92. mutex->owner = this_thread;
  93. mutex->count = 1;
  94. } else {
  95. retval = SDL_MUTEX_TIMEDOUT;
  96. }
  97. }
  98. return retval;
  99. }
  100. static int SDL_UnlockMutex_srw(SDL_mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  101. {
  102. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  103. if (mutex->owner == GetCurrentThreadId()) {
  104. if (--mutex->count == 0) {
  105. mutex->owner = 0;
  106. pReleaseSRWLockExclusive(&mutex->srw);
  107. }
  108. } else {
  109. return SDL_SetError("mutex not owned by this thread");
  110. }
  111. return 0;
  112. }
  113. static const SDL_mutex_impl_t SDL_mutex_impl_srw = {
  114. &SDL_CreateMutex_srw,
  115. &SDL_DestroyMutex_srw,
  116. &SDL_LockMutex_srw,
  117. &SDL_TryLockMutex_srw,
  118. &SDL_UnlockMutex_srw,
  119. SDL_MUTEX_SRW,
  120. };
  121. /**
  122. * Fallback Mutex implementation using Critical Sections (before Win 7)
  123. */
  124. /* Create a mutex */
  125. static SDL_mutex *SDL_CreateMutex_cs(void)
  126. {
  127. SDL_mutex_cs *mutex;
  128. /* Allocate mutex memory */
  129. mutex = (SDL_mutex_cs *)SDL_malloc(sizeof(*mutex));
  130. if (mutex != NULL) {
  131. /* Initialize */
  132. /* On SMP systems, a non-zero spin count generally helps performance */
  133. #if __WINRT__
  134. InitializeCriticalSectionEx(&mutex->cs, 2000, 0);
  135. #else
  136. InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
  137. #endif
  138. } else {
  139. SDL_OutOfMemory();
  140. }
  141. return (SDL_mutex *)mutex;
  142. }
  143. /* Free the mutex */
  144. static void SDL_DestroyMutex_cs(SDL_mutex *mutex_)
  145. {
  146. SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
  147. DeleteCriticalSection(&mutex->cs);
  148. SDL_free(mutex);
  149. }
  150. /* Lock the mutex */
  151. static int SDL_LockMutex_cs(SDL_mutex *mutex_) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  152. {
  153. SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
  154. EnterCriticalSection(&mutex->cs);
  155. return 0;
  156. }
  157. /* TryLock the mutex */
  158. static int SDL_TryLockMutex_cs(SDL_mutex *mutex_)
  159. {
  160. SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
  161. int retval = 0;
  162. if (TryEnterCriticalSection(&mutex->cs) == 0) {
  163. retval = SDL_MUTEX_TIMEDOUT;
  164. }
  165. return retval;
  166. }
  167. /* Unlock the mutex */
  168. static int SDL_UnlockMutex_cs(SDL_mutex *mutex_) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  169. {
  170. SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
  171. LeaveCriticalSection(&mutex->cs);
  172. return 0;
  173. }
  174. static const SDL_mutex_impl_t SDL_mutex_impl_cs = {
  175. &SDL_CreateMutex_cs,
  176. &SDL_DestroyMutex_cs,
  177. &SDL_LockMutex_cs,
  178. &SDL_TryLockMutex_cs,
  179. &SDL_UnlockMutex_cs,
  180. SDL_MUTEX_CS,
  181. };
  182. /**
  183. * Runtime selection and redirection
  184. */
  185. SDL_mutex *SDL_CreateMutex(void)
  186. {
  187. if (SDL_mutex_impl_active.Create == NULL) {
  188. /* Default to fallback implementation */
  189. const SDL_mutex_impl_t *impl = &SDL_mutex_impl_cs;
  190. if (!SDL_GetHintBoolean(SDL_HINT_WINDOWS_FORCE_MUTEX_CRITICAL_SECTIONS, SDL_FALSE)) {
  191. #if __WINRT__
  192. /* Link statically on this platform */
  193. impl = &SDL_mutex_impl_srw;
  194. #else
  195. /* Try faster implementation for Windows 7 and newer */
  196. HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  197. if (kernel32) {
  198. /* Requires Vista: */
  199. pReleaseSRWLockExclusive = (pfnReleaseSRWLockExclusive)GetProcAddress(kernel32, "ReleaseSRWLockExclusive");
  200. pAcquireSRWLockExclusive = (pfnAcquireSRWLockExclusive)GetProcAddress(kernel32, "AcquireSRWLockExclusive");
  201. /* Requires 7: */
  202. pTryAcquireSRWLockExclusive = (pfnTryAcquireSRWLockExclusive)GetProcAddress(kernel32, "TryAcquireSRWLockExclusive");
  203. if (pReleaseSRWLockExclusive && pAcquireSRWLockExclusive && pTryAcquireSRWLockExclusive) {
  204. impl = &SDL_mutex_impl_srw;
  205. }
  206. }
  207. #endif
  208. }
  209. /* Copy instead of using pointer to save one level of indirection */
  210. SDL_copyp(&SDL_mutex_impl_active, impl);
  211. }
  212. return SDL_mutex_impl_active.Create();
  213. }
  214. void SDL_DestroyMutex(SDL_mutex *mutex)
  215. {
  216. if (mutex) {
  217. SDL_mutex_impl_active.Destroy(mutex);
  218. }
  219. }
  220. int SDL_LockMutex(SDL_mutex *mutex)
  221. {
  222. if (mutex == NULL) {
  223. return 0;
  224. }
  225. return SDL_mutex_impl_active.Lock(mutex);
  226. }
  227. int SDL_TryLockMutex(SDL_mutex *mutex)
  228. {
  229. if (mutex == NULL) {
  230. return 0;
  231. }
  232. return SDL_mutex_impl_active.TryLock(mutex);
  233. }
  234. int SDL_UnlockMutex(SDL_mutex *mutex)
  235. {
  236. if (mutex == NULL) {
  237. return 0;
  238. }
  239. return SDL_mutex_impl_active.Unlock(mutex);
  240. }
  241. #endif /* SDL_THREAD_WINDOWS */
  242. /* vi: set ts=4 sw=4 expandtab: */