style: use macro for status

This commit is contained in:
Alessandre Laguierce 2025-04-02 22:04:56 +02:00
parent 589c6551f7
commit bb29b2f53a

View File

@ -9,17 +9,20 @@
#include <valgrind/valgrind.h> #include <valgrind/valgrind.h>
#include <errno.h> #include <errno.h>
#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 FINISHED 0x1
#define IS_FINISHED(entry) (entry->status & FINISHED) #define IS_FINISHED(entry) (HAS_STATUS(entry, FINISHED))
#define ALLOCATED 0x2 #define ALLOCATED 0x2
#define WAS_ALLOCATED(entry) (entry->status & ALLOCATED) #define WAS_ALLOCATED(entry) (HAS_STATUS(entry, ALLOCATED))
#define WAITING 0x4 #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 GET_LAST_WAITED_THREAD(entry) (entry->last_waited ? entry->last_waited->last_thread : NULL)
#define WAITED 0x8 #define WAITED 0x8
#define MUTEX_WAITING 0xf0 #define MUTEX_WAITING (char) 0x10
#define IS_WAITED(entry) (entry->status & WAITED) #define IS_WAITED(entry) (HAS_STATUS(entry, WAITED))
#define IS_MUTEX_WAITING(entry) (entry->status & MUTEX_WAITING) #define IS_MUTEX_WAITING(entry) (HAS_STATUS(entry, MUTEX_WAITING))
#ifndef STACK_SIZE #ifndef STACK_SIZE
#define STACK_SIZE 4096 #define STACK_SIZE 4096
@ -102,10 +105,6 @@ int thread_yield(void)
thread_t thread_self(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; return running;
} }
@ -183,9 +182,9 @@ int thread_join(thread_t thread, void** retval)
if (!IS_FINISHED(entry)) { if (!IS_FINISHED(entry)) {
// Use status to be in waiting state // 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. // 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 // Use retvalue to share which thread is currently waiting for this thread
entry->retvalue = running; entry->retvalue = running;
@ -233,7 +232,7 @@ int thread_join(thread_t thread, void** retval)
thread_yield(); thread_yield();
} while (!IS_FINISHED(entry)); } while (!IS_FINISHED(entry));
// Exit from waiting state // Exit from waiting state
running->status &= ~WAITING; UNSET_STATUS(running, WAITING);
if (running->last_waited) { if (running->last_waited) {
// Release the last waited thread if no one use it anymore // 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); TRACE("Exit thread %p", running);
print_entry(running); print_entry(running);
running->status |= FINISHED; SET_STATUS(running, FINISHED);
if (IS_WAITED(running)) { if (IS_WAITED(running)) {
// If the thread was waited by another thread, we need to wake it up. // If the thread was waited by another thread, we need to wake it up.
struct context_entry* waited = running->retvalue; 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); STAILQ_INSERT_TAIL(mutex_fifo_hashmap[id], running->mutex_fifo_entry, link);
// Use status to be in waiting state // Use status to be in waiting state
running->status |= MUTEX_WAITING; SET_STATUS(running, MUTEX_WAITING);
running->mutex_fifo_entry->thread = running; running->mutex_fifo_entry->thread = running;
thread_yield(); 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]); struct mutex_fifo_entry_t* first = STAILQ_FIRST(mutex_fifo_hashmap[id]);
STAILQ_REMOVE_HEAD(mutex_fifo_hashmap[id], link); 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); TAILQ_INSERT_TAIL(&head, first->thread, link);
} }