xopxe / lumen

Lua Multitasking Environment.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

designing module code to work with Lumen

lePereT opened this issue · comments

Hi there. Lumen is great, thank you. Still coming to grips with it and wanted to ask a question that might reveal my lack of understanding!

Say I'm running a lumen scheduling loop and from one of the tasks in that loop I'm calling some code in a library (that I'm writing) that slowly flashes an led in a complex pattern. I'd like the periods of sleep between led flashes to be available to run other elements of my program.

Is this as simple as adding calls to sched.sleep() in my library code? Or is there a more elegant solution?

commented

Basically yes. Lumen is single thread and non-preemptive Thus, any code you write will run exactly as you wrote it, and the only places it will release the cpu for some other task are sleep(), wait(), signal() and run() calls.

So, if you have a long running block of code which you want to "paralelize" with other code, you must add sched.sleep() calls in places where it would be called periodically.

In this case, the library of course not a generic Lua library, because it must require lumen. Sometimes you can organize you code differently, with a library of lower-level plain blocking Lua calls, and the timing logic in another, higher level, library (perhaps together with the task definition?). That's a common pattern, but depends on you application.

Dear Xopxe, thanks so much for this fantastic codebase. We're using it in a tiny low-power system, it's proven so amazingly powerful, flexible and reliable. The wealth of patterns that can be created using these primitives, the ability to wait on multiple signals, for timeouts to be propagated, for so many other things is quite incredible.

I cannot for the life of me understand why Lumen isn't shouted about from the hilltops or presented as the go-to pure Lua multitasking framework. Many thanks from team Jangala (www.janga.la)