RetroArch
psp_pthread.h
Go to the documentation of this file.
1 /* Copyright (C) 2010-2017 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;
42 typedef void* pthread_mutexattr_t;
43 typedef int pthread_attr_t;
44 
45 typedef struct
46 {
49  int waiting;
51 
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;
64 
65 
66 static int psp_thread_wrap(SceSize args, void *argp)
67 {
68  sthread_args_struct* sthread_args = (sthread_args_struct*)argp;
69 
70  return (int)sthread_args->start_routine(sthread_args->arg);
71 }
72 
73 static INLINE int pthread_create(pthread_t *thread,
74  const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
75 {
76  snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) thread);
77 
78 #ifdef VITA
79  *thread = sceKernelCreateThread(name_buffer, psp_thread_wrap,
80  0x10000100, 0x10000, 0, 0, NULL);
81 #else
82  *thread = sceKernelCreateThread(name_buffer,
83  psp_thread_wrap, 0x20, STACKSIZE, 0, NULL);
84 #endif
85 
86  sthread_args_struct sthread_args;
87  sthread_args.arg = arg;
88  sthread_args.start_routine = start_routine;
89 
90  return sceKernelStartThread(*thread, sizeof(sthread_args), &sthread_args);
91 }
92 
94  const pthread_mutexattr_t *attr)
95 {
96  snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) mutex);
97 
98 #ifdef VITA
99  *mutex = sceKernelCreateMutex(name_buffer, 0, 0, 0);
100  if(*mutex<0)
101  return *mutex;
102  return 0;
103 #else
104  return *mutex = sceKernelCreateSema(name_buffer, 0, 1, 1, NULL);
105 #endif
106 }
107 
109 {
110 #ifdef VITA
111  return sceKernelDeleteMutex(*mutex);
112 #else
113  return sceKernelDeleteSema(*mutex);
114 #endif
115 }
116 
118 {
119 #ifdef VITA
120  int ret = sceKernelLockMutex(*mutex, 1, 0);
121  return ret;
122 
123 #else
124  /* FIXME: stub */
125  return 1;
126 #endif
127 }
128 
130 {
131 #ifdef VITA
132  int ret = sceKernelUnlockMutex(*mutex, 1);
133  return ret;
134 #else
135  /* FIXME: stub */
136  return 1;
137 #endif
138 }
139 
140 
141 static INLINE int pthread_join(pthread_t thread, void **retval)
142 {
143 #ifdef VITA
144  int res = sceKernelWaitThreadEnd(thread, 0, 0);
145  if (res < 0)
146  return res;
147  return sceKernelDeleteThread(thread);
148 #else
149  SceUInt timeout = (SceUInt)-1;
150  sceKernelWaitThreadEnd(thread, &timeout);
151  exit_status = sceKernelGetThreadExitStatus(thread);
152  sceKernelDeleteThread(thread);
153  return exit_status;
154 #endif
155 }
156 
158 {
159 #ifdef VITA
160  return sceKernelTryLockMutex(*mutex, 1 /* not sure about this last param */);
161 #else
162  /* FIXME: stub */
163  return 1;
164 #endif
165 }
166 
169 {
170 #ifdef VITA
171  int ret = pthread_mutex_lock(&cond->mutex);
172  if (ret < 0)
173  return ret;
174  ++cond->waiting;
176  pthread_mutex_unlock(&cond->mutex);
177 
178  ret = sceKernelWaitSema(cond->sema, 1, 0);
179  if (ret < 0)
180  sceClibPrintf("Premature wakeup: %08X", ret);
182  return ret;
183 #else
184  /* FIXME: stub */
185  sceKernelDelayThread(10000);
186  return 1;
187 #endif
188 }
189 
191  pthread_mutex_t *mutex, const struct timespec *abstime)
192 {
193 #ifdef VITA
194  int ret = pthread_mutex_lock(&cond->mutex);
195  if (ret < 0)
196  return ret;
197  ++cond->waiting;
199  pthread_mutex_unlock(&cond->mutex);
200 
201  SceUInt timeout = 0;
202 
203  timeout = abstime->tv_sec;
204  timeout += abstime->tv_nsec / 1.0e6;
205 
206  ret = sceKernelWaitSema(cond->sema, 1, &timeout);
207  if (ret < 0)
208  sceClibPrintf("Premature wakeup: %08X", ret);
210  return ret;
211 
212 #else
213  /* FIXME: stub */
214  return 1;
215 #endif
216 }
217 
219  const pthread_condattr_t *attr)
220 {
221 #ifdef VITA
222 
223  pthread_mutex_init(&cond->mutex,NULL);
224 
225  if(cond->mutex<0)
226  return cond->mutex;
227 
228  snprintf(name_buffer, sizeof(name_buffer), "0x%08X", (unsigned int) cond);
229  cond->sema = sceKernelCreateSema(name_buffer, 0, 0, 1, 0);
230 
231  if(cond->sema < 0)
232  {
233  pthread_mutex_destroy(&cond->mutex);
234  return cond->sema;
235  }
236 
237  cond->waiting = 0;
238 
239  return 0;
240 
241 
242 #else
243  /* FIXME: stub */
244  return 1;
245 #endif
246 }
247 
249 {
250 #ifdef VITA
251  pthread_mutex_lock(&cond->mutex);
252  if (cond->waiting)
253  {
254  --cond->waiting;
255  sceKernelSignalSema(cond->sema, 1);
256  }
257  pthread_mutex_unlock(&cond->mutex);
258  return 0;
259 #else
260  /* FIXME: stub */
261  return 1;
262 #endif
263 }
264 
266 {
267  /* FIXME: stub */
268  return 1;
269 }
270 
272 {
273 #ifdef VITA
274  int ret = sceKernelDeleteSema(cond->sema);
275  if(ret < 0)
276  return ret;
277 
278  return sceKernelDeleteMutex(cond->mutex);
279 #else
280  /* FIXME: stub */
281  return 1;
282 #endif
283 }
284 
285 
286 static INLINE int pthread_detach(pthread_t thread)
287 {
288  return 0;
289 }
290 
291 static INLINE void pthread_exit(void *retval)
292 {
293 #ifdef VITA
294  sceKernelExitDeleteThread(sceKernelGetThreadId());
295 #endif
296 }
297 
299 {
300  /* zero 20-mar-2016: untested */
301  return sceKernelGetThreadId();
302 }
303 
305 {
306  return t1 == t2;
307 }
308 
309 #endif //_PSP_PTHREAD_WRAP__
static INLINE int pthread_detach(pthread_t thread)
Definition: psp_pthread.h:286
static INLINE int pthread_equal(pthread_t t1, pthread_t t2)
Definition: psp_pthread.h:304
GLbitfield GLuint64 timeout
Definition: glext.h:7831
static INLINE int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
Definition: psp_pthread.h:190
static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: psp_pthread.h:108
#define INLINE
Definition: retro_inline.h:35
static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: psp_pthread.h:129
static INLINE int pthread_cond_signal(pthread_cond_t *cond)
Definition: psp_pthread.h:248
static INLINE int pthread_join(pthread_t thread, void **retval)
Definition: psp_pthread.h:141
SceUID pthread_t
Definition: psp_pthread.h:40
GLuint res
Definition: glext.h:10520
struct pthread_cond_t_ * pthread_cond_t
Definition: pthread.h:404
static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: psp_pthread.h:117
SceUID sema
Definition: psp_pthread.h:48
static int cond(LexState *ls)
Definition: lparser.c:1177
int waiting
Definition: psp_pthread.h:49
void * arg
Definition: psp_pthread.h:61
static sys_sem mutex
Definition: memp.c:120
static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
Definition: psp_pthread.h:271
static INLINE int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: psp_pthread.h:73
SceUID pthread_condattr_t
Definition: psp_pthread.h:52
Definition: psp_pthread.h:59
static int psp_thread_wrap(SceSize args, void *argp)
Definition: psp_pthread.h:66
#define NULL
Pointer to 0.
Definition: gctypes.h:65
static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
Definition: psp_pthread.h:157
SceUID pthread_mutex_t
Definition: psp_pthread.h:41
Definition: implement.h:146
unsigned int SceUInt
Definition: pte_types.h:31
Definition: psp_pthread.h:45
static INLINE pthread_t pthread_self(void)
Definition: psp_pthread.h:298
#define STACKSIZE
Definition: psp_pthread.h:38
unsigned int SceSize
Definition: pte_types.h:27
static INLINE int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: psp_pthread.h:93
void * pthread_t
Definition: pthread.h:398
static INLINE int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: psp_pthread.h:218
dictionary args
Definition: test_shaders.py:20
SceUID mutex
Definition: psp_pthread.h:47
static INLINE int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: psp_pthread.h:167
void *(* sthreadEntry)(void *argp)
Definition: psp_pthread.h:57
int SceUID
Definition: pte_types.h:24
int pthread_attr_t
Definition: psp_pthread.h:43
char name_buffer[256]
Definition: psp_pthread.h:55
static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: psp_pthread.h:265
Definition: implement.h:163
sthreadEntry start_routine
Definition: psp_pthread.h:62
Definition: implement.h:116
Definition: implement.h:251
void * pthread_mutexattr_t
Definition: psp_pthread.h:42
static INLINE void pthread_exit(void *retval)
Definition: psp_pthread.h:291