fix: valgrind-friendly <3
This commit is contained in:
parent
0228c1c616
commit
544aefb1f9
@ -7,6 +7,7 @@
|
||||
#include <bits/pthreadtypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <ucontext.h>
|
||||
#include <valgrind/valgrind.h>
|
||||
|
||||
#ifndef STACK_SIZE
|
||||
#define STACK_SIZE 4096*4
|
||||
@ -16,8 +17,10 @@ struct context_entry {
|
||||
ucontext_t context;
|
||||
thread_t id;
|
||||
thread_t parent;
|
||||
int was_allocated;
|
||||
int finished;
|
||||
void *retvalue;
|
||||
int valgrind_id;
|
||||
TAILQ_ENTRY(context_entry) link;
|
||||
};
|
||||
|
||||
@ -60,6 +63,10 @@ int thread_create(thread_t *newthread, void *(*func)(void *), void *funcarg) {
|
||||
struct context_entry *main = malloc(sizeof(*main));
|
||||
getcontext(&main->context);
|
||||
main->id = 0;
|
||||
main->parent = 0;
|
||||
main->finished = 0;
|
||||
main->retvalue = 0;
|
||||
main->was_allocated = 0;
|
||||
TAILQ_INSERT_HEAD(&head, main, link);
|
||||
running = main;
|
||||
counter++;
|
||||
@ -68,10 +75,15 @@ int thread_create(thread_t *newthread, void *(*func)(void *), void *funcarg) {
|
||||
getcontext(&new_entry->context);
|
||||
new_entry->context.uc_stack.ss_sp = malloc(STACK_SIZE);
|
||||
new_entry->context.uc_stack.ss_size = STACK_SIZE;
|
||||
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;
|
||||
new_entry->parent = running->id;
|
||||
new_entry->finished = 0;
|
||||
new_entry->retvalue = 0;
|
||||
new_entry->was_allocated = 1;
|
||||
*newthread = new_entry;
|
||||
makecontext(&new_entry->context, (void (*)()) func, 1, funcarg);
|
||||
counter++;
|
||||
@ -96,11 +108,17 @@ int thread_join(thread_t thread, void **retval) {
|
||||
if (retval)
|
||||
*retval = entry->retvalue;
|
||||
TAILQ_REMOVE(&head, entry, link);
|
||||
free(entry->context.uc_stack.ss_sp);
|
||||
if (entry->was_allocated) {
|
||||
VALGRIND_STACK_DEREGISTER(entry->valgrind_id);
|
||||
free(entry->context.uc_stack.ss_sp);
|
||||
}
|
||||
free(entry);
|
||||
if (--counter == 1) {
|
||||
TAILQ_REMOVE(&head, running, link);
|
||||
free(running->context.uc_stack.ss_sp);
|
||||
if (running->was_allocated) {
|
||||
VALGRIND_STACK_DEREGISTER(running->valgrind_id);
|
||||
free(running->context.uc_stack.ss_sp);
|
||||
}
|
||||
free(running);
|
||||
running = 0;
|
||||
counter = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user