diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/thread/vita | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/thread/vita')
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/vita/SDL_sysmutex.c | 97 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/vita/SDL_sysmutex_c.h | 21 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/vita/SDL_syssem.c | 122 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/vita/SDL_systhread.c | 139 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/vita/SDL_systhread_c.h | 24 |
5 files changed, 403 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/thread/vita/SDL_sysmutex.c b/contrib/SDL-3.2.8/src/thread/vita/SDL_sysmutex.c new file mode 100644 index 0000000..8a3664c --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/vita/SDL_sysmutex.c | |||
| @@ -0,0 +1,97 @@ | |||
| 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_VITA | ||
| 24 | |||
| 25 | #include "SDL_systhread_c.h" | ||
| 26 | |||
| 27 | #include <psp2/kernel/threadmgr.h> | ||
| 28 | #include <psp2/kernel/error.h> | ||
| 29 | |||
| 30 | struct SDL_Mutex | ||
| 31 | { | ||
| 32 | SceKernelLwMutexWork lock; | ||
| 33 | }; | ||
| 34 | |||
| 35 | SDL_Mutex *SDL_CreateMutex(void) | ||
| 36 | { | ||
| 37 | SDL_Mutex *mutex = (SDL_Mutex *)SDL_malloc(sizeof(*mutex)); | ||
| 38 | if (mutex) { | ||
| 39 | const SceInt32 res = sceKernelCreateLwMutex( | ||
| 40 | &mutex->lock, | ||
| 41 | "SDL mutex", | ||
| 42 | SCE_KERNEL_MUTEX_ATTR_RECURSIVE, | ||
| 43 | 0, | ||
| 44 | NULL); | ||
| 45 | |||
| 46 | if (res < 0) { | ||
| 47 | SDL_free(mutex); | ||
| 48 | mutex = NULL; | ||
| 49 | SDL_SetError("Error trying to create mutex: %x", res); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | return mutex; | ||
| 53 | } | ||
| 54 | |||
| 55 | void SDL_DestroyMutex(SDL_Mutex *mutex) | ||
| 56 | { | ||
| 57 | if (mutex) { | ||
| 58 | sceKernelDeleteLwMutex(&mutex->lock); | ||
| 59 | SDL_free(mutex); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes | ||
| 64 | { | ||
| 65 | if (mutex) { | ||
| 66 | const SceInt32 res = sceKernelLockLwMutex(&mutex->lock, 1, NULL); | ||
| 67 | SDL_assert(res == SCE_KERNEL_OK); // assume we're in a lot of trouble if this assert fails. | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | bool SDL_TryLockMutex(SDL_Mutex *mutex) | ||
| 72 | { | ||
| 73 | bool result = true; | ||
| 74 | |||
| 75 | if (mutex) { | ||
| 76 | const SceInt32 res = sceKernelTryLockLwMutex(&mutex->lock, 1); | ||
| 77 | if (res == SCE_KERNEL_OK) { | ||
| 78 | result = true; | ||
| 79 | } else if (res == SCE_KERNEL_ERROR_MUTEX_FAILED_TO_OWN) { | ||
| 80 | result = false; | ||
| 81 | } else { | ||
| 82 | SDL_assert(res == SCE_KERNEL_OK); // assume we're in a lot of trouble if this assert fails. | ||
| 83 | result = false; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | return result; | ||
| 87 | } | ||
| 88 | |||
| 89 | void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes | ||
| 90 | { | ||
| 91 | if (mutex) { | ||
| 92 | const SceInt32 res = sceKernelUnlockLwMutex(&mutex->lock, 1); | ||
| 93 | SDL_assert(res == SCE_KERNEL_OK); // assume we're in a lot of trouble if this assert fails. | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | #endif // SDL_THREAD_VITA | ||
diff --git a/contrib/SDL-3.2.8/src/thread/vita/SDL_sysmutex_c.h b/contrib/SDL-3.2.8/src/thread/vita/SDL_sysmutex_c.h new file mode 100644 index 0000000..4b0c6f8 --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/vita/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/vita/SDL_syssem.c b/contrib/SDL-3.2.8/src/thread/vita/SDL_syssem.c new file mode 100644 index 0000000..3c6d28a --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/vita/SDL_syssem.c | |||
| @@ -0,0 +1,122 @@ | |||
| 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_VITA | ||
| 24 | |||
| 25 | // Semaphore functions for the VITA. | ||
| 26 | |||
| 27 | #include <stdio.h> | ||
| 28 | #include <stdlib.h> | ||
| 29 | |||
| 30 | #include <psp2/types.h> | ||
| 31 | #include <psp2/kernel/error.h> | ||
| 32 | #include <psp2/kernel/threadmgr.h> | ||
| 33 | |||
| 34 | struct SDL_Semaphore | ||
| 35 | { | ||
| 36 | SceUID semid; | ||
| 37 | }; | ||
| 38 | |||
| 39 | // Create a semaphore | ||
| 40 | SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value) | ||
| 41 | { | ||
| 42 | SDL_Semaphore *sem; | ||
| 43 | |||
| 44 | sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem)); | ||
| 45 | if (sem) { | ||
| 46 | // TODO: Figure out the limit on the maximum value. | ||
| 47 | sem->semid = sceKernelCreateSema("SDL sema", 0, initial_value, 255, NULL); | ||
| 48 | if (sem->semid < 0) { | ||
| 49 | SDL_SetError("Couldn't create semaphore"); | ||
| 50 | SDL_free(sem); | ||
| 51 | sem = NULL; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | return sem; | ||
| 56 | } | ||
| 57 | |||
| 58 | // Free the semaphore | ||
| 59 | void SDL_DestroySemaphore(SDL_Semaphore *sem) | ||
| 60 | { | ||
| 61 | if (sem) { | ||
| 62 | if (sem->semid > 0) { | ||
| 63 | sceKernelDeleteSema(sem->semid); | ||
| 64 | sem->semid = 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | SDL_free(sem); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | /* TODO: This routine is a bit overloaded. | ||
| 72 | * If the timeout is 0 then just poll the semaphore; if it's -1, pass | ||
| 73 | * NULL to sceKernelWaitSema() so that it waits indefinitely; and if the timeout | ||
| 74 | * is specified, convert it to microseconds. */ | ||
| 75 | bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS) | ||
| 76 | { | ||
| 77 | SceUInt timeoutUS; | ||
| 78 | SceUInt *pTimeout = NULL; | ||
| 79 | |||
| 80 | if (!sem) { | ||
| 81 | return true; | ||
| 82 | } | ||
| 83 | |||
| 84 | if (timeoutNS == 0) { | ||
| 85 | return (sceKernelPollSema(sem->semid, 1) == 0); | ||
| 86 | } | ||
| 87 | |||
| 88 | if (timeoutNS > 0) { | ||
| 89 | timeoutUS = (SceUInt)SDL_NS_TO_US(timeoutNS); // Convert to microseconds. | ||
| 90 | pTimeout = &timeoutUS; | ||
| 91 | } | ||
| 92 | |||
| 93 | return (sceKernelWaitSema(sem->semid, 1, pTimeout) == 0); | ||
| 94 | } | ||
| 95 | |||
| 96 | // Returns the current count of the semaphore | ||
| 97 | Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem) | ||
| 98 | { | ||
| 99 | SceKernelSemaInfo info; | ||
| 100 | info.size = sizeof(info); | ||
| 101 | |||
| 102 | if (!sem) { | ||
| 103 | return 0; | ||
| 104 | } | ||
| 105 | |||
| 106 | if (sceKernelGetSemaInfo(sem->semid, &info) >= 0) { | ||
| 107 | return info.currentCount; | ||
| 108 | } | ||
| 109 | |||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | void SDL_SignalSemaphore(SDL_Semaphore *sem) | ||
| 114 | { | ||
| 115 | if (!sem) { | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | sceKernelSignalSema(sem->semid, 1); | ||
| 120 | } | ||
| 121 | |||
| 122 | #endif // SDL_THREAD_VITA | ||
diff --git a/contrib/SDL-3.2.8/src/thread/vita/SDL_systhread.c b/contrib/SDL-3.2.8/src/thread/vita/SDL_systhread.c new file mode 100644 index 0000000..344d911 --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/vita/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_VITA | ||
| 24 | |||
| 25 | // VITA 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 <psp2/types.h> | ||
| 33 | #include <psp2/kernel/threadmgr.h> | ||
| 34 | |||
| 35 | #define VITA_THREAD_STACK_SIZE_MIN 0x1000 // 4KiB | ||
| 36 | #define VITA_THREAD_STACK_SIZE_MAX 0x2000000 // 32MiB | ||
| 37 | #define VITA_THREAD_STACK_SIZE_DEFAULT 0x10000 // 64KiB | ||
| 38 | #define VITA_THREAD_NAME_MAX 32 | ||
| 39 | |||
| 40 | #define VITA_THREAD_PRIORITY_LOW 191 | ||
| 41 | #define VITA_THREAD_PRIORITY_NORMAL 160 | ||
| 42 | #define VITA_THREAD_PRIORITY_HIGH 112 | ||
| 43 | #define VITA_THREAD_PRIORITY_TIME_CRITICAL 64 | ||
| 44 | |||
| 45 | static int ThreadEntry(SceSize args, void *argp) | ||
| 46 | { | ||
| 47 | SDL_RunThread(*(SDL_Thread **)argp); | ||
| 48 | return 0; | ||
| 49 | } | ||
| 50 | |||
| 51 | bool SDL_SYS_CreateThread(SDL_Thread *thread, | ||
| 52 | SDL_FunctionPointer pfnBeginThread, | ||
| 53 | SDL_FunctionPointer pfnEndThread) | ||
| 54 | |||
| 55 | { | ||
| 56 | char thread_name[VITA_THREAD_NAME_MAX]; | ||
| 57 | size_t stack_size = VITA_THREAD_STACK_SIZE_DEFAULT; | ||
| 58 | |||
| 59 | SDL_strlcpy(thread_name, "SDL thread", VITA_THREAD_NAME_MAX); | ||
| 60 | if (thread->name) { | ||
| 61 | SDL_strlcpy(thread_name, thread->name, VITA_THREAD_NAME_MAX); | ||
| 62 | } | ||
| 63 | |||
| 64 | if (thread->stacksize) { | ||
| 65 | if (thread->stacksize < VITA_THREAD_STACK_SIZE_MIN) { | ||
| 66 | thread->stacksize = VITA_THREAD_STACK_SIZE_MIN; | ||
| 67 | } | ||
| 68 | if (thread->stacksize > VITA_THREAD_STACK_SIZE_MAX) { | ||
| 69 | thread->stacksize = VITA_THREAD_STACK_SIZE_MAX; | ||
| 70 | } | ||
| 71 | stack_size = thread->stacksize; | ||
| 72 | } | ||
| 73 | |||
| 74 | // Create new thread with the same priority as the current thread | ||
| 75 | thread->handle = sceKernelCreateThread( | ||
| 76 | thread_name, // name | ||
| 77 | ThreadEntry, // function to run | ||
| 78 | 0, // priority. 0 means priority of calling thread | ||
| 79 | stack_size, // stack size | ||
| 80 | 0, // attributes. always 0 | ||
| 81 | 0, // cpu affinity mask. 0 = all CPUs | ||
| 82 | NULL // opt. always NULL | ||
| 83 | ); | ||
| 84 | |||
| 85 | if (thread->handle < 0) { | ||
| 86 | return SDL_SetError("sceKernelCreateThread() failed"); | ||
| 87 | } | ||
| 88 | |||
| 89 | sceKernelStartThread(thread->handle, 4, &thread); | ||
| 90 | return true; | ||
| 91 | } | ||
| 92 | |||
| 93 | void SDL_SYS_SetupThread(const char *name) | ||
| 94 | { | ||
| 95 | // Do nothing. | ||
| 96 | } | ||
| 97 | |||
| 98 | SDL_ThreadID SDL_GetCurrentThreadID(void) | ||
| 99 | { | ||
| 100 | return (SDL_ThreadID)sceKernelGetThreadId(); | ||
| 101 | } | ||
| 102 | |||
| 103 | void SDL_SYS_WaitThread(SDL_Thread *thread) | ||
| 104 | { | ||
| 105 | sceKernelWaitThreadEnd(thread->handle, NULL, NULL); | ||
| 106 | sceKernelDeleteThread(thread->handle); | ||
| 107 | } | ||
| 108 | |||
| 109 | void SDL_SYS_DetachThread(SDL_Thread *thread) | ||
| 110 | { | ||
| 111 | // Do nothing. | ||
| 112 | } | ||
| 113 | |||
| 114 | bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority) | ||
| 115 | { | ||
| 116 | int value = VITA_THREAD_PRIORITY_NORMAL; | ||
| 117 | |||
| 118 | switch (priority) { | ||
| 119 | case SDL_THREAD_PRIORITY_LOW: | ||
| 120 | value = VITA_THREAD_PRIORITY_LOW; | ||
| 121 | break; | ||
| 122 | case SDL_THREAD_PRIORITY_NORMAL: | ||
| 123 | value = VITA_THREAD_PRIORITY_NORMAL; | ||
| 124 | break; | ||
| 125 | case SDL_THREAD_PRIORITY_HIGH: | ||
| 126 | value = VITA_THREAD_PRIORITY_HIGH; | ||
| 127 | break; | ||
| 128 | case SDL_THREAD_PRIORITY_TIME_CRITICAL: | ||
| 129 | value = VITA_THREAD_PRIORITY_TIME_CRITICAL; | ||
| 130 | break; | ||
| 131 | } | ||
| 132 | |||
| 133 | if (sceKernelChangeThreadPriority(0, value) < 0) { | ||
| 134 | return SDL_SetError("sceKernelChangeThreadPriority() failed"); | ||
| 135 | } | ||
| 136 | return true; | ||
| 137 | } | ||
| 138 | |||
| 139 | #endif // SDL_THREAD_VITA | ||
diff --git a/contrib/SDL-3.2.8/src/thread/vita/SDL_systhread_c.h b/contrib/SDL-3.2.8/src/thread/vita/SDL_systhread_c.h new file mode 100644 index 0000000..b231ec7 --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/vita/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 <psp2/types.h> | ||
| 23 | |||
| 24 | typedef SceUID SYS_ThreadHandle; | ||
