RetroArch
gx_pthread.h
Go to the documentation of this file.
1 /* Copyright (C) 2010-2018 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 
87 typedef void* pthread_mutexattr_t;
88 typedef int pthread_attr_t;
91 
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 
101 {
102  /* zero 20-mar-2016: untested */
103  return LWP_GetSelf();
104 }
105 
107  const pthread_mutexattr_t *attr)
108 {
109  return OSInitMutex(mutex);
110 }
111 
113 {
114  return LWP_MutexDestroy(*mutex);
115 }
116 
118 {
119  return OSLockMutex(*mutex);
120 }
121 
123 {
124  return OSUnlockMutex(*mutex);
125 }
126 
127 static INLINE void pthread_exit(void *retval)
128 {
129  /* FIXME: No LWP equivalent for this? */
130  (void)retval;
131 }
132 
133 static INLINE int pthread_detach(pthread_t thread)
134 {
135  /* FIXME: pthread_detach equivalent missing? */
136  (void)thread;
137  return 0;
138 }
139 
140 static INLINE int pthread_join(pthread_t thread, void **retval)
141 {
142  return OSJoinThread(thread, retval);
143 }
144 
146 {
147  return OSTryLockMutex(*mutex);
148 }
149 
152 {
153  return OSWaitCond(*cond, *mutex);
154 }
155 
157  pthread_mutex_t *mutex, const struct timespec *abstime)
158 {
159  return LWP_CondTimedWait(*cond, *mutex, abstime);
160 }
161 
163  const pthread_condattr_t *attr)
164 {
165  return OSInitCond(cond);
166 }
167 
169 {
170  return LWP_CondSignal(*cond);
171 }
172 
174 {
175  return LWP_CondBroadcast(*cond);
176 }
177 
179 {
180  return LWP_CondDestroy(*cond);
181 }
182 
183 extern int pthread_equal(pthread_t t1, pthread_t t2);
184 
185 #endif
int pthread_attr_t
Definition: gx_pthread.h:88
int mutex_t
typedef for the mutex handle
Definition: lock.c:6
#define INLINE
Definition: retro_inline.h:35
void * pthread_mutexattr_t
Definition: gx_pthread.h:87
s32 LWP_MutexDestroy(mutex_t mutex)
Close mutex lock, release all threads and handles locked on this mutex.
Definition: mutex.c:122
mutex_t pthread_mutex_t
Definition: gx_pthread.h:86
static INLINE void pthread_exit(void *retval)
Definition: gx_pthread.h:127
static int cond(LexState *ls)
Definition: lparser.c:1177
OSCond pthread_cond_t
Definition: gx_pthread.h:89
#define OSCond
Definition: gx_pthread.h:36
typedef void(__stdcall *PFN_DESTRUCTION_CALLBACK)(void *pData)
static sys_sem mutex
Definition: memp.c:120
Core header which includes all subsequent subsystem headers.
lwp_t LWP_GetSelf()
Return the handle to the current thread.
Definition: lwp.c:252
#define OSTryLockMutex(mutex)
Definition: gx_pthread.h:56
s32 LWP_CondDestroy(cond_t cond)
Destroy condition variable, release all threads and handles blocked on that condition variable.
Definition: cond.c:186
#define OSJoinThread(thread, val)
Definition: gx_pthread.h:76
s32 LWP_CondSignal(cond_t cond)
Signal a specific thread waiting on this condition variable to wake up.
Definition: cond.c:167
#define OSInitCond(cond)
Definition: gx_pthread.h:60
OSCond pthread_condattr_t
Definition: gx_pthread.h:90
Definition: implement.h:146
Definition: thread.h:137
Definition: psp_pthread.h:45
static INLINE pthread_t pthread_self(void)
Definition: gx_pthread.h:100
static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: gx_pthread.h:117
static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
Definition: gx_pthread.h:145
static INLINE int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
Definition: gx_pthread.h:156
#define OSLockMutex(mutex)
Definition: gx_pthread.h:48
void * pthread_t
Definition: pthread.h:398
static INLINE int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: gx_pthread.h:92
static INLINE int pthread_cond_signal(pthread_cond_t *cond)
Definition: gx_pthread.h:168
static INLINE int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: gx_pthread.h:150
s32 LWP_CondBroadcast(cond_t cond)
Broadcast all threads waiting on this condition variable to wake up.
Definition: cond.c:172
static INLINE int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: gx_pthread.h:106
#define OSWaitCond(cond, mutex)
Definition: gx_pthread.h:64
OSThread pthread_t
Definition: gx_pthread.h:85
static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: gx_pthread.h:112
static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: gx_pthread.h:122
#define OSUnlockMutex(mutex)
Definition: gx_pthread.h:52
#define OSInitMutex(mutex)
Definition: gx_pthread.h:44
static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: gx_pthread.h:173
#define OSCreateThread(thread, func, intarg, ptrarg, stackbase, stacksize, priority, attrs)
Definition: gx_pthread.h:80
Definition: implement.h:163
Definition: implement.h:116
int pthread_equal(pthread_t t1, pthread_t t2)
Definition: pthread.c:615
#define STACKSIZE
Definition: gx_pthread.h:83
s32 LWP_CondTimedWait(cond_t cond, mutex_t mutex, const struct timespec *abstime)
Timed wait on a conditionvariable.
Definition: cond.c:177
static INLINE int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: gx_pthread.h:162
Definition: implement.h:251
static INLINE int pthread_join(pthread_t thread, void **retval)
Definition: gx_pthread.h:140
static INLINE int pthread_detach(pthread_t thread)
Definition: gx_pthread.h:133
static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
Definition: gx_pthread.h:178