1 /* Copyright  (C) 2010-2016 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (gx_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 #ifndef _GX_PTHREAD_WRAP_GX_
24 #define _GX_PTHREAD_WRAP_GX_
25 
26 #include <ogcsys.h>
27 #include <gccore.h>
28 #include <ogc/cond.h>
29 #include <retro_inline.h>
30 
31 #ifndef OSThread
32 #define OSThread lwp_t
33 #endif
34 
35 #ifndef OSCond
36 #define OSCond lwpq_t
37 #endif
38 
39 #ifndef OSThreadQueue
40 #define OSThreadQueue lwpq_t
41 #endif
42 
43 #ifndef OSInitMutex
44 #define OSInitMutex(mutex) LWP_MutexInit(mutex, 0)
45 #endif
46 
47 #ifndef OSLockMutex
48 #define OSLockMutex(mutex) LWP_MutexLock(mutex)
49 #endif
50 
51 #ifndef OSUnlockMutex
52 #define OSUnlockMutex(mutex) LWP_MutexUnlock(mutex)
53 #endif
54 
55 #ifndef OSTryLockMutex
56 #define OSTryLockMutex(mutex) LWP_MutexTryLock(mutex)
57 #endif
58 
59 #ifndef OSInitCond
60 #define OSInitCond(cond) LWP_CondInit(cond)
61 #endif
62 
63 #ifndef OSWaitCond
64 #define OSWaitCond(cond, mutex) LWP_CondWait(cond, mutex)
65 #endif
66 
67 #ifndef OSInitThreadQueue
68 #define OSInitThreadQueue(queue) LWP_InitQueue(queue)
69 #endif
70 
71 #ifndef OSSleepThread
72 #define OSSleepThread(queue) LWP_ThreadSleep(queue)
73 #endif
74 
75 #ifndef OSJoinThread
76 #define OSJoinThread(thread, val) LWP_JoinThread(thread, val)
77 #endif
78 
79 #ifndef OSCreateThread
80 #define OSCreateThread(thread, func, intarg, ptrarg, stackbase, stacksize, priority, attrs) LWP_CreateThread(thread, func, ptrarg, stackbase, stacksize, priority)
81 #endif
82 
83 #define STACKSIZE (8 * 1024)
84 
85 typedef OSThread pthread_t;
86 typedef mutex_t pthread_mutex_t;
87 typedef void* pthread_mutexattr_t;
88 typedef int pthread_attr_t;
89 typedef OSCond pthread_cond_t;
90 typedef OSCond pthread_condattr_t;
91 
pthread_create(pthread_t * thread,const pthread_attr_t * attr,void * (* start_routine)(void *),void * arg)92 static INLINE int pthread_create(pthread_t *thread,
93       const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
94 {
95    *thread = 0;
96    return OSCreateThread(thread, start_routine, 0 /* unused */, arg,
97          0, STACKSIZE, 64, 0 /* unused */);
98 }
99 
pthread_self(void)100 static INLINE pthread_t pthread_self(void)
101 {
102    /* zero 20-mar-2016: untested */
103    return LWP_GetSelf();
104 }
105 
pthread_mutex_init(pthread_mutex_t * mutex,const pthread_mutexattr_t * attr)106 static INLINE int pthread_mutex_init(pthread_mutex_t *mutex,
107       const pthread_mutexattr_t *attr)
108 {
109    return OSInitMutex(mutex);
110 }
111 
pthread_mutex_destroy(pthread_mutex_t * mutex)112 static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
113 {
114    return LWP_MutexDestroy(*mutex);
115 }
116 
pthread_mutex_lock(pthread_mutex_t * mutex)117 static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
118 {
119    return OSLockMutex(*mutex);
120 }
121 
pthread_mutex_unlock(pthread_mutex_t * mutex)122 static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
123 {
124    return OSUnlockMutex(*mutex);
125 }
126 
pthread_exit(void * retval)127 static INLINE void pthread_exit(void *retval)
128 {
129    /* FIXME: No LWP equivalent for this? */
130    (void)retval;
131 }
132 
pthread_detach(pthread_t thread)133 static INLINE int pthread_detach(pthread_t thread)
134 {
135    /* FIXME: pthread_detach equivalent missing? */
136    (void)thread;
137    return 0;
138 }
139 
pthread_join(pthread_t thread,void ** retval)140 static INLINE int pthread_join(pthread_t thread, void **retval)
141 {
142    return OSJoinThread(thread, retval);
143 }
144 
pthread_mutex_trylock(pthread_mutex_t * mutex)145 static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
146 {
147    return OSTryLockMutex(*mutex);
148 }
149 
pthread_cond_wait(pthread_cond_t * cond,pthread_mutex_t * mutex)150 static INLINE int pthread_cond_wait(pthread_cond_t *cond,
151       pthread_mutex_t *mutex)
152 {
153    return OSWaitCond(*cond, *mutex);
154 }
155 
pthread_cond_timedwait(pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec * abstime)156 static INLINE int pthread_cond_timedwait(pthread_cond_t *cond,
157       pthread_mutex_t *mutex, const struct timespec *abstime)
158 {
159    return LWP_CondTimedWait(*cond, *mutex, abstime);
160 }
161 
pthread_cond_init(pthread_cond_t * cond,const pthread_condattr_t * attr)162 static INLINE int pthread_cond_init(pthread_cond_t *cond,
163       const pthread_condattr_t *attr)
164 {
165    return OSInitCond(cond);
166 }
167 
pthread_cond_signal(pthread_cond_t * cond)168 static INLINE int pthread_cond_signal(pthread_cond_t *cond)
169 {
170    return LWP_CondSignal(*cond);
171 }
172 
pthread_cond_broadcast(pthread_cond_t * cond)173 static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
174 {
175    return LWP_CondBroadcast(*cond);
176 }
177 
pthread_cond_destroy(pthread_cond_t * cond)178 static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
179 {
180    return LWP_CondDestroy(*cond);
181 }
182 
183 extern int pthread_equal(pthread_t t1, pthread_t t2);
184 
185 #endif
186