1 /* Copyright (C) 2010-2016 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (wiiu_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 _WIIU_PTHREAD_WRAP_WIIU_
24 #define _WIIU_PTHREAD_WRAP_WIIU_
26 #include <retro_inline.h>
27 #include <wiiu/os/condition.h>
28 #include <wiiu/os/thread.h>
29 #include <wiiu/os/mutex.h>
31 #define STACKSIZE (8 * 1024)
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;
40 static INLINE int pthread_create(pthread_t *thread,
41 const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
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);
54 return (ret == true) ? 0 : -1;
57 static INLINE pthread_t pthread_self(void)
59 return OSGetCurrentThread();
62 static INLINE int pthread_mutex_init(pthread_mutex_t *mutex,
63 const pthread_mutexattr_t *attr)
65 OSMutex *m = malloc(sizeof(OSMutex));
71 static INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
79 static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
85 static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
87 OSUnlockMutex(*mutex);
91 static INLINE void pthread_exit(void *retval)
97 static INLINE int pthread_detach(pthread_t thread)
99 OSDetachThread(thread);
103 static INLINE int pthread_join(pthread_t thread, void **retval)
106 bool ret = OSJoinThread(thread, NULL);
109 free(thread->stackEnd);
112 return (ret == true) ? 0 : -1;
115 static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
117 return OSTryLockMutex(*mutex);
120 static INLINE int pthread_cond_wait(pthread_cond_t *cond,
121 pthread_mutex_t *mutex)
123 OSWaitCond(*cond, *mutex);
127 static INLINE int pthread_cond_timedwait(pthread_cond_t *cond,
128 pthread_mutex_t *mutex, const struct timespec *abstime)
130 //FIXME: actual timeout needed
132 return pthread_cond_wait(cond, mutex);
135 static INLINE int pthread_cond_init(pthread_cond_t *cond,
136 const pthread_condattr_t *attr)
138 OSCondition *c = malloc(sizeof(OSCondition));
144 static INLINE int pthread_cond_signal(pthread_cond_t *cond)
150 static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
152 //FIXME: no OS equivalent
157 static INLINE int pthread_cond_destroy(pthread_cond_t *cond)
165 extern int pthread_equal(pthread_t t1, pthread_t t2);