|
Maybe I'll need to use preemptive multi-threading, but it sounds a bit complicated given that Lua doesn't provide something that works out of the box.
You might take a look at LuaTask: http://luatask.luaforge.net/This gives you multiple "regular" threads (natively on Windows and via pthreads on Linux), each with its own Lua state, and a very simple set of message-passing routines for synchronisation (so no need to muck about with mutexen, semaphores and the like). The whole library compiles to about 12KB on both Linux and Windows and is easy to learn and use.
You can portably integrate LuaTask-driven subthreads with a coroutine-only top-level dispatcher by having a single main LuaTask subthread which communicates with the other LuaTasks using LuaTask's own inter-task messaging (which permits non-busy waiting), and which comunicates with a COPAS-like (coroutine-driven) master dispatcher using a plain, local TCP LuaSocket. (Sorry, that was a rather long sentence.) This makes the implementation portable between Linux/Unix and Windows.
As long as the amount of data to be shuffled between the subtasks and the master dispatcher is comparatively small, performance should be fine, and both the master-task select()-based dispatcher and the master-subthread task.receive()-based dispatcher will sit idly by in a blocked state when nothing at all is going on.
(Both LuaSocket's socket.select() and LuaTask's task.receive() can be given optional timeout parameters so that they don't block indefinitely during periods of general inactivity.)
The nice thing about LuaTask is its simplicitly and corresponding ease-of-use.