Commit | Line | Data |
---|---|---|
3719602c PC |
1 | /* Copyright (C) 2010-2020 The RetroArch team |
2 | * | |
3 | * --------------------------------------------------------------------------------------- | |
4 | * The following license statement only applies to this file (psp_pthread.h). | |
5 | * --------------------------------------------------------------------------------------- | |
6 | * | |
7 | * Permission is hereby granted, free of charge, | |
8 | * to any person obtaining a copy of this software and associated documentation files (the "Software"), | |
9 | * to deal in the Software without restriction, including without limitation the rights to | |
10 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
11 | * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
16 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
19 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
21 | */ | |
22 | ||
23 | /* FIXME: unfinished on PSP, mutexes and condition variables basically a stub. */ | |
24 | #ifndef _PSP_PTHREAD_WRAP__ | |
25 | #define _PSP_PTHREAD_WRAP__ | |
26 | ||
27 | #ifdef VITA | |
28 | #include <psp2/kernel/threadmgr.h> | |
29 | #include <sys/time.h> | |
30 | #else | |
31 | #include <pspkernel.h> | |
32 | #include <pspthreadman.h> | |
33 | #include <pspthreadman_kernel.h> | |
34 | #endif | |
35 | #include <stdio.h> | |
36 | #include <retro_inline.h> | |
37 | ||
38 | #define STACKSIZE (8 * 1024) | |
39 | ||
40 | typedef SceUID pthread_t; | |
41 | typedef SceUID pthread_mutex_t; | |
42 | typedef void* pthread_mutexattr_t; | |
43 | typedef int pthread_attr_t; | |
44 | ||
45 | typedef struct | |
46 | { | |
47 | SceUID mutex; | |
48 | SceUID sema; | |
49 | int waiting; | |
50 | } pthread_cond_t; | |
51 | ||
52 | typedef SceUID pthread_condattr_t; | |
53 | ||
54 | /* Use pointer values to create unique names for threads/mutexes */ | |
55 | char name_buffer[256]; | |
56 | ||
57 | typedef void* (*sthreadEntry)(void *argp); | |
58 | ||
59 | typedef struct | |
60 | { | |
61 | void* arg; | |
62 | sthreadEntry start_routine; | |
63 | } sthread_args_struct; | |
64 | ||
65 | static int psp_thread_wrap(SceSize args, void *argp) | |
66 | { | |
67 | sthread_args_struct* sthread_args = (sthread_args_struct*)argp; | |
68 | ||
69 | return (int)sthread_args->start_routine(sthread_args->arg); | |
70 | } | |
71 | ||
72 | static INLINE int pthread_create(pthread_t *thread, | |
73 | const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg) | |
74 | { | |
75 | snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) thread); | |
76 | ||
77 | #ifdef VITA | |
78 | *thread = sceKernelCreateThread(name_buffer, psp_thread_wrap, | |
79 | 0x10000100, 0x10000, 0, 0, NULL); | |
80 | #else | |
81 | *thread = sceKernelCreateThread(name_buffer, | |
82 | psp_thread_wrap, 0x20, STACKSIZE, 0, NULL); | |
83 | #endif | |
84 | ||
85 | sthread_args_struct sthread_args; | |
86 | sthread_args.arg = arg; | |
87 | sthread_args.start_routine = start_routine; | |
88 | ||
89 | return sceKernelStartThread(*thread, sizeof(sthread_args), &sthread_args); | |
90 | } | |
91 | ||
92 | static INLINE int pthread_mutex_init(pthread_mutex_t *mutex, | |
93 | const pthread_mutexattr_t *attr) | |
94 | { | |
95 | snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) mutex); | |
96 | ||
97 | #ifdef VITA | |
98 | *mutex = sceKernelCreateMutex(name_buffer, 0, 0, 0); | |
99 | if (*mutex<0) | |
100 | return *mutex; | |
101 | return 0; | |
102 | #else | |
103 | return *mutex = sceKernelCreateSema(name_buffer, 0, 1, 1, NULL); | |
104 | #endif | |
105 | } | |
106 | ||
107 | static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex) | |
108 | { | |
109 | #ifdef VITA | |
110 | return sceKernelDeleteMutex(*mutex); | |
111 | #else | |
112 | return sceKernelDeleteSema(*mutex); | |
113 | #endif | |
114 | } | |
115 | ||
116 | static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex) | |
117 | { | |
118 | #ifdef VITA | |
119 | int ret = sceKernelLockMutex(*mutex, 1, 0); | |
120 | return ret; | |
121 | ||
122 | #else | |
123 | /* FIXME: stub */ | |
124 | return 1; | |
125 | #endif | |
126 | } | |
127 | ||
128 | static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex) | |
129 | { | |
130 | #ifdef VITA | |
131 | int ret = sceKernelUnlockMutex(*mutex, 1); | |
132 | return ret; | |
133 | #else | |
134 | /* FIXME: stub */ | |
135 | return 1; | |
136 | #endif | |
137 | } | |
138 | ||
139 | static INLINE int pthread_join(pthread_t thread, void **retval) | |
140 | { | |
141 | #ifdef VITA | |
142 | int res = sceKernelWaitThreadEnd(thread, 0, 0); | |
143 | if (res < 0) | |
144 | return res; | |
145 | return sceKernelDeleteThread(thread); | |
146 | #else | |
147 | SceUInt timeout = (SceUInt)-1; | |
148 | sceKernelWaitThreadEnd(thread, &timeout); | |
149 | exit_status = sceKernelGetThreadExitStatus(thread); | |
150 | sceKernelDeleteThread(thread); | |
151 | return exit_status; | |
152 | #endif | |
153 | } | |
154 | ||
155 | static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex) | |
156 | { | |
157 | #ifdef VITA | |
158 | return sceKernelTryLockMutex(*mutex, 1 /* not sure about this last param */); | |
159 | #else | |
160 | /* FIXME: stub */ | |
161 | return 1; | |
162 | #endif | |
163 | } | |
164 | ||
165 | static INLINE int pthread_cond_wait(pthread_cond_t *cond, | |
166 | pthread_mutex_t *mutex) | |
167 | { | |
168 | #ifdef VITA | |
169 | int ret = pthread_mutex_lock(&cond->mutex); | |
170 | if (ret < 0) | |
171 | return ret; | |
172 | ++cond->waiting; | |
173 | pthread_mutex_unlock(mutex); | |
174 | pthread_mutex_unlock(&cond->mutex); | |
175 | ||
176 | ret = sceKernelWaitSema(cond->sema, 1, 0); | |
177 | if (ret < 0) | |
178 | sceClibPrintf("Premature wakeup: %08X", ret); | |
179 | pthread_mutex_lock(mutex); | |
180 | return ret; | |
181 | #else | |
182 | /* FIXME: stub */ | |
183 | sceKernelDelayThread(10000); | |
184 | return 1; | |
185 | #endif | |
186 | } | |
187 | ||
188 | static INLINE int pthread_cond_timedwait(pthread_cond_t *cond, | |
189 | pthread_mutex_t *mutex, const struct timespec *abstime) | |
190 | { | |
191 | #ifdef VITA | |
192 | int ret = pthread_mutex_lock(&cond->mutex); | |
193 | if (ret < 0) | |
194 | return ret; | |
195 | ++cond->waiting; | |
196 | pthread_mutex_unlock(mutex); | |
197 | pthread_mutex_unlock(&cond->mutex); | |
198 | ||
199 | SceUInt timeout = 0; | |
200 | ||
201 | timeout = abstime->tv_sec; | |
202 | timeout += abstime->tv_nsec / 1.0e6; | |
203 | ||
204 | ret = sceKernelWaitSema(cond->sema, 1, &timeout); | |
205 | if (ret < 0) | |
206 | sceClibPrintf("Premature wakeup: %08X", ret); | |
207 | pthread_mutex_lock(mutex); | |
208 | return ret; | |
209 | ||
210 | #else | |
211 | /* FIXME: stub */ | |
212 | return 1; | |
213 | #endif | |
214 | } | |
215 | ||
216 | static INLINE int pthread_cond_init(pthread_cond_t *cond, | |
217 | const pthread_condattr_t *attr) | |
218 | { | |
219 | #ifdef VITA | |
220 | ||
221 | pthread_mutex_init(&cond->mutex,NULL); | |
222 | ||
223 | if (cond->mutex<0) | |
224 | return cond->mutex; | |
225 | ||
226 | snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) cond); | |
227 | cond->sema = sceKernelCreateSema(name_buffer, 0, 0, 1, 0); | |
228 | ||
229 | if (cond->sema < 0) | |
230 | { | |
231 | pthread_mutex_destroy(&cond->mutex); | |
232 | return cond->sema; | |
233 | } | |
234 | ||
235 | cond->waiting = 0; | |
236 | ||
237 | return 0; | |
238 | ||
239 | #else | |
240 | /* FIXME: stub */ | |
241 | return 1; | |
242 | #endif | |
243 | } | |
244 | ||
245 | static INLINE int pthread_cond_signal(pthread_cond_t *cond) | |
246 | { | |
247 | #ifdef VITA | |
248 | pthread_mutex_lock(&cond->mutex); | |
249 | if (cond->waiting) | |
250 | { | |
251 | --cond->waiting; | |
252 | sceKernelSignalSema(cond->sema, 1); | |
253 | } | |
254 | pthread_mutex_unlock(&cond->mutex); | |
255 | return 0; | |
256 | #else | |
257 | /* FIXME: stub */ | |
258 | return 1; | |
259 | #endif | |
260 | } | |
261 | ||
262 | static INLINE int pthread_cond_broadcast(pthread_cond_t *cond) | |
263 | { | |
264 | /* FIXME: stub */ | |
265 | return 1; | |
266 | } | |
267 | ||
268 | static INLINE int pthread_cond_destroy(pthread_cond_t *cond) | |
269 | { | |
270 | #ifdef VITA | |
271 | int ret = sceKernelDeleteSema(cond->sema); | |
272 | if (ret < 0) | |
273 | return ret; | |
274 | ||
275 | return sceKernelDeleteMutex(cond->mutex); | |
276 | #else | |
277 | /* FIXME: stub */ | |
278 | return 1; | |
279 | #endif | |
280 | } | |
281 | ||
282 | static INLINE int pthread_detach(pthread_t thread) | |
283 | { | |
284 | return 0; | |
285 | } | |
286 | ||
287 | static INLINE void pthread_exit(void *retval) | |
288 | { | |
289 | #ifdef VITA | |
290 | sceKernelExitDeleteThread(sceKernelGetThreadId()); | |
291 | #endif | |
292 | } | |
293 | ||
294 | static INLINE pthread_t pthread_self(void) | |
295 | { | |
296 | /* zero 20-mar-2016: untested */ | |
297 | return sceKernelGetThreadId(); | |
298 | } | |
299 | ||
300 | static INLINE int pthread_equal(pthread_t t1, pthread_t t2) | |
301 | { | |
302 | return t1 == t2; | |
303 | } | |
304 | ||
305 | #endif //_PSP_PTHREAD_WRAP__ |