3

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()?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Yuri Geinish
  • 16,744
  • 6
  • 38
  • 40

1 Answers1

1

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().

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • I wonder if you know why that code quoted in your last paragraph does the `as u64` and then back with `as i32`. Why not keep it `i32` through the whole expression? – Zan Lynx Apr 12 '18 at 23:48
  • @ZanLynx `millis()` return an `u64`, In order to avoid overflow because timeout is an C `int` (they suppose C `int` has 32 bits), they compare this value to `i32::MAX` to do that need to first cast it to u64, then they take the minimum, so the final result is positive and can't overflow an `i32`. – Stargateur Apr 13 '18 at 00:10