lua-users home
lua-l archive

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


> Is it possible that the select() works both for socket and timer? or
> is there any good practise to dispatch both threads yield by socket
> timeout and those who yield by timer like sleep()?

You can't use blocking calls like sleep() in a select driven program.

Its annoying, but your best bet is for your coroutine to yield with
the amount of time it wants to "sleep".

Your scheduler will then have to keep track of every coroutine that is
"sleeping", and how much longer
it should be sleeping. Every time before calling socket.select(), you
will have to figure out which coroutine
will wake the soonest, and how long until it should wake, and provide
the timeout argument to socket.select()
so it wakes up even if no socket is ready. Then after every wakeup,
you will have to search for any sleeping coroutines that can be
resumed...

This should be portable, but you'll want a decent data structure that
sorts coroutines by wake-up time to do this.

The other approach that will work on Linux at least is to use the
timerfd_create() calls. They allow you to create a fd
that you can pass to socket.select(), and it will become readable when
the timer expires.

It might be that copas does this, and if so you can steal some of
their code (or maybe just use copas?).

Cheers,
Sam