Added SDL_ShouldInit() and SDL_ShouldQuit()

These are handy functions to support thread-safe initialization and shutdown.
This commit is contained in:
Sam Lantinga
2024-09-26 15:49:59 -07:00
parent 0e2c4e407a
commit 125e592844
7 changed files with 178 additions and 52 deletions

View File

@@ -517,3 +517,37 @@ bool SDL_WaitConditionTimeout(SDL_Condition *cond, SDL_Mutex *mutex, Sint32 time
return SDL_WaitConditionTimeoutNS(cond, mutex, timeoutNS);
}
bool SDL_ShouldInit(SDL_InitState *state)
{
while (SDL_GetAtomicInt(&state->status) != SDL_INIT_STATUS_INITIALIZED) {
if (SDL_CompareAndSwapAtomicInt(&state->status, SDL_INIT_STATUS_UNINITIALIZED, SDL_INIT_STATUS_INITIALIZING)) {
state->thread = SDL_GetCurrentThreadID();
return true;
}
// Wait for the other thread to complete transition
SDL_Delay(1);
}
return false;
}
bool SDL_ShouldQuit(SDL_InitState *state)
{
if (SDL_CompareAndSwapAtomicInt(&state->status, SDL_INIT_STATUS_INITIALIZED, SDL_INIT_STATUS_UNINITIALIZING)) {
state->thread = SDL_GetCurrentThreadID();
return true;
}
return false;
}
void SDL_SetInitialized(SDL_InitState *state, bool initialized)
{
SDL_assert(state->thread == SDL_GetCurrentThreadID());
if (initialized) {
SDL_SetAtomicInt(&state->status, SDL_INIT_STATUS_INITIALIZED);
} else {
SDL_SetAtomicInt(&state->status, SDL_INIT_STATUS_UNINITIALIZED);
}
}