diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread.c | 139 |
1 files changed, 139 insertions, 0 deletions
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 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #ifdef SDL_THREAD_N3DS | ||
| 24 | |||
| 25 | // Thread management routines for SDL | ||
| 26 | |||
| 27 | #include "../SDL_systhread.h" | ||
| 28 | |||
| 29 | // N3DS has very limited RAM (128MB), so we set a low default thread stack size. | ||
| 30 | #define N3DS_THREAD_STACK_SIZE_DEFAULT (80 * 1024) | ||
| 31 | |||
| 32 | #define N3DS_THREAD_PRIORITY_LOW 0x3F /**< Minimum priority */ | ||
| 33 | #define N3DS_THREAD_PRIORITY_MEDIUM 0x2F /**< Slightly higher than main thread (0x30) */ | ||
| 34 | #define N3DS_THREAD_PRIORITY_HIGH 0x19 /**< High priority for non-video work */ | ||
| 35 | #define N3DS_THREAD_PRIORITY_TIME_CRITICAL 0x18 /**< Highest priority */ | ||
| 36 | |||
| 37 | static size_t GetStackSize(size_t requested_size); | ||
| 38 | |||
| 39 | static void ThreadEntry(void *arg) | ||
| 40 | { | ||
| 41 | SDL_RunThread((SDL_Thread *)arg); | ||
| 42 | threadExit(0); | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | bool SDL_SYS_CreateThread(SDL_Thread *thread, | ||
| 47 | SDL_FunctionPointer pfnBeginThread, | ||
| 48 | SDL_FunctionPointer pfnEndThread) | ||
| 49 | { | ||
| 50 | s32 priority = 0x30; | ||
| 51 | int cpu = -1; | ||
| 52 | size_t stack_size = GetStackSize(thread->stacksize); | ||
| 53 | |||
| 54 | svcGetThreadPriority(&priority, CUR_THREAD_HANDLE); | ||
| 55 | |||
| 56 | // prefer putting audio thread on system core | ||
| 57 | if (thread->name && (SDL_strncmp(thread->name, "SDLAudioP", 9) == 0) && R_SUCCEEDED(APT_SetAppCpuTimeLimit(30))) { | ||
| 58 | cpu = 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | thread->handle = threadCreate(ThreadEntry, | ||
| 62 | thread, | ||
| 63 | stack_size, | ||
| 64 | priority, | ||
| 65 | cpu, | ||
| 66 | false); | ||
| 67 | |||
| 68 | if (!thread->handle) { | ||
| 69 | return SDL_SetError("Couldn't create thread"); | ||
| 70 | } | ||
| 71 | |||
| 72 | return true; | ||
| 73 | } | ||
| 74 | |||
| 75 | static size_t GetStackSize(size_t requested_size) | ||
| 76 | { | ||
| 77 | if (requested_size == 0) { | ||
| 78 | return N3DS_THREAD_STACK_SIZE_DEFAULT; | ||
| 79 | } | ||
| 80 | |||
| 81 | return requested_size; | ||
| 82 | } | ||
| 83 | |||
| 84 | void SDL_SYS_SetupThread(const char *name) | ||
| 85 | { | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 | SDL_ThreadID SDL_GetCurrentThreadID(void) | ||
| 90 | { | ||
| 91 | u32 thread_ID = 0; | ||
| 92 | svcGetThreadId(&thread_ID, CUR_THREAD_HANDLE); | ||
| 93 | return (SDL_ThreadID)thread_ID; | ||
| 94 | } | ||
| 95 | |||
| 96 | bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority sdl_priority) | ||
| 97 | { | ||
| 98 | s32 svc_priority; | ||
| 99 | switch (sdl_priority) { | ||
| 100 | case SDL_THREAD_PRIORITY_LOW: | ||
| 101 | svc_priority = N3DS_THREAD_PRIORITY_LOW; | ||
| 102 | break; | ||
| 103 | case SDL_THREAD_PRIORITY_NORMAL: | ||
| 104 | svc_priority = N3DS_THREAD_PRIORITY_MEDIUM; | ||
| 105 | break; | ||
| 106 | case SDL_THREAD_PRIORITY_HIGH: | ||
| 107 | svc_priority = N3DS_THREAD_PRIORITY_HIGH; | ||
| 108 | break; | ||
| 109 | case SDL_THREAD_PRIORITY_TIME_CRITICAL: | ||
| 110 | svc_priority = N3DS_THREAD_PRIORITY_TIME_CRITICAL; | ||
| 111 | break; | ||
| 112 | default: | ||
| 113 | svc_priority = N3DS_THREAD_PRIORITY_MEDIUM; | ||
| 114 | } | ||
| 115 | if (svcSetThreadPriority(CUR_THREAD_HANDLE, svc_priority) < 0) { | ||
| 116 | return SDL_SetError("svcSetThreadPriority failed"); | ||
| 117 | } | ||
| 118 | return true; | ||
| 119 | } | ||
| 120 | |||
| 121 | void SDL_SYS_WaitThread(SDL_Thread *thread) | ||
| 122 | { | ||
| 123 | Result res = threadJoin(thread->handle, U64_MAX); | ||
| 124 | |||
| 125 | /* | ||
| 126 | Detached threads can be waited on, but should NOT be cleaned manually | ||
| 127 | as it would result in a fatal error. | ||
| 128 | */ | ||
| 129 | if (R_SUCCEEDED(res) && SDL_GetThreadState(thread) != SDL_THREAD_DETACHED) { | ||
| 130 | threadFree(thread->handle); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | void SDL_SYS_DetachThread(SDL_Thread *thread) | ||
| 135 | { | ||
| 136 | threadDetach(thread->handle); | ||
| 137 | } | ||
| 138 | |||
| 139 | #endif // SDL_THREAD_N3DS | ||
