From 5a079a2d114f96d4847d1ee305d5b7c16eeec50e Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 27 Dec 2025 12:03:39 -0800 Subject: Initial commit --- contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex.c | 67 ++++++++++ contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex_c.h | 33 +++++ contrib/SDL-3.2.8/src/thread/n3ds/SDL_syssem.c | 112 +++++++++++++++++ contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread.c | 139 +++++++++++++++++++++ .../SDL-3.2.8/src/thread/n3ds/SDL_systhread_c.h | 30 +++++ 5 files changed, 381 insertions(+) create mode 100644 contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex.c create mode 100644 contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex_c.h create mode 100644 contrib/SDL-3.2.8/src/thread/n3ds/SDL_syssem.c create mode 100644 contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread.c create mode 100644 contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread_c.h (limited to 'contrib/SDL-3.2.8/src/thread/n3ds') diff --git a/contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex.c b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex.c new file mode 100644 index 0000000..0abb9f5 --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex.c @@ -0,0 +1,67 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_THREAD_N3DS + +// An implementation of mutexes using libctru's RecursiveLock + +#include "SDL_sysmutex_c.h" + +SDL_Mutex *SDL_CreateMutex(void) +{ + SDL_Mutex *mutex = (SDL_Mutex *)SDL_malloc(sizeof(*mutex)); + if (mutex) { + RecursiveLock_Init(&mutex->lock); + } + return mutex; +} + +void SDL_DestroyMutex(SDL_Mutex *mutex) +{ + if (mutex) { + SDL_free(mutex); + } +} + +void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes +{ + if (mutex) { + RecursiveLock_Lock(&mutex->lock); + } +} + +bool SDL_TryLockMutex(SDL_Mutex *mutex) +{ + if (mutex) { + return RecursiveLock_TryLock(&mutex->lock); + } + return true; +} + +void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes +{ + if (mutex) { + RecursiveLock_Unlock(&mutex->lock); + } +} + +#endif // SDL_THREAD_N3DS diff --git a/contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex_c.h b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex_c.h new file mode 100644 index 0000000..ddf8069 --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex_c.h @@ -0,0 +1,33 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifndef SDL_sysmutex_c_h_ +#define SDL_sysmutex_c_h_ + +#include <3ds.h> + +struct SDL_Mutex +{ + RecursiveLock lock; +}; + +#endif // SDL_sysmutex_c_h diff --git a/contrib/SDL-3.2.8/src/thread/n3ds/SDL_syssem.c b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_syssem.c new file mode 100644 index 0000000..3207a2d --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_syssem.c @@ -0,0 +1,112 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_THREAD_N3DS + +// An implementation of semaphores using libctru's LightSemaphore + +#include <3ds.h> + +struct SDL_Semaphore +{ + LightSemaphore semaphore; +}; + +SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value) +{ + SDL_Semaphore *sem; + + if (initial_value > SDL_MAX_SINT16) { + SDL_SetError("Initial semaphore value too high for this platform"); + return NULL; + } + + sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem)); + if (!sem) { + return NULL; + } + + LightSemaphore_Init(&sem->semaphore, initial_value, SDL_MAX_SINT16); + + return sem; +} + +/* WARNING: + You cannot call this function when another thread is using the semaphore. +*/ +void SDL_DestroySemaphore(SDL_Semaphore *sem) +{ + SDL_free(sem); +} + +static bool WaitOnSemaphoreFor(SDL_Semaphore *sem, Sint64 timeoutNS) +{ + Uint64 stop_time = SDL_GetTicksNS() + timeoutNS; + while (SDL_GetTicksNS() < stop_time) { + if (LightSemaphore_TryAcquire(&sem->semaphore, 1) == 0) { + return true; + } + // 100 microseconds seems to be the sweet spot + SDL_DelayNS(SDL_US_TO_NS(100)); + } + + // If we failed, yield to avoid starvation on busy waits + SDL_DelayNS(1); + return false; +} + +bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS) +{ + if (!sem) { + return true; + } + + if (timeoutNS < 0) { // -1 == wait indefinitely. + LightSemaphore_Acquire(&sem->semaphore, 1); + return true; + } + + if (LightSemaphore_TryAcquire(&sem->semaphore, 1) == 0) { + return true; + } + + return WaitOnSemaphoreFor(sem, timeoutNS); +} + +Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem) +{ + if (!sem) { + return 0; + } + return sem->semaphore.current_count; +} + +void SDL_SignalSemaphore(SDL_Semaphore *sem) +{ + if (sem) { + return; + } + + LightSemaphore_Release(&sem->semaphore, 1); +} + +#endif // SDL_THREAD_N3DS diff --git a/contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread.c b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread.c new file mode 100644 index 0000000..6bd826b --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread.c @@ -0,0 +1,139 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_THREAD_N3DS + +// Thread management routines for SDL + +#include "../SDL_systhread.h" + +// N3DS has very limited RAM (128MB), so we set a low default thread stack size. +#define N3DS_THREAD_STACK_SIZE_DEFAULT (80 * 1024) + +#define N3DS_THREAD_PRIORITY_LOW 0x3F /**< Minimum priority */ +#define N3DS_THREAD_PRIORITY_MEDIUM 0x2F /**< Slightly higher than main thread (0x30) */ +#define N3DS_THREAD_PRIORITY_HIGH 0x19 /**< High priority for non-video work */ +#define N3DS_THREAD_PRIORITY_TIME_CRITICAL 0x18 /**< Highest priority */ + +static size_t GetStackSize(size_t requested_size); + +static void ThreadEntry(void *arg) +{ + SDL_RunThread((SDL_Thread *)arg); + threadExit(0); +} + + +bool SDL_SYS_CreateThread(SDL_Thread *thread, + SDL_FunctionPointer pfnBeginThread, + SDL_FunctionPointer pfnEndThread) +{ + s32 priority = 0x30; + int cpu = -1; + size_t stack_size = GetStackSize(thread->stacksize); + + svcGetThreadPriority(&priority, CUR_THREAD_HANDLE); + + // prefer putting audio thread on system core + if (thread->name && (SDL_strncmp(thread->name, "SDLAudioP", 9) == 0) && R_SUCCEEDED(APT_SetAppCpuTimeLimit(30))) { + cpu = 1; + } + + thread->handle = threadCreate(ThreadEntry, + thread, + stack_size, + priority, + cpu, + false); + + if (!thread->handle) { + return SDL_SetError("Couldn't create thread"); + } + + return true; +} + +static size_t GetStackSize(size_t requested_size) +{ + if (requested_size == 0) { + return N3DS_THREAD_STACK_SIZE_DEFAULT; + } + + return requested_size; +} + +void SDL_SYS_SetupThread(const char *name) +{ + return; +} + +SDL_ThreadID SDL_GetCurrentThreadID(void) +{ + u32 thread_ID = 0; + svcGetThreadId(&thread_ID, CUR_THREAD_HANDLE); + return (SDL_ThreadID)thread_ID; +} + +bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority sdl_priority) +{ + s32 svc_priority; + switch (sdl_priority) { + case SDL_THREAD_PRIORITY_LOW: + svc_priority = N3DS_THREAD_PRIORITY_LOW; + break; + case SDL_THREAD_PRIORITY_NORMAL: + svc_priority = N3DS_THREAD_PRIORITY_MEDIUM; + break; + case SDL_THREAD_PRIORITY_HIGH: + svc_priority = N3DS_THREAD_PRIORITY_HIGH; + break; + case SDL_THREAD_PRIORITY_TIME_CRITICAL: + svc_priority = N3DS_THREAD_PRIORITY_TIME_CRITICAL; + break; + default: + svc_priority = N3DS_THREAD_PRIORITY_MEDIUM; + } + if (svcSetThreadPriority(CUR_THREAD_HANDLE, svc_priority) < 0) { + return SDL_SetError("svcSetThreadPriority failed"); + } + return true; +} + +void SDL_SYS_WaitThread(SDL_Thread *thread) +{ + Result res = threadJoin(thread->handle, U64_MAX); + + /* + Detached threads can be waited on, but should NOT be cleaned manually + as it would result in a fatal error. + */ + if (R_SUCCEEDED(res) && SDL_GetThreadState(thread) != SDL_THREAD_DETACHED) { + threadFree(thread->handle); + } +} + +void SDL_SYS_DetachThread(SDL_Thread *thread) +{ + threadDetach(thread->handle); +} + +#endif // SDL_THREAD_N3DS diff --git a/contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread_c.h b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread_c.h new file mode 100644 index 0000000..d87d636 --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread_c.h @@ -0,0 +1,30 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifndef SDL_systhread_c_h_ +#define SDL_systhread_c_h_ + +#include <3ds.h> + +typedef Thread SYS_ThreadHandle; + +#endif // SDL_systhread_c_h_ -- cgit v1.2.3