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.
main
Ryan C. Gordon 2024-04-16 15:01:33 -04:00
parent bf0bf80df6
commit fdcc3e1151
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
1 changed files with 11 additions and 3 deletions

View File

@ -52,9 +52,9 @@ typedef struct SDL_cond_generic
/* Create a condition variable */ /* Create a condition variable */
SDL_Condition *SDL_CreateCondition_generic(void) 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) { if (cond) {
cond->lock = SDL_CreateMutex(); cond->lock = SDL_CreateMutex();
cond->wait_sem = SDL_CreateSemaphore(0); cond->wait_sem = SDL_CreateSemaphore(0);
@ -65,6 +65,8 @@ SDL_Condition *SDL_CreateCondition_generic(void)
cond = NULL; cond = NULL;
} }
} }
#endif
return (SDL_Condition *)cond; return (SDL_Condition *)cond;
} }
@ -94,6 +96,7 @@ int SDL_SignalCondition_generic(SDL_Condition *_cond)
return SDL_InvalidParamError("cond"); return SDL_InvalidParamError("cond");
} }
#ifndef SDL_THREADS_DISABLED
/* If there are waiting threads not already signalled, then /* If there are waiting threads not already signalled, then
signal the condition and wait for the thread to respond. signal the condition and wait for the thread to respond.
*/ */
@ -106,6 +109,7 @@ int SDL_SignalCondition_generic(SDL_Condition *_cond)
} else { } else {
SDL_UnlockMutex(cond->lock); SDL_UnlockMutex(cond->lock);
} }
#endif
return 0; return 0;
} }
@ -118,6 +122,7 @@ int SDL_BroadcastCondition_generic(SDL_Condition *_cond)
return SDL_InvalidParamError("cond"); return SDL_InvalidParamError("cond");
} }
#ifndef SDL_THREADS_DISABLED
/* If there are waiting threads not already signalled, then /* If there are waiting threads not already signalled, then
signal the condition and wait for the thread to respond. signal the condition and wait for the thread to respond.
*/ */
@ -140,6 +145,7 @@ int SDL_BroadcastCondition_generic(SDL_Condition *_cond)
} else { } else {
SDL_UnlockMutex(cond->lock); SDL_UnlockMutex(cond->lock);
} }
#endif
return 0; return 0;
} }
@ -168,12 +174,13 @@ Thread B:
int SDL_WaitConditionTimeoutNS_generic(SDL_Condition *_cond, SDL_Mutex *mutex, Sint64 timeoutNS) int SDL_WaitConditionTimeoutNS_generic(SDL_Condition *_cond, SDL_Mutex *mutex, Sint64 timeoutNS)
{ {
SDL_cond_generic *cond = (SDL_cond_generic *)_cond; SDL_cond_generic *cond = (SDL_cond_generic *)_cond;
int retval; int retval = 0;
if (!cond) { if (!cond) {
return SDL_InvalidParamError("cond"); return SDL_InvalidParamError("cond");
} }
#ifndef SDL_THREADS_DISABLED
/* Obtain the protection mutex, and increment the number of waiters. /* Obtain the protection mutex, and increment the number of waiters.
This allows the signal mechanism to only perform a signal if there This allows the signal mechanism to only perform a signal if there
are waiting threads. 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 */ /* Lock the mutex, as is required by condition variable semantics */
SDL_LockMutex(mutex); SDL_LockMutex(mutex);
#endif
return retval; return retval;
} }