mirror of
https://github.com/switchbrew/libnx.git
synced 2025-06-21 20:42:44 +02:00
38 lines
805 B
C
38 lines
805 B
C
// Copyright 2018 Kevoot
|
|
#include "kernel/semaphore.h"
|
|
#include "kernel/svc.h"
|
|
|
|
void semaphoreInit(Semaphore *s, u64 initial_count) {
|
|
s->count = initial_count;
|
|
mutexInit(&s->mutex);
|
|
condvarInit(&s->condvar, &s->mutex);
|
|
}
|
|
|
|
void semaphoreSignal(Semaphore *s) {
|
|
mutexLock(&s->mutex);
|
|
s->count++;
|
|
condvarWakeOne(&s->condvar);
|
|
mutexUnlock(&s->mutex);
|
|
}
|
|
|
|
void semaphoreWait(Semaphore *s) {
|
|
mutexLock(&s->mutex);
|
|
// Wait until signalled.
|
|
while (!s->count) {
|
|
condvarWait(&s->condvar);
|
|
}
|
|
s->count--;
|
|
mutexUnlock(&s->mutex);
|
|
}
|
|
|
|
bool semaphoreTryWait(Semaphore *s) {
|
|
mutexLock(&s->mutex);
|
|
bool success = false;
|
|
// Check and immediately return success.
|
|
if (s->count) {
|
|
s->count--;
|
|
success = true;
|
|
}
|
|
return success;
|
|
}
|