From bb29b2f53a3d8bcd840ab84cf333f74174da3770 Mon Sep 17 00:00:00 2001 From: Alessandre Laguierce Date: Wed, 2 Apr 2025 22:04:56 +0200 Subject: [PATCH] style: use macro for status --- src/thread/thread.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/thread/thread.c b/src/thread/thread.c index 994f558..eeff5bb 100644 --- a/src/thread/thread.c +++ b/src/thread/thread.c @@ -9,17 +9,20 @@ #include #include +#define HAS_STATUS(entry, value) ((entry->status) & (value)) +#define SET_STATUS(entry, value) ((entry->status) |= (value)) +#define UNSET_STATUS(entry, value) ((entry->status) &= ~(value)) #define FINISHED 0x1 -#define IS_FINISHED(entry) (entry->status & FINISHED) +#define IS_FINISHED(entry) (HAS_STATUS(entry, FINISHED)) #define ALLOCATED 0x2 -#define WAS_ALLOCATED(entry) (entry->status & ALLOCATED) +#define WAS_ALLOCATED(entry) (HAS_STATUS(entry, ALLOCATED)) #define WAITING 0x4 -#define IS_WAITING(entry) (entry->status & WAITING) +#define IS_WAITING(entry) (HAS_STATUS(entry, WAITING)) #define GET_LAST_WAITED_THREAD(entry) (entry->last_waited ? entry->last_waited->last_thread : NULL) #define WAITED 0x8 -#define MUTEX_WAITING 0xf0 -#define IS_WAITED(entry) (entry->status & WAITED) -#define IS_MUTEX_WAITING(entry) (entry->status & MUTEX_WAITING) +#define MUTEX_WAITING (char) 0x10 +#define IS_WAITED(entry) (HAS_STATUS(entry, WAITED)) +#define IS_MUTEX_WAITING(entry) (HAS_STATUS(entry, MUTEX_WAITING)) #ifndef STACK_SIZE #define STACK_SIZE 4096 @@ -102,10 +105,6 @@ int thread_yield(void) thread_t thread_self(void) { - // This condition should not be true at any moment after main call - if (running == NULL) { - return 0; - } return running; } @@ -183,9 +182,9 @@ int thread_join(thread_t thread, void** retval) if (!IS_FINISHED(entry)) { // Use status to be in waiting state - running->status |= WAITING; + SET_STATUS(running, WAITING); // Mark the waited thread as waited to not be waited by any other thread. - entry->status |= WAITED; + SET_STATUS(entry, WAITED); // Use retvalue to share which thread is currently waiting for this thread entry->retvalue = running; @@ -233,7 +232,7 @@ int thread_join(thread_t thread, void** retval) thread_yield(); } while (!IS_FINISHED(entry)); // Exit from waiting state - running->status &= ~WAITING; + UNSET_STATUS(running, WAITING); if (running->last_waited) { // Release the last waited thread if no one use it anymore @@ -268,7 +267,7 @@ void thread_exit(void* retval) TRACE("Exit thread %p", running); print_entry(running); - running->status |= FINISHED; + SET_STATUS(running, FINISHED); if (IS_WAITED(running)) { // If the thread was waited by another thread, we need to wake it up. struct context_entry* waited = running->retvalue; @@ -401,7 +400,7 @@ int thread_mutex_lock(thread_mutex_t* mutex) STAILQ_INSERT_TAIL(mutex_fifo_hashmap[id], running->mutex_fifo_entry, link); // Use status to be in waiting state - running->status |= MUTEX_WAITING; + SET_STATUS(running, MUTEX_WAITING); running->mutex_fifo_entry->thread = running; thread_yield(); } @@ -419,7 +418,7 @@ int thread_mutex_unlock(thread_mutex_t* mutex) struct mutex_fifo_entry_t* first = STAILQ_FIRST(mutex_fifo_hashmap[id]); STAILQ_REMOVE_HEAD(mutex_fifo_hashmap[id], link); - first->thread->status &= ~MUTEX_WAITING; + UNSET_STATUS(first->thread, MUTEX_WAITING); TAILQ_INSERT_TAIL(&head, first->thread, link); }