lua-users home
lua-l archive

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


On Mon, Dec 1, 2008 at 12:01 PM, Sam Roberts <vieuxtech@gmail.com> wrote:
>> 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.

This is because the main lua_State thread is resuming threads and is
not available to execute the callback function. I thought perhaps I
would need to make a simple throwaway thread for running each callback
function but was hoping to avoid that by just using the yielded
thread.

>  testme:
>  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?? */
>    fatal("...");
>
>  // push args to use when resumeing thread
>  lua_pushnumber(thread, 1);
>  lua_push(thread, ...)
>
>  goto testme;
> }

There actually is no lua_pushthread (lua_State *, lua_State *) API
function available (although I feel there should be). It seems I'll
need to find an alternate approach as there has been no clear response
as to the legality of making (protected) function calls on a yielded
thread.

Thanks,

-- 
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant