summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/thread/n3ds
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/thread/n3ds
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/thread/n3ds')
-rw-r--r--contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex.c67
-rw-r--r--contrib/SDL-3.2.8/src/thread/n3ds/SDL_sysmutex_c.h33
-rw-r--r--contrib/SDL-3.2.8/src/thread/n3ds/SDL_syssem.c112
-rw-r--r--contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread.c139
-rw-r--r--contrib/SDL-3.2.8/src/thread/n3ds/SDL_systhread_c.h30
5 files changed, 381 insertions, 0 deletions
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 @@
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// An implementation of mutexes using libctru's RecursiveLock
26
27#include "SDL_sysmutex_c.h"
28
29SDL_Mutex *SDL_CreateMutex(void)
30{
31 SDL_Mutex *mutex = (SDL_Mutex *)SDL_malloc(sizeof(*mutex));
32 if (mutex) {
33 RecursiveLock_Init(&mutex->lock);
34 }
35 return mutex;
36}
37
38void SDL_DestroyMutex(SDL_Mutex *mutex)
39{
40 if (mutex) {
41 SDL_free(mutex);
42 }
43}
44
45void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
46{
47 if (mutex) {
48 RecursiveLock_Lock(&mutex->lock);
49 }
50}
51
52bool SDL_TryLockMutex(SDL_Mutex *mutex)
53{
54 if (mutex) {
55 return RecursiveLock_TryLock(&mutex->lock);
56 }
57 return true;
58}
59
60void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
61{
62 if (mutex) {
63 RecursiveLock_Unlock(&mutex->lock);
64 }
65}
66
67#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 @@
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#ifndef SDL_sysmutex_c_h_
24#define SDL_sysmutex_c_h_
25
26#include <3ds.h>
27
28struct SDL_Mutex
29{
30 RecursiveLock lock;
31};
32
33#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 @@
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// An implementation of semaphores using libctru's LightSemaphore
26
27#include <3ds.h>
28
29struct SDL_Semaphore
30{
31 LightSemaphore semaphore;
32};
33
34SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
35{
36 SDL_Semaphore *sem;
37
38 if (initial_value > SDL_MAX_SINT16) {
39 SDL_SetError("Initial semaphore value too high for this platform");
40 return NULL;
41 }
42
43 sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem));
44 if (!sem) {
45 return NULL;
46 }
47
48 LightSemaphore_Init(&sem->semaphore, initial_value, SDL_MAX_SINT16);
49
50 return sem;
51}
52
53/* WARNING:
54 You cannot call this function when another thread is using the semaphore.
55*/
56void SDL_DestroySemaphore(SDL_Semaphore *sem)
57{
58 SDL_free(sem);
59}
60
61static bool WaitOnSemaphoreFor(SDL_Semaphore *sem, Sint64 timeoutNS)
62{
63 Uint64 stop_time = SDL_GetTicksNS() + timeoutNS;
64 while (SDL_GetTicksNS() < stop_time) {
65 if (LightSemaphore_TryAcquire(&sem->semaphore, 1) == 0) {
66 return true;
67 }
68 // 100 microseconds seems to be the sweet spot
69 SDL_DelayNS(SDL_US_TO_NS(100));
70 }
71
72 // If we failed, yield to avoid starvation on busy waits
73 SDL_DelayNS(1);
74 return false;
75}
76
77bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
78{
79 if (!sem) {
80 return true;
81 }
82
83 if (timeoutNS < 0) { // -1 == wait indefinitely.
84 LightSemaphore_Acquire(&sem->semaphore, 1);
85 return true;
86 }
87
88 if (LightSemaphore_TryAcquire(&sem->semaphore, 1) == 0) {
89 return true;
90 }
91
92 return WaitOnSemaphoreFor(sem, timeoutNS);
93}
94
95Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)
96{
97 if (!sem) {
98 return 0;
99 }
100 return sem->semaphore.current_count;
101}
102
103void SDL_SignalSemaphore(SDL_Semaphore *sem)
104{
105 if (sem) {
106 return;
107 }
108
109 LightSemaphore_Release(&sem->semaphore, 1);
110}
111
112#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 @@
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
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
45
46bool 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
75static 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
84void SDL_SYS_SetupThread(const char *name)
85{
86 return;
87}
88
89SDL_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
96bool 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
121void 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
134void SDL_SYS_DetachThread(SDL_Thread *thread)
135{
136 threadDetach(thread->handle);
137}
138
139#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 @@
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#ifndef SDL_systhread_c_h_
24#define SDL_systhread_c_h_
25
26#include <3ds.h>
27
28typedef Thread SYS_ThreadHandle;
29
30#endif // SDL_systhread_c_h_