mirror of
https://github.com/Atmosphere-NX/Atmosphere-libs.git
synced 2025-06-27 05:22:46 +02:00
Add HosSignal primitive
This commit is contained in:
parent
05015b9354
commit
fa37b70b0e
@ -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};
|
||||
|
Loading…
Reference in New Issue
Block a user