From 73e37d1ba2e8052ebc10a5552b74cfaa41f536e1 Mon Sep 17 00:00:00 2001 From: Kevoot Date: Tue, 26 Jun 2018 20:15:33 -0400 Subject: [PATCH] Readded changes from code review for code style and doxygen comments --- nx/include/switch/kernel/semaphore.h | 38 +++++++++++++++++++------ nx/source/kernel/semaphore.c | 42 +++++++++++++++------------- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/nx/include/switch/kernel/semaphore.h b/nx/include/switch/kernel/semaphore.h index f7d27302..17bbc4c1 100644 --- a/nx/include/switch/kernel/semaphore.h +++ b/nx/include/switch/kernel/semaphore.h @@ -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); diff --git a/nx/source/kernel/semaphore.c b/nx/source/kernel/semaphore.c index 86bfbeaa..328e8414 100644 --- a/nx/source/kernel/semaphore.c +++ b/nx/source/kernel/semaphore.c @@ -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; -} \ No newline at end of file +}