lua-users home
lua-l archive

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

> My principle concern is whether it is considered legal to have a
> thread yield and then make a protected call using that thread before
> resuming it. We are allowed to push arguments in order to resume the
> thread but are we allowed to make function calls using that thread
> before doing so? My current implementation shows this is possible, but
> I question whether it is considered legal or safe. Essentially:
> testme:
> if (lua_resume(thread, lua_gettop(thread)) == LUA_YIELD)
> {
>  lua_getfield(thread, LUA_REGISTRYINDEX, "some_Lua_function");
>  lua_pushthread(thread);
>  if (lua_pcall(thread, 1, 0, 0) != 0)  /* <---- Is this legal?? */
>    fatal("...");
>  /* push other arguments to the thread */
>  goto testme;
> }

Looks legal to me, but I can see why you wonder. I don't see why you use the
thread lua_State instead of the parent lua_State.

A thread shares everything with its parent, except execution stack,
and at the end of the pcall, you haven't changed the thread's stack
(it returns zero arguments).

Maybe it's because I don't know what some_Lua_function does, but why not
use the "parent" state of the thread for calling it?

lua_State* L = ...
lua_State* thread  = lua_newstate(L);
/* You store the thread object somewhere? It will get GCed if you don't make
sure its referenced from L. Just leaving in on L's stack is enough,
but usually you
want to do something with L. */

 if (lua_resume(thread, lua_gettop(thread)) == LUA_YIELD)
  // thread and L have same registry
  // call: REGISTRY["some_Lua_function"](thread)
  lua_getfield(L, LUA_REGISTRYINDEX, "some_Lua_function");
  lua_pushthread(L, thread);
  if (lua_pcall(L, 1, 0, 0) != 0)  /* <---- Is this legal?? */

  // push args to use when resumeing thread
  lua_pushnumber(thread, 1);
  lua_push(thread, ...)

  goto testme;