From: Jérémie Astor <astor.jeremie@wanadoo.fr>
Date: Sat, 9 May 2020 21:11:53 +0000 (+0200)
Subject: :art: Improve fork (towards retval)
X-Git-Tag: nightly~1607
X-Git-Url: http://10.11.0.4:5575/?a=commitdiff_plain;h=5b8bda98e986b39d98a0a7c2568b4f3180294e9a;p=gwion.git

:art: Improve fork (towards retval)
---

diff --git a/src/lib/shred.c b/src/lib/shred.c
index de943664..0ec0e32b 100644
--- a/src/lib/shred.c
+++ b/src/lib/shred.c
@@ -13,12 +13,13 @@
 #include "specialid.h"
 #include "gwi.h"
 
-static m_int o_fork_thread, o_fork_cond, o_fork_mutex, o_shred_cancel, o_fork_done, o_fork_ev, o_fork_retsize;
+static m_int o_fork_thread, o_fork_cond, o_fork_mutex, o_shred_cancel, o_fork_done, o_fork_ev, o_fork_retsize, o_fork_ptr;
 
 #define FORK_THREAD(o) *(THREAD_TYPE*)(o->data + o_fork_thread)
 #define FORK_COND(o) *(THREAD_COND_TYPE*)(o->data + o_fork_cond)
 #define FORK_MUTEX(o) *(MUTEX_TYPE*)(o->data + o_fork_mutex)
 #define FORK_RETSIZE(o) *(m_int*)(o->data + o_fork_retsize)
+#define FORK_PTR(o) *(m_uint**)(o->data + o_fork_ptr)
 
 VM_Shred new_shred_base(const VM_Shred shred, const VM_Code code) {
   const VM_Shred sh = new_vm_shred(shred->info->mp, code);
@@ -181,6 +182,7 @@ static DTOR(fork_dtor) {
     vector_init(&parent->gwion->data->child2);
   vector_add(&parent->gwion->data->child2, (vtype)ME(o)->info->vm->gwion);
   REM_REF(ME(o)->code, ME(o)->info->vm->gwion);
+  mp_free2(shred->info->vm->gwion->mp, FORK_RETSIZE(o), FORK_PTR(o));
   MUTEX_UNLOCK(parent->shreduler->mutex);
 }
 
@@ -237,19 +239,19 @@ struct ThreadLauncher *tl = data;
     vm_run(vm);
     ++vm->bbq->pos;
   }
+  memcpy(FORK_PTR(me), ME(me)->reg, FORK_RETSIZE(me));
   gwion_end_child(ME(me), vm->gwion);
   MUTEX_LOCK(vm->parent->shreduler->mutex);
+  *(m_int*)(me->data + o_fork_done) = 1;
   if(!*(m_int*)(me->data + o_shred_cancel))
     broadcast(*(M_Object*)(me->data + o_fork_ev));
   MUTEX_UNLOCK(vm->parent->shreduler->mutex);
-  *(m_int*)(me->data + o_fork_done) = 1;
-//  if(!*(m_int*)(me->data + o_shred_cancel))
-//    _release(me, ME(me));
   THREAD_RETURN(0);
 }
 
 ANN void fork_launch(const M_Object o, const m_uint sz) {
   FORK_RETSIZE(o) = sz;
+  FORK_PTR(o) = mp_calloc2(ME(o)->info->vm->gwion->mp, sz);
   MUTEX_SETUP(FORK_MUTEX(o));
   THREAD_COND_SETUP(FORK_COND(o));
   struct ThreadLauncher tl = { .mutex=FORK_MUTEX(o), .cond=FORK_COND(o), .vm=ME(o)->info->vm };
@@ -371,6 +373,8 @@ GWION_IMPORT(shred) {
   GWI_BB((o_fork_ev = gwi_item_end(gwi, ae_flag_const, NULL)))
   gwi_item_ini(gwi, "int", "retsize");
   GWI_BB((o_fork_retsize = gwi_item_end(gwi, ae_flag_const, NULL)))
+  gwi_item_ini(gwi, "@internal", "@ptr");
+  GWI_BB((o_fork_ptr = gwi_item_end(gwi, ae_flag_const, NULL)))
   gwi_func_ini(gwi, "void", "join");
   GWI_BB(gwi_func_end(gwi, fork_join, ae_flag_none))
   gwi_func_ini(gwi, "void", "test_cancel");