lua-users home
lua-l archive

Re: Can I stop and continue lua code from a C function called from lua?

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


> 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?
Sure, coroutines are the perfect tool for that:
1. write you Lua function as
 local function dispatch()
	 wait()
	 while true do
		 msg = ...
		 ...
		 local response = send(msg)
		 ...
	 end
 end
 DISPATCH=coroutine.wrap(dispatch)
2. define wait and send to be coroutine.yield
3. create a new Lua thread and call DISPATCH from you C program in this thread.
 The first time DISPATCH is called, it just returns. You do nothing.
 In you C main loop, just keep calling DISPATCH, now with the response.
 Depending on what you want to do, you may be able to avoid wait().
I did someting like that in my token filters, about which I talked at the
Workshop last year, and which I still have to finish and release.
--lhf

AltStyle によって変換されたページ (->オリジナル) /