1 /* Copyright (C) 2010-2015 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>
30 #include <pspkernel.h>
31 #include <pspthreadman.h>
32 #include <pspthreadman_kernel.h>
35 #include <retro_inline.h>
37 #define STACKSIZE (64 * 1024)
39 typedef SceUID pthread_t;
40 typedef SceUID pthread_mutex_t;
41 typedef void* pthread_mutexattr_t;
42 typedef int pthread_attr_t;
43 typedef SceUID pthread_cond_t;
44 typedef SceUID pthread_condattr_t;
46 /* Use pointer values to create unique names for threads/mutexes */
47 char name_buffer[256];
49 typedef void* (*sthreadEntry)(void *argp);
54 sthreadEntry start_routine;
55 } sthread_args_struct;
58 static int psp_thread_wrap(SceSize args, void *argp)
60 sthread_args_struct* sthread_args = (sthread_args_struct*)argp;
62 return (int)sthread_args->start_routine(sthread_args->arg);
65 static INLINE int pthread_create(pthread_t *thread,
66 const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
68 sprintf(name_buffer, "0x%08X", (uint32_t) thread);
70 *thread = sceKernelCreateThread(name_buffer,
71 psp_thread_wrap, 0x20, STACKSIZE, 0,
77 sthread_args_struct sthread_args;
78 sthread_args.arg = arg;
79 sthread_args.start_routine = start_routine;
81 return sceKernelStartThread(*thread, sizeof(sthread_args), &sthread_args);
84 static INLINE int pthread_mutex_init(pthread_mutex_t *mutex,
85 const pthread_mutexattr_t *attr)
87 sprintf(name_buffer, "0x%08X", (uint32_t) mutex);
90 return *mutex = sceKernelCreateMutex(name_buffer, 0, 0, 0);
92 return *mutex = sceKernelCreateSema(name_buffer, 0, 1, 1, NULL);
96 static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
99 return sceKernelDeleteMutex(*mutex);
101 return sceKernelDeleteSema(*mutex);
105 static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
108 return sceKernelLockMutex(*mutex, 1, 0);
115 static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
118 return sceKernelUnlockMutex(*mutex, 1);
126 static INLINE int pthread_join(pthread_t thread, void **retval)
129 SceUInt timeout = (SceUInt)-1;
131 sceKernelWaitThreadEnd(thread, &exit_status, &timeout);
133 sceKernelWaitThreadEnd(thread, &timeout);
134 exit_status = sceKernelGetThreadExitStatus(thread);
136 sceKernelDeleteThread(thread);
140 static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
143 return sceKernelTryLockMutex(*mutex, 1 /* not sure about this last param */);
150 static INLINE int pthread_cond_wait(pthread_cond_t *cond,
151 pthread_mutex_t *mutex)
153 sceKernelDelayThread(10000);
157 static INLINE int pthread_cond_timedwait(pthread_cond_t *cond,
158 pthread_mutex_t *mutex, const struct timespec *abstime)
164 static INLINE int pthread_cond_init(pthread_cond_t *cond,
165 const pthread_condattr_t *attr)
171 static INLINE int pthread_cond_signal(pthread_cond_t *cond)
177 static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
183 static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
190 static INLINE int pthread_detach(pthread_t thread)
195 static INLINE void pthread_exit(void *retval)
200 #endif //_PSP_PTHREAD_WRAP__