lua-users home
lua-l archive

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


On Fri, Oct 7, 2011 at 5:38 PM, Thijs Schreijer <thijs@thijsschreijer.nl> wrote:
> Iâm in a project where I have a server config (luasockets, copas, etc), but
> also need to use an external library that uses a lot of async
> calls/multithreading/callbacks. How would I get the callbacks back into lua
> (running on the interpreter, not embedded) without running into thread
> issues?

if you're not set on your design of multiple threads driving the Lua
code, you might consider my Helper Threads Toolkit, where a single
coroutine-driven Lua code drives several C tasks running on several
threads [1].

if you already decided on several threads interacting with a LuaSocket
loop, i'd say that your idea is workable; except that I'd replace the
TCP packet with an event on another kind of socket.  In POSIX systems,
you can create a socketpair, or a pipe.  On Linux, there's and
eventfd, BSD have a similar thing.  All these are file-like objects,
handled by a filedescriptor, and you can wait on signals there with
the classic select() call, the same used by LuaSocket.

So, your Lua code could get a socket representing each C task (or a
single one multiplexing several tasks) and add to the LuaSocket
select() call.  when it returns, act according to the type: on TCP
sockets do the usual read/write thing, on event sockets do the
corresponding callback.

I'm pretty sure Copas would work too, you'd have to add an 'event
socket server' creator that works like the basic TCP socket server
creator, associating a coroutine with a socket.


[1]:https://github.com/javierguerragiraldez/helper-threads

-- 
Javier