fix: resolve segfaults

This commit is contained in:
Alessandre Laguierce 2025-04-02 19:21:55 +02:00
parent 7f1aa05f3e
commit f6b03da814
2 changed files with 7 additions and 5 deletions

View File

@ -109,7 +109,7 @@ ${check_targets}: check_%: ${build_dir}/%
$^ ${check_argv}
${bins_target}: ${build_dir}/%: ${objs} ${build_dir}/${tst_dir}/%.o
${CC} -o $@ $^ ${CFLAGS} ${LDFLAGS}
${CC} -o $@ $^ ${CFLAGS} ./lib/libmimalloc ${LDFLAGS}
${build_dir}/libthread.so: ${objs}
${CC} -o $@ -shared $^ ${CFLAGS} ${LDFLAGS}

View File

@ -48,7 +48,6 @@ struct context_entry {
struct mutex_fifo_entry_t* mutex_fifo_entry;
int valgrind_id;
char status;
char stack[STACK_SIZE];
};
struct last_thread_t {
@ -80,7 +79,7 @@ int thread_yield(void)
if (TAILQ_EMPTY(&head)) {
return 0;
}
if (!(running->status & YIELD)) {
if (!(running->status & YIELD) && !IS_FINISHED(running)) {
running->status |= YIELD;
return 0;
}
@ -138,9 +137,9 @@ int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
} else {
TRACE("Allocating new entry");
new_entry = malloc(sizeof(*new_entry));
memset(new_entry->stack, 0, STACK_SIZE);
new_entry->context.uc_stack.ss_sp = new_entry->stack;
new_entry->context.uc_stack.ss_sp = malloc(STACK_SIZE);
memset(new_entry->context.uc_stack.ss_sp, 0, STACK_SIZE);
new_entry->context.uc_stack.ss_size = STACK_SIZE;
new_entry->context.uc_stack.ss_flags = 0;
@ -262,6 +261,7 @@ int thread_join(thread_t thread, void** retval)
DBG("ADDING (%p) TO FREED TAIL", entry);
TAILQ_INSERT_TAIL(&context_to_freed, entry, link);
} else {
free(entry->context.uc_stack.ss_sp);
free(entry);
}
@ -297,6 +297,7 @@ void clear_context(void)
TAILQ_REMOVE(&head, last, link);
free(last->mutex_fifo_entry);
if (WAS_ALLOCATED(last)) {
free(last->context.uc_stack.ss_sp);
VALGRIND_STACK_DEREGISTER(last->valgrind_id);
}
if (IS_WAITED(last)) {
@ -310,6 +311,7 @@ void clear_context(void)
free(last->mutex_fifo_entry);
TAILQ_REMOVE(&context_to_freed, last, link);
if (WAS_ALLOCATED(last)) {
free(last->context.uc_stack.ss_sp);
VALGRIND_STACK_DEREGISTER(last->valgrind_id);
}
free(last);