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 _CTR_PTHREAD_WRAP_CTR_
24 #define _CTR_PTHREAD_WRAP_CTR_
26 #include <3ds/thread.h>
27 #include <3ds/services/apt.h>
28 #include "3ds_utils.h"
34 #define STACKSIZE (4 * 1024)
37 #ifndef PTHREAD_SCOPE_PROCESS
38 /* An earlier version of devkitARM does not define the pthread types. Can remove in r54+. */
40 typedef Thread pthread_t;
41 typedef LightLock pthread_mutex_t;
42 typedef void* pthread_mutexattr_t;
43 typedef int pthread_attr_t;
44 typedef LightEvent pthread_cond_t;
45 typedef int pthread_condattr_t;
49 /* Backported CondVar API from libctru 2.0, and under its license:
50 https://github.com/devkitPro/libctru
51 Slightly modified for compatibility with older libctru. */
55 static inline Result syncArbitrateAddress(s32* addr, ArbitrationType type, s32 value)
57 return svcArbitrateAddress(__sync_get_arbiter(), (u32)addr, type, value, 0);
60 static inline Result syncArbitrateAddressWithTimeout(s32* addr, ArbitrationType type, s32 value, s64 timeout_ns)
62 return svcArbitrateAddress(__sync_get_arbiter(), (u32)addr, type, value, timeout_ns);
65 static inline void __dmb(void)
67 __asm__ __volatile__("mcr p15, 0, %[val], c7, c10, 5" :: [val] "r" (0) : "memory");
70 static inline void CondVar_BeginWait(CondVar* cv, LightLock* lock)
74 val = __ldrex(cv) - 1;
75 while (__strex(cv, val));
76 LightLock_Unlock(lock);
79 static inline bool CondVar_EndWait(CondVar* cv, s32 num_threads)
91 else if (val <= -num_threads)
96 } while (__strex(cv, val));
101 static inline void CondVar_Init(CondVar* cv)
106 static inline void CondVar_Wait(CondVar* cv, LightLock* lock)
108 CondVar_BeginWait(cv, lock);
109 syncArbitrateAddress(cv, ARBITRATION_WAIT_IF_LESS_THAN, 0);
110 LightLock_Lock(lock);
113 static inline int CondVar_WaitTimeout(CondVar* cv, LightLock* lock, s64 timeout_ns)
115 CondVar_BeginWait(cv, lock);
117 bool timedOut = false;
118 Result rc = syncArbitrateAddressWithTimeout(cv, ARBITRATION_WAIT_IF_LESS_THAN_TIMEOUT, 0, timeout_ns);
119 if (R_DESCRIPTION(rc) == RD_TIMEOUT)
121 timedOut = CondVar_EndWait(cv, 1);
125 LightLock_Lock(lock);
129 static inline void CondVar_WakeUp(CondVar* cv, s32 num_threads)
132 if (CondVar_EndWait(cv, num_threads))
133 syncArbitrateAddress(cv, ARBITRATION_SIGNAL, num_threads);
138 static inline void CondVar_Signal(CondVar* cv)
140 CondVar_WakeUp(cv, 1);
143 static inline void CondVar_Broadcast(CondVar* cv)
145 CondVar_WakeUp(cv, ARBITRATION_SIGNAL_ALL);
147 /* End libctru 2.0 backport */
150 /* libctru threads return void but pthreads return void pointer */
151 static bool mutex_inited = false;
152 static LightLock safe_double_thread_launch;
153 static void *(*start_routine_jump)(void*);
155 static void ctr_thread_launcher(void* data)
157 void *(*start_routine_jump_safe)(void*) = start_routine_jump;
158 LightLock_Unlock(&safe_double_thread_launch);
159 start_routine_jump_safe(data);
162 static inline int pthread_create(pthread_t *thread,
163 const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
166 Thread new_ctr_thread;
167 int procnum = -2; // use default cpu
170 APT_CheckNew3DS(&isNew3DS);
177 LightLock_Init(&safe_double_thread_launch);
181 /*Must wait if attempting to launch 2 threads at once to prevent corruption of function pointer*/
182 while (LightLock_TryLock(&safe_double_thread_launch) != 0);
184 svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
186 start_routine_jump = start_routine;
187 new_ctr_thread = threadCreate(ctr_thread_launcher, arg, STACKSIZE, prio - 1, procnum, FALSE);
191 LightLock_Unlock(&safe_double_thread_launch);
195 *thread = (pthread_t)new_ctr_thread;
199 static inline pthread_t pthread_self(void)
201 return (pthread_t)threadGetCurrent();
204 static inline int pthread_mutex_init(pthread_mutex_t *mutex,
205 const pthread_mutexattr_t *attr)
207 LightLock_Init((LightLock *)mutex);
211 static inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
213 /*Nothing to destroy*/
217 static inline int pthread_mutex_lock(pthread_mutex_t *mutex)
219 LightLock_Lock((LightLock *)mutex);
223 static inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
225 LightLock_Unlock((LightLock *)mutex);
229 static inline void pthread_exit(void *retval)
231 /*Yes the pointer to int cast is not ideal*/
232 /*threadExit((int)retval);*/
238 static inline int pthread_detach(pthread_t thread)
240 threadDetach((Thread)thread);
244 static inline int pthread_join(pthread_t thread, void **retval)
246 /*retval is ignored*/
247 if(threadJoin((Thread)thread, INT64_MAX))
250 threadFree((Thread)thread);
255 static inline int pthread_mutex_trylock(pthread_mutex_t *mutex)
257 return LightLock_TryLock((LightLock *)mutex);
260 static inline int pthread_cond_wait(pthread_cond_t *cond,
261 pthread_mutex_t *mutex)
263 CondVar_Wait((CondVar *)cond, (LightLock *)mutex);
267 static inline int pthread_cond_timedwait(pthread_cond_t *cond,
268 pthread_mutex_t *mutex, const struct timespec *abstime)
270 struct timespec now = {0};
271 /* Missing clock_gettime*/
275 gettimeofday(&tm, NULL);
276 now.tv_sec = tm.tv_sec;
277 now.tv_nsec = tm.tv_usec * 1000;
278 s64 timeout = (abstime->tv_sec - now.tv_sec) * 1000000000 + (abstime->tv_nsec - now.tv_nsec);
284 else if (CondVar_WaitTimeout((CondVar *)cond, (LightLock *)mutex, timeout))
292 static inline int pthread_cond_init(pthread_cond_t *cond,
293 const pthread_condattr_t *attr)
295 CondVar_Init((CondVar *)cond);
299 static inline int pthread_cond_signal(pthread_cond_t *cond)
301 CondVar_Signal((CondVar *)cond);
305 static inline int pthread_cond_broadcast(pthread_cond_t *cond)
307 CondVar_Broadcast((CondVar *)cond);
311 static inline int pthread_cond_destroy(pthread_cond_t *cond)
313 /*Nothing to destroy*/
317 static inline int pthread_equal(pthread_t t1, pthread_t t2)
319 if (threadGetHandle((Thread)t1) == threadGetHandle((Thread)t2))