From: Jérémie Astor Date: Mon, 2 Dec 2019 23:15:18 +0000 (+0100) Subject: :art: Improve vm locks X-Git-Tag: nightly~2070^2~9 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=3bdd21cb96a895e71dbdf8a69f5ef46b8f292d8e;p=gwion.git :art: Improve vm locks --- 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))