fix: fix the most annoying bug of the world

This commit is contained in:
Nemo D'ACREMONT 2025-04-24 12:31:20 +02:00
parent 9c256a26ea
commit 81abd5113a
No known key found for this signature in database
GPG Key ID: 85F245EC3BB1E022

View File

@ -15,8 +15,7 @@
#define UNSET_STATUS(entry, value) ((entry->status) &= ~(value))
#define FINISHED (1 << 0)
#define IS_FINISHED(entry) (HAS_STATUS(entry, FINISHED))
#define ALLOCATED (1 << 1)
#define WAS_ALLOCATED(entry) (HAS_STATUS(entry, ALLOCATED))
#define IS_MAIN(entry) (entry == main)
#define WAITING (1 << 2)
#define IS_WAITING(entry) (HAS_STATUS(entry, WAITING))
#define WAITED (1 << 3)
@ -41,6 +40,7 @@
static char stack_for_freeing[STACK_SIZE] = {0};
static int stack_valgrind_id = 0;
static ucontext_t context_for_freeing;
static struct context_entry_t* main = NULL;
struct mutex_fifo_entry_t;
@ -64,15 +64,14 @@ TAILQ_HEAD(scheduler_fifo_t, context_entry_t);
static struct scheduler_fifo_t scheduler_fifo = TAILQ_HEAD_INITIALIZER(scheduler_fifo);
// Linked list used to store what's needed to be freed at the very end
TAILQ_HEAD(ctx_to_free_fifo_t, context_entry_t);
static struct ctx_to_free_fifo_t context_to_freed = TAILQ_HEAD_INITIALIZER(context_to_freed);
TAILQ_HEAD(available_threads_fifo, context_entry_t);
static struct available_threads_fifo available_threads = TAILQ_HEAD_INITIALIZER(available_threads);
int thread_yield(void)
{
//TRACE("thread_yield");
if (TAILQ_EMPTY(&scheduler_fifo)) {
if (TAILQ_EMPTY(&scheduler_fifo))
return 0;
}
if (HAS_STATUS(running, MUTEX_LOCKING)) {
if (running->mutex_prio > 0) {
@ -94,9 +93,8 @@ int thread_yield(void)
struct context_entry_t* first = TAILQ_FIRST(&scheduler_fifo);
TAILQ_REMOVE(&scheduler_fifo, first, link);
if (!IS_FINISHED(running) && !IS_WAITING(running) && !IS_MUTEX_WAITING(running)) {
if (!IS_FINISHED(running) && !IS_WAITING(running) && !IS_MUTEX_WAITING(running))
TAILQ_INSERT_TAIL(&scheduler_fifo, running, link);
}
TRACE("PICKING %p (previous was %p)", first, running);
// Switch to the new thread.
@ -129,9 +127,10 @@ int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
DBG("Create a new thread that execute function %p", func);
struct context_entry_t* new_entry;
TRACE("Checking for previous allocated entry");
if (!TAILQ_EMPTY(&context_to_freed)) {
new_entry = TAILQ_FIRST(&context_to_freed);
TAILQ_REMOVE(&context_to_freed, new_entry, link);
if (!TAILQ_EMPTY(&available_threads)) {
TRACE("TAKE THREAD AVAILABLE");
new_entry = TAILQ_FIRST(&available_threads);
TAILQ_REMOVE(&available_threads, new_entry, link);
} else {
TRACE("Allocating new entry");
new_entry = malloc(sizeof(*new_entry));
@ -151,7 +150,7 @@ int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
// Use the entry's memory address as an id.
TRACE("ALLOCATED %p", new_entry);
new_entry->status = ALLOCATED;
new_entry->status = 0;
new_entry->retvalue = NULL;
ufd__init(&new_entry->waited_threads, new_entry);
new_entry->mutex_prio = MUTEX_MAXPRIO;
@ -170,7 +169,7 @@ int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
void print_entry(struct context_entry_t* entry)
{
TRACE("CONTEXT (%p, %p, %d);", entry, entry, WAS_ALLOCATED(entry));
TRACE("CONTEXT (%p, %p, %d);", entry, entry, IS_MAIN(entry));
}
int thread_join(thread_t thread, void** retval)
@ -188,32 +187,30 @@ int thread_join(thread_t thread, void** retval)
}
if (!IS_FINISHED(entry)) {
DBG("%p is waiting for %p", running, entry);
// Use status to be in waiting state
SET_STATUS(running, WAITING);
// Mark the waited thread as waited to not be waited by any other thread.
SET_STATUS(entry, WAITED);
// Use retvalue to share which thread is currently waiting for this thread
entry->retvalue = running;
ufd__join(&running->waited_threads, &entry->waited_threads);
struct context_entry_t* running_last_waited = ufd__find(&running->waited_threads)->thread;
DBG("%p is waiting for %p", running, entry);
DBG("MUTEX WAITING %d %p", IS_MUTEX_WAITING(running_last_waited));
#ifdef FIBO_STRAT
#else
if (!IS_MUTEX_WAITING(running_last_waited)) {
// Opti: prioritize the scheduling of the most waited thread
struct context_entry_t* running_last_waited = ufd__find(&running->waited_threads)->thread;
// theorically, this thread can be finished and on the way of being joined
if (!IS_FINISHED(running_last_waited) && !IS_MUTEX_WAITING(running_last_waited)) {
TAILQ_REMOVE(&scheduler_fifo, running_last_waited, link);
TAILQ_INSERT_HEAD(&scheduler_fifo, running_last_waited, link);
}
#endif
do {
thread_yield();
} while (!IS_FINISHED(entry));
ufd__delete(&entry->waited_threads);
}
@ -221,18 +218,12 @@ int thread_join(thread_t thread, void** retval)
TRACE("RETURNING %p IN %p", entry->retvalue, retval);
if (retval)
*retval = entry->retvalue;
UNSET_STATUS(entry, WAITED);
// Exit from waiting state
UNSET_STATUS(running, WAITING);
// Clean up
DBG("(entry, was_alloacted) : %p,%d", entry, WAS_ALLOCATED(entry));
if (WAS_ALLOCATED(entry)) {
DBG("ADDING (%p) TO FREED TAIL", entry);
TAILQ_INSERT_TAIL(&context_to_freed, entry, link);
} else {
free(entry);
}
DBG("(entry, was_alloacted) : %p,%d", entry, IS_MAIN(entry));
if (!IS_MAIN(entry))
TAILQ_INSERT_TAIL(&available_threads, entry, link);
return 0;
}
@ -245,12 +236,13 @@ void thread_exit(void* retval)
if (IS_WAITED(running)) {
// If the thread was waited by another thread, we need to wake it up.
struct context_entry_t* waiting = running->retvalue;
UNSET_STATUS(running, WAITED);
UNSET_STATUS(waiting, WAITING);
TRACE("WAS WAITED BY %p", waiting);
#ifdef FIBO_STRAT
TAILQ_INSERT_HEAD(&scheduler_fifo, waiting, link);
#else
TAILQ_INSERT_TAIL(&scheduler_fifo, waiting, link);
TAILQ_INSERT_HEAD(&scheduler_fifo, waiting, link);
#endif
}
running->retvalue = retval;
@ -272,7 +264,7 @@ void clear_context(void)
last = TAILQ_FIRST(&scheduler_fifo);
TAILQ_REMOVE(&scheduler_fifo, last, link);
if (WAS_ALLOCATED(last))
if (!IS_MAIN(last))
VALGRIND_STACK_DEREGISTER(last->valgrind_id);
if (IS_WAITED(last)) {
@ -283,13 +275,14 @@ void clear_context(void)
free(last);
}
while (!TAILQ_EMPTY(&context_to_freed)) {
last = TAILQ_FIRST(&context_to_freed);
TAILQ_REMOVE(&context_to_freed, last, link);
while (!TAILQ_EMPTY(&available_threads)) {
last = TAILQ_FIRST(&available_threads);
TAILQ_REMOVE(&available_threads, last, link);
if (IS_MAIN(last))
continue;
if (WAS_ALLOCATED(last))
VALGRIND_STACK_DEREGISTER(last->valgrind_id);
free(last);
}
@ -319,10 +312,10 @@ void __attribute__((constructor)) setup_main_thread()
ufd__init(&new_entry->waited_threads, new_entry);
new_entry->retvalue = NULL;
new_entry->status = 0;
TAILQ_INSERT_TAIL(&context_to_freed, new_entry, link);
TAILQ_INSERT_TAIL(&available_threads, new_entry, link);
}
struct context_entry_t* main = malloc(sizeof(*main));
main = malloc(sizeof(*main));
// memset(main, 0, sizeof(*main));
getcontext(&main->context);
main->status = 0;
@ -344,11 +337,12 @@ void __attribute__((destructor)) clear_last_thread()
TRACE("POST");
// Running is the initial main thread. No need to switch to a static stack.
TAILQ_INSERT_HEAD(&scheduler_fifo, running, link);
if (!WAS_ALLOCATED(running)) {
if (IS_MAIN(running)) {
clear_context();
exit(0);
}
free(main);
// Running's stack was allocated by us, lets switch to a static stack first.
swapcontext(&running->context, &context_for_freeing);
exit(0);