From 3bdd21cb96a895e71dbdf8a69f5ef46b8f292d8e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Tue, 3 Dec 2019 00:15:18 +0100 Subject: [PATCH] :art: Improve vm locks --- src/gwion.c | 2 -- src/lib/shred.c | 4 ---- src/vm/vm.c | 33 ++++++++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/gwion.c b/src/gwion.c index 8589d676..07937ae6 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -120,12 +120,10 @@ ANN static void fork_clean2(const VM_Shred shred, const Vector v) { } ANN static void gwion_end_child(const VM_Shred shred, const Gwion gwion) { - MUTEX_LOCK(gwion->vm->shreduler->mutex); if(gwion->data->child.ptr) fork_clean(shred, &gwion->data->child); if(gwion->data->child2.ptr) fork_clean2(shred, &gwion->data->child2); - MUTEX_UNLOCK(gwion->vm->shreduler->mutex); } ANN void gwion_end(const Gwion gwion) { diff --git a/src/lib/shred.c b/src/lib/shred.c index c1f3cfd9..7f974516 100644 --- a/src/lib/shred.c +++ b/src/lib/shred.c @@ -212,10 +212,6 @@ static ANN void* fork_run(void* data) { } ANN void fork_launch(const VM* vm, const M_Object o, const m_uint sz) { - o->ref += 1; - if(!vm->gwion->data->child.ptr) - vector_init(&vm->gwion->data->child); - vector_add(&vm->gwion->data->child, (vtype)o); FORK_RETSIZE(o) = sz; THREAD_CREATE(FORK_THREAD(o), fork_run, o); } diff --git a/src/vm/vm.c b/src/vm/vm.c index d1d4368e..c3456ae8 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -68,12 +68,35 @@ ANN void vm_add_shred(const VM* vm, const VM_Shred shred) { shreduler_add(vm->shreduler, shred); } -#include "gwion.h" +ANN void vm_lock(VM *vm) { + do MUTEX_LOCK(vm->shreduler->mutex); + while((vm = vm->parent)); +} + +ANN void vm_unlock(VM *vm) { + do MUTEX_UNLOCK(vm->shreduler->mutex); + while((vm = vm->parent)); +} + +ANN m_bool vm_running(VM *vm) { + do if(!vm->shreduler->bbq->is_running) return 0; + while((vm = vm->parent)); + return 1; +} + ANN static void vm_fork(VM* src, const VM_Shred shred) { - VM* vm = (shred->info->vm = gwion_cpy(src)); - vm->parent = src; - shred->info->me = new_shred(shred, 0); - shreduler_add(vm->shreduler, shred); + vm_lock(src); + if(vm_running(src)) { + VM* vm = (shred->info->vm = gwion_cpy(src)); + vm->parent = src; + const M_Object o = shred->info->me = new_shred(shred, 0); + ++shred->info->me->ref; + if(!src->gwion->data->child.ptr) + vector_init(&src->gwion->data->child); + vector_add(&src->gwion->data->child, (vtype)o); + shreduler_add(vm->shreduler, shred); + } + vm_unlock(src); } __attribute__((hot)) -- 2.43.0