Merge commit '852f2a6343518919e5ca8d3c1bbcab9f493e3cd8'

This commit is contained in:
2024-01-17 17:02:59 +01:00
1244 changed files with 50102 additions and 28146 deletions

View File

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -37,7 +37,7 @@ void *SDL_GetTLS(SDL_TLSID id)
SDL_TLSData *storage;
storage = SDL_SYS_GetTLSData();
if (storage == NULL || id == 0 || id > storage->limit) {
if (!storage || id == 0 || id > storage->limit) {
return NULL;
}
return storage->array[id - 1].data;
@ -52,15 +52,17 @@ int SDL_SetTLS(SDL_TLSID id, const void *value, void(SDLCALL *destructor)(void *
}
storage = SDL_SYS_GetTLSData();
if (storage == NULL || (id > storage->limit)) {
if (!storage || (id > storage->limit)) {
unsigned int i, oldlimit, newlimit;
SDL_TLSData *new_storage;
oldlimit = storage ? storage->limit : 0;
newlimit = (id + TLS_ALLOC_CHUNKSIZE);
storage = (SDL_TLSData *)SDL_realloc(storage, sizeof(*storage) + (newlimit - 1) * sizeof(storage->array[0]));
if (storage == NULL) {
return SDL_OutOfMemory();
new_storage = (SDL_TLSData *)SDL_realloc(storage, sizeof(*storage) + (newlimit - 1) * sizeof(storage->array[0]));
if (!new_storage) {
return -1;
}
storage = new_storage;
storage->limit = newlimit;
for (i = oldlimit; i < newlimit; ++i) {
storage->array[i].data = NULL;
@ -118,14 +120,14 @@ SDL_TLSData *SDL_Generic_GetTLSData(void)
SDL_TLSData *storage = NULL;
#ifndef SDL_THREADS_DISABLED
if (SDL_generic_TLS_mutex == NULL) {
if (!SDL_generic_TLS_mutex) {
static SDL_SpinLock tls_lock;
SDL_AtomicLock(&tls_lock);
if (SDL_generic_TLS_mutex == NULL) {
if (!SDL_generic_TLS_mutex) {
SDL_Mutex *mutex = SDL_CreateMutex();
SDL_MemoryBarrierRelease();
SDL_generic_TLS_mutex = mutex;
if (SDL_generic_TLS_mutex == NULL) {
if (!SDL_generic_TLS_mutex) {
SDL_AtomicUnlock(&tls_lock);
return NULL;
}
@ -159,10 +161,10 @@ int SDL_Generic_SetTLSData(SDL_TLSData *data)
prev = NULL;
for (entry = SDL_generic_TLS; entry; entry = entry->next) {
if (entry->thread == thread) {
if (data != NULL) {
if (data) {
entry->storage = data;
} else {
if (prev != NULL) {
if (prev) {
prev->next = entry->next;
} else {
SDL_generic_TLS = entry->next;
@ -173,7 +175,7 @@ int SDL_Generic_SetTLSData(SDL_TLSData *data)
}
prev = entry;
}
if (entry == NULL) {
if (!entry) {
entry = (SDL_TLSEntry *)SDL_malloc(sizeof(*entry));
if (entry) {
entry->thread = thread;
@ -184,10 +186,7 @@ int SDL_Generic_SetTLSData(SDL_TLSData *data)
}
SDL_UnlockMutex(SDL_generic_TLS_mutex);
if (entry == NULL) {
return SDL_OutOfMemory();
}
return 0;
return entry ? 0 : -1;
}
/* Non-thread-safe global error variable */
@ -213,7 +212,7 @@ static void SDLCALL SDL_FreeErrBuf(void *data)
#endif
/* Routine to get the thread-specific error variable */
SDL_error *SDL_GetErrBuf(void)
SDL_error *SDL_GetErrBuf(SDL_bool create)
{
#ifdef SDL_THREADS_DISABLED
return SDL_GetStaticErrBuf();
@ -224,6 +223,10 @@ SDL_error *SDL_GetErrBuf(void)
const SDL_error *ALLOCATION_IN_PROGRESS = (SDL_error *)-1;
SDL_error *errbuf;
if (!tls_errbuf && !create) {
return NULL;
}
/* tls_being_created is there simply to prevent recursion if SDL_CreateTLS() fails.
It also means it's possible for another thread to also use SDL_global_errbuf,
but that's very unlikely and hopefully won't cause issues.
@ -249,7 +252,7 @@ SDL_error *SDL_GetErrBuf(void)
if (errbuf == ALLOCATION_IN_PROGRESS) {
return SDL_GetStaticErrBuf();
}
if (errbuf == NULL) {
if (!errbuf) {
/* Get the original memory functions for this allocation because the lifetime
* of the error buffer may span calls to SDL_SetMemoryFunctions() by the app
*/
@ -260,7 +263,7 @@ SDL_error *SDL_GetErrBuf(void)
/* Mark that we're in the middle of allocating our buffer */
SDL_SetTLS(tls_errbuf, ALLOCATION_IN_PROGRESS, NULL);
errbuf = (SDL_error *)realloc_func(NULL, sizeof(*errbuf));
if (errbuf == NULL) {
if (!errbuf) {
SDL_SetTLS(tls_errbuf, NULL, NULL);
return SDL_GetStaticErrBuf();
}
@ -328,18 +331,16 @@ SDL_Thread *SDL_CreateThreadWithStackSize(int(SDLCALL *fn)(void *),
/* Allocate memory for the thread info structure */
thread = (SDL_Thread *)SDL_calloc(1, sizeof(*thread));
if (thread == NULL) {
SDL_OutOfMemory();
if (!thread) {
return NULL;
}
thread->status = -1;
SDL_AtomicSet(&thread->state, SDL_THREAD_STATE_ALIVE);
/* Set up the arguments for the thread */
if (name != NULL) {
if (name) {
thread->name = SDL_strdup(name);
if (thread->name == NULL) {
SDL_OutOfMemory();
if (!thread->name) {
SDL_free(thread);
return NULL;
}
@ -381,7 +382,7 @@ DECLSPEC SDL_Thread *SDLCALL SDL_CreateThread(int(SDLCALL *fn)(void *),
size_t stacksize = 0;
/* If the SDL_HINT_THREAD_STACK_SIZE exists, use it */
if (stackhint != NULL) {
if (stackhint) {
char *endp = NULL;
const Sint64 hintval = SDL_strtoll(stackhint, &endp, 10);
if ((*stackhint != '\0') && (*endp == '\0')) { /* a valid number? */
@ -450,7 +451,7 @@ void SDL_WaitThread(SDL_Thread *thread, int *status)
void SDL_DetachThread(SDL_Thread *thread)
{
if (thread == NULL) {
if (!thread) {
return;
}
@ -472,7 +473,7 @@ void SDL_DetachThread(SDL_Thread *thread)
int SDL_WaitSemaphore(SDL_Semaphore *sem)
{
return SDL_WaitSemaphoreTimeoutNS(sem, SDL_MUTEX_MAXWAIT);
return SDL_WaitSemaphoreTimeoutNS(sem, -1);
}
int SDL_TryWaitSemaphore(SDL_Semaphore *sem)
@ -494,7 +495,7 @@ int SDL_WaitSemaphoreTimeout(SDL_Semaphore *sem, Sint32 timeoutMS)
int SDL_WaitCondition(SDL_Condition *cond, SDL_Mutex *mutex)
{
return SDL_WaitConditionTimeoutNS(cond, mutex, SDL_MUTEX_MAXWAIT);
return SDL_WaitConditionTimeoutNS(cond, mutex, -1);
}
int SDL_WaitConditionTimeout(SDL_Condition *cond, SDL_Mutex *mutex, Sint32 timeoutMS)