diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/thread/ps2')
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/ps2/SDL_syssem.c | 122 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/ps2/SDL_systhread.c | 143 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/ps2/SDL_systhread_c.h | 22 |
3 files changed, 287 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/thread/ps2/SDL_syssem.c b/contrib/SDL-3.2.8/src/thread/ps2/SDL_syssem.c new file mode 100644 index 0000000..ccd09c0 --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/ps2/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_PS2 | ||
| 24 | |||
| 25 | // Semaphore functions for the PS2. | ||
| 26 | |||
| 27 | #include <stdio.h> | ||
| 28 | #include <stdlib.h> | ||
| 29 | #include <kernel_util.h> | ||
| 30 | |||
| 31 | #include <kernel.h> | ||
| 32 | |||
| 33 | struct SDL_Semaphore | ||
| 34 | { | ||
| 35 | s32 semid; | ||
| 36 | }; | ||
| 37 | |||
| 38 | // Create a semaphore | ||
| 39 | SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value) | ||
| 40 | { | ||
| 41 | SDL_Semaphore *sem; | ||
| 42 | ee_sema_t sema; | ||
| 43 | |||
| 44 | sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem)); | ||
| 45 | if (sem) { | ||
| 46 | // TODO: Figure out the limit on the maximum value. | ||
| 47 | sema.init_count = initial_value; | ||
| 48 | sema.max_count = 255; | ||
| 49 | sema.option = 0; | ||
| 50 | sem->semid = CreateSema(&sema); | ||
| 51 | |||
| 52 | if (sem->semid < 0) { | ||
| 53 | SDL_SetError("Couldn't create semaphore"); | ||
| 54 | SDL_free(sem); | ||
| 55 | sem = NULL; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | return sem; | ||
| 60 | } | ||
| 61 | |||
| 62 | // Free the semaphore | ||
| 63 | void SDL_DestroySemaphore(SDL_Semaphore *sem) | ||
| 64 | { | ||
| 65 | if (sem) { | ||
| 66 | if (sem->semid > 0) { | ||
| 67 | DeleteSema(sem->semid); | ||
| 68 | sem->semid = 0; | ||
| 69 | } | ||
| 70 | |||
| 71 | SDL_free(sem); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS) | ||
| 76 | { | ||
| 77 | u64 timeout_usec; | ||
| 78 | u64 *timeout_ptr; | ||
| 79 | |||
| 80 | if (!sem) { | ||
| 81 | return true; | ||
| 82 | } | ||
| 83 | |||
| 84 | if (timeoutNS == 0) { | ||
| 85 | return (PollSema(sem->semid) == 0); | ||
| 86 | } | ||
| 87 | |||
| 88 | timeout_ptr = NULL; | ||
| 89 | |||
| 90 | if (timeoutNS != -1) { // -1 == wait indefinitely. | ||
| 91 | timeout_usec = SDL_NS_TO_US(timeoutNS); | ||
| 92 | timeout_ptr = &timeout_usec; | ||
| 93 | } | ||
| 94 | |||
| 95 | return (WaitSemaEx(sem->semid, 1, timeout_ptr) == 0); | ||
| 96 | } | ||
| 97 | |||
| 98 | // Returns the current count of the semaphore | ||
| 99 | Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem) | ||
| 100 | { | ||
| 101 | ee_sema_t info; | ||
| 102 | |||
| 103 | if (!sem) { | ||
| 104 | return 0; | ||
| 105 | } | ||
| 106 | |||
| 107 | if (ReferSemaStatus(sem->semid, &info) == 0) { | ||
| 108 | return info.count; | ||
| 109 | } | ||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | void SDL_SignalSemaphore(SDL_Semaphore *sem) | ||
| 114 | { | ||
| 115 | if (!sem) { | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | SignalSema(sem->semid); | ||
| 120 | } | ||
| 121 | |||
| 122 | #endif // SDL_THREAD_PS2 | ||
diff --git a/contrib/SDL-3.2.8/src/thread/ps2/SDL_systhread.c b/contrib/SDL-3.2.8/src/thread/ps2/SDL_systhread.c new file mode 100644 index 0000000..a13da42 --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/ps2/SDL_systhread.c | |||
| @@ -0,0 +1,143 @@ | |||
| 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_PS2 | ||
| 24 | |||
| 25 | // PS2 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 <kernel.h> | ||
| 33 | |||
| 34 | static void FinishThread(SDL_Thread *thread) | ||
| 35 | { | ||
| 36 | ee_thread_status_t info; | ||
| 37 | int res; | ||
| 38 | |||
| 39 | res = ReferThreadStatus(thread->handle, &info); | ||
| 40 | TerminateThread(thread->handle); | ||
| 41 | DeleteThread(thread->handle); | ||
| 42 | DeleteSema((int)thread->endfunc); | ||
| 43 | |||
| 44 | if (res > 0) { | ||
| 45 | SDL_free(info.stack); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | static int childThread(void *arg) | ||
| 50 | { | ||
| 51 | SDL_Thread *thread = (SDL_Thread *)arg; | ||
| 52 | int res = thread->userfunc(thread->userdata); | ||
| 53 | SignalSema((int)thread->endfunc); | ||
| 54 | return res; | ||
| 55 | } | ||
| 56 | |||
| 57 | bool SDL_SYS_CreateThread(SDL_Thread *thread, | ||
| 58 | SDL_FunctionPointer pfnBeginThread, | ||
| 59 | SDL_FunctionPointer pfnEndThread) | ||
| 60 | { | ||
| 61 | ee_thread_status_t status; | ||
| 62 | ee_thread_t eethread; | ||
| 63 | ee_sema_t sema; | ||
| 64 | size_t stack_size; | ||
| 65 | int priority = 32; | ||
| 66 | |||
| 67 | // Set priority of new thread to the same as the current thread | ||
| 68 | // status.size = sizeof(ee_thread_t); | ||
| 69 | if (ReferThreadStatus(GetThreadId(), &status) == 0) { | ||
| 70 | priority = status.current_priority; | ||
| 71 | } | ||
| 72 | |||
| 73 | stack_size = thread->stacksize ? ((int)thread->stacksize) : 0x1800; | ||
| 74 | |||
| 75 | // Create EE Thread | ||
| 76 | eethread.attr = 0; | ||
| 77 | eethread.option = 0; | ||
| 78 | eethread.func = &childThread; | ||
| 79 | eethread.stack = SDL_malloc(stack_size); | ||
| 80 | eethread.stack_size = stack_size; | ||
| 81 | eethread.gp_reg = &_gp; | ||
| 82 | eethread.initial_priority = priority; | ||
| 83 | thread->handle = CreateThread(&eethread); | ||
| 84 | |||
| 85 | if (thread->handle < 0) { | ||
| 86 | return SDL_SetError("CreateThread() failed"); | ||
| 87 | } | ||
| 88 | |||
| 89 | // Prepare el semaphore for the ending function | ||
| 90 | sema.init_count = 0; | ||
| 91 | sema.max_count = 1; | ||
| 92 | sema.option = 0; | ||
| 93 | thread->endfunc = (void *)CreateSema(&sema); | ||
| 94 | |||
| 95 | if (StartThread(thread->handle, thread) < 0) { | ||
| 96 | return SDL_SetError("StartThread() failed"); | ||
| 97 | } | ||
| 98 | return true; | ||
| 99 | } | ||
| 100 | |||
| 101 | void SDL_SYS_SetupThread(const char *name) | ||
| 102 | { | ||
| 103 | // Do nothing. | ||
| 104 | } | ||
| 105 | |||
| 106 | SDL_ThreadID SDL_GetCurrentThreadID(void) | ||
| 107 | { | ||
| 108 | return (SDL_ThreadID)GetThreadId(); | ||
| 109 | } | ||
| 110 | |||
| 111 | void SDL_SYS_WaitThread(SDL_Thread *thread) | ||
| 112 | { | ||
| 113 | WaitSema((int)thread->endfunc); | ||
| 114 | ReleaseWaitThread(thread->handle); | ||
| 115 | FinishThread(thread); | ||
| 116 | } | ||
| 117 | |||
| 118 | void SDL_SYS_DetachThread(SDL_Thread *thread) | ||
| 119 | { | ||
| 120 | // Do nothing. | ||
| 121 | } | ||
| 122 | |||
| 123 | bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority) | ||
| 124 | { | ||
| 125 | int value; | ||
| 126 | |||
| 127 | if (priority == SDL_THREAD_PRIORITY_LOW) { | ||
| 128 | value = 111; | ||
| 129 | } else if (priority == SDL_THREAD_PRIORITY_HIGH) { | ||
| 130 | value = 32; | ||
| 131 | } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) { | ||
| 132 | value = 16; | ||
| 133 | } else { | ||
| 134 | value = 50; | ||
| 135 | } | ||
| 136 | |||
| 137 | if (ChangeThreadPriority(GetThreadId(), value) < 0) { | ||
| 138 | return SDL_SetError("ChangeThreadPriority() failed"); | ||
| 139 | } | ||
| 140 | return true; | ||
| 141 | } | ||
| 142 | |||
| 143 | #endif // SDL_THREAD_PS2 | ||
diff --git a/contrib/SDL-3.2.8/src/thread/ps2/SDL_systhread_c.h b/contrib/SDL-3.2.8/src/thread/ps2/SDL_systhread_c.h new file mode 100644 index 0000000..2a811ce --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/ps2/SDL_systhread_c.h | |||
| @@ -0,0 +1,22 @@ | |||
| 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 | typedef int32_t SYS_ThreadHandle; | ||
