1 /* Copyright  (C) 2010-2020 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 OSCond pthread_cond_t;
88 
89 #if defined(GX_PTHREAD_LEGACY)
90 typedef void* pthread_mutexattr_t;
91 typedef int pthread_attr_t;
92 typedef OSCond pthread_condattr_t;
93 #endif
94 
pthread_create(pthread_t * thread,const pthread_attr_t * attr,void * (* start_routine)(void *),void * arg)95 static INLINE int pthread_create(pthread_t *thread,
96       const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
97 {
98    *thread = 0;
99    return OSCreateThread(thread, start_routine, 0 /* unused */, arg,
100          0, STACKSIZE, 64, 0 /* unused */);
101 }
102 
pthread_self(void)103 static INLINE pthread_t pthread_self(void)
104 {
105    /* zero 20-mar-2016: untested */
106    return LWP_GetSelf();
107 }
108 
pthread_mutex_init(pthread_mutex_t * mutex,const pthread_mutexattr_t * attr)109 static INLINE int pthread_mutex_init(pthread_mutex_t *mutex,
110       const pthread_mutexattr_t *attr)
111 {
112    return OSInitMutex(mutex);
113 }
114 
pthread_mutex_destroy(pthread_mutex_t * mutex)115 static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
116 {
117    return LWP_MutexDestroy(*mutex);
118 }
119 
pthread_mutex_lock(pthread_mutex_t * mutex)120 static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
121 {
122    return OSLockMutex(*mutex);
123 }
124 
pthread_mutex_unlock(pthread_mutex_t * mutex)125 static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
126 {
127    return OSUnlockMutex(*mutex);
128 }
129 
pthread_exit(void * retval)130 static INLINE void pthread_exit(void *retval)
131 {
132    /* FIXME: No LWP equivalent for this? */
133    (void)retval;
134 }
135 
pthread_detach(pthread_t thread)136 static INLINE int pthread_detach(pthread_t thread)
137 {
138    /* FIXME: pthread_detach equivalent missing? */
139    (void)thread;
140    return 0;
141 }
142 
pthread_join(pthread_t thread,void ** retval)143 static INLINE int pthread_join(pthread_t thread, void **retval)
144 {
145    return OSJoinThread(thread, retval);
146 }
147 
pthread_mutex_trylock(pthread_mutex_t * mutex)148 static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
149 {
150    return OSTryLockMutex(*mutex);
151 }
152 
pthread_cond_wait(pthread_cond_t * cond,pthread_mutex_t * mutex)153 static INLINE int pthread_cond_wait(pthread_cond_t *cond,
154       pthread_mutex_t *mutex)
155 {
156    return OSWaitCond(*cond, *mutex);
157 }
158 
pthread_cond_timedwait(pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec * abstime)159 static INLINE int pthread_cond_timedwait(pthread_cond_t *cond,
160       pthread_mutex_t *mutex, const struct timespec *abstime)
161 {
162    return LWP_CondTimedWait(*cond, *mutex, abstime);
163 }
164 
pthread_cond_init(pthread_cond_t * cond,const pthread_condattr_t * attr)165 static INLINE int pthread_cond_init(pthread_cond_t *cond,
166       const pthread_condattr_t *attr)
167 {
168    return OSInitCond(cond);
169 }
170 
pthread_cond_signal(pthread_cond_t * cond)171 static INLINE int pthread_cond_signal(pthread_cond_t *cond)
172 {
173    return LWP_CondSignal(*cond);
174 }
175 
pthread_cond_broadcast(pthread_cond_t * cond)176 static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
177 {
178    return LWP_CondBroadcast(*cond);
179 }
180 
pthread_cond_destroy(pthread_cond_t * cond)181 static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
182 {
183    return LWP_CondDestroy(*cond);
184 }
185 
186 extern int pthread_equal(pthread_t t1, pthread_t t2);
187 
188 #endif
189