lua-users home
lua-l archive

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



On Mar 31, 2008, at 2:27 PM, Alex Davies wrote:

Hey,

I don't have a need for coroutines (or at least, haven't thought of one yet), but may be able to help. Firstly:

int w_cfunc_call(lua_State *pLuaState)
{
   enqueue_request(...);
   lua_yield(pLuaState,1);
   return 1;
}

should be:

int w_cfunc_call(lua_State *pLuaState)
{
   enqueue_request(...);
   return lua_yield(pLuaState,1);
}

(lua_yield returns -1, to indicate to the vm that it's yielding).
This is exactly what I was missing :)


Secondly, somewhere in your code you've got a C call waiting to return. Eg, if you try and yield from inside an __index metamethod, you'll get the error your seeing. Reason being that the (c) gettable function is still waiting for your metamethod to finish. The most common cause of that I believe is a lua_pcall, which does not allow yielding. One solution to this is to install luajit (recommended anyway), which brings with it coco which allows yielding and resuming from anywhere, even mid function. It does that by allocating c stacks as required, something that is impossible in ansi c but works well in practice.

- Alex
For now I'll stick to this simple solution, since there will be no need for such a functionality in my code.

Thank you very much