Fix up some threading gc stuff before diving into real debugging

This commit is contained in:
K. Lange 2021-02-09 21:34:02 +09:00
parent fe9af7c274
commit 854a12f68b
6 changed files with 41 additions and 22 deletions

View File

@ -2612,23 +2612,7 @@ static ParseRule * getRule(KrkTokenType type) {
}
#ifdef ENABLE_THREADING
#include <sched.h>
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) {

View File

@ -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) {

View File

@ -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);

View File

@ -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;
}

View File

@ -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;

View File

@ -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 <sched.h>
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