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