lua-users home
lua-l archive

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

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...)...)
	  ... do other things with it

and in lua:

-- lua --
function dispatch(msg)
	-- this will send a msg for which a response msg is expected
	waiting_for_response = true
	-- msg is the response to above
	waiting_for_response = false
  return -- back to C

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!