From: falcovorbis <fal...@us...> - 2023-08-25 05:00:27
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "A pseudo Operating System for the Dreamcast.". The branch, master has been updated via 44a00891dbb947f17d34fcc9cf66cc0aa67f37e1 (commit) via 8cb450f8bdb86995faa78d6b8245f7dca2f16361 (commit) from f848bf2b1e89a10fc7c5a097e81f28a7ab39ea03 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 44a00891dbb947f17d34fcc9cf66cc0aa67f37e1 Merge: f848bf2 8cb450f Author: Falco Girgis <gyr...@gm...> Date: Thu Aug 24 23:58:47 2023 -0500 Merge pull request #276 from KallistiOS/simple-once Simplify kthread_once_t into a simple variable rather than a struct commit 8cb450f8bdb86995faa78d6b8245f7dca2f16361 Author: Lawrence Sebald <ljs...@us...> Date: Fri Aug 25 00:23:00 2023 -0400 Simplify kthread_once_t into a simple variable rather than a struct. ----------------------------------------------------------------------- Summary of changes: doc/CHANGELOG | 1 + examples/dreamcast/basic/threading/once/once_test.c | 8 ++++++-- include/kos/once.h | 15 ++++++--------- kernel/thread/once.c | 11 +++++++---- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/doc/CHANGELOG b/doc/CHANGELOG index 2d163c9..df01c9b 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -183,6 +183,7 @@ KallistiOS version 2.1.0 ----------------------------------------------- - DC Refactored controller API, added capability groups and types [FG] - DC VMU driver update for date/time, buttons, buzzer, and docs [FG] - DC Add spinlock_trylock macro [LS] +- *** Simplify kthread_once_t into a simple variable rather than a struct [LS] KallistiOS version 2.0.0 ----------------------------------------------- - DC Broadband Adapter driver fixes [Megan Potter == MP] diff --git a/examples/dreamcast/basic/threading/once/once_test.c b/examples/dreamcast/basic/threading/once/once_test.c index da3a77e..3029894 100644 --- a/examples/dreamcast/basic/threading/once/once_test.c +++ b/examples/dreamcast/basic/threading/once/once_test.c @@ -1,11 +1,11 @@ /* KallistiOS ##version## once_test.c - Copyright (C) 2009 Lawrence Sebald + Copyright (C) 2009, 2023 Lawrence Sebald */ -/* This program is a test for the kthread_once_t type added in KOS 1.3.0. A once +/* This program is a test for the kthread_once_t type added in KOS 2.0.0. A once object is used with the kthread_once function to ensure that an initializer function is only run once in a program (meaning multiple threads will not run the function. */ @@ -15,6 +15,7 @@ #include <kos/once.h> #include <arch/arch.h> +#include <arch/spinlock.h> #include <dc/maple.h> #include <dc/maple/controller.h> @@ -22,10 +23,13 @@ #define THD_COUNT 600 kthread_once_t once = KTHREAD_ONCE_INIT; +spinlock_t lock = SPINLOCK_INITIALIZER; int counter = 0; void once_func(void) { + spinlock_lock(&lock); ++counter; + spinlock_unlock(&lock); } void *thd_func(void *param UNUSED) { diff --git a/include/kos/once.h b/include/kos/once.h index b303b37..5bdcc9f 100644 --- a/include/kos/once.h +++ b/include/kos/once.h @@ -1,7 +1,7 @@ /* KallistiOS ##version## include/kos/once.h - Copyright (C) 2009, 2010 Lawrence Sebald + Copyright (C) 2009, 2010, 2023 Lawrence Sebald */ @@ -31,13 +31,10 @@ __BEGIN_DECLS \headerfile kos/once.h */ -typedef struct { - int initialized; - int run; -} kthread_once_t; +typedef volatile int kthread_once_t; /** \brief Initializer for a kthread_once_t object. */ -#define KTHREAD_ONCE_INIT { 1, 0 } +#define KTHREAD_ONCE_INIT 0 /** \brief Run a function once. @@ -48,9 +45,9 @@ typedef struct { \param once_control The kthread_once_t object to run against. \param init_routine The function to call. - \retval -1 On failure, and sets errno to one of the following: ENOMEM - if out of memory, EPERM if called inside an interrupt, or - EINTR if interrupted. + \retval -1 On failure, and sets errno to one of the following: EPERM if + called inside an interrupt or EINVAL if *once_control is not + valid or was not initialized with KTHREAD_ONCE_INIT. \retval 0 On success. */ int kthread_once(kthread_once_t *once_control, void (*init_routine)(void)); diff --git a/kernel/thread/once.c b/kernel/thread/once.c index 953ac27..1d3f29c 100644 --- a/kernel/thread/once.c +++ b/kernel/thread/once.c @@ -1,7 +1,7 @@ /* KallistiOS ##version## once.c - Copyright (C) 2009 Lawrence Sebald + Copyright (C) 2009, 2023 Lawrence Sebald */ #include <malloc.h> @@ -18,7 +18,10 @@ static mutex_t lock = RECURSIVE_MUTEX_INITIALIZER; int kthread_once(kthread_once_t *once_control, void (*init_routine)(void)) { - assert(once_control); + if(!once_control || *once_control < 0 || *once_control > 1) { + errno = EINVAL; + return -1; + } /* Lock the lock. */ if(mutex_lock(&lock) == -1) { @@ -26,13 +29,13 @@ int kthread_once(kthread_once_t *once_control, void (*init_routine)(void)) { } /* If the function has already been run, unlock the lock and return. */ - if(once_control->run) { + if(*once_control) { mutex_unlock(&lock); return 0; } /* Run the function, set the control, and unlock the lock. */ - once_control->run = 1; + *once_control = 1; init_routine(); mutex_unlock(&lock); hooks/post-receive -- A pseudo Operating System for the Dreamcast. |