From 9de27c2f9824b086405a1a05a13b861106a664d7 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Mon, 21 Dec 2015 23:06:10 -0500 Subject: [PATCH] Use a lock to synchronize writes to symbol table --- gc-notes.txt | 2 -- runtime.c | 10 ++++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/gc-notes.txt b/gc-notes.txt index 98014d9c..65a31003 100644 --- a/gc-notes.txt +++ b/gc-notes.txt @@ -12,8 +12,6 @@ TODO: - multiple mutators, and threading functions/types. probably want this on a new branch, when ready part of this is implementing the beginnings of srfi-18, to create multiple threads, sync them, etc - TODO items: - - lock writes to symbol table - need to cooperate when a mutator is blocked might be able to stop a thread and do a minor GC on it, but no longjmp until after major GC. diff --git a/runtime.c b/runtime.c index 82db23c9..2f084fa9 100644 --- a/runtime.c +++ b/runtime.c @@ -102,6 +102,7 @@ static symbol_type __EOF = {{0}, eof_tag, "", nil}; // symbol_type in lieu of cu const object Cyc_EOF = &__EOF; static ck_hs_t symbol_table; static int symbol_table_size = 65536; +static pthread_mutex_t symbol_table_lock; // Functions to support concurrency kit hashset // These are specifically for a table of symbols @@ -168,6 +169,10 @@ void gc_init_heap(long heap_size) fprintf(stderr, "Unable to initialize symbol table\n"); exit(1); } + if (pthread_mutex_init(&(symbol_table_lock), NULL) != 0) { + fprintf(stderr, "Unable to initialize symbol_table_lock mutex\n"); + exit(1); + } } gc_heap *gc_get_heap() @@ -245,13 +250,14 @@ object find_symbol_by_name(const char *name) { object add_symbol(symbol_type *psym) { //printf("Adding symbol %s, table size = %ld\n", symbol_pname(psym), ck_hs_count(&symbol_table)); - // TODO: lock table here, only allow one writer at a time - // TODO: grow table if it is not big enough + pthread_mutex_lock(&symbol_table_lock); // Only 1 "writer" allowed if (ck_hs_count(&symbol_table) == symbol_table_size) { + // TODO: grow table if it is not big enough fprintf(stderr, "Ran out of symbol table entries\n"); exit(1); } set_insert(&symbol_table, psym); + pthread_mutex_unlock(&symbol_table_lock); return psym; }