1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (psp_pthread.h).
5 * ---------------------------------------------------------------------------------------
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:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
23 /* FIXME: unfinished on PSP, mutexes and condition variables basically a stub. */
24 #ifndef _PSP_PTHREAD_WRAP__
25 #define _PSP_PTHREAD_WRAP__
28 #include <psp2/kernel/threadmgr.h>
31 #include <pspkernel.h>
32 #include <pspthreadman.h>
33 #include <pspthreadman_kernel.h>
36 #include <retro_inline.h>
38 #define STACKSIZE (8 * 1024)
40 typedef SceUID pthread_t;
41 typedef SceUID pthread_mutex_t;
42 typedef void* pthread_mutexattr_t;
43 typedef int pthread_attr_t;
52 typedef SceUID pthread_condattr_t;
54 /* Use pointer values to create unique names for threads/mutexes */
55 char name_buffer[256];
57 typedef void* (*sthreadEntry)(void *argp);
62 sthreadEntry start_routine;
63 } sthread_args_struct;
65 static int psp_thread_wrap(SceSize args, void *argp)
67 sthread_args_struct* sthread_args = (sthread_args_struct*)argp;
69 return (int)sthread_args->start_routine(sthread_args->arg);
72 static INLINE int pthread_create(pthread_t *thread,
73 const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
75 snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) thread);
78 *thread = sceKernelCreateThread(name_buffer, psp_thread_wrap,
79 0x10000100, 0x10000, 0, 0, NULL);
81 *thread = sceKernelCreateThread(name_buffer,
82 psp_thread_wrap, 0x20, STACKSIZE, 0, NULL);
85 sthread_args_struct sthread_args;
86 sthread_args.arg = arg;
87 sthread_args.start_routine = start_routine;
89 return sceKernelStartThread(*thread, sizeof(sthread_args), &sthread_args);
92 static INLINE int pthread_mutex_init(pthread_mutex_t *mutex,
93 const pthread_mutexattr_t *attr)
95 snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) mutex);
98 *mutex = sceKernelCreateMutex(name_buffer, 0, 0, 0);
103 return *mutex = sceKernelCreateSema(name_buffer, 0, 1, 1, NULL);
107 static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
110 return sceKernelDeleteMutex(*mutex);
112 return sceKernelDeleteSema(*mutex);
116 static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
119 int ret = sceKernelLockMutex(*mutex, 1, 0);
128 static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
131 int ret = sceKernelUnlockMutex(*mutex, 1);
139 static INLINE int pthread_join(pthread_t thread, void **retval)
142 int res = sceKernelWaitThreadEnd(thread, 0, 0);
145 return sceKernelDeleteThread(thread);
147 SceUInt timeout = (SceUInt)-1;
148 sceKernelWaitThreadEnd(thread, &timeout);
149 exit_status = sceKernelGetThreadExitStatus(thread);
150 sceKernelDeleteThread(thread);
155 static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
158 return sceKernelTryLockMutex(*mutex, 1 /* not sure about this last param */);
165 static INLINE int pthread_cond_wait(pthread_cond_t *cond,
166 pthread_mutex_t *mutex)
169 int ret = pthread_mutex_lock(&cond->mutex);
173 pthread_mutex_unlock(mutex);
174 pthread_mutex_unlock(&cond->mutex);
176 ret = sceKernelWaitSema(cond->sema, 1, 0);
178 sceClibPrintf("Premature wakeup: %08X", ret);
179 pthread_mutex_lock(mutex);
183 sceKernelDelayThread(10000);
188 static INLINE int pthread_cond_timedwait(pthread_cond_t *cond,
189 pthread_mutex_t *mutex, const struct timespec *abstime)
192 int ret = pthread_mutex_lock(&cond->mutex);
196 pthread_mutex_unlock(mutex);
197 pthread_mutex_unlock(&cond->mutex);
201 timeout = abstime->tv_sec;
202 timeout += abstime->tv_nsec / 1.0e6;
204 ret = sceKernelWaitSema(cond->sema, 1, &timeout);
206 sceClibPrintf("Premature wakeup: %08X", ret);
207 pthread_mutex_lock(mutex);
216 static INLINE int pthread_cond_init(pthread_cond_t *cond,
217 const pthread_condattr_t *attr)
221 pthread_mutex_init(&cond->mutex,NULL);
226 snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) cond);
227 cond->sema = sceKernelCreateSema(name_buffer, 0, 0, 1, 0);
231 pthread_mutex_destroy(&cond->mutex);
245 static INLINE int pthread_cond_signal(pthread_cond_t *cond)
248 pthread_mutex_lock(&cond->mutex);
252 sceKernelSignalSema(cond->sema, 1);
254 pthread_mutex_unlock(&cond->mutex);
262 static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
268 static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
271 int ret = sceKernelDeleteSema(cond->sema);
275 return sceKernelDeleteMutex(cond->mutex);
282 static INLINE int pthread_detach(pthread_t thread)
287 static INLINE void pthread_exit(void *retval)
290 sceKernelExitDeleteThread(sceKernelGetThreadId());
294 static INLINE pthread_t pthread_self(void)
296 /* zero 20-mar-2016: untested */
297 return sceKernelGetThreadId();
300 static INLINE int pthread_equal(pthread_t t1, pthread_t t2)
305 #endif //_PSP_PTHREAD_WRAP__