diff --git a/gc.c b/gc.c index a98219ff..87263319 100644 --- a/gc.c +++ b/gc.c @@ -731,11 +731,30 @@ void gc_collector() //sweep : max_freed = gc_sweep(Cyc_get_heap(), &freed); #if GC_DEBUG_CONCISE_PRINTFS - printf("sweep done, freed = %d, max_freed = %d, elapsed = %ld\n", freed, max_freed, time(NULL) - sweep_start); + printf("sweep done, freed = %d, max_freed = %d, elapsed = %ld\n", + freed, max_freed, time(NULL) - sweep_start); #endif gc_stage = STAGE_RESTING; } +void *collector_main(void *arg) +{ + while (1) { + gc_collector(); + sleep(1); // TODO: how to schedule this thread? + } +} + +static pthread_t collector_thread; + +void gc_start_collector() +{ + if (pthread_create(&collector_thread, NULL, collector_main, &collector_thread)) { + fprintf(stderr, "Error creating collector thread\n"); + exit(1); + } +} + ///////////////////////////////////////////// // END tri-color marking section ///////////////////////////////////////////// diff --git a/include/cyclone/runtime-main.h b/include/cyclone/runtime-main.h index 9f7edf8e..2ab0b1ab 100644 --- a/include/cyclone/runtime-main.h +++ b/include/cyclone/runtime-main.h @@ -29,6 +29,7 @@ static void Cyc_heap_init(long heap_size) #endif Cyc_heap = gc_heap_create(heap_size / 2, 0, 0); gc_init_mutators(); + gc_start_collector(); } #endif /* CYCLONE_RUNTIME_MAIN_H */ diff --git a/include/cyclone/types.h b/include/cyclone/types.h index c680fbdc..a1bfc527 100644 --- a/include/cyclone/types.h +++ b/include/cyclone/types.h @@ -150,6 +150,7 @@ void gc_empty_collector_stack(); void gc_handshake(gc_status_type s); void gc_post_handshake(gc_status_type s); void gc_wait_handshake(); +void gc_start_collector(); gc_heap *Cyc_get_heap(); /////////////////////////////////////////////