Atlas - SDL_systhread.c

Home / ext / SDL / src / thread / n3ds Lines: 1 | Size: 4293 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 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 37static size_t GetStackSize(size_t requested_size); 38 39static void ThreadEntry(void *arg) 40{ 41 SDL_RunThread((SDL_Thread *)arg); 42 threadExit(0); 43} 44 45bool SDL_SYS_CreateThread(SDL_Thread *thread, 46 SDL_FunctionPointer pfnBeginThread, 47 SDL_FunctionPointer pfnEndThread) 48{ 49 s32 priority = 0x30; 50 int cpu = -1; 51 size_t stack_size = GetStackSize(thread->stacksize); 52 53 svcGetThreadPriority(&priority, CUR_THREAD_HANDLE); 54 55 // on New 3DS, prefer putting audio thread on system core 56 if (thread->name && (SDL_strncmp(thread->name, "SDLAudioP", 9) == 0)) { 57 bool new3ds = false; 58 APT_CheckNew3DS(&new3ds); 59 if (new3ds && R_SUCCEEDED(APT_SetAppCpuTimeLimit(30))) { 60 cpu = 1; 61 } 62 } 63 64 thread->handle = threadCreate(ThreadEntry, 65 thread, 66 stack_size, 67 priority, 68 cpu, 69 false); 70 71 if (!thread->handle) { 72 return SDL_SetError("Couldn't create thread"); 73 } 74 75 return true; 76} 77 78static size_t GetStackSize(size_t requested_size) 79{ 80 if (requested_size == 0) { 81 return N3DS_THREAD_STACK_SIZE_DEFAULT; 82 } 83 84 return requested_size; 85} 86 87void SDL_SYS_SetupThread(const char *name) 88{ 89 return; 90} 91 92SDL_ThreadID SDL_GetCurrentThreadID(void) 93{ 94 u32 thread_ID = 0; 95 svcGetThreadId(&thread_ID, CUR_THREAD_HANDLE); 96 return (SDL_ThreadID)thread_ID; 97} 98 99bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority sdl_priority) 100{ 101 s32 svc_priority; 102 switch (sdl_priority) { 103 case SDL_THREAD_PRIORITY_LOW: 104 svc_priority = N3DS_THREAD_PRIORITY_LOW; 105 break; 106 case SDL_THREAD_PRIORITY_NORMAL: 107 svc_priority = N3DS_THREAD_PRIORITY_MEDIUM; 108 break; 109 case SDL_THREAD_PRIORITY_HIGH: 110 svc_priority = N3DS_THREAD_PRIORITY_HIGH; 111 break; 112 case SDL_THREAD_PRIORITY_TIME_CRITICAL: 113 svc_priority = N3DS_THREAD_PRIORITY_TIME_CRITICAL; 114 break; 115 default: 116 svc_priority = N3DS_THREAD_PRIORITY_MEDIUM; 117 } 118 if (svcSetThreadPriority(CUR_THREAD_HANDLE, svc_priority) < 0) { 119 return SDL_SetError("svcSetThreadPriority failed"); 120 } 121 return true; 122} 123 124void SDL_SYS_WaitThread(SDL_Thread *thread) 125{ 126 Result res = threadJoin(thread->handle, U64_MAX); 127 128 /* 129 Detached threads can be waited on, but should NOT be cleaned manually 130 as it would result in a fatal error. 131 */ 132 if (R_SUCCEEDED(res) && SDL_GetThreadState(thread) != SDL_THREAD_DETACHED) { 133 threadFree(thread->handle); 134 } 135} 136 137void SDL_SYS_DetachThread(SDL_Thread *thread) 138{ 139 threadDetach(thread->handle); 140} 141 142#endif // SDL_THREAD_N3DS 143
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.