From: ljsebald <ljs...@us...> - 2023-08-28 23:00:18
|
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 7094048da2d2c90301611fb963473702f281ef8e (commit) from 2c79c740e9fb3809158076ded9efd25b50b3b32e (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 7094048da2d2c90301611fb963473702f281ef8e Author: Lawrence Sebald <ljs...@us...> Date: Mon Aug 28 18:59:28 2023 -0400 Simplify the structures of some sync primitives. (#281) * Simplify the structures of some sync primitives. Mostly, this removes the useless "initialized" member of structs. * Update CHANGELOG * Fix spelling ----------------------------------------------------------------------- Summary of changes: doc/CHANGELOG | 1 + include/kos/cond.h | 4 +-- include/kos/mutex.h | 3 ++- include/kos/rwsem.h | 6 ++--- kernel/thread/cond.c | 34 ++++++-------------------- kernel/thread/mutex.c | 8 ++++-- kernel/thread/rwsem.c | 68 +++++++++------------------------------------------ 7 files changed, 32 insertions(+), 92 deletions(-) diff --git a/doc/CHANGELOG b/doc/CHANGELOG index fc8e1a7..c9c8bf5 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -184,6 +184,7 @@ KallistiOS version 2.1.0 ----------------------------------------------- - 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] +- *** Simplify sync primitive structures to remove initialized member [LS] - *** Move definition of __RESTRICT from <sys/_types.h> to <kos/cdefs.h> [LS] KallistiOS version 2.0.0 ----------------------------------------------- diff --git a/include/kos/cond.h b/include/kos/cond.h index aa50a19..5bccfa5 100644 --- a/include/kos/cond.h +++ b/include/kos/cond.h @@ -59,12 +59,12 @@ __BEGIN_DECLS \headerfile kos/cond.h */ typedef struct condvar { - int initialized; + int dummy; int dynamic; } condvar_t; /** \brief Initializer for a transient condvar. */ -#define COND_INITIALIZER { 1, 0 } +#define COND_INITIALIZER { 0, 0 } /** \brief Allocate a new condition variable. diff --git a/include/kos/mutex.h b/include/kos/mutex.h index 088f894..5a2f936 100644 --- a/include/kos/mutex.h +++ b/include/kos/mutex.h @@ -78,7 +78,8 @@ typedef struct kos_mutex { @{ */ -#define MUTEX_TYPE_NORMAL 1 /**< \brief Normal mutex type */ +#define MUTEX_TYPE_NORMAL 0 /**< \brief Normal mutex type */ +#define MUTEX_TYPE_OLDNORMAL 1 /**< \brief Alias for MUTEX_TYPE_NORMAL */ #define MUTEX_TYPE_ERRORCHECK 2 /**< \brief Error-checking mutex type */ #define MUTEX_TYPE_RECURSIVE 3 /**< \brief Recursive mutex type */ diff --git a/include/kos/rwsem.h b/include/kos/rwsem.h index dea6925..0995375 100644 --- a/include/kos/rwsem.h +++ b/include/kos/rwsem.h @@ -40,8 +40,8 @@ __BEGIN_DECLS \headerfile kos/rwsem.h */ typedef struct rw_semaphore { - /** \brief Are we initialized? */ - int initialized; + /** \brief Was this structure created with rwsem_create()? */ + int dynamic; /** \brief The number of readers that are currently holding the lock. */ int read_count; @@ -54,7 +54,7 @@ typedef struct rw_semaphore { } rw_semaphore_t; /** \brief Initializer for a transient reader/writer semaphore */ -#define RWSEM_INITIALIZER { 1, 0, NULL, NULL } +#define RWSEM_INITIALIZER { 0, 0, NULL, NULL } /** \brief Allocate a reader/writer semaphore. diff --git a/kernel/thread/cond.c b/kernel/thread/cond.c index 89600fa..979b317 100644 --- a/kernel/thread/cond.c +++ b/kernel/thread/cond.c @@ -34,14 +34,13 @@ condvar_t *cond_create(void) { return NULL; } - cv->initialized = 1; cv->dynamic = 1; return cv; } int cond_init(condvar_t *cv) { - cv->initialized = 1; + cv->dummy = 0; cv->dynamic = 0; return 0; } @@ -51,8 +50,6 @@ int cond_destroy(condvar_t *cv) { /* Give all sleeping threads a timed out error */ genwait_wake_all_err(cv, ENOTRECOVERABLE); - cv->initialized = 0; - /* Free the memory */ if(cv->dynamic) free(cv); @@ -71,13 +68,8 @@ int cond_wait_timed(condvar_t *cv, mutex_t *m, int timeout) { old = irq_disable(); - if(!cv->initialized) { - errno = EINVAL; - irq_restore(old); - return -1; - } - else if(m->type < MUTEX_TYPE_NORMAL || m->type > MUTEX_TYPE_RECURSIVE || - !mutex_is_locked(m)) { + if(m->type < MUTEX_TYPE_NORMAL || m->type > MUTEX_TYPE_RECURSIVE || + !mutex_is_locked(m)) { errno = EINVAL; irq_restore(old); return -1; @@ -111,14 +103,8 @@ int cond_signal(condvar_t *cv) { old = irq_disable(); - if(!cv->initialized) { - errno = EINVAL; - rv = -1; - } - else { - /* Wake one thread who's waiting */ - genwait_wake_one(cv); - } + /* Wake one thread who's waiting, if any */ + genwait_wake_one(cv); irq_restore(old); @@ -130,14 +116,8 @@ int cond_broadcast(condvar_t *cv) { old = irq_disable(); - if(!cv->initialized) { - errno = EINVAL; - rv = -1; - } - else { - /* Wake all threads who are waiting */ - genwait_wake_all(cv); - } + /* Wake all threads who are waiting */ + genwait_wake_all(cv); irq_restore(old); diff --git a/kernel/thread/mutex.c b/kernel/thread/mutex.c index 04022bd..14a60bb 100644 --- a/kernel/thread/mutex.c +++ b/kernel/thread/mutex.c @@ -86,8 +86,10 @@ int mutex_lock_timed(mutex_t *m, int timeout) { int old, rv = 0; if((rv = irq_inside_int())) { - dbglog(DBG_WARNING, "%s: called inside an interrupt with code: %x evt: %.4x\n", - timeout ? "mutex_lock_timed" : "mutex_lock", ((rv>>16) & 0xf), (rv & 0xffff)); + dbglog(DBG_WARNING, "%s: called inside an interrupt with code: " + "%x evt: %.4x\n", + timeout ? "mutex_lock_timed" : "mutex_lock", + ((rv >> 16) & 0xf), (rv & 0xffff)); errno = EPERM; return -1; } @@ -165,6 +167,7 @@ int mutex_trylock(mutex_t *m) { switch(m->type) { case MUTEX_TYPE_NORMAL: + case MUTEX_TYPE_OLDNORMAL: case MUTEX_TYPE_ERRORCHECK: if(m->count) { errno = EDEADLK; @@ -198,6 +201,7 @@ static int mutex_unlock_common(mutex_t *m, kthread_t *thd) { switch(m->type) { case MUTEX_TYPE_NORMAL: + case MUTEX_TYPE_OLDNORMAL: m->count = 0; m->holder = NULL; wakeup = 1; diff --git a/kernel/thread/rwsem.c b/kernel/thread/rwsem.c index e917f92..e1b5875 100644 --- a/kernel/thread/rwsem.c +++ b/kernel/thread/rwsem.c @@ -25,7 +25,7 @@ rw_semaphore_t *rwsem_create(void) { return NULL; } - s->initialized = 2; + s->dynamic = 1; s->read_count = 0; s->write_lock = NULL; s->reader_waiting = NULL; @@ -34,7 +34,7 @@ rw_semaphore_t *rwsem_create(void) { } int rwsem_init(rw_semaphore_t *s) { - s->initialized = 1; + s->dynamic = 0; s->read_count = 0; s->write_lock = NULL; s->reader_waiting = NULL; @@ -52,12 +52,9 @@ int rwsem_destroy(rw_semaphore_t *s) { errno = EBUSY; rv = -1; } - else if(s->initialized == 2) { + else if(s->dynamic) { free(s); } - else { - s->initialized = 0; - } irq_restore(old); return rv; @@ -68,9 +65,10 @@ int rwsem_read_lock_timed(rw_semaphore_t *s, int timeout) { int old, rv = 0; if((rv = irq_inside_int())) { - dbglog(DBG_WARNING, "%s: called inside an interrupt with code: %x evt: %.4x\n", + dbglog(DBG_WARNING, "%s: called inside an interrupt with code: " + "%x evt: %.4x\n", timeout ? "rwsem_read_lock_timed" : "rwsem_read_lock", - ((rv>>16) & 0xf), (rv & 0xffff)); + ((rv >> 16) & 0xf), (rv & 0xffff)); errno = EPERM; return -1; } @@ -82,12 +80,6 @@ int rwsem_read_lock_timed(rw_semaphore_t *s, int timeout) { old = irq_disable(); - if(s->initialized != 1 && s->initialized != 2) { - irq_restore(old); - errno = EINVAL; - return -1; - } - /* If the write lock is not held, let the thread proceed */ if(!s->write_lock) { ++s->read_count; @@ -133,12 +125,6 @@ int rwsem_write_lock_timed(rw_semaphore_t *s, int timeout) { old = irq_disable(); - if(s->initialized != 1 && s->initialized != 2) { - irq_restore(old); - errno = EINVAL; - return -1; - } - /* If the write lock is not held and there are no readers in their critical sections, let the thread proceed. */ if(!s->write_lock && !s->read_count) { @@ -174,12 +160,6 @@ int rwsem_read_unlock(rw_semaphore_t *s) { old = irq_disable(); - if(s->initialized != 1 && s->initialized != 2) { - irq_restore(old); - errno = EINVAL; - return -1; - } - if(!s->read_count) { irq_restore(old); errno = EPERM; @@ -210,12 +190,6 @@ int rwsem_write_unlock(rw_semaphore_t *s) { old = irq_disable(); - if(s->initialized != 1 && s->initialized != 2) { - irq_restore(old); - errno = EINVAL; - return -1; - } - if(s->write_lock != thd_current) { irq_restore(old); errno = EPERM; @@ -242,11 +216,7 @@ int rwsem_unlock(rw_semaphore_t *s) { old = irq_disable(); - if(s->initialized != 1 && s->initialized != 2) { - errno = EINVAL; - rv = -1; - } - else if(!s->write_lock && !s->read_count) { + if(!s->write_lock && !s->read_count) { errno = EPERM; rv = -1; } @@ -269,12 +239,8 @@ int rwsem_read_trylock(rw_semaphore_t *s) { old = irq_disable(); - if(s->initialized != 1 && s->initialized != 2) { - rv = -1; - errno = EINVAL; - } /* Is the write lock held? */ - else if(s->write_lock) { + if(s->write_lock) { rv = -1; errno = EWOULDBLOCK; } @@ -293,13 +259,9 @@ int rwsem_write_trylock(rw_semaphore_t *s) { old = irq_disable(); - if(s->initialized != 1 && s->initialized != 2) { - rv = -1; - errno = EINVAL; - } /* Are there any readers in their critical sections, or is the write lock already held, if so we can't do anything about that now. */ - else if(s->read_count || s->write_lock) { + if(s->read_count || s->write_lock) { rv = -1; errno = EWOULDBLOCK; } @@ -330,13 +292,9 @@ int rwsem_read_upgrade_timed(rw_semaphore_t *s, int timeout) { old = irq_disable(); - if(s->initialized != 1 && s->initialized != 2) { - rv = -1; - errno = EINVAL; - } /* If there are still other readers, see if any other readers have tried to upgrade or not... */ - else if(s->read_count > 1) { + if(s->read_count > 1) { if(s->reader_waiting) { /* We've got someone ahead of us, so there's really not anything that can be done at this point... */ @@ -383,11 +341,7 @@ int rwsem_read_tryupgrade(rw_semaphore_t *s) { old = irq_disable(); - if(s->initialized != 1 && s->initialized != 2) { - rv = -1; - errno = EINVAL; - } - else if(s->reader_waiting) { + if(s->reader_waiting) { rv = -1; errno = EBUSY; } hooks/post-receive -- A pseudo Operating System for the Dreamcast. |