From 776820526b06792bb26b318c16aeadf466dd3ab5 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 26 Apr 2023 14:04:57 -0400 Subject: [PATCH] windows: Use InitializeSRWLock explicitly. The docs say you should, if not statically initializing an SRWLOCK--which we aren't--but in practice this is probably just being pedantic. Still, better (thread) safe than sorry! --- src/thread/windows/SDL_sysmutex.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/thread/windows/SDL_sysmutex.c b/src/thread/windows/SDL_sysmutex.c index 25d33a9bfb..023a82b280 100644 --- a/src/thread/windows/SDL_sysmutex.c +++ b/src/thread/windows/SDL_sysmutex.c @@ -41,13 +41,16 @@ SDL_mutex_impl_t SDL_mutex_impl_active = { 0 }; #ifdef __WINRT__ /* Functions are guaranteed to be available */ +#define pInitializeSRWLock InitializeSRWLock #define pReleaseSRWLockExclusive ReleaseSRWLockExclusive #define pAcquireSRWLockExclusive AcquireSRWLockExclusive #define pTryAcquireSRWLockExclusive TryAcquireSRWLockExclusive #else +typedef VOID(WINAPI *pfnInitializeSRWLock)(PSRWLOCK); typedef VOID(WINAPI *pfnReleaseSRWLockExclusive)(PSRWLOCK); typedef VOID(WINAPI *pfnAcquireSRWLockExclusive)(PSRWLOCK); typedef BOOLEAN(WINAPI *pfnTryAcquireSRWLockExclusive)(PSRWLOCK); +static pfnInitializeSRWLock pInitializeSRWLock = NULL; static pfnReleaseSRWLockExclusive pReleaseSRWLockExclusive = NULL; static pfnAcquireSRWLockExclusive pAcquireSRWLockExclusive = NULL; static pfnTryAcquireSRWLockExclusive pTryAcquireSRWLockExclusive = NULL; @@ -57,12 +60,13 @@ static SDL_mutex *SDL_CreateMutex_srw(void) { SDL_mutex_srw *mutex; - /* Relies on SRWLOCK_INIT == 0. */ mutex = (SDL_mutex_srw *)SDL_calloc(1, sizeof(*mutex)); if (mutex == NULL) { SDL_OutOfMemory(); } + pInitializeSRWLock(&mutex->srw); + return (SDL_mutex *)mutex; } @@ -232,11 +236,12 @@ SDL_CreateMutex(void) HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll")); if (kernel32) { /* Requires Vista: */ + pInitializeSRWLock = (pfnInitializeSRWLock)GetProcAddress(kernel32, "InitializeSRWLock"); pReleaseSRWLockExclusive = (pfnReleaseSRWLockExclusive)GetProcAddress(kernel32, "ReleaseSRWLockExclusive"); pAcquireSRWLockExclusive = (pfnAcquireSRWLockExclusive)GetProcAddress(kernel32, "AcquireSRWLockExclusive"); /* Requires 7: */ pTryAcquireSRWLockExclusive = (pfnTryAcquireSRWLockExclusive)GetProcAddress(kernel32, "TryAcquireSRWLockExclusive"); - if (pReleaseSRWLockExclusive && pAcquireSRWLockExclusive && pTryAcquireSRWLockExclusive) { + if (pInitializeSRWLock && pReleaseSRWLockExclusive && pAcquireSRWLockExclusive && pTryAcquireSRWLockExclusive) { impl = &SDL_mutex_impl_srw; } }