Block-I/O in Haskell

Tomasz Zielonka t.zielonka at students.mimuw.edu.pl
Thu Oct 14 02:52:56 EDT 2004


On Thu, Oct 14, 2004 at 12:36:37AM +0200, Peter Simons wrote:
> Benjamin Franksen writes:
> 
>  > milliseconds (1/10^6 seconds)
> 
> Duh! I meant microseconds of course. Thanks for catching
> that.

Did you consider using floating-point Timeout type, with its
values representing seconds, possibly fractional? This could
have some negative performance impact, but would be elegant IMHO.

I prepared a darcs patch with such changes, added timeoutDelay
which is analogous to threadDelay, but works with Timeout values,
and made threadDelay immune to delays which overflow Int with
number of microseconds (2^31 microseconds is not such long).

Feel free to reject it :)

Best regards,
Tom

-- 
.signature: Too many levels of symbolic links
-------------- next part --------------

New patches:

[changed Timeouts to be possibly fractional amount of seconds (type Double)
Tomasz Zielonka <t.zielonka at students.mimuw.edu.pl>**20041014063618] {
hunk ./Childs.hs 25
-  , Timeout     --  = Int
+  , Timeout     --  = Double
+  , timeoutDelay--  :: Timeout -> IO ()
hunk ./Childs.hs 114
--- |Timeouts are given in milliseconds (@1\/10^6@ seconds).
+-- |Timeouts are given in seconds, which can be fractional.
hunk ./Childs.hs 117
-type Timeout = Int
+type Timeout = Double
+
+timeoutDelay :: Timeout -> IO ()
+timeoutDelay t = threadDelay (round (t * 1000000.0))
hunk ./Childs.hs 123
--- 'Nothing' after @n@ milliseconds, otherwise @'Just' a@ is
+-- 'Nothing' after @n@ seconds, otherwise @'Just' a@ is
hunk ./Childs.hs 131
-      r <- par (threadDelay n >> return []) (fmap return f)
+      r <- par (timeoutDelay n >> return []) (fmap return f)
hunk ./Childs.hs 137
--- |@sleep n@ @=@ @threadDelay (abs(n) * 1000000)@
+-- |@sleep n@ @=@ @timeoutDelay (fromIntegral (min n 0))@
hunk ./Childs.hs 140
-sleep n = threadDelay (abs(n) * 1000000)
+sleep n = timeoutDelay (fromIntegral (min n 0))
}

[fixed timeoutDelay to handle delays overflowing the number of microseconds in Int
Tomasz Zielonka <t.zielonka at students.mimuw.edu.pl>**20041014063805] {
hunk ./Childs.hs 120
-timeoutDelay t = threadDelay (round (t * 1000000.0))
+timeoutDelay t | t > 100.0 = threadDelay 100000000 >> timeoutDelay (t - 100.0)
+               | otherwise = threadDelay (round (t * 1000000.0))
}



Context:

[initial version
simons at cryp.to**20041012201657] 

Patch bundle hash:
c2369d21a3326586b350cc55f693644b72fd08cb


More information about the Libraries mailing list