What is Mio's behaviour on a Poll with a zero duration timeout?

485 Views Asked by At

According to the mio::Poll docs:

The function will block until either at least one readiness event has been received or timeout has elapsed. A timeout of None means that poll will block until a readiness event has been received. ... Note that the timeout will be rounded up to the system clock granularity (usually 1ms), and kernel scheduling delays mean that the blocking interval may be overrun by a small amount.

Meanwhile, Linux's select() has the zero timeout feature:

If both fields of the timeval structure are zero, then select() returns immediately. (This is useful for polling.)

What is Mio's behaviour on a Duration::from_secs(0), would it work like Linux's select()?

1

There are 1 best solutions below

2
On

I suppose you want a Linux answer cause you link to a Linux manual.

mio uses epoll(), not select(), on Linux:

/// |      OS    |  Selector |
/// |------------|-----------|
/// | Linux      | [epoll]   |
/// | OS X, iOS  | [kqueue]  |
/// | Windows    | [IOCP]    |
/// | FreeBSD    | [kqueue]  |
/// | Android    | [epoll]   |

And the relevant quote from epoll() is:

The timeout argument specifies the minimum number of milliseconds that epoll_wait() will block. (This interval will be rounded up to the system clock granularity, and kernel scheduling delays mean that the blocking interval may overrun by a small amount.) Specifying a timeout of -1 causes epoll_wait() to block indefinitely, while specifying a timeout equal to zero cause epoll_wait() to return immediately, even if no events are available.

So, Duration::from_secs(0) will not wait for incoming event. You can check the code of mio here

let timeout_ms = timeout
            .map(|to| cmp::min(millis(to), i32::MAX as u64) as i32)
            .unwrap_or(-1);

We can see that the behavior of mio will copy the behavior of epoll().