[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Strategies for Lua coroutines in C programs?
- From: Daurnimator <quae@...>
- Date: Fri, 10 Apr 2015 16:53:47 +1000
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.