git subrepo clone https://github.com/libretro/libretro-common.git deps/libretro-common
[pcsx_rearmed.git] / deps / libretro-common / rthreads / wiiu_pthread.h
1 /* Copyright  (C) 2010-2016 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (wiiu_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 _WIIU_PTHREAD_WRAP_WIIU_
24 #define _WIIU_PTHREAD_WRAP_WIIU_
25
26 #include <retro_inline.h>
27 #include <wiiu/os/condition.h>
28 #include <wiiu/os/thread.h>
29 #include <wiiu/os/mutex.h>
30 #include <malloc.h>
31 #define STACKSIZE (8 * 1024)
32
33 typedef OSThread* pthread_t;
34 typedef OSMutex* pthread_mutex_t;
35 typedef void* pthread_mutexattr_t;
36 typedef int pthread_attr_t;
37 typedef OSCondition* pthread_cond_t;
38 typedef OSCondition* pthread_condattr_t;
39
40 static INLINE int pthread_create(pthread_t *thread,
41       const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
42 {
43    OSThread *t = memalign(8, sizeof(OSThread));
44    void *stack = memalign(32, STACKSIZE);
45    bool ret = OSCreateThread(t, (OSThreadEntryPointFn)start_routine, 
46       (uint32_t)arg, NULL, (void*)(((uint32_t)stack)+STACKSIZE), STACKSIZE, 8, OS_THREAD_ATTRIB_AFFINITY_ANY);
47    if(ret == true)
48    {
49       OSResumeThread(t);
50       *thread = t;
51    }
52    else
53       *thread = NULL;
54    return (ret == true) ? 0 : -1;
55 }
56
57 static INLINE pthread_t pthread_self(void)
58 {
59    return OSGetCurrentThread();
60 }
61
62 static INLINE int pthread_mutex_init(pthread_mutex_t *mutex,
63       const pthread_mutexattr_t *attr)
64 {
65    OSMutex *m = malloc(sizeof(OSMutex));
66    OSInitMutex(m);
67    *mutex = m;
68    return 0;
69 }
70
71 static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
72 {
73    if(*mutex)
74       free(*mutex);
75    *mutex = NULL;
76    return 0;
77 }
78
79 static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
80 {
81    OSLockMutex(*mutex);
82    return 0;
83 }
84
85 static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
86 {
87    OSUnlockMutex(*mutex);
88    return 0;
89 }
90
91 static INLINE void pthread_exit(void *retval)
92 {
93    (void)retval;
94    OSExitThread(0);
95 }
96
97 static INLINE int pthread_detach(pthread_t thread)
98 {
99    OSDetachThread(thread);
100    return 0;
101 }
102
103 static INLINE int pthread_join(pthread_t thread, void **retval)
104 {
105    (void)retval;
106    bool ret = OSJoinThread(thread, NULL);
107    if(ret == true)
108    {
109       free(thread->stackEnd);
110       free(thread);
111    }
112    return (ret == true) ? 0 : -1;
113 }
114
115 static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
116 {
117    return OSTryLockMutex(*mutex);
118 }
119
120 static INLINE int pthread_cond_wait(pthread_cond_t *cond,
121       pthread_mutex_t *mutex)
122 {
123    OSWaitCond(*cond, *mutex);
124    return 0;
125 }
126
127 static INLINE int pthread_cond_timedwait(pthread_cond_t *cond,
128       pthread_mutex_t *mutex, const struct timespec *abstime)
129 {
130    //FIXME: actual timeout needed
131    (void)abstime;
132    return pthread_cond_wait(cond, mutex);
133 }
134
135 static INLINE int pthread_cond_init(pthread_cond_t *cond,
136       const pthread_condattr_t *attr)
137 {
138    OSCondition *c = malloc(sizeof(OSCondition));
139    OSInitCond(c);
140    *cond = c;
141    return 0;
142 }
143
144 static INLINE int pthread_cond_signal(pthread_cond_t *cond)
145 {
146    OSSignalCond(*cond);
147    return 0;
148 }
149
150 static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
151 {
152    //FIXME: no OS equivalent
153    (void)cond;
154    return 0;
155 }
156
157 static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
158 {
159    if(*cond)
160       free(*cond);
161    *cond = NULL;
162    return 0;
163 }
164
165 extern int pthread_equal(pthread_t t1, pthread_t t2);
166
167 #endif