mirror of
https://github.com/switchbrew/libnx.git
synced 2025-07-05 19:02:14 +02:00
Readded changes from code review for code style and doxygen comments
This commit is contained in:
parent
e8fc3dbe13
commit
73e37d1ba2
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* @file semaphore.h
|
* @file semaphore.h
|
||||||
* @brief Thread synchronization based on Mutex.
|
* @brief Thread synchronization based on Mutex.
|
||||||
* @author Kevoot
|
* @author SciresM & Kevoot
|
||||||
* @copyright libnx Authors
|
* @copyright libnx Authors
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
@ -9,14 +9,36 @@
|
|||||||
#include "mutex.h"
|
#include "mutex.h"
|
||||||
#include "condvar.h"
|
#include "condvar.h"
|
||||||
|
|
||||||
|
/// Semaphore structure.
|
||||||
typedef struct Semaphore
|
typedef struct Semaphore
|
||||||
{
|
{
|
||||||
CondVar condvar;
|
CondVar condvar; ///< Conditional Variable Object.
|
||||||
Mutex mutex;
|
Mutex mutex; ///< Mutex Object.
|
||||||
u64 count;
|
u64 count; ///< Internal Counter.
|
||||||
} Semaphore;
|
} Semaphore;
|
||||||
|
|
||||||
void semaphoreInit(Semaphore *, u64);
|
/**
|
||||||
void semaphoreSignal(Semaphore *);
|
* @brief Initializes a semaphore and its internal counter.
|
||||||
void semaphoreWait(Semaphore *);
|
* @param s Semaphore object.
|
||||||
bool semaphoreTryWait(Semaphore *);
|
* @param initial_count initial value for internal counter (typically the # of free resources).
|
||||||
|
*/
|
||||||
|
void semaphoreInit(Semaphore * s, u64 initial_count);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Increments the Semaphore to allow other threads to continue.
|
||||||
|
* @param s Semaphore object.
|
||||||
|
*/
|
||||||
|
void semaphoreSignal(Semaphore * s);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Decrements Semaphore and waits if 0.
|
||||||
|
* @param s Semaphore object.
|
||||||
|
*/
|
||||||
|
void semaphoreWait(Semaphore * s);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Attempts to get lock without waiting.
|
||||||
|
* @param s Semaphore object.
|
||||||
|
* @return 1 if no wait is needed for lock, 0 otherwise.
|
||||||
|
*/
|
||||||
|
bool semaphoreTryWait(Semaphore * s);
|
||||||
|
@ -2,33 +2,35 @@
|
|||||||
#include "kernel/semaphore.h"
|
#include "kernel/semaphore.h"
|
||||||
#include "kernel/svc.h"
|
#include "kernel/svc.h"
|
||||||
|
|
||||||
void semaphoreInit(Semaphore *sem, u64 c) {
|
void semaphoreInit(Semaphore *s, u64 initial_count) {
|
||||||
sem->count = c;
|
s->count = initial_count;
|
||||||
mutexInit(&sem->mutex);
|
mutexInit(&s->mutex);
|
||||||
condvarInit(&sem->condvar, &sem->mutex);
|
condvarInit(&s->condvar, &s->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void semaphoreSignal(Semaphore *sem) {
|
void semaphoreSignal(Semaphore *s) {
|
||||||
mutexLock(&sem->mutex);
|
mutexLock(&s->mutex);
|
||||||
sem->count++;
|
s->count++;
|
||||||
condvarWakeOne(&sem->condvar);
|
condvarWakeOne(&s->condvar);
|
||||||
mutexUnlock(&sem->mutex);
|
mutexUnlock(&s->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void semaphoreWait(Semaphore *sem) {
|
void semaphoreWait(Semaphore *s) {
|
||||||
mutexLock(&sem->mutex);
|
mutexLock(&s->mutex);
|
||||||
while(!(&sem->count)) {
|
// Wait until signalled.
|
||||||
condvarWait(&sem->condvar);
|
while (!s->count) {
|
||||||
|
condvarWait(&s->condvar);
|
||||||
}
|
}
|
||||||
sem->count--;
|
s->count--;
|
||||||
mutexUnlock(&sem->mutex);
|
mutexUnlock(&s->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool semaphoreTryWait(Semaphore *sem) {
|
bool semaphoreTryWait(Semaphore *s) {
|
||||||
mutexLock(&sem->mutex);
|
mutexLock(&s->mutex);
|
||||||
bool success = false;
|
bool success = false;
|
||||||
if(sem->count) {
|
// Check and immediately return success.
|
||||||
(sem->count)--;
|
if (s->count) {
|
||||||
|
s->count--;
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
|
Loading…
Reference in New Issue
Block a user