This library must provide a simple scheduler with a fair signal queue. The scheduler call coroutine.resume
on each signal and push the new signal coroutine yielded to the queue.
Mostly, any asynchronous library is used for I/O programming in real life. But it's hard for away, which designed to be implemented by pure lua. Then the only choice is to use a fair signal queue, which treat every signal equally, to offer better friendly interface to other library.
Auto signal provides a way to wake a thread on every "step" start. scheduler.set_auto_signal
in fact add the function given to scheduler.auto_signals
. The function called auto signal generator, which return a new signal (a table) every single time.
You may noticed that the scheduler doesn't keep references to threads. Instead, it requires program provide the target thread every time when you push signal.
To run a thread, the scheduler will resume
it with a signal and push the value it yielded/returned into signal queue if it's a table. If the table could not be considered as a signal, push_signal
will call error
. More details see scheduler.run_thread
.
For every start of a "step" of the scheduler, it copy all the signal currently have to another internal table, then reset the public queue. In the step, it only walk on these signal it copied. It's for fair of the auto signals. After signals walked, it run all auto signal generators and push them into queue.