diff --git a/nx/include/machine/_threads.h b/nx/include/machine/_threads.h index fb898d8c..78ef19cb 100644 --- a/nx/include/machine/_threads.h +++ b/nx/include/machine/_threads.h @@ -2,6 +2,7 @@ #include "../switch/types.h" #include "../switch/result.h" #include "../switch/kernel/mutex.h" +#include "../switch/kernel/condvar.h" #include "../switch/kernel/thread.h" #define TSS_DTOR_ITERATIONS 1 @@ -11,7 +12,7 @@ typedef struct { int rc; } __thrd_t; -typedef u32 cnd_t; +typedef CondVar cnd_t; typedef __thrd_t* thrd_t; typedef u32 tss_t; diff --git a/nx/source/runtime/c11-threads.c b/nx/source/runtime/c11-threads.c index 27694fce..2d0e522d 100644 --- a/nx/source/runtime/c11-threads.c +++ b/nx/source/runtime/c11-threads.c @@ -32,7 +32,7 @@ int cnd_broadcast(cnd_t *cond) if (!cond) return thrd_error; - Result rc = svcSignalProcessWideKey(cond, -1); + Result rc = condvarWakeAll(cond); return R_SUCCEEDED(rc) ? thrd_success : thrd_error; } @@ -46,7 +46,7 @@ int cnd_init(cnd_t *cond) if (!cond) return thrd_error; - *cond = 0; + condvarInit(cond); return thrd_success; } @@ -55,7 +55,7 @@ int cnd_signal(cnd_t *cond) if (!cond) return thrd_error; - Result rc = svcSignalProcessWideKey(cond, 1); + Result rc = condvarWakeOne(cond); return R_SUCCEEDED(rc) ? thrd_success : thrd_error; } @@ -64,13 +64,7 @@ static int __cnd_timedwait(cnd_t *__restrict cond, mtx_t *__restrict mtx, u64 ti if (!cond || !mtx || mtx->type != mtx_plain) return thrd_error; - Result rc = svcWaitProcessWideKeyAtomic((u32*)&mtx->mutex, cond, getThreadVars()->handle, timeout); - - // On timeout, we need to acquire it manually. - if (rc == 0xEA01) { - mutexLock(&mtx->mutex); - return thrd_busy; - } + Result rc = condvarWaitTimeout(cond, &mtx->mutex, timeout); return R_SUCCEEDED(rc) ? thrd_success : thrd_error; } @@ -285,8 +279,7 @@ int thrd_sleep(const struct timespec *duration, struct timespec *remaining) void thrd_yield(void) { - // This is trash. - svcSleepThread(10000); + svcSleepThread(-1); } /*