lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,

 

I’ve got a simulation engine using c.coroutines. I’d like to implement some of them with lua. However I’d like to share the same lua context for all coroutines. So basically I’d like this c.coroutine package to control execution of lua.coroutines with scheduling them.

Is it possible to write something like this (sorry only algo, I haven’t looked in detail Lua API, hope it is still clear enough) ?

 

// C side

Lua_State* global_state;

 

c_start() {

  // create interpreter and start code after

  global_state = lua_open();

  handle_script(L, …); 

  c_coroutine_schedule();

}

 

my_c_coroutine() {

  me = id++;

  while (1) {

    condition = lua_resume(global_state, coroutines[id]);

    c_wait(condition);

    // return thanks to c_coroutine scheduling

  }

}

 

my_c_coroutinecreate(co) {

  coroutines[numcoroutines++] = co;

  c_spawn(&my_c_coroutine);

}

 

condition my_c_conditioncreate(conditionparms…) {

   return condition;

}

 

-- lua side

function f()

  …

  coroutine.yield(my_c_conditioncreate(…))

  …

end

Co1 = coroutine.create(f)

my_c_coroutinecreate(co1)

Co2 = coroutine.create(f)

my_c_coroutinecreate(co2)

Co3 = coroutine.create(f)

my_c_coroutinecreate(co3)

Co4 = coroutine.create(f)

my_c_coroutinecreate(co4)

 

regards,

 

Thierry