From fdcc3e1151c968d36c192546ec60fd0b6c97ccb7 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Tue, 16 Apr 2024 15:01:33 -0400 Subject: [PATCH] thread: make generic SDL_Condition a no-op if built with SDL_THREADS_DISABLED. Otherwise, when you call SDL_CreateCondition() in something that can otherwise survive in a single-threaded build, you'll get an error that seems fatal. We already do this for mutexes and rwlocks (but not semaphores!) Fixes #9558. --- src/thread/generic/SDL_syscond.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/thread/generic/SDL_syscond.c b/src/thread/generic/SDL_syscond.c index 291c21f6e..d9cf41a90 100644 --- a/src/thread/generic/SDL_syscond.c +++ b/src/thread/generic/SDL_syscond.c @@ -52,9 +52,9 @@ typedef struct SDL_cond_generic /* Create a condition variable */ SDL_Condition *SDL_CreateCondition_generic(void) { - SDL_cond_generic *cond; + SDL_cond_generic *cond = (SDL_cond_generic *)SDL_calloc(1, sizeof(*cond)); - cond = (SDL_cond_generic *)SDL_malloc(sizeof(SDL_cond_generic)); +#ifndef SDL_THREADS_DISABLED if (cond) { cond->lock = SDL_CreateMutex(); cond->wait_sem = SDL_CreateSemaphore(0); @@ -65,6 +65,8 @@ SDL_Condition *SDL_CreateCondition_generic(void) cond = NULL; } } +#endif + return (SDL_Condition *)cond; } @@ -94,6 +96,7 @@ int SDL_SignalCondition_generic(SDL_Condition *_cond) return SDL_InvalidParamError("cond"); } +#ifndef SDL_THREADS_DISABLED /* If there are waiting threads not already signalled, then signal the condition and wait for the thread to respond. */ @@ -106,6 +109,7 @@ int SDL_SignalCondition_generic(SDL_Condition *_cond) } else { SDL_UnlockMutex(cond->lock); } +#endif return 0; } @@ -118,6 +122,7 @@ int SDL_BroadcastCondition_generic(SDL_Condition *_cond) return SDL_InvalidParamError("cond"); } +#ifndef SDL_THREADS_DISABLED /* If there are waiting threads not already signalled, then signal the condition and wait for the thread to respond. */ @@ -140,6 +145,7 @@ int SDL_BroadcastCondition_generic(SDL_Condition *_cond) } else { SDL_UnlockMutex(cond->lock); } +#endif return 0; } @@ -168,12 +174,13 @@ Thread B: int SDL_WaitConditionTimeoutNS_generic(SDL_Condition *_cond, SDL_Mutex *mutex, Sint64 timeoutNS) { SDL_cond_generic *cond = (SDL_cond_generic *)_cond; - int retval; + int retval = 0; if (!cond) { return SDL_InvalidParamError("cond"); } +#ifndef SDL_THREADS_DISABLED /* Obtain the protection mutex, and increment the number of waiters. This allows the signal mechanism to only perform a signal if there are waiting threads. @@ -211,6 +218,7 @@ int SDL_WaitConditionTimeoutNS_generic(SDL_Condition *_cond, SDL_Mutex *mutex, S /* Lock the mutex, as is required by condition variable semantics */ SDL_LockMutex(mutex); +#endif return retval; }