From 854a12f68b54a2bc7d320bf5c8d71497a46da44a Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Tue, 9 Feb 2021 21:34:02 +0900 Subject: [PATCH] Fix up some threading gc stuff before diving into real debugging --- src/compiler.c | 16 ---------------- src/memory.c | 2 +- src/module_os.c | 3 +-- src/threads.c | 18 +++++++++++++++++- src/vm.c | 4 ++-- src/vm.h | 20 ++++++++++++++++++++ 6 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/compiler.c b/src/compiler.c index 9956489..8b75ed5 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -2612,23 +2612,7 @@ static ParseRule * getRule(KrkTokenType type) { } #ifdef ENABLE_THREADING -#include -static void spin_lock(int volatile * lock) { - while(__sync_lock_test_and_set(lock, 0x01)) { - sched_yield(); - } -} - -static void spin_unlock(int volatile * lock) { - __sync_lock_release(lock); -} - static volatile int _compilerLock = 0; -#define _obtain_lock(v) spin_lock(&v); -#define _release_lock(v) spin_unlock(&v); -#else -#define _obtain_lock(v) -#define _release_lock(v) #endif KrkFunction * krk_compile(const char * src, int newScope, char * fileName) { diff --git a/src/memory.c b/src/memory.c index 0cc3403..86561c1 100644 --- a/src/memory.c +++ b/src/memory.c @@ -245,7 +245,7 @@ void krk_tableRemoveWhite(KrkTable * table) { } static void markThreadRoots(KrkThreadState * thread) { - for (KrkValue * slot = thread->stack; slot < thread->stackTop; ++slot) { + for (KrkValue * slot = thread->stack; slot && slot < thread->stackTop; ++slot) { krk_markValue(*slot); } for (KrkUpvalue * upvalue = thread->openUpvalues; upvalue; upvalue = upvalue->next) { diff --git a/src/module_os.c b/src/module_os.c index 21a8eba..2662280 100644 --- a/src/module_os.c +++ b/src/module_os.c @@ -294,8 +294,7 @@ KrkValue krk_module_onload_os(void) { krk_attachNamedObject(&module->fields, "pardir", (KrkObj*)S("..")); krk_attachNamedObject(&module->fields, "extsep", (KrkObj*)S(".")); - OSError = krk_newClass(S("OSError"), vm.exceptions->baseException); - krk_attachNamedObject(&module->fields, "OSError", (KrkObj*)OSError); + krk_makeClass(module, &OSError, "OSError", vm.exceptions->baseException); krk_finalizeClass(OSError); BIND_FUNC(module,uname); diff --git a/src/threads.c b/src/threads.c index 633a7d9..96d0b68 100644 --- a/src/threads.c +++ b/src/threads.c @@ -38,13 +38,16 @@ KRK_FUNC(current_thread,{ #define CURRENT_CTYPE struct Thread * #define CURRENT_NAME self +static volatile int _threadLock = 0; static void * _startthread(void * _threadObj) { memset(&krk_currentThread, 0, sizeof(KrkThreadState)); - /* TODO: Wrap this in a mutex */ + + _obtain_lock(_threadLock); if (vm.threads->next) { krk_currentThread.next = vm.threads->next; } vm.threads->next = &krk_currentThread; + _release_lock(_threadLock); /* Get our run function */ struct Thread * self = _threadObj; @@ -63,6 +66,19 @@ static void * _startthread(void * _threadObj) { self->alive = 0; + /* Remove this thread from the thread pool, its stack is garbage anyway */ + _obtain_lock(_threadLock); + krk_resetStack(); + KrkThreadState * previous = vm.threads; + while (previous) { + if (previous->next == &krk_currentThread) { + previous->next = krk_currentThread.next; + break; + } + previous = previous->next; + } + _release_lock(_threadLock); + return NULL; } diff --git a/src/vm.c b/src/vm.c index 0384dbd..c3d7df7 100644 --- a/src/vm.c +++ b/src/vm.c @@ -1072,12 +1072,12 @@ static KrkValue krk_setclean(int argc, KrkValue argv[]) { } void krk_initVM(int flags) { - vm.globalFlags = flags & 0xF0; + vm.globalFlags = flags & 0xFF00; KRK_PAUSE_GC(); /* Reset current thread */ krk_resetStack(); - krk_currentThread.flags = flags & 0x0F; + krk_currentThread.flags = flags & 0x00FF; krk_currentThread.module = NULL; krk_currentThread.watchdog = 0; vm.threads = &krk_currentThread; diff --git a/src/vm.h b/src/vm.h index 627dbfc..0ed2ce3 100644 --- a/src/vm.h +++ b/src/vm.h @@ -233,3 +233,23 @@ extern int krk_doRecursiveModuleLoad(KrkString * name); extern KrkValue krk_operator_lt(KrkValue,KrkValue); extern KrkValue krk_operator_gt(KrkValue,KrkValue); + +#ifdef ENABLE_THREADING +#include +static inline void _krk_internal_spin_lock(int volatile * lock) { + while(__sync_lock_test_and_set(lock, 0x01)) { + sched_yield(); + } +} + +static inline void _krk_internal_spin_unlock(int volatile * lock) { + __sync_lock_release(lock); +} + +#define _obtain_lock(v) _krk_internal_spin_lock(&v); +#define _release_lock(v) _krk_internal_spin_unlock(&v); +#else +#define _obtain_lock(v) +#define _release_lock(v) +#endif +