Added thread lock

This commit is contained in:
Justin Ethier 2015-10-29 21:58:47 -04:00
parent 40f2ac6207
commit 90609443f4
3 changed files with 25 additions and 6 deletions

23
gc.c
View file

@ -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. // 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?? 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)? // 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 = vpbuffer_add(thd->mark_buffer,
&(thd->mark_buffer_len), &(thd->mark_buffer_len),
thd->last_write, thd->last_write,
obj); obj);
// pthread_mutex_unlock pthread_mutex_unlock(&(thd->lock));
// unlock mark buffer
ATOMIC_INC(&(thd->last_write)); ATOMIC_INC(&(thd->last_write));
} }
} }
@ -526,6 +525,24 @@ void gc_col_empty_collector_stack()
// END tri-color marking section // 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: //// Unit testing:
//int main(int argc, char **argv) { //int main(int argc, char **argv) {
// int a = 1, b = 2, c = 3, i; // int a = 1, b = 2, c = 3, i;

View file

@ -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(heap_size / 2, 0, 0);
//Cyc_heap = gc_heap_create(1024, 0, 0); //Cyc_heap = gc_heap_create(1024, 0, 0);
Cyc_thread = (gc_thread_data *)malloc(sizeof(gc_thread_data)); Cyc_thread = malloc(sizeof(gc_thread_data));
Cyc_thread->moveBufLen = 0; gc_thread_data_init(Cyc_thread);
gc_thr_grow_move_buffer(Cyc_thread); // Initialize the buffer
// JAE TODO: clean up below (and all of this old code, really) // JAE TODO: clean up below (and all of this old code, really)

View file

@ -16,6 +16,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <pthread.h>
/* Define general object type. */ /* Define general object type. */
typedef void *object; typedef void *object;
@ -32,6 +33,7 @@ struct gc_thread_data_t {
int last_read; int last_read;
void **mark_buffer; void **mark_buffer;
int mark_buffer_len; int mark_buffer_len;
pthread_mutex_t lock;
}; };
/* GC data structures */ /* 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); size_t gc_collect(gc_heap *h, size_t *sum_freed);
void gc_thr_grow_move_buffer(gc_thread_data *d); 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_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 */ /* GC debugging flags */
//#define DEBUG_GC 0 //#define DEBUG_GC 0