1 /* Copyright (C) 2010-2016 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;
66 static int psp_thread_wrap(SceSize args, void *argp)
68 sthread_args_struct* sthread_args = (sthread_args_struct*)argp;
70 return (int)sthread_args->start_routine(sthread_args->arg);
73 static INLINE int pthread_create(pthread_t *thread,
74 const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
76 sprintf(name_buffer, "0x%08X", (unsigned int) thread);
79 *thread = sceKernelCreateThread(name_buffer, psp_thread_wrap,
80 0x10000100, 0x10000, 0, 0, NULL);
82 *thread = sceKernelCreateThread(name_buffer,
83 psp_thread_wrap, 0x20, STACKSIZE, 0, NULL);
86 sthread_args_struct sthread_args;
87 sthread_args.arg = arg;
88 sthread_args.start_routine = start_routine;
90 return sceKernelStartThread(*thread, sizeof(sthread_args), &sthread_args);
93 static INLINE int pthread_mutex_init(pthread_mutex_t *mutex,
94 const pthread_mutexattr_t *attr)
96 sprintf(name_buffer, "0x%08X", (unsigned int) mutex);
99 *mutex = sceKernelCreateMutex(name_buffer, 0, 0, 0);
104 return *mutex = sceKernelCreateSema(name_buffer, 0, 1, 1, NULL);
108 static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
111 return sceKernelDeleteMutex(*mutex);
113 return sceKernelDeleteSema(*mutex);
117 static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
120 int ret = sceKernelLockMutex(*mutex, 1, 0);
129 static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
132 int ret = sceKernelUnlockMutex(*mutex, 1);
141 static INLINE int pthread_join(pthread_t thread, void **retval)
144 int res = sceKernelWaitThreadEnd(thread, 0, 0);
147 return sceKernelDeleteThread(thread);
149 SceUInt timeout = (SceUInt)-1;
150 sceKernelWaitThreadEnd(thread, &timeout);
151 exit_status = sceKernelGetThreadExitStatus(thread);
152 sceKernelDeleteThread(thread);
157 static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
160 return sceKernelTryLockMutex(*mutex, 1 /* not sure about this last param */);
167 static INLINE int pthread_cond_wait(pthread_cond_t *cond,
168 pthread_mutex_t *mutex)
171 int ret = pthread_mutex_lock(&cond->mutex);
175 pthread_mutex_unlock(mutex);
176 pthread_mutex_unlock(&cond->mutex);
178 ret = sceKernelWaitSema(cond->sema, 1, 0);
180 sceClibPrintf("Premature wakeup: %08X", ret);
181 pthread_mutex_lock(mutex);
185 sceKernelDelayThread(10000);
190 static INLINE int pthread_cond_timedwait(pthread_cond_t *cond,
191 pthread_mutex_t *mutex, const struct timespec *abstime)
194 int ret = pthread_mutex_lock(&cond->mutex);
198 pthread_mutex_unlock(mutex);
199 pthread_mutex_unlock(&cond->mutex);
203 timeout = abstime->tv_sec;
204 timeout += abstime->tv_nsec / 1.0e6;
206 ret = sceKernelWaitSema(cond->sema, 1, &timeout);
208 sceClibPrintf("Premature wakeup: %08X", ret);
209 pthread_mutex_lock(mutex);
218 static INLINE int pthread_cond_init(pthread_cond_t *cond,
219 const pthread_condattr_t *attr)
223 pthread_mutex_init(&cond->mutex,NULL);
227 sprintf(name_buffer, "0x%08X", (unsigned int) cond);
228 //cond->sema = sceKernelCreateCond(name_buffer, 0, cond->mutex, 0);
229 cond->sema = sceKernelCreateSema(name_buffer, 0, 0, 1, 0);
231 pthread_mutex_destroy(&cond->mutex);
247 static INLINE int pthread_cond_signal(pthread_cond_t *cond)
250 pthread_mutex_lock(&cond->mutex);
254 sceKernelSignalSema(cond->sema, 1);
256 pthread_mutex_unlock(&cond->mutex);
264 static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
270 static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
273 int ret = sceKernelDeleteSema(cond->sema);
277 return sceKernelDeleteMutex(cond->mutex);
285 static INLINE int pthread_detach(pthread_t thread)
290 static INLINE void pthread_exit(void *retval)
293 sceKernelExitDeleteThread(sceKernelGetThreadId());
297 static INLINE pthread_t pthread_self(void)
299 /* zero 20-mar-2016: untested */
300 return sceKernelGetThreadId();
303 static INLINE int pthread_equal(pthread_t t1, pthread_t t2)
308 #endif //_PSP_PTHREAD_WRAP__