Skip to content

Commit

Permalink
thread: make generic SDL_Condition a no-op if built with SDL_THREADS_…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
icculus committed Apr 16, 2024
1 parent bf0bf80 commit fdcc3e1
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/thread/generic/SDL_syscond.c
Expand Up @@ -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);
Expand All @@ -65,6 +65,8 @@ SDL_Condition *SDL_CreateCondition_generic(void)
cond = NULL;
}
}
#endif

return (SDL_Condition *)cond;
}

Expand Down Expand Up @@ -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.
*/
Expand All @@ -106,6 +109,7 @@ int SDL_SignalCondition_generic(SDL_Condition *_cond)
} else {
SDL_UnlockMutex(cond->lock);
}
#endif

return 0;
}
Expand All @@ -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.
*/
Expand All @@ -140,6 +145,7 @@ int SDL_BroadcastCondition_generic(SDL_Condition *_cond)
} else {
SDL_UnlockMutex(cond->lock);
}
#endif

return 0;
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

0 comments on commit fdcc3e1

Please sign in to comment.