Allow initializing hints and properties from any thread

This commit is contained in:
Sam Lantinga
2024-09-17 02:55:23 -07:00
parent d0edf68774
commit 807b8a9d4d
3 changed files with 79 additions and 47 deletions

View File

@@ -36,20 +36,37 @@ typedef struct SDL_Hint
SDL_HintWatch *callbacks;
} SDL_Hint;
static SDL_PropertiesID SDL_hint_props = 0;
static SDL_AtomicU32 SDL_hint_props;
static SDL_PropertiesID GetHintProperties(bool create)
{
if (!SDL_hint_props && create) {
SDL_hint_props = SDL_CreateProperties();
}
return SDL_hint_props;
}
void SDL_InitHints(void)
{
// Just make sure the hint properties are created on the main thread
(void)GetHintProperties(true);
}
void SDL_QuitHints(void)
{
SDL_PropertiesID props;
do {
props = SDL_GetAtomicU32(&SDL_hint_props);
} while (!SDL_CompareAndSwapAtomicU32(&SDL_hint_props, props, 0));
if (props) {
SDL_DestroyProperties(props);
}
}
static SDL_PropertiesID GetHintProperties(bool create)
{
SDL_PropertiesID props = SDL_GetAtomicU32(&SDL_hint_props);
if (!props && create) {
props = SDL_CreateProperties();
if (!SDL_CompareAndSwapAtomicU32(&SDL_hint_props, 0, props)) {
// Somebody else created hint properties before us, just use those
SDL_DestroyProperties(props);
props = SDL_GetAtomicU32(&SDL_hint_props);
}
}
return props;
}
static void SDLCALL CleanupHintProperty(void *userdata, void *value)
@@ -336,9 +353,3 @@ void SDL_RemoveHintCallback(const char *name, SDL_HintCallback callback, void *u
SDL_UnlockProperties(hints);
}
void SDL_QuitHints(void)
{
SDL_DestroyProperties(SDL_hint_props);
SDL_hint_props = 0;
}