mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-09-17 16:58:13 +00:00
Added SDL_ShouldInit() and SDL_ShouldQuit()
These are handy functions to support thread-safe initialization and shutdown.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user