summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/thread/ps2/SDL_systhread.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/thread/ps2/SDL_systhread.c')
-rw-r--r--contrib/SDL-3.2.8/src/thread/ps2/SDL_systhread.c143
1 files changed, 143 insertions, 0 deletions
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
34static 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
49static 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
57bool 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
101void SDL_SYS_SetupThread(const char *name)
102{
103 // Do nothing.
104}
105
106SDL_ThreadID SDL_GetCurrentThreadID(void)
107{
108 return (SDL_ThreadID)GetThreadId();
109}
110
111void SDL_SYS_WaitThread(SDL_Thread *thread)
112{
113 WaitSema((int)thread->endfunc);
114 ReleaseWaitThread(thread->handle);
115 FinishThread(thread);
116}
117
118void SDL_SYS_DetachThread(SDL_Thread *thread)
119{
120 // Do nothing.
121}
122
123bool 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