base: Fix fdReady() returning immediately for pipes on Windows.
authorNiklas Hamb├╝chen <mail@nh2.me>
Tue, 19 Sep 2017 19:11:05 +0000 (15:11 -0400)
committerBen Gamari <ben@smart-cactus.org>
Tue, 19 Sep 2017 19:58:46 +0000 (15:58 -0400)
commit66240c9bc77408f841e8cf974d44580434fb1a48
tree79995c0ba6f02cddea27865d35eec74d1940e027
parent826c3b11780abcb74e5aba987e16369d69ac79a5
base: Fix fdReady() returning immediately for pipes on Windows.

See https://ghc.haskell.org/trac/ghc/ticket/13497#comment:17

Until now, the program

  import System.IO
  main = hWaitForInput stdin (5 * 1000)

didn't wait 5 seconds for input on Winodws, it terminated immediately.

This was because the `PeekNamedPipe()` function introduced in commit
94fee9e7 really only peeks, it doesn't block.  So if there's no data,
`fdReady(fd, msec)` would return immediately even when the given `msec`
timeout is not zero.

This commit fixes it by looping around `PeekNamedPipe()` with a `sleep(1
ms)`.

Apparently there's no better way to do this on Windows without switching
to IOCP.

In any case, this change should be strictly better than what was there
before.

Reviewers: bgamari, austin, hvr

Reviewed By: bgamari

Subscribers: Phyx, rwbarton, thomie

Differential Revision: https://phabricator.haskell.org/D3956
libraries/base/cbits/inputReady.c