libnx/nx/source/kernel/semaphore.c
2018-07-01 00:45:41 +02:00

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;
}