threadDelayNano method in base
Motivation
The unix package provides a method nanosleep :: Integer -> IO ()
method that sleeps for a given number of nanoseconds, but says not to use it because threadDelay
is a better choice.
However, threadDelay : Int -> IO ()
only allows specifying the precision in microseconds. Hence, for nanosecond time specifications, the base library is currently lacking.
Proposal
I propose adding a new function threadDelayNano : Int -> IO ()
which delays a given number of nanoseconds. If the user needs to sleep longer than fits in an Int they can use the high-resolution timer from the clock package (or GHC's timer functions) and delay multiple times in a loop using either the threadDelay
or threadDelayNano
functions.
As far as implementation, it seems a little tricky because the thread manager uses a mixture of nanoseconds and microseconds. On Unix it looks like there is no issue because internally it uses nanoseconds. Windows seems to be using microseconds for the old Posix FD backend but nanoseconds for the new WinIO backend. So the implementation should probably fall back to threadDelayNano t = threadDelay (t `div` 1000)
(suitably modified for overflow) for those cases where nanosecond precision isn't available.