mirror of
https://github.com/switchbrew/libnx.git
synced 2025-07-05 02:52:13 +02:00
Update for review #125
This commit is contained in:
parent
18234cbcc3
commit
c243530687
@ -1,29 +1,24 @@
|
||||
// Copyright 2018 Kevoot
|
||||
|
||||
#ifndef __SEMAPHORE_H
|
||||
#define __SEMAPHORE_H
|
||||
/**
|
||||
* @file semaphore.h
|
||||
* @brief Thread synchronization based on Mutex.
|
||||
* @author Kevoot
|
||||
* @copyright libnx Authors
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "mutex.h"
|
||||
|
||||
#define SEM_DOWN false
|
||||
#define SEM_UP true
|
||||
|
||||
#define EBUSY 1
|
||||
|
||||
typedef struct sem_t
|
||||
typedef struct Semaphore
|
||||
{
|
||||
bool flag;
|
||||
bool is_up;
|
||||
Mutex mutex;
|
||||
} sem_t;
|
||||
} Semaphore;
|
||||
|
||||
void sem_init(sem_t *);
|
||||
void sem_uninit(sem_t *);
|
||||
void semaphoreInit(Semaphore *);
|
||||
/* TODO */
|
||||
void set_post(sem_t *);
|
||||
void sem_up(sem_t *);
|
||||
void sem_down(sem_t *);
|
||||
void sem_wait(sem_t *);
|
||||
void sem_waitup(sem_t *);
|
||||
bool sem_isup(sem_t *);
|
||||
|
||||
#endif
|
||||
void semaphorePost(Semaphore *);
|
||||
void semaphoreUp(Semaphore *);
|
||||
void semaphoreDown(Semaphore *);
|
||||
void semaphoreWait(Semaphore *);
|
||||
void semaphoreWaitUp(Semaphore *);
|
||||
bool semaphoreIsUp(Semaphore *);
|
||||
|
@ -2,52 +2,48 @@
|
||||
#include "kernel/semaphore.h"
|
||||
#include "kernel/svc.h"
|
||||
|
||||
void _priv_sem_wait(sem_t *sem, bool flag);
|
||||
void _privSemaphoreWait(Semaphore *sem, bool is_up);
|
||||
|
||||
void sem_down(sem_t *sem) {
|
||||
void semaphoreDown(Semaphore *sem) {
|
||||
mutexLock(&sem->mutex);
|
||||
sem->flag = SEM_DOWN;
|
||||
sem->is_up = false;
|
||||
mutexUnlock(&sem->mutex);
|
||||
}
|
||||
|
||||
void sem_init(sem_t *sem) {
|
||||
sem->flag = SEM_DOWN;
|
||||
void semaphoreInit(Semaphore *sem) {
|
||||
sem->is_up = false;
|
||||
mutexInit(&sem->mutex);
|
||||
}
|
||||
|
||||
void sem_uninit(sem_t *sem) {
|
||||
sem->flag = SEM_DOWN;
|
||||
}
|
||||
|
||||
void sem_up(sem_t *sem) {
|
||||
void semaphoreUp(Semaphore *sem) {
|
||||
mutexLock(&sem->mutex);
|
||||
sem->flag = SEM_UP;
|
||||
sem->is_up = true;
|
||||
mutexUnlock(&sem->mutex);
|
||||
}
|
||||
|
||||
void sem_wait(sem_t *sem) {
|
||||
_priv_sem_wait(sem, SEM_DOWN);
|
||||
void semaphoreWait(Semaphore *sem) {
|
||||
_privSemaphoreWait(sem, false);
|
||||
}
|
||||
|
||||
void sem_waitup(sem_t *sem) {
|
||||
_priv_sem_wait(sem, SEM_UP);
|
||||
void semaphoreWaitUp(Semaphore *sem) {
|
||||
_privSemaphoreWait(sem, true);
|
||||
}
|
||||
|
||||
bool sem_isup(sem_t *sem) {
|
||||
bool flag;
|
||||
bool semaphoreIsUp(Semaphore *sem) {
|
||||
bool is_up;
|
||||
|
||||
mutexLock(&sem->mutex);
|
||||
flag = sem->flag;
|
||||
is_up = sem->is_up;
|
||||
mutexUnlock(&sem->mutex);
|
||||
|
||||
return flag;
|
||||
return is_up;
|
||||
}
|
||||
|
||||
void _priv_sem_wait(sem_t *sem, bool flag) {
|
||||
void _SemaphoreWait(Semaphore *sem, bool is_up) {
|
||||
while (true) {
|
||||
mutexLock(&sem->mutex);
|
||||
|
||||
if (sem->flag == flag) {
|
||||
if (sem->is_up == is_up) {
|
||||
mutexUnlock(&sem->mutex);
|
||||
break;
|
||||
}
|
||||
@ -55,4 +51,4 @@ void _priv_sem_wait(sem_t *sem, bool flag) {
|
||||
mutexUnlock(&sem->mutex);
|
||||
svcSleepThread(1000000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user