|
Recently there was a thread regarding callbacks from Qt, I’m planning to create a binding also for a library that has its own threadpool, and hence face similar synchronization issues. I created a library (called DarkSideSync) to handle this in a generic way using a socket. Background threads can call its ‘deliver’ function where the data will be queued in a thread safe way. From the Lua side a ‘poll’ method is available to collect the data. The library can fire a UDP packet on a local port for every delivery queued by a background thread. Its intended to be cross-platform, but only does linux for now. As a test project I created another library, LuaSignal, that will catch the SIGTERM signal to allow Lua to do a clean exit. An example is included that does a controlled exit from a Copas loop when SIGTERM in received. I would appreciate feedback on the code and the usefulness in general. - is sharing a c pointer (to a c function) from one c lib to another, through a lightuserdata on the register, safe? - how to do unsafe calls in a signal handler (LuaSignal specific)? - public/private declarations used properly? - static and volatile declarations used properly? - doesn’t yet handle a c callback that expects a return value The luasignal example uses Copas, CopasTimer, LuaRocks and LuaSocket but it requires the head revision of Copas as the current 1.1.6 version does not handle UDP and head does. DarkSideSync code and LuaSignal example; https://github.com/Tieske/DarkSideSync/tree/master/DarkSideSync Required Copas version; https://github.com/keplerproject/copas Some explanation on how I intended it to work; https://github.com/Tieske/DarkSideSync/raw/master/background%20worker%20lib%20spec%200v1.pdf All in all it’s probably messy, it took me some weeks, my first c project ever (I skipped ‘hello world’), typing with one hand, while the other was tracking lines in either K&R, Pil or LuaRef. Looking forward to your feedback, and thanks for reading even this far :-) Thijs |