From 6463b2bf06c588d89e977e6383e377d2c3cf94c7 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Sun, 20 Dec 2015 21:59:40 -0500 Subject: [PATCH] Trigger GC prior to thread exit --- include/cyclone/runtime.h | 1 + runtime.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/cyclone/runtime.h b/include/cyclone/runtime.h index 91e3927d..088e2024 100644 --- a/include/cyclone/runtime.h +++ b/include/cyclone/runtime.h @@ -189,6 +189,7 @@ object memqp(void *,object,list); object Cyc_spawn_thread(object thunk); void Cyc_start_thread(gc_thread_data *thd); void Cyc_end_thread(gc_thread_data *thd); +void Cyc_exit_thread(gc_thread_data *thd); object Cyc_thread_sleep(void *data, object timeout); void GC(void *,closure,object*,int); diff --git a/runtime.c b/runtime.c index 57e9aa96..160f8680 100644 --- a/runtime.c +++ b/runtime.c @@ -3152,6 +3152,7 @@ void *Cyc_init_thread(object thunk) // returns instance so would need to malloc here // would also need to update termination code to free that memory gc_add_mutator(thd); + ATOMIC_SET_IF_EQ(&(thd->thread_state), CYC_THREAD_STATE_NEW, CYC_THREAD_STATE_RUNNABLE); Cyc_start_thread(thd); return NULL; } @@ -3195,6 +3196,12 @@ to look at the lock-free structures provided by ck? * Terminate a thread */ void Cyc_end_thread(gc_thread_data *thd) +{ + mclosure0(clo, Cyc_exit_thread); + GC(thd, &clo, thd->gc_args, 0); +} + +void Cyc_exit_thread(gc_thread_data *thd) { // alternatively could call longjmp with a null continuation, but that seems // more complicated than necessary. or does it... see next comment: @@ -3203,8 +3210,8 @@ void Cyc_end_thread(gc_thread_data *thd) // referenced? might want to do one more minor GC to clear the stack before // terminating the thread -// TODO: use ATOMIC set to modify this - thd->thread_state = CYC_THREAD_STATE_TERMINATED; +printf("DEBUG - exiting thread\n"); + ATOMIC_SET_IF_EQ(&(thd->thread_state), CYC_THREAD_STATE_RUNNABLE, CYC_THREAD_STATE_TERMINATED); pthread_exit(NULL); // For now, just a proof of concept }