lua-users home
lua-l archive

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


Hello,

Here is a little scenario that I want to use:

-- /tmp/test.lua --
function test()
        print("Before")
        a=cfunc(123)
        print("After",a)
end
-- end tmp/test.lua --


cfunc is a function defined in C++ like this:

-- cut here --
int w_cfunc_call(lua_State *pLuaState)
{
    //Get the parameters from pLuaState
    .....

   //I enqueue a request. I don't know when it will finish
   //It will run in a separate thread. When it will be finished
   //I will get signaled and I will do a lua_resume. Till then
   //we must issue a lua_yield
    enqueue_request(...);

    lua_yield(pLuaState,1);

    return 1;
}
....

lua_pushcfunction(pLuaState, w_cfunc_call);
lua_setglobal(pLuaState, "cfunc");

...
-- end cut --

Basically, when I do `a=cfunc(123)` it will take a looong time (database access). I want to yield the current coroutine from C++ so the lua script writer won't feel anything. But When I run my app I get '/tmp/test.lua: attempt to yield across metamethod/C-call boundary'

To further summarize the problem:

1. Have a c++ working thread that gets it's work chunk from a queue
2. Wrap all the internals in a c++ function callable from Lua
	a. enqueue the work chunk
	b. yield Lua
	c. wait for the results from thread (we will get signaled)
	d. resume Lua by invoking lua_resume
3. The writer of the Lua script must not know anything about the internals of the call (we must not force him to make a yield/resume from Lua)


Is this possible?