| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifndef _PSP_PTHREAD_WRAP__ |
| 25 |
#define _PSP_PTHREAD_WRAP__ |
| 26 |
|
| 27 |
#ifdef VITA |
| 28 |
#include <psp2/kernel/threadmgr.h> |
| 29 |
#else |
| 30 |
#include <pspkernel.h> |
| 31 |
#include <pspthreadman.h> |
| 32 |
#include <pspthreadman_kernel.h> |
| 33 |
#endif |
| 34 |
#include <stdio.h> |
| 35 |
#include <retro_inline.h> |
| 36 |
|
| 37 |
#define STACKSIZE (64 * 1024) |
| 38 |
|
| 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; |
| 45 |
|
| 46 |
|
| 47 |
char name_buffer[256]; |
| 48 |
|
| 49 |
typedef void* (*sthreadEntry)(void *argp); |
| 50 |
|
| 51 |
typedef struct |
| 52 |
{ |
| 53 |
void* arg; |
| 54 |
sthreadEntry start_routine; |
| 55 |
} sthread_args_struct; |
| 56 |
|
| 57 |
|
| 58 |
static int psp_thread_wrap(SceSize args, void *argp) |
| 59 |
{ |
| 60 |
sthread_args_struct* sthread_args = (sthread_args_struct*)argp; |
| 61 |
|
| 62 |
return (int)sthread_args->start_routine(sthread_args->arg); |
| 63 |
} |
| 64 |
|
| 65 |
static INLINE int pthread_create(pthread_t *thread, |
| 66 |
const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg) |
| 67 |
{ |
| 68 |
sprintf(name_buffer, "0x%08X", (uint32_t) thread); |
| 69 |
|
| 70 |
*thread = sceKernelCreateThread(name_buffer, |
| 71 |
psp_thread_wrap, 0x20, STACKSIZE, 0, NULL); |
| 72 |
|
| 73 |
sthread_args_struct sthread_args; |
| 74 |
sthread_args.arg = arg; |
| 75 |
sthread_args.start_routine = start_routine; |
| 76 |
|
| 77 |
return sceKernelStartThread(*thread, sizeof(sthread_args), &sthread_args); |
| 78 |
} |
| 79 |
|
| 80 |
static INLINE int pthread_mutex_init(pthread_mutex_t *mutex, |
| 81 |
const pthread_mutexattr_t *attr) |
| 82 |
{ |
| 83 |
sprintf(name_buffer, "0x%08X", (uint32_t) mutex); |
| 84 |
|
| 85 |
#ifdef VITA |
| 86 |
return *mutex = sceKernelCreateMutex(name_buffer, 0, 0, 0); |
| 87 |
#else |
| 88 |
return *mutex = sceKernelCreateSema(name_buffer, 0, 1, 1, NULL); |
| 89 |
#endif |
| 90 |
} |
| 91 |
|
| 92 |
static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex) |
| 93 |
{ |
| 94 |
#ifdef VITA |
| 95 |
return sceKernelDeleteMutex(*mutex); |
| 96 |
#else |
| 97 |
return sceKernelDeleteSema(*mutex); |
| 98 |
#endif |
| 99 |
} |
| 100 |
|
| 101 |
static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex) |
| 102 |
{ |
| 103 |
#ifdef VITA |
| 104 |
return sceKernelLockMutex(*mutex, 1, 0); |
| 105 |
#else |
| 106 |
|
| 107 |
return 1; |
| 108 |
#endif |
| 109 |
} |
| 110 |
|
| 111 |
static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex) |
| 112 |
{ |
| 113 |
#ifdef VITA |
| 114 |
return sceKernelUnlockMutex(*mutex, 1); |
| 115 |
#else |
| 116 |
|
| 117 |
return 1; |
| 118 |
#endif |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
static INLINE int pthread_join(pthread_t thread, void **retval) |
| 123 |
{ |
| 124 |
int exit_status; |
| 125 |
SceUInt timeout = (SceUInt)-1; |
| 126 |
|
| 127 |
sceKernelWaitThreadEnd(thread, &timeout); |
| 128 |
exit_status = sceKernelGetThreadExitStatus(thread); |
| 129 |
sceKernelDeleteThread(thread); |
| 130 |
return exit_status; |
| 131 |
} |
| 132 |
|
| 133 |
static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex) |
| 134 |
{ |
| 135 |
#ifdef VITA |
| 136 |
return sceKernelTryLockMutex(*mutex, 1 ); |
| 137 |
#else |
| 138 |
|
| 139 |
return 1; |
| 140 |
#endif |
| 141 |
} |
| 142 |
|
| 143 |
static INLINE int pthread_cond_wait(pthread_cond_t *cond, |
| 144 |
pthread_mutex_t *mutex) |
| 145 |
{ |
| 146 |
sceKernelDelayThread(10000); |
| 147 |
return 1; |
| 148 |
} |
| 149 |
|
| 150 |
static INLINE int pthread_cond_timedwait(pthread_cond_t *cond, |
| 151 |
pthread_mutex_t *mutex, const struct timespec *abstime) |
| 152 |
{ |
| 153 |
|
| 154 |
return 1; |
| 155 |
} |
| 156 |
|
| 157 |
static INLINE int pthread_cond_init(pthread_cond_t *cond, |
| 158 |
const pthread_condattr_t *attr) |
| 159 |
{ |
| 160 |
|
| 161 |
return 1; |
| 162 |
} |
| 163 |
|
| 164 |
static INLINE int pthread_cond_signal(pthread_cond_t *cond) |
| 165 |
{ |
| 166 |
|
| 167 |
return 1; |
| 168 |
} |
| 169 |
|
| 170 |
static INLINE int pthread_cond_broadcast(pthread_cond_t *cond) |
| 171 |
{ |
| 172 |
|
| 173 |
return 1; |
| 174 |
} |
| 175 |
|
| 176 |
static INLINE int pthread_cond_destroy(pthread_cond_t *cond) |
| 177 |
{ |
| 178 |
|
| 179 |
return 1; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
static INLINE int pthread_detach(pthread_t thread) |
| 184 |
{ |
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
static INLINE void pthread_exit(void *retval) |
| 189 |
{ |
| 190 |
(void)retval; |
| 191 |
} |
| 192 |
|
| 193 |
#endif |