diff --git a/include/stratosphere/hossynch.hpp b/include/stratosphere/hossynch.hpp index 4f0cbd2d..6a393a4f 100644 --- a/include/stratosphere/hossynch.hpp +++ b/include/stratosphere/hossynch.hpp @@ -178,6 +178,16 @@ class TimeoutHelper { return (tick * 625) / 12; } + u64 NsUntilTimeout() { + u64 diff = TickToNs(this->end_tick - armGetSystemTick()); + + if (TimedOut()) { + return 0; + } + + return diff; + } + bool TimedOut() { if (this->end_tick == 0) { return true; @@ -187,6 +197,63 @@ class TimeoutHelper { } }; +class HosSignal { + private: + CondVar cv; + Mutex m; + bool signaled; + public: + HosSignal() { + condvarInit(&cv); + mutexInit(&m); + signaled = false; + } + + void Signal() { + mutexLock(&m); + signaled = true; + condvarWakeAll(&cv); + mutexUnlock(&m); + } + + void Reset() { + mutexLock(&m); + signaled = false; + mutexUnlock(&m); + } + + void Wait() { + mutexLock(&m); + + while (!signaled) { + condvarWait(&cv, &m); + } + + mutexUnlock(&m); + } + + bool TryWait() { + mutexLock(&m); + bool success = signaled; + mutexUnlock(&m); + return success; + } + + Result TimedWait(u64 ns) { + mutexLock(&m); + TimeoutHelper timeout_helper(ns); + + while (!signaled) { + if (R_FAILED(condvarWaitTimeout(&cv, &m, timeout_helper.NsUntilTimeout()))) { + return false; + } + } + + mutexUnlock(&m); + return true; + } +}; + class HosThread { private: Thread thr = {0};