mirror of
https://github.com/switchbrew/libnx.git
synced 2025-07-05 10:52:15 +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
|
||||
* @brief Thread synchronization based on Mutex.
|
||||
* @author Kevoot
|
||||
* @author SciresM & Kevoot
|
||||
* @copyright libnx Authors
|
||||
*/
|
||||
#pragma once
|
||||
@ -9,14 +9,36 @@
|
||||
#include "mutex.h"
|
||||
#include "condvar.h"
|
||||
|
||||
/// Semaphore structure.
|
||||
typedef struct Semaphore
|
||||
{
|
||||
CondVar condvar;
|
||||
Mutex mutex;
|
||||
u64 count;
|
||||
CondVar condvar; ///< Conditional Variable Object.
|
||||
Mutex mutex; ///< Mutex Object.
|
||||
u64 count; ///< Internal Counter.
|
||||
} Semaphore;
|
||||
|
||||
void semaphoreInit(Semaphore *, u64);
|
||||
void semaphoreSignal(Semaphore *);
|
||||
void semaphoreWait(Semaphore *);
|
||||
bool semaphoreTryWait(Semaphore *);
|
||||
/**
|
||||
* @brief Initializes a semaphore and its internal counter.
|
||||
* @param s Semaphore object.
|
||||
* @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,34 +2,36 @@
|
||||
#include "kernel/semaphore.h"
|
||||
#include "kernel/svc.h"
|
||||
|
||||
void semaphoreInit(Semaphore *sem, u64 c) {
|
||||
sem->count = c;
|
||||
mutexInit(&sem->mutex);
|
||||
condvarInit(&sem->condvar, &sem->mutex);
|
||||
void semaphoreInit(Semaphore *s, u64 initial_count) {
|
||||
s->count = initial_count;
|
||||
mutexInit(&s->mutex);
|
||||
condvarInit(&s->condvar, &s->mutex);
|
||||
}
|
||||
|
||||
void semaphoreSignal(Semaphore *sem) {
|
||||
mutexLock(&sem->mutex);
|
||||
sem->count++;
|
||||
condvarWakeOne(&sem->condvar);
|
||||
mutexUnlock(&sem->mutex);
|
||||
void semaphoreSignal(Semaphore *s) {
|
||||
mutexLock(&s->mutex);
|
||||
s->count++;
|
||||
condvarWakeOne(&s->condvar);
|
||||
mutexUnlock(&s->mutex);
|
||||
}
|
||||
|
||||
void semaphoreWait(Semaphore *sem) {
|
||||
mutexLock(&sem->mutex);
|
||||
while(!(&sem->count)) {
|
||||
condvarWait(&sem->condvar);
|
||||
void semaphoreWait(Semaphore *s) {
|
||||
mutexLock(&s->mutex);
|
||||
// Wait until signalled.
|
||||
while (!s->count) {
|
||||
condvarWait(&s->condvar);
|
||||
}
|
||||
sem->count--;
|
||||
mutexUnlock(&sem->mutex);
|
||||
s->count--;
|
||||
mutexUnlock(&s->mutex);
|
||||
}
|
||||
|
||||
bool semaphoreTryWait(Semaphore *sem) {
|
||||
mutexLock(&sem->mutex);
|
||||
bool semaphoreTryWait(Semaphore *s) {
|
||||
mutexLock(&s->mutex);
|
||||
bool success = false;
|
||||
if(sem->count) {
|
||||
(sem->count)--;
|
||||
// Check and immediately return success.
|
||||
if (s->count) {
|
||||
s->count--;
|
||||
success = true;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user