lua-users home
lua-l archive

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



Does anyone has use Lua in any parallel system?

Yes, we do. A bit over two years ago [phew... where *does* the time go?!?] I created a runtime environment where separate Lua states run parallel and communicate using light-weight, asynchronous message queues.

This model is very easy to program, since stuff running in one state is blissfully unaware of everything outside that state. And since the messages are asynchronous, code can pretty much run at full speed and decide when to handle what in an efficient manner (without silly waiting or polling or whatnot).

An interesting twist to our scheme is that we actually (can) have 2 threads per Lua state. While many seem to go for symmetrical scenarios I decided for some fearless asymmetry and came up with a "dual-threading" approach, where a controller and worker (OS) thread share one state.

The controller thread has higher priority, that is: it can interrupt the worker to provide (initial) handling of incoming messages. It's also the one that starts the worker thread and will be signalled when the worker is done. The worker thread runs a tad slower, since it runs under the line-hook [to give the controller a means to butt in]. But that's okay, since it's completely up to the programmer to decide which thread to use for what logic.

Initially I came up with the whole "extended dual-threading" model to make sure that the GUI of the "smart client" I was developing stayed responsive, especially when there's some serious communication going on and such. As things turned out, it all worked so well we decided to use the same runtime for our back-end.

A typical scenario here is a server that has one "box" [read: Lua state + message queue + controller/worker thread pair] for the console GUI, one for TCP/IP communication (based on Luasocket, btw.) and one for each service the back-end provides (typically database access and such).

Anyway, I'm not at all sure the above information is in any way relevant to "parallel systems". I'm seeing references to the recently unveiled Cell processor... But since threading was mentioned I thought I'd toss the story in to give people an idea of what Lua can do for developers.

Ashwin.
--
no signature is a signature.