+typedef Thread pthread_t;
+typedef LightLock pthread_mutex_t;
+typedef void* pthread_mutexattr_t;
+typedef int pthread_attr_t;
+typedef LightEvent pthread_cond_t;
+typedef int pthread_condattr_t;
+#endif
+
+#ifndef USE_CTRULIB_2
+/* Backported CondVar API from libctru 2.0, and under its license:
+ https://github.com/devkitPro/libctru
+ Slightly modified for compatibility with older libctru. */
+
+typedef s32 CondVar;
+
+static inline Result syncArbitrateAddress(s32* addr, ArbitrationType type, s32 value)
+{
+ return svcArbitrateAddress(__sync_get_arbiter(), (u32)addr, type, value, 0);
+}
+
+static inline Result syncArbitrateAddressWithTimeout(s32* addr, ArbitrationType type, s32 value, s64 timeout_ns)
+{
+ return svcArbitrateAddress(__sync_get_arbiter(), (u32)addr, type, value, timeout_ns);
+}
+
+static inline void __dmb(void)
+{
+ __asm__ __volatile__("mcr p15, 0, %[val], c7, c10, 5" :: [val] "r" (0) : "memory");
+}
+
+static inline void CondVar_BeginWait(CondVar* cv, LightLock* lock)
+{
+ s32 val;
+ do
+ val = __ldrex(cv) - 1;
+ while (__strex(cv, val));
+ LightLock_Unlock(lock);
+}
+
+static inline bool CondVar_EndWait(CondVar* cv, s32 num_threads)
+{
+ bool hasWaiters;
+ s32 val;
+
+ do {
+ val = __ldrex(cv);
+ hasWaiters = val < 0;
+ if (hasWaiters)
+ {
+ if (num_threads < 0)
+ val = 0;
+ else if (val <= -num_threads)
+ val += num_threads;
+ else
+ val = 0;
+ }
+ } while (__strex(cv, val));
+
+ return hasWaiters;
+}
+
+static inline void CondVar_Init(CondVar* cv)
+{
+ *cv = 0;
+}
+
+static inline void CondVar_Wait(CondVar* cv, LightLock* lock)
+{
+ CondVar_BeginWait(cv, lock);
+ syncArbitrateAddress(cv, ARBITRATION_WAIT_IF_LESS_THAN, 0);
+ LightLock_Lock(lock);
+}
+
+static inline int CondVar_WaitTimeout(CondVar* cv, LightLock* lock, s64 timeout_ns)
+{
+ CondVar_BeginWait(cv, lock);
+
+ bool timedOut = false;
+ Result rc = syncArbitrateAddressWithTimeout(cv, ARBITRATION_WAIT_IF_LESS_THAN_TIMEOUT, 0, timeout_ns);
+ if (R_DESCRIPTION(rc) == RD_TIMEOUT)
+ {
+ timedOut = CondVar_EndWait(cv, 1);
+ __dmb();
+ }
+
+ LightLock_Lock(lock);
+ return timedOut;
+}