feat: save freed thread to not realloc after

This commit is contained in:
Alessandre Laguierce 2025-03-27 18:03:14 +01:00
parent a43d94dede
commit 177e8807c5

View File

@ -58,6 +58,8 @@ static TAILQ_HEAD(context_head, context_entry) head = TAILQ_HEAD_INITIALIZER(hea
// Current running thread // Current running thread
static struct context_entry* running = NULL; static struct context_entry* running = NULL;
static TAILQ_HEAD(freed_context_head, context_entry) context_to_freed = TAILQ_HEAD_INITIALIZER(context_to_freed);
int thread_yield(void) int thread_yield(void)
{ {
//TRACE("thread_yield"); //TRACE("thread_yield");
@ -112,12 +114,17 @@ void thread_function_wrapper(void* (*func)(void*), void* funcarg)
*/ */
int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg) int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
{ {
TRACE("Create a new thread that execute function %p", func); DBG("Create a new thread that execute function %p", func);
struct context_entry* new_entry = malloc(sizeof(*new_entry)); struct context_entry* 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);
} else {
TRACE("Allocating new entry");
new_entry = malloc(sizeof(*new_entry));
memset(new_entry->stack, 0, STACK_SIZE); memset(new_entry->stack, 0, STACK_SIZE);
getcontext(&new_entry->context);
new_entry->context.uc_stack.ss_sp = new_entry->stack; new_entry->context.uc_stack.ss_sp = new_entry->stack;
new_entry->context.uc_stack.ss_size = STACK_SIZE; new_entry->context.uc_stack.ss_size = STACK_SIZE;
new_entry->context.uc_stack.ss_flags = 0; new_entry->context.uc_stack.ss_flags = 0;
@ -127,8 +134,12 @@ int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
new_entry->context.uc_stack.ss_sp, new_entry->context.uc_stack.ss_sp,
new_entry->context.uc_stack.ss_sp + new_entry->context.uc_stack.ss_size); new_entry->context.uc_stack.ss_sp + new_entry->context.uc_stack.ss_size);
// Use the entry's memory address as an id.
new_entry->id = new_entry; new_entry->id = new_entry;
}
getcontext(&new_entry->context);
// Use the entry's memory address as an id.
TRACE("ALLOCATED %p", new_entry); TRACE("ALLOCATED %p", new_entry);
new_entry->status = ALLOCATED; new_entry->status = ALLOCATED;
new_entry->retvalue = NULL; new_entry->retvalue = NULL;
@ -225,9 +236,11 @@ int thread_join(thread_t thread, void** retval)
// Clean up // Clean up
DBG("(entry, was_alloacted) : %p,%d", entry, WAS_ALLOCATED(entry)); DBG("(entry, was_alloacted) : %p,%d", entry, WAS_ALLOCATED(entry));
if (WAS_ALLOCATED(entry)) { if (WAS_ALLOCATED(entry)) {
VALGRIND_STACK_DEREGISTER(entry->valgrind_id); DBG("ADDING (%p) TO FREED TAIL", entry);
} TAILQ_INSERT_TAIL(&context_to_freed, entry, link);
} else {
free(entry); free(entry);
}
return 0; return 0;
} }
@ -268,6 +281,14 @@ void clear_context(void)
} }
free(last); free(last);
} }
while (!TAILQ_EMPTY(&context_to_freed)) {
last = TAILQ_FIRST(&context_to_freed);
TAILQ_REMOVE(&context_to_freed, last, link);
if (WAS_ALLOCATED(last)) {
VALGRIND_STACK_DEREGISTER(last->valgrind_id);
}
free(last);
}
VALGRIND_STACK_DEREGISTER(stack_valgrind_id); VALGRIND_STACK_DEREGISTER(stack_valgrind_id);
exit(0); exit(0);
} }