SDL_sysmutex.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #ifdef 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_sysmutex_c.h"
  28. /* Implementation will be chosen at runtime based on available Kernel features */
  29. SDL_mutex_impl_t SDL_mutex_impl_active = { 0 };
  30. /**
  31. * Implementation based on Slim Reader/Writer (SRW) Locks for Win 7 and newer.
  32. */
  33. #ifdef SDL_PLATFORM_WINRT
  34. /* Functions are guaranteed to be available */
  35. #define pInitializeSRWLock InitializeSRWLock
  36. #define pReleaseSRWLockExclusive ReleaseSRWLockExclusive
  37. #define pAcquireSRWLockExclusive AcquireSRWLockExclusive
  38. #define pTryAcquireSRWLockExclusive TryAcquireSRWLockExclusive
  39. #else
  40. typedef VOID(WINAPI *pfnInitializeSRWLock)(PSRWLOCK);
  41. typedef VOID(WINAPI *pfnReleaseSRWLockExclusive)(PSRWLOCK);
  42. typedef VOID(WINAPI *pfnAcquireSRWLockExclusive)(PSRWLOCK);
  43. typedef BOOLEAN(WINAPI *pfnTryAcquireSRWLockExclusive)(PSRWLOCK);
  44. static pfnInitializeSRWLock pInitializeSRWLock = NULL;
  45. static pfnReleaseSRWLockExclusive pReleaseSRWLockExclusive = NULL;
  46. static pfnAcquireSRWLockExclusive pAcquireSRWLockExclusive = NULL;
  47. static pfnTryAcquireSRWLockExclusive pTryAcquireSRWLockExclusive = NULL;
  48. #endif
  49. static SDL_Mutex *SDL_CreateMutex_srw(void)
  50. {
  51. SDL_mutex_srw *mutex = (SDL_mutex_srw *)SDL_calloc(1, sizeof(*mutex));
  52. if (mutex) {
  53. pInitializeSRWLock(&mutex->srw);
  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 void 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. const DWORD this_thread = GetCurrentThreadId();
  66. if (mutex->owner == this_thread) {
  67. ++mutex->count;
  68. } else {
  69. /* The order of operations is important.
  70. We set the locking thread id after we obtain the lock
  71. so unlocks from other threads will fail.
  72. */
  73. pAcquireSRWLockExclusive(&mutex->srw);
  74. SDL_assert(mutex->count == 0 && mutex->owner == 0);
  75. mutex->owner = this_thread;
  76. mutex->count = 1;
  77. }
  78. }
  79. static int SDL_TryLockMutex_srw(SDL_Mutex *_mutex)
  80. {
  81. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  82. const DWORD this_thread = GetCurrentThreadId();
  83. int retval = 0;
  84. if (mutex->owner == this_thread) {
  85. ++mutex->count;
  86. } else {
  87. if (pTryAcquireSRWLockExclusive(&mutex->srw) != 0) {
  88. SDL_assert(mutex->count == 0 && mutex->owner == 0);
  89. mutex->owner = this_thread;
  90. mutex->count = 1;
  91. } else {
  92. retval = SDL_MUTEX_TIMEDOUT;
  93. }
  94. }
  95. return retval;
  96. }
  97. static void SDL_UnlockMutex_srw(SDL_Mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  98. {
  99. SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
  100. if (mutex->owner == GetCurrentThreadId()) {
  101. if (--mutex->count == 0) {
  102. mutex->owner = 0;
  103. pReleaseSRWLockExclusive(&mutex->srw);
  104. }
  105. } else {
  106. SDL_assert(!"mutex not owned by this thread"); // undefined behavior...!
  107. }
  108. }
  109. static const SDL_mutex_impl_t SDL_mutex_impl_srw = {
  110. &SDL_CreateMutex_srw,
  111. &SDL_DestroyMutex_srw,
  112. &SDL_LockMutex_srw,
  113. &SDL_TryLockMutex_srw,
  114. &SDL_UnlockMutex_srw,
  115. SDL_MUTEX_SRW,
  116. };
  117. /**
  118. * Fallback Mutex implementation using Critical Sections (before Win 7)
  119. */
  120. static SDL_Mutex *SDL_CreateMutex_cs(void)
  121. {
  122. SDL_mutex_cs *mutex = (SDL_mutex_cs *)SDL_malloc(sizeof(*mutex));
  123. if (mutex) {
  124. // Initialize
  125. // On SMP systems, a non-zero spin count generally helps performance
  126. #ifdef SDL_PLATFORM_WINRT
  127. InitializeCriticalSectionEx(&mutex->cs, 2000, 0);
  128. #else
  129. // This function always succeeds
  130. (void)InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
  131. #endif
  132. }
  133. return (SDL_Mutex *)mutex;
  134. }
  135. static void SDL_DestroyMutex_cs(SDL_Mutex *mutex_)
  136. {
  137. SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
  138. DeleteCriticalSection(&mutex->cs);
  139. SDL_free(mutex);
  140. }
  141. static void SDL_LockMutex_cs(SDL_Mutex *mutex_) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  142. {
  143. SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
  144. EnterCriticalSection(&mutex->cs);
  145. }
  146. static int SDL_TryLockMutex_cs(SDL_Mutex *mutex_)
  147. {
  148. SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
  149. return (TryEnterCriticalSection(&mutex->cs) == 0) ? SDL_MUTEX_TIMEDOUT : 0;
  150. }
  151. static void SDL_UnlockMutex_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. LeaveCriticalSection(&mutex->cs);
  155. }
  156. static const SDL_mutex_impl_t SDL_mutex_impl_cs = {
  157. &SDL_CreateMutex_cs,
  158. &SDL_DestroyMutex_cs,
  159. &SDL_LockMutex_cs,
  160. &SDL_TryLockMutex_cs,
  161. &SDL_UnlockMutex_cs,
  162. SDL_MUTEX_CS,
  163. };
  164. /**
  165. * Runtime selection and redirection
  166. */
  167. SDL_Mutex *SDL_CreateMutex(void)
  168. {
  169. if (!SDL_mutex_impl_active.Create) {
  170. #ifdef SDL_PLATFORM_WINRT
  171. const SDL_mutex_impl_t *impl = &SDL_mutex_impl_srw;
  172. #else
  173. const SDL_mutex_impl_t *impl = &SDL_mutex_impl_cs;
  174. // Try faster implementation for Windows 7 and newer
  175. HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  176. if (kernel32) {
  177. // Requires Vista:
  178. pInitializeSRWLock = (pfnInitializeSRWLock)GetProcAddress(kernel32, "InitializeSRWLock");
  179. pReleaseSRWLockExclusive = (pfnReleaseSRWLockExclusive)GetProcAddress(kernel32, "ReleaseSRWLockExclusive");
  180. pAcquireSRWLockExclusive = (pfnAcquireSRWLockExclusive)GetProcAddress(kernel32, "AcquireSRWLockExclusive");
  181. // Requires 7:
  182. pTryAcquireSRWLockExclusive = (pfnTryAcquireSRWLockExclusive)GetProcAddress(kernel32, "TryAcquireSRWLockExclusive");
  183. if (pInitializeSRWLock && pReleaseSRWLockExclusive && pAcquireSRWLockExclusive && pTryAcquireSRWLockExclusive) {
  184. impl = &SDL_mutex_impl_srw;
  185. }
  186. }
  187. #endif // SDL_PLATFORM_WINRT
  188. // Copy instead of using pointer to save one level of indirection
  189. SDL_copyp(&SDL_mutex_impl_active, impl);
  190. }
  191. return SDL_mutex_impl_active.Create();
  192. }
  193. void SDL_DestroyMutex(SDL_Mutex *mutex)
  194. {
  195. if (mutex) {
  196. SDL_mutex_impl_active.Destroy(mutex);
  197. }
  198. }
  199. void SDL_LockMutex(SDL_Mutex *mutex)
  200. {
  201. if (mutex) {
  202. SDL_mutex_impl_active.Lock(mutex);
  203. }
  204. }
  205. int SDL_TryLockMutex(SDL_Mutex *mutex)
  206. {
  207. return mutex ? SDL_mutex_impl_active.TryLock(mutex) : 0;
  208. }
  209. void SDL_UnlockMutex(SDL_Mutex *mutex)
  210. {
  211. if (mutex) {
  212. SDL_mutex_impl_active.Unlock(mutex);
  213. }
  214. }
  215. #endif // SDL_THREAD_WINDOWS