feat: correct few things to converge to Faverge's happiness

This commit is contained in:
Alessandre Laguierce 2025-03-28 09:36:08 +01:00
parent 177e8807c5
commit fe48f0d61b

View File

@ -17,9 +17,7 @@
#define WAS_ALLOCATED(entry) (entry->status & ALLOCATED)
#define WAITING 0x4
#define IS_WAITING(entry) (entry->status & WAITING)
#define GET_WAITED_THREAD(entry) ((struct context_entry*)entry->waiting)
#define GET_LAST_WAITED_THREAD(entry) (entry->last_waited ? entry->last_waited->last_thread : NULL)
#define IS_WAITED_THREAD_FINISHED(entry) (GET_WAITED_THREAD(entry)->status & FINISHED)
#define WAITED 0x8
#define IS_WAITED(entry) (entry->status & WAITED)
@ -27,7 +25,7 @@
#define STACK_SIZE 4096
#endif
// Variables used to clean up everything at the end of the processus
// Variables used to clean up everything at the end of the processes
static char stack_for_freeing[STACK_SIZE] = {0};
static int stack_valgrind_id = 0;
static ucontext_t context_for_freeing;
@ -39,8 +37,6 @@ struct context_entry {
TAILQ_ENTRY(context_entry)
link; // Use to navigate inside the list
ucontext_t context;
thread_t id;
void *waiting; // the thread that the entry is waiting for
void *retvalue; // return value or if the thread is waited, the id of the thread that wait for it
struct last_thread_t *last_waited;
int valgrind_id;
@ -75,11 +71,8 @@ int thread_yield(void)
* check if the thread is not the running one.
*/
struct context_entry* first = TAILQ_FIRST(&head);
if (!first) {
return -1;
}
TAILQ_REMOVE(&head, first, link);
if (!IS_FINISHED(running) && !(IS_WAITING(running) && !IS_WAITED_THREAD_FINISHED(running))) {
if (!IS_FINISHED(running) && !IS_WAITING(running)) {
TAILQ_INSERT_TAIL(&head, running, link);
}
TRACE("PICKING %p (previous was %p)", first->id, running->id);
@ -96,7 +89,7 @@ thread_t thread_self(void)
if (running == NULL) {
return 0;
}
return running->id;
return running;
}
/**
@ -133,8 +126,6 @@ int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
new_entry->valgrind_id = VALGRIND_STACK_REGISTER(
new_entry->context.uc_stack.ss_sp,
new_entry->context.uc_stack.ss_sp + new_entry->context.uc_stack.ss_size);
new_entry->id = new_entry;
}
getcontext(&new_entry->context);
@ -145,7 +136,7 @@ int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
new_entry->retvalue = NULL;
new_entry->last_waited = NULL;
*newthread = new_entry->id;
*newthread = new_entry;
makecontext(&new_entry->context, (void (*)(void))thread_function_wrapper, 2, func, funcarg);
@ -175,7 +166,6 @@ int thread_join(thread_t thread, void** retval)
if (!IS_FINISHED(entry)) {
// Use status to be in waiting state
running->status |= WAITING;
running->waiting = entry;
// Mark the waited thread as waited to not be waited by any other thread.
entry->status |= WAITED;
// Use retvalue to share which thread is currently waiting for this thread
@ -212,6 +202,8 @@ int thread_join(thread_t thread, void** retval)
DBG("%p is waiting for %p", running, entry);
TAILQ_REMOVE(&head, GET_LAST_WAITED_THREAD(running), link);
TAILQ_INSERT_HEAD(&head, GET_LAST_WAITED_THREAD(running), link);
do {
thread_yield();
} while (!IS_FINISHED(entry));
@ -300,7 +292,6 @@ void __attribute__((constructor)) setup_main_thread()
struct context_entry* main = malloc(sizeof(*main));
// memset(main, 0, sizeof(*main));
getcontext(&main->context);
main->id = main;
main->status = 0;
main->retvalue = NULL;
main->last_waited = NULL;