mirror of
https://github.com/switchbrew/libnx.git
synced 2025-07-05 10:52:15 +02:00
Changed implementation to match SciresM version
Now similar to SciresM HosSemaphore class
This commit is contained in:
parent
c243530687
commit
e8fc3dbe13
@ -7,18 +7,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "mutex.h"
|
#include "mutex.h"
|
||||||
|
#include "condvar.h"
|
||||||
|
|
||||||
typedef struct Semaphore
|
typedef struct Semaphore
|
||||||
{
|
{
|
||||||
bool is_up;
|
CondVar condvar;
|
||||||
Mutex mutex;
|
Mutex mutex;
|
||||||
|
u64 count;
|
||||||
} Semaphore;
|
} Semaphore;
|
||||||
|
|
||||||
void semaphoreInit(Semaphore *);
|
void semaphoreInit(Semaphore *, u64);
|
||||||
/* TODO */
|
void semaphoreSignal(Semaphore *);
|
||||||
void semaphorePost(Semaphore *);
|
|
||||||
void semaphoreUp(Semaphore *);
|
|
||||||
void semaphoreDown(Semaphore *);
|
|
||||||
void semaphoreWait(Semaphore *);
|
void semaphoreWait(Semaphore *);
|
||||||
void semaphoreWaitUp(Semaphore *);
|
bool semaphoreTryWait(Semaphore *);
|
||||||
bool semaphoreIsUp(Semaphore *);
|
|
||||||
|
@ -2,53 +2,34 @@
|
|||||||
#include "kernel/semaphore.h"
|
#include "kernel/semaphore.h"
|
||||||
#include "kernel/svc.h"
|
#include "kernel/svc.h"
|
||||||
|
|
||||||
void _privSemaphoreWait(Semaphore *sem, bool is_up);
|
void semaphoreInit(Semaphore *sem, u64 c) {
|
||||||
|
sem->count = c;
|
||||||
void semaphoreDown(Semaphore *sem) {
|
|
||||||
mutexLock(&sem->mutex);
|
|
||||||
sem->is_up = false;
|
|
||||||
mutexUnlock(&sem->mutex);
|
|
||||||
}
|
|
||||||
|
|
||||||
void semaphoreInit(Semaphore *sem) {
|
|
||||||
sem->is_up = false;
|
|
||||||
mutexInit(&sem->mutex);
|
mutexInit(&sem->mutex);
|
||||||
|
condvarInit(&sem->condvar, &sem->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void semaphoreUp(Semaphore *sem) {
|
void semaphoreSignal(Semaphore *sem) {
|
||||||
mutexLock(&sem->mutex);
|
mutexLock(&sem->mutex);
|
||||||
sem->is_up = true;
|
sem->count++;
|
||||||
|
condvarWakeOne(&sem->condvar);
|
||||||
mutexUnlock(&sem->mutex);
|
mutexUnlock(&sem->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void semaphoreWait(Semaphore *sem) {
|
void semaphoreWait(Semaphore *sem) {
|
||||||
_privSemaphoreWait(sem, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void semaphoreWaitUp(Semaphore *sem) {
|
|
||||||
_privSemaphoreWait(sem, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool semaphoreIsUp(Semaphore *sem) {
|
|
||||||
bool is_up;
|
|
||||||
|
|
||||||
mutexLock(&sem->mutex);
|
mutexLock(&sem->mutex);
|
||||||
is_up = sem->is_up;
|
while(!(&sem->count)) {
|
||||||
mutexUnlock(&sem->mutex);
|
condvarWait(&sem->condvar);
|
||||||
|
|
||||||
return is_up;
|
|
||||||
}
|
|
||||||
|
|
||||||
void _SemaphoreWait(Semaphore *sem, bool is_up) {
|
|
||||||
while (true) {
|
|
||||||
mutexLock(&sem->mutex);
|
|
||||||
|
|
||||||
if (sem->is_up == is_up) {
|
|
||||||
mutexUnlock(&sem->mutex);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
mutexUnlock(&sem->mutex);
|
|
||||||
svcSleepThread(1000000);
|
|
||||||
}
|
}
|
||||||
|
sem->count--;
|
||||||
|
mutexUnlock(&sem->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool semaphoreTryWait(Semaphore *sem) {
|
||||||
|
mutexLock(&sem->mutex);
|
||||||
|
bool success = false;
|
||||||
|
if(sem->count) {
|
||||||
|
(sem->count)--;
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user