[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Can I stop and continue lua code from a C function called from lua?
- From: Sam Roberts <sroberts@...>
- Date: Wed, 31 May 2006 16:24:38 -0700
I work with a system where there is something like RPC over UDP. I've a
main loop in C that receives msgs, and passes (some of) them to lua. Lua
sends messages out that will get msg responses. I want it to look from
lua like its doing a blocking wait for a response message (or error) to
come back, but I need to return control to the main C message loop.
Any suggestions on how to do this? Some cool pure lua coroutine magic?
Something in C? Anything?
To give an idea of the problem I have code like this in C:
-- C --
while(e = get_event())
{
if(e is destined for lua interpreter "L")
lua_pcall(L, "dispatch", lua_userdata(...e...)...)
else
... do other things with it
}
--
and in lua:
-- lua --
function dispatch(msg)
if(!waiting_for_response)
send_a_msg_request()
-- this will send a msg for which a response msg is expected
waiting_for_response = true
else
-- msg is the response to above
waiting_for_response = false
end
return -- back to C
end
--
where send_a_msg_request() is implemented in C.
What I'd like is to hide this nasty unstructured-state-machine approach,
so I can write lua code like:
-- lua
response = send_msg_and_wait()
response = send_msg_and_wait()
--
with control returned to the main C message loop during the "wait",
and the main loop returning back into lua when its ready.
Btw, I can't use O/S threads, so don't suggest it!
Sam