diff --git a/gc.c b/gc.c index e12886d7..67e5de3c 100644 --- a/gc.c +++ b/gc.c @@ -498,13 +498,12 @@ void gc_mark_gray(gc_thread_data *thd, object obj) // into the heap, with the collector being the only thread that changes marks. but double-check. if (is_object_type(obj) && mark(obj) == ATOMIC_GET(&gc_color_clear)) { // TODO: sync?? // TODO: lock mark buffer (not ideal, but a possible first step)? - // pthread_mutex_lock + pthread_mutex_lock(&(thd->lock)); thd->mark_buffer = vpbuffer_add(thd->mark_buffer, &(thd->mark_buffer_len), thd->last_write, obj); - // pthread_mutex_unlock - // unlock mark buffer + pthread_mutex_unlock(&(thd->lock)); ATOMIC_INC(&(thd->last_write)); } } @@ -526,6 +525,24 @@ void gc_col_empty_collector_stack() // END tri-color marking section + +// Initialize a thread from scratch +void gc_thread_data_init(gc_thread_data *thd) +{ + thd->moveBufLen = 0; + gc_thr_grow_move_buffer(thd); +// TODO: depends on collector state: thd->gc_alloc_color = ATOMIC_GET(&gc_; +// TODO: depends on collector state: thd->gc_mut_status; + thd->last_write = 0; + thd->last_read = 0; + thd->mark_buffer_len = 128; + thd->mark_buffer = vpbuffer_realloc(thd->mark_buffer, &(thd->mark_buffer_len)); + if (pthread_mutex(&(thd->lock), NULL) != 0) { + fprintf(stderr, "Unable to initialize thread mutex\n"); + exit(1); + } +} + //// Unit testing: //int main(int argc, char **argv) { // int a = 1, b = 2, c = 3, i; diff --git a/include/cyclone/runtime-main.h b/include/cyclone/runtime-main.h index ad98878a..5a4af9eb 100644 --- a/include/cyclone/runtime-main.h +++ b/include/cyclone/runtime-main.h @@ -59,9 +59,8 @@ static void Cyc_main (stack_size,heap_size,stack_base) Cyc_heap = gc_heap_create(heap_size / 2, 0, 0); //Cyc_heap = gc_heap_create(1024, 0, 0); - Cyc_thread = (gc_thread_data *)malloc(sizeof(gc_thread_data)); - Cyc_thread->moveBufLen = 0; - gc_thr_grow_move_buffer(Cyc_thread); // Initialize the buffer + Cyc_thread = malloc(sizeof(gc_thread_data)); + gc_thread_data_init(Cyc_thread); // JAE TODO: clean up below (and all of this old code, really) diff --git a/include/cyclone/types.h b/include/cyclone/types.h index 631058cd..b83b80ef 100644 --- a/include/cyclone/types.h +++ b/include/cyclone/types.h @@ -16,6 +16,7 @@ #include #include #include +#include /* Define general object type. */ typedef void *object; @@ -32,6 +33,7 @@ struct gc_thread_data_t { int last_read; void **mark_buffer; int mark_buffer_len; + pthread_mutex_t lock; }; /* GC data structures */ @@ -91,6 +93,7 @@ size_t gc_sweep(gc_heap *h, size_t *sum_freed_ptr); size_t gc_collect(gc_heap *h, size_t *sum_freed); void gc_thr_grow_move_buffer(gc_thread_data *d); void gc_thr_add_to_move_buffer(gc_thread_data *d, int *alloci, object obj); +void gc_thread_data_init(gc_thread_data *thd); /* GC debugging flags */ //#define DEBUG_GC 0