summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/thread/psp
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/psp
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/thread/psp')
-rw-r--r--contrib/SDL-3.2.8/src/thread/psp/SDL_sysmutex.c100
-rw-r--r--contrib/SDL-3.2.8/src/thread/psp/SDL_sysmutex_c.h21
-rw-r--r--contrib/SDL-3.2.8/src/thread/psp/SDL_syssem.c119
-rw-r--r--contrib/SDL-3.2.8/src/thread/psp/SDL_systhread.c112
-rw-r--r--contrib/SDL-3.2.8/src/thread/psp/SDL_systhread_c.h24
5 files changed, 376 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/thread/psp/SDL_sysmutex.c b/contrib/SDL-3.2.8/src/thread/psp/SDL_sysmutex.c
new file mode 100644
index 0000000..c8ed80d
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/thread/psp/SDL_sysmutex.c
@@ -0,0 +1,100 @@
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_PSP
24
25// An implementation of mutexes using semaphores
26
27#include "SDL_systhread_c.h"
28
29#include <pspthreadman.h>
30#include <pspkerror.h>
31
32#define SCE_KERNEL_MUTEX_ATTR_RECURSIVE 0x0200U
33
34struct SDL_Mutex
35{
36 SceLwMutexWorkarea lock;
37};
38
39SDL_Mutex *SDL_CreateMutex(void)
40{
41 SDL_Mutex *mutex = (SDL_Mutex *)SDL_malloc(sizeof(*mutex));
42 if (mutex) {
43 const SceInt32 res = sceKernelCreateLwMutex(
44 &mutex->lock,
45 "SDL mutex",
46 SCE_KERNEL_MUTEX_ATTR_RECURSIVE,
47 0,
48 NULL);
49
50 if (res < 0) {
51 SDL_free(mutex);
52 mutex = NULL;
53 SDL_SetError("Error trying to create mutex: %lx", res);
54 }
55 }
56 return mutex;
57}
58
59void SDL_DestroyMutex(SDL_Mutex *mutex)
60{
61 if (mutex) {
62 sceKernelDeleteLwMutex(&mutex->lock);
63 SDL_free(mutex);
64 }
65}
66
67void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
68{
69 if (mutex) {
70 const SceInt32 res = sceKernelLockLwMutex(&mutex->lock, 1, NULL);
71 SDL_assert(res == SCE_KERNEL_ERROR_OK); // assume we're in a lot of trouble if this assert fails.
72 }
73}
74
75bool SDL_TryLockMutex(SDL_Mutex *mutex)
76{
77 bool result = true;
78 if (mutex) {
79 const SceInt32 res = sceKernelTryLockLwMutex(&mutex->lock, 1);
80 if (res == SCE_KERNEL_ERROR_OK) {
81 result = true;
82 } else if (res == SCE_KERNEL_ERROR_WAIT_TIMEOUT) {
83 result = false;
84 } else {
85 SDL_assert(res == SCE_KERNEL_ERROR_OK); // assume we're in a lot of trouble if this assert fails.
86 result = false;
87 }
88 }
89 return result;
90}
91
92void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
93{
94 if (mutex) {
95 const SceInt32 res = sceKernelUnlockLwMutex(&mutex->lock, 1);
96 SDL_assert(res == SCE_KERNEL_ERROR_OK); // assume we're in a lot of trouble if this assert fails.
97 }
98}
99
100#endif // SDL_THREAD_PSP
diff --git a/contrib/SDL-3.2.8/src/thread/psp/SDL_sysmutex_c.h b/contrib/SDL-3.2.8/src/thread/psp/SDL_sysmutex_c.h
new file mode 100644
index 0000000..4b0c6f8
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/thread/psp/SDL_sysmutex_c.h
@@ -0,0 +1,21 @@
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"
diff --git a/contrib/SDL-3.2.8/src/thread/psp/SDL_syssem.c b/contrib/SDL-3.2.8/src/thread/psp/SDL_syssem.c
new file mode 100644
index 0000000..27e332f
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/thread/psp/SDL_syssem.c
@@ -0,0 +1,119 @@
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_PSP
24
25// Semaphore functions for the PSP.
26
27#include <stdio.h>
28#include <stdlib.h>
29
30#include <pspthreadman.h>
31#include <pspkerror.h>
32
33struct SDL_Semaphore
34{
35 SceUID semid;
36};
37
38// Create a semaphore
39SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
40{
41 SDL_Semaphore *sem;
42
43 sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem));
44 if (sem) {
45 // TODO: Figure out the limit on the maximum value.
46 sem->semid = sceKernelCreateSema("SDL sema", 0, initial_value, 255, NULL);
47 if (sem->semid < 0) {
48 SDL_SetError("Couldn't create semaphore");
49 SDL_free(sem);
50 sem = NULL;
51 }
52 }
53
54 return sem;
55}
56
57// Free the semaphore
58void SDL_DestroySemaphore(SDL_Semaphore *sem)
59{
60 if (sem) {
61 if (sem->semid > 0) {
62 sceKernelDeleteSema(sem->semid);
63 sem->semid = 0;
64 }
65
66 SDL_free(sem);
67 }
68}
69
70/* TODO: This routine is a bit overloaded.
71 * If the timeout is 0 then just poll the semaphore; if it's -1, pass
72 * NULL to sceKernelWaitSema() so that it waits indefinitely; and if the timeout
73 * is specified, convert it to microseconds. */
74bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
75{
76 SceUInt timeoutUS;
77 SceUInt *pTimeout = NULL;
78
79 if (!sem) {
80 return true;
81 }
82
83 if (timeoutNS == 0) {
84 return (sceKernelPollSema(sem->semid, 1) == 0);
85 }
86
87 if (timeoutNS > 0) {
88 timeoutUS = (SceUInt)SDL_NS_TO_US(timeoutNS); // Convert to microseconds.
89 pTimeout = &timeoutUS;
90 }
91
92 return (sceKernelWaitSema(sem->semid, 1, pTimeout) == 0);
93}
94
95// Returns the current count of the semaphore
96Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)
97{
98 SceKernelSemaInfo info;
99
100 if (!sem) {
101 return 0;
102 }
103
104 if (sceKernelReferSemaStatus(sem->semid, &info) == 0) {
105 return info.currentCount;
106 }
107 return 0;
108}
109
110void SDL_SignalSemaphore(SDL_Semaphore *sem)
111{
112 if (!sem) {
113 return;
114 }
115
116 sceKernelSignalSema(sem->semid, 1);
117}
118
119#endif // SDL_THREAD_PSP
diff --git a/contrib/SDL-3.2.8/src/thread/psp/SDL_systhread.c b/contrib/SDL-3.2.8/src/thread/psp/SDL_systhread.c
new file mode 100644
index 0000000..3d60718
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/thread/psp/SDL_systhread.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_PSP
24
25// PSP thread management routines for SDL
26
27#include <stdio.h>
28#include <stdlib.h>
29
30#include "../SDL_systhread.h"
31#include "../SDL_thread_c.h"
32#include <pspkerneltypes.h>
33#include <pspthreadman.h>
34
35static int ThreadEntry(SceSize args, void *argp)
36{
37 SDL_RunThread(*(SDL_Thread **)argp);
38 return 0;
39}
40
41bool SDL_SYS_CreateThread(SDL_Thread *thread,
42 SDL_FunctionPointer pfnBeginThread,
43 SDL_FunctionPointer pfnEndThread)
44{
45 SceKernelThreadInfo status;
46 int priority = 32;
47
48 // Set priority of new thread to the same as the current thread
49 status.size = sizeof(SceKernelThreadInfo);
50 if (sceKernelReferThreadStatus(sceKernelGetThreadId(), &status) == 0) {
51 priority = status.currentPriority;
52 }
53
54 thread->handle = sceKernelCreateThread(thread->name, ThreadEntry,
55 priority, thread->stacksize ? ((int)thread->stacksize) : 0x8000,
56 PSP_THREAD_ATTR_VFPU, NULL);
57 if (thread->handle < 0) {
58 return SDL_SetError("sceKernelCreateThread() failed");
59 }
60
61 sceKernelStartThread(thread->handle, 4, &thread);
62 return true;
63}
64
65void SDL_SYS_SetupThread(const char *name)
66{
67 // Do nothing.
68}
69
70SDL_ThreadID SDL_GetCurrentThreadID(void)
71{
72 return (SDL_ThreadID)sceKernelGetThreadId();
73}
74
75void SDL_SYS_WaitThread(SDL_Thread *thread)
76{
77 sceKernelWaitThreadEnd(thread->handle, NULL);
78 sceKernelDeleteThread(thread->handle);
79}
80
81void SDL_SYS_DetachThread(SDL_Thread *thread)
82{
83 // !!! FIXME: is this correct?
84 sceKernelDeleteThread(thread->handle);
85}
86
87void SDL_SYS_KillThread(SDL_Thread *thread)
88{
89 sceKernelTerminateDeleteThread(thread->handle);
90}
91
92bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
93{
94 int value;
95
96 if (priority == SDL_THREAD_PRIORITY_LOW) {
97 value = 111;
98 } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
99 value = 32;
100 } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
101 value = 16;
102 } else {
103 value = 50;
104 }
105
106 if (sceKernelChangeThreadPriority(sceKernelGetThreadId(), value) < 0) {
107 return SDL_SetError("sceKernelChangeThreadPriority() failed");
108 }
109 return true;
110}
111
112#endif // SDL_THREAD_PSP
diff --git a/contrib/SDL-3.2.8/src/thread/psp/SDL_systhread_c.h b/contrib/SDL-3.2.8/src/thread/psp/SDL_systhread_c.h
new file mode 100644
index 0000000..9e951ec
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/thread/psp/SDL_systhread_c.h
@@ -0,0 +1,24 @@
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
22#include <pspkerneltypes.h>
23
24typedef SceUID SYS_ThreadHandle;