From 507f9f9c579e47b91ed16dd78f2f7438fa37be19 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Fri, 30 Mar 2018 16:12:30 -0400 Subject: [PATCH] Make global state thread local. This means multiple threads of dst can run, but cannot yet share state. --- doc/bytecode.md | 6 +++--- src/core/gc.c | 14 +++++++------- src/core/symcache.c | 8 ++++---- src/core/vm.c | 2 +- src/include/dst/dstconfig.h | 10 +++++++++- src/include/dst/dststate.h | 24 ++++++++++++------------ 6 files changed, 36 insertions(+), 28 deletions(-) diff --git a/doc/bytecode.md b/doc/bytecode.md index 6702f2c1..90261f88 100644 --- a/doc/bytecode.md +++ b/doc/bytecode.md @@ -1,9 +1,9 @@ -# Dst Bytecode Interpreter +# Dst Bytecode Reference ### Dst alpha 0.0.0 -This document outlines the Dst bytecode format, and core ideas in the runtime. -the are closely related to the bytecode. It should enable the reader +This document outlines the Dst bytecode format, and core ideas in the runtime +that are closely related to the bytecode. It should enable the reader to write dst assembly code and hopefully understand the dst internals better. It will also talk about the C abstractions used to implement some of these ideas. Some experience with basic computer organization is helpful for understanding diff --git a/src/core/gc.c b/src/core/gc.c index fa80a5d8..e593a7ab 100644 --- a/src/core/gc.c +++ b/src/core/gc.c @@ -25,15 +25,15 @@ #include "gc.h" /* GC State */ -void *dst_vm_blocks; -uint32_t dst_vm_gc_interval; -uint32_t dst_vm_next_collection; -int dst_vm_gc_suspend = 0; +DST_THREAD_LOCAL void *dst_vm_blocks; +DST_THREAD_LOCAL uint32_t dst_vm_gc_interval; +DST_THREAD_LOCAL uint32_t dst_vm_next_collection; +DST_THREAD_LOCAL int dst_vm_gc_suspend = 0; /* Roots */ -Dst *dst_vm_roots; -uint32_t dst_vm_root_count; -uint32_t dst_vm_root_capacity; +DST_THREAD_LOCAL Dst *dst_vm_roots; +DST_THREAD_LOCAL uint32_t dst_vm_root_count; +DST_THREAD_LOCAL uint32_t dst_vm_root_capacity; /* Helpers for marking the various gc types */ static void dst_mark_funcenv(DstFuncEnv *env); diff --git a/src/core/symcache.c b/src/core/symcache.c index 7d0f053b..de1c9183 100644 --- a/src/core/symcache.c +++ b/src/core/symcache.c @@ -30,10 +30,10 @@ #include "util.h" /* Cache state */ -const uint8_t **dst_vm_cache = NULL; -uint32_t dst_vm_cache_capacity = 0; -uint32_t dst_vm_cache_count = 0; -uint32_t dst_vm_cache_deleted = 0; +DST_THREAD_LOCAL const uint8_t **dst_vm_cache = NULL; +DST_THREAD_LOCAL uint32_t dst_vm_cache_capacity = 0; +DST_THREAD_LOCAL uint32_t dst_vm_cache_count = 0; +DST_THREAD_LOCAL uint32_t dst_vm_cache_deleted = 0; /* Initialize the cache (allocate cache memory) */ void dst_symcache_init() { diff --git a/src/core/vm.c b/src/core/vm.c index 4ee9360c..24771ab4 100644 --- a/src/core/vm.c +++ b/src/core/vm.c @@ -27,7 +27,7 @@ #include "symcache.h" /* VM state */ -int dst_vm_stackn = 0; +DST_THREAD_LOCAL int dst_vm_stackn = 0; /* Start running the VM from where it left off. */ Dst dst_run(DstFiber *fiber) { diff --git a/src/include/dst/dstconfig.h b/src/include/dst/dstconfig.h index cc9991e4..77247f0f 100644 --- a/src/include/dst/dstconfig.h +++ b/src/include/dst/dstconfig.h @@ -27,7 +27,6 @@ extern "C" { #endif - #define DST_VERSION "0.0.0 alpha" /* @@ -90,6 +89,15 @@ extern "C" { #define DST_LITTLE_ENDIAN 1 #endif +/* Check compiler */ +#if defined(__GNUC__) +#define DST_THREAD_LOCAL __thread +#elif defined(_MSC_BUILD) +#define DST_THREAD_LOCAL __declspec(thread) +#else +#define DST_THREAD_LOCAL +#endif + /* Include default headers */ #include diff --git a/src/include/dst/dststate.h b/src/include/dst/dststate.h index 168e1704..e4b1dc84 100644 --- a/src/include/dst/dststate.h +++ b/src/include/dst/dststate.h @@ -38,24 +38,24 @@ extern const char *dst_type_names[16]; * around, the vm state is global for simplicity. */ /* How many VM stacks have been entered */ -extern int dst_vm_stackn; +extern DST_THREAD_LOCAL int dst_vm_stackn; /* Garbage collection */ -extern void *dst_vm_blocks; -extern uint32_t dst_vm_gc_interval; -extern uint32_t dst_vm_next_collection; -extern int dst_vm_gc_suspend; +extern DST_THREAD_LOCAL void *dst_vm_blocks; +extern DST_THREAD_LOCAL uint32_t dst_vm_gc_interval; +extern DST_THREAD_LOCAL uint32_t dst_vm_next_collection; +extern DST_THREAD_LOCAL int dst_vm_gc_suspend; /* Immutable value cache */ -extern const uint8_t **dst_vm_cache; -extern uint32_t dst_vm_cache_capacity; -extern uint32_t dst_vm_cache_count; -extern uint32_t dst_vm_cache_deleted; +extern DST_THREAD_LOCAL const uint8_t **dst_vm_cache; +extern DST_THREAD_LOCAL uint32_t dst_vm_cache_capacity; +extern DST_THREAD_LOCAL uint32_t dst_vm_cache_count; +extern DST_THREAD_LOCAL uint32_t dst_vm_cache_deleted; /* GC roots */ -extern Dst *dst_vm_roots; -extern uint32_t dst_vm_root_count; -extern uint32_t dst_vm_root_capacity; +extern DST_THREAD_LOCAL Dst *dst_vm_roots; +extern DST_THREAD_LOCAL uint32_t dst_vm_root_count; +extern DST_THREAD_LOCAL uint32_t dst_vm_root_capacity; #ifdef __cplusplus }