lua-users home
lua-l archive

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



> -----Oorspronkelijk bericht-----
> Van: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org]
> Namens Daurnimator
> Verzonden: Friday, 22 April, 2016 8:52
> Aan: Lua mailing list
> Onderwerp: Re: Lua callback in another thread
> 
> On 22 April 2016 at 15:08, Thijs Schreijer <thijs@thijsschreijer.nl>
> wrote:
> > I had the same issue long ago with the PUPnP library, hence I wrote
> > “darksidesync” [1], which signals the main thread using a udp packet
> > (or just by polling). Not the most elegant solution, but works and is
> > very portable (though I think the Windows build is broken right now)
> >
> >
> >
> > Thijs
> >
> >
> >
> > [1] http://tieske.github.io/DarkSideSync/topics/readme.md.html
> >
> >
> >
> > Van: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org]
> > Namens Laurent FAILLIE
> > Verzonden: Wednesday, 20 April, 2016 11:33
> > Aan: Lua Mailing List
> > Onderwerp: Lua callback in another thread
> >
> >
> >
> > Hello,
> >
> >
> >
> > I have a C multi-threaded program using Lua as user end scripting
> > language ( https://github.com/destroyedlolo/Selene , I already
> > presented some months back :) ).
> >
> >
> >
> > My main thread is used also to declare some Lua callbacks needs to be
> > called when an MQTT data arrived. So I keep a reference to this
> > function using classical luaL_ref().
> >
> >
> >
> > Now, my problem : an MQTT data arrives and is handled by another
> > thread, on which I need to created a dedicated Lua_State for this newly
> created thread.
> > But how can I retrieve then launch this function in the new lua_state.
> >
> > Because, for the moment, I'm retrieving the function from the main
> > state, using xmove() it to the new one and this call it ... obviously,
> > it's crashing as the main state may be busy by other tasks.
> >
> > In addition, I don't want to block the main thread using mutex as I
> > don't want to have to rely on main thead activities.
> >
> >
> >
> > So any idea welcome :)
> 
> Infact, sending a packet to a socket *is* actually the best option for
> this sort of thing.
> On linux, you have a special fast kernel provided file descriptor type
> 'eventfd' just for this purpose.
> In general, you can fall back to using a pipe() or socketpair() and
> writing a single byte so that the other end polls readable.

The way I generally use DarkSideSync, is with Copas as a scheduler, with an event driven paradigm. All code is scheduled as tasks, combined with timers and socket events.
The incoming mqtt stuff would just be another socket event in your case.

Whether or not you can implement it in your application I don't know, but my first guess would be to decouple the main application from the Lua state as well. Or stated otherwise, run a Lua state to do your Lua stuff and feed it with events from both the mqtt stack as well as the main application, both through DSS.

It works best with an event driven paradigm.

Btw; Another example, the GPIO interface for the raspberry pi [1] also uses DSS for background event triggering from foreign threads.

[1] GPIO Lua; https://github.com/Tieske/rpi-gpio/blob/master/lua/README.md 
[2] example code; https://github.com/Tieske/rpi-gpio/blob/master/lua/scripts/test6_callbacks.lua