lua-users home
lua-l archive

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


On 10 April 2015 at 16:18, Marc Balmer <marc@msys.ch> wrote:
> My second approach, which seems to work well for now, was to return a
> number in coroutine.yield(), which is interpreted by the C program as a
> interval value in milliseconds.  The C program would then create an
> XtInterval and schedule the coroutine to be resumed in the specified
> time.  This way a coroutine schedules itself.
>
> What other strategies are being used?  How do others handle coroutines
> in hosted programs?

This is essentially how cqueues
(http://25thandclement.com/~william/projects/cqueues) works:
You yield any number of objects with 'timeout', 'pollfd' and 'events'
fields (which can be constant or functions returning constants),
The host application (or parent cqueues controller) waits for the
given fd to poll readable or writable (depending on 'events' field) or
the timeout elapses, then resumes the thread.

e.g, to sleep for 5 seconds:
cqueues.poll({timeout = 5})
e.g. to wait up to 10 seconds for stdin to poll readable:
cqueues.poll({pollfd = 0; events = "r"; timeout = 10})

If you recall, I maintain a cqueues wrapper around your own luapgsql:
https://github.com/daurnimator/cqueues-pgsql
It uses this yielding protocol to get non-blocking postgres operations.