f901d0cd |
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, |
72 | #ifdef VITA |
73 | 0, |
74 | #endif |
75 | NULL); |
76 | |
77 | sthread_args_struct sthread_args; |
78 | sthread_args.arg = arg; |
79 | sthread_args.start_routine = start_routine; |
80 | |
81 | return sceKernelStartThread(*thread, sizeof(sthread_args), &sthread_args); |
82 | } |
83 | |
84 | static INLINE int pthread_mutex_init(pthread_mutex_t *mutex, |
85 | const pthread_mutexattr_t *attr) |
86 | { |
87 | sprintf(name_buffer, "0x%08X", (uint32_t) mutex); |
88 | |
89 | #ifdef VITA |
90 | return *mutex = sceKernelCreateMutex(name_buffer, 0, 0, 0); |
91 | #else |
92 | return *mutex = sceKernelCreateSema(name_buffer, 0, 1, 1, NULL); |
93 | #endif |
94 | } |
95 | |
96 | static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex) |
97 | { |
98 | #ifdef VITA |
99 | return sceKernelDeleteMutex(*mutex); |
100 | #else |
101 | return sceKernelDeleteSema(*mutex); |
102 | #endif |
103 | } |
104 | |
105 | static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex) |
106 | { |
107 | #ifdef VITA |
108 | return sceKernelLockMutex(*mutex, 1, 0); |
109 | #else |
110 | /* FIXME: stub */ |
111 | return 1; |
112 | #endif |
113 | } |
114 | |
115 | static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex) |
116 | { |
117 | #ifdef VITA |
118 | return sceKernelUnlockMutex(*mutex, 1); |
119 | #else |
120 | /* FIXME: stub */ |
121 | return 1; |
122 | #endif |
123 | } |
124 | |
125 | |
126 | static INLINE int pthread_join(pthread_t thread, void **retval) |
127 | { |
128 | int exit_status; |
129 | SceUInt timeout = (SceUInt)-1; |
130 | #ifdef VITA |
131 | sceKernelWaitThreadEnd(thread, &exit_status, &timeout); |
132 | #else |
133 | sceKernelWaitThreadEnd(thread, &timeout); |
134 | exit_status = sceKernelGetThreadExitStatus(thread); |
135 | #endif |
136 | sceKernelDeleteThread(thread); |
137 | return exit_status; |
138 | } |
139 | |
140 | static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex) |
141 | { |
142 | #ifdef VITA |
143 | return sceKernelTryLockMutex(*mutex, 1 /* not sure about this last param */); |
144 | #else |
145 | /* FIXME: stub */ |
146 | return 1; |
147 | #endif |
148 | } |
149 | |
150 | static INLINE int pthread_cond_wait(pthread_cond_t *cond, |
151 | pthread_mutex_t *mutex) |
152 | { |
153 | sceKernelDelayThread(10000); |
154 | return 1; |
155 | } |
156 | |
157 | static INLINE int pthread_cond_timedwait(pthread_cond_t *cond, |
158 | pthread_mutex_t *mutex, const struct timespec *abstime) |
159 | { |
160 | //FIXME: stub |
161 | return 1; |
162 | } |
163 | |
164 | static INLINE int pthread_cond_init(pthread_cond_t *cond, |
165 | const pthread_condattr_t *attr) |
166 | { |
167 | //FIXME: stub |
168 | return 1; |
169 | } |
170 | |
171 | static INLINE int pthread_cond_signal(pthread_cond_t *cond) |
172 | { |
173 | //FIXME: stub |
174 | return 1; |
175 | } |
176 | |
177 | static INLINE int pthread_cond_broadcast(pthread_cond_t *cond) |
178 | { |
179 | //FIXME: stub |
180 | return 1; |
181 | } |
182 | |
183 | static INLINE int pthread_cond_destroy(pthread_cond_t *cond) |
184 | { |
185 | //FIXME: stub |
186 | return 1; |
187 | } |
188 | |
189 | |
190 | static INLINE int pthread_detach(pthread_t thread) |
191 | { |
192 | return 1; |
193 | } |
194 | |
195 | static INLINE void pthread_exit(void *retval) |
196 | { |
197 | (void)retval; |
198 | } |
199 | |
200 | #endif //_PSP_PTHREAD_WRAP__ |