1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 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 #if SDL_THREAD_PSP
24 
25 /* PSP thread management routines for SDL */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #include "SDL_error.h"
31 #include "SDL_thread.h"
32 #include "../SDL_systhread.h"
33 #include "../SDL_thread_c.h"
34 #include <pspkerneltypes.h>
35 #include <pspthreadman.h>
36 
37 
ThreadEntry(SceSize args,void * argp)38 static int ThreadEntry(SceSize args, void *argp)
39 {
40     SDL_RunThread(*(SDL_Thread **) argp);
41     return 0;
42 }
43 
SDL_SYS_CreateThread(SDL_Thread * thread)44 int SDL_SYS_CreateThread(SDL_Thread *thread)
45 {
46     SceKernelThreadInfo status;
47     int priority = 32;
48 
49     /* Set priority of new thread to the same as the current thread */
50     status.size = sizeof(SceKernelThreadInfo);
51     if (sceKernelReferThreadStatus(sceKernelGetThreadId(), &status) == 0) {
52         priority = status.currentPriority;
53     }
54 
55     thread->handle = sceKernelCreateThread(thread->name, ThreadEntry,
56                            priority, thread->stacksize ? ((int) thread->stacksize) : 0x8000,
57                            PSP_THREAD_ATTR_VFPU, NULL);
58     if (thread->handle < 0) {
59         return SDL_SetError("sceKernelCreateThread() failed");
60     }
61 
62     sceKernelStartThread(thread->handle, 4, &thread);
63     return 0;
64 }
65 
SDL_SYS_SetupThread(const char * name)66 void SDL_SYS_SetupThread(const char *name)
67 {
68     /* Do nothing. */
69 }
70 
SDL_ThreadID(void)71 SDL_threadID SDL_ThreadID(void)
72 {
73     return (SDL_threadID) sceKernelGetThreadId();
74 }
75 
SDL_SYS_WaitThread(SDL_Thread * thread)76 void SDL_SYS_WaitThread(SDL_Thread *thread)
77 {
78     sceKernelWaitThreadEnd(thread->handle, NULL);
79     sceKernelDeleteThread(thread->handle);
80 }
81 
SDL_SYS_DetachThread(SDL_Thread * thread)82 void SDL_SYS_DetachThread(SDL_Thread *thread)
83 {
84     /* !!! FIXME: is this correct? */
85     sceKernelDeleteThread(thread->handle);
86 }
87 
SDL_SYS_KillThread(SDL_Thread * thread)88 void SDL_SYS_KillThread(SDL_Thread *thread)
89 {
90     sceKernelTerminateDeleteThread(thread->handle);
91 }
92 
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)93 int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
94 {
95     int value;
96 
97     if (priority == SDL_THREAD_PRIORITY_LOW) {
98         value = 19;
99     } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
100         value = -10;
101     } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
102         value = -20;
103     } else {
104         value = 0;
105     }
106 
107     return sceKernelChangeThreadPriority(sceKernelGetThreadId(),value);
108 
109 }
110 
111 #endif /* SDL_THREAD_PSP */
112 
113 /* vim: ts=4 sw=4
114  */
115