1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (gx_pthread.h).
5 * ---------------------------------------------------------------------------------------
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:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
23 #ifndef _GX_PTHREAD_WRAP_GX_
24 #define _GX_PTHREAD_WRAP_GX_
29 #include <retro_inline.h>
32 #define OSThread lwp_t
40 #define OSThreadQueue lwpq_t
44 #define OSInitMutex(mutex) LWP_MutexInit(mutex, 0)
48 #define OSLockMutex(mutex) LWP_MutexLock(mutex)
52 #define OSUnlockMutex(mutex) LWP_MutexUnlock(mutex)
55 #ifndef OSTryLockMutex
56 #define OSTryLockMutex(mutex) LWP_MutexTryLock(mutex)
60 #define OSInitCond(cond) LWP_CondInit(cond)
64 #define OSWaitCond(cond, mutex) LWP_CondWait(cond, mutex)
67 #ifndef OSInitThreadQueue
68 #define OSInitThreadQueue(queue) LWP_InitQueue(queue)
72 #define OSSleepThread(queue) LWP_ThreadSleep(queue)
76 #define OSJoinThread(thread, val) LWP_JoinThread(thread, val)
79 #ifndef OSCreateThread
80 #define OSCreateThread(thread, func, intarg, ptrarg, stackbase, stacksize, priority, attrs) LWP_CreateThread(thread, func, ptrarg, stackbase, stacksize, priority)
83 #define STACKSIZE (8 * 1024)
85 typedef OSThread pthread_t;
86 typedef mutex_t pthread_mutex_t;
87 typedef OSCond pthread_cond_t;
89 #if defined(GX_PTHREAD_LEGACY)
90 typedef void* pthread_mutexattr_t;
91 typedef int pthread_attr_t;
92 typedef OSCond pthread_condattr_t;
95 static INLINE int pthread_create(pthread_t *thread,
96 const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
99 return OSCreateThread(thread, start_routine, 0 /* unused */, arg,
100 0, STACKSIZE, 64, 0 /* unused */);
103 static INLINE pthread_t pthread_self(void)
105 /* zero 20-mar-2016: untested */
106 return LWP_GetSelf();
109 static INLINE int pthread_mutex_init(pthread_mutex_t *mutex,
110 const pthread_mutexattr_t *attr)
112 return OSInitMutex(mutex);
115 static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
117 return LWP_MutexDestroy(*mutex);
120 static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
122 return OSLockMutex(*mutex);
125 static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
127 return OSUnlockMutex(*mutex);
130 static INLINE void pthread_exit(void *retval)
132 /* FIXME: No LWP equivalent for this? */
136 static INLINE int pthread_detach(pthread_t thread)
138 /* FIXME: pthread_detach equivalent missing? */
143 static INLINE int pthread_join(pthread_t thread, void **retval)
145 return OSJoinThread(thread, retval);
148 static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
150 return OSTryLockMutex(*mutex);
153 static INLINE int pthread_cond_wait(pthread_cond_t *cond,
154 pthread_mutex_t *mutex)
156 return OSWaitCond(*cond, *mutex);
159 static INLINE int pthread_cond_timedwait(pthread_cond_t *cond,
160 pthread_mutex_t *mutex, const struct timespec *abstime)
162 return LWP_CondTimedWait(*cond, *mutex, abstime);
165 static INLINE int pthread_cond_init(pthread_cond_t *cond,
166 const pthread_condattr_t *attr)
168 return OSInitCond(cond);
171 static INLINE int pthread_cond_signal(pthread_cond_t *cond)
173 return LWP_CondSignal(*cond);
176 static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
178 return LWP_CondBroadcast(*cond);
181 static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
183 return LWP_CondDestroy(*cond);