From c9a8143558309158b760ca75a36d7193d140c12b Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sat, 1 Dec 2007 15:05:20 +0100 Subject: [PATCH] we must copy the closure temporarily. Signed-off-by: Pierre Habouzit --- threads.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/threads.c b/threads.c index 249a123..16d0518 100644 --- a/threads.c +++ b/threads.c @@ -41,8 +41,8 @@ static struct { int count, size; } morgue; -struct thread_foo { - void *(*fun)(int fd, void *); +struct job_closure { + void *(*f)(int, void*); int fd; void *data; }; @@ -59,19 +59,21 @@ void thread_register_dead(void *tid) static void *thread_wrapper(void *arg) { - struct thread_foo *foo = arg; + struct job_closure *closure = arg; void *res; + pthread_cleanup_push(thread_register_dead, (void *)pthread_self()); - res = (*foo->fun)(foo->fd, foo->data); + res = (*closure->f)(closure->fd, closure->data); pthread_cleanup_pop(1); + p_delete(&closure); return res; } int thread_launch(void *(*f)(int, void *), int fd, void *data) { - struct thread_foo foo = { f, fd, data }; + struct job_closure closure = { .f = f, .fd = fd, .data = data }; pthread_t t; - return pthread_create(&t, NULL, &thread_wrapper, &foo); + return pthread_create(&t, NULL, &thread_wrapper, p_dup(&closure, 1)); } void threads_join(void) -- 2.20.1