Debian Codegraph @

libretro-common/rthreads/psp_pthread.h

raw symbols SCIP
1 /* Copyright (C) 2010-2015 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 #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 /* Use pointer values to create unique names for threads/mutexes */
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 /* FIXME: stub */
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 /* FIXME: stub */
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 /* not sure about this last param */);
137 #else
138 /* FIXME: stub */
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 //FIXME: stub
154 return 1;
155 }
156
157 static INLINE int pthread_cond_init(pthread_cond_t *cond,
158 const pthread_condattr_t *attr)
159 {
160 //FIXME: stub
161 return 1;
162 }
163
164 static INLINE int pthread_cond_signal(pthread_cond_t *cond)
165 {
166 //FIXME: stub
167 return 1;
168 }
169
170 static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
171 {
172 //FIXME: stub
173 return 1;
174 }
175
176 static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
177 {
178 //FIXME: stub
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 //_PSP_PTHREAD_WRAP__