SDL
1/*
2Simple DirectMedia Layer
3Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5This software is provided 'as-is', without any express or implied
6warranty. In no event will the authors be held liable for any damages
7arising from the use of this software.
8
9Permission is granted to anyone to use this software for any purpose,
10including commercial applications, and to alter it and redistribute it
11freely, subject to the following restrictions:
12
131. The origin of this software must not be misrepresented; you must not
14claim that you wrote the original software. If you use this software
15in a product, an acknowledgment in the product documentation would be
16appreciated but is not required.
172. Altered source versions must be plainly marked as such, and must not be
18misrepresented as being the original software.
193. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"22
23#ifdef SDL_THREAD_PS224
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
34static void FinishThread(SDL_Thread *thread)35{
36ee_thread_status_t info;37int res;38
39res = ReferThreadStatus(thread->handle, &info);40TerminateThread(thread->handle);41DeleteThread(thread->handle);42DeleteSema((int)thread->endfunc);43
44if (res > 0) {45SDL_free(info.stack);46}47}
48
49static int childThread(void *arg)50{
51SDL_Thread *thread = (SDL_Thread *)arg;52int res = thread->userfunc(thread->userdata);53SignalSema((int)thread->endfunc);54return res;55}
56
57bool SDL_SYS_CreateThread(SDL_Thread *thread,58SDL_FunctionPointer pfnBeginThread,59SDL_FunctionPointer pfnEndThread)60{
61ee_thread_status_t status;62ee_thread_t eethread;63ee_sema_t sema;64size_t stack_size;65int priority = 32;66
67// Set priority of new thread to the same as the current thread68// status.size = sizeof(ee_thread_t);69if (ReferThreadStatus(GetThreadId(), &status) == 0) {70priority = status.current_priority;71}72
73stack_size = thread->stacksize ? ((int)thread->stacksize) : 0x1800;74
75// Create EE Thread76eethread.attr = 0;77eethread.option = 0;78eethread.func = &childThread;79eethread.stack = SDL_malloc(stack_size);80eethread.stack_size = stack_size;81eethread.gp_reg = &_gp;82eethread.initial_priority = priority;83thread->handle = CreateThread(&eethread);84
85if (thread->handle < 0) {86return SDL_SetError("CreateThread() failed");87}88
89// Prepare el semaphore for the ending function90sema.init_count = 0;91sema.max_count = 1;92sema.option = 0;93thread->endfunc = (void *)CreateSema(&sema);94
95if (StartThread(thread->handle, thread) < 0) {96return SDL_SetError("StartThread() failed");97}98return true;99}
100
101void SDL_SYS_SetupThread(const char *name)102{
103// Do nothing.104}
105
106SDL_ThreadID SDL_GetCurrentThreadID(void)107{
108return (SDL_ThreadID)GetThreadId();109}
110
111void SDL_SYS_WaitThread(SDL_Thread *thread)112{
113WaitSema((int)thread->endfunc);114ReleaseWaitThread(thread->handle);115FinishThread(thread);116}
117
118void SDL_SYS_DetachThread(SDL_Thread *thread)119{
120// Do nothing.121}
122
123bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)124{
125int value;126
127if (priority == SDL_THREAD_PRIORITY_LOW) {128value = 111;129} else if (priority == SDL_THREAD_PRIORITY_HIGH) {130value = 32;131} else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {132value = 16;133} else {134value = 50;135}136
137if (ChangeThreadPriority(GetThreadId(), value) < 0) {138return SDL_SetError("ChangeThreadPriority() failed");139}140return true;141}
142
143#endif // SDL_THREAD_PS2144