Skip to content

Commit 6781369

Browse files
committed
si-timers: round difftime to microseconds in IOSim
This makes `threadDelay` from `si-timers` sublibrary behave the same way for `IOSim` and `IO`.
1 parent 6be63a9 commit 6781369

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed

io-classes/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
### Breaking changes
66

7-
+* Changed `Time` show instance, which now is designed for pasting
8-
+ counterexamples from terminal to an editor.
7+
* Changed `Time` show instance, which now is designed for pasting
8+
* counterexamples from terminal to an editor.
99

1010
### Non-breaking changes
1111

1212
* Improved performance of `tryReadTBQueueDefault`.
1313
* Added module `Control.Monad.Class.MonadUnique` generalising `Data.Unique`.
1414
* mtl: Added module `Control.Monad.Class.MonadUnique.Trans` providing monad transformer instances for `MonadUnique`.
15+
* Added `roundDiffTimeToMicroseconds` utility function to `si-timers` package (in the `MonadTimer.SI` module).
1516

1617
## 1.8.0.1
1718

io-classes/si-timers/src/Control/Monad/Class/MonadTimer/SI.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Control.Monad.Class.MonadTimer.SI
55
-- * Auxiliary functions
66
, diffTimeToMicrosecondsAsInt
77
, microsecondsAsIntToDiffTime
8+
, roundDiffTimeToMicroseconds
89
-- * Re-exports
910
, DiffTime
1011
, MonadFork
@@ -54,6 +55,16 @@ diffTimeToMicrosecondsAsInt d =
5455
microsecondsAsIntToDiffTime :: Int -> DiffTime
5556
microsecondsAsIntToDiffTime = (/ 1_000_000) . fromIntegral
5657

58+
-- | Round to microseconds.
59+
--
60+
roundDiffTimeToMicroseconds :: DiffTime -> DiffTime
61+
roundDiffTimeToMicroseconds d = fromIntegral usec / 1_000_000
62+
where
63+
-- microseconds
64+
usec :: Integer
65+
usec = diffTimeToPicoseconds d `div` 1_000_000
66+
67+
5768
class ( MonadTimer.MonadDelay m
5869
, MonadMonotonicTime m
5970
) => MonadDelay m where

io-classes/si-timers/test/Test/MonadTimer.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ tests =
1717
prop_diffTimeToMicrosecondsAsIntLeftInverse
1818
, testProperty "diffTimeToMicroseconds right inverse"
1919
prop_diffTimeToMicrosecondsAsIntRightInverse
20+
, testProperty "roundToMicroseconds"
21+
prop_roundDiffTimeToMicroseconds
2022
]
2123

2224
newtype IntDistr = IntDistr Int
@@ -88,3 +90,19 @@ prop_diffTimeToMicrosecondsAsIntRightInverse (DiffTimeDistr a) =
8890
-> "large"
8991
| otherwise
9092
-> "average"
93+
94+
95+
prop_roundDiffTimeToMicroseconds :: DiffTimeDistr -> Property
96+
prop_roundDiffTimeToMicroseconds (DiffTimeDistr d) =
97+
-- rounded is less or equal to d
98+
d' <= d
99+
.&&.
100+
-- difference is less than 1 microsecond
101+
abs (d - d') < 0.000_001
102+
.&&.
103+
-- rounded has no fractional microseconds
104+
case properFraction (d' * 1_000_000) of
105+
(_ :: Integer, f) -> f === 0
106+
where
107+
d' = roundDiffTimeToMicroseconds d
108+

io-sim/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
* Added support for unique symbol generation à la `Data.Unique`.
1010
* Removed a misleading internal comment.
11+
* Round `si-timers` `threadDelay` to microsecond to match `IO` behaviour.
1112

1213
## 1.8.0.1
1314

io-sim/src/Control/Monad/IOSim/Types.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ instance MonadDelay (IOSim s) where
725725

726726
instance SI.MonadDelay (IOSim s) where
727727
threadDelay d =
728-
IOSim $ oneShot $ \k -> ThreadDelay d (k ())
728+
IOSim $ oneShot $ \k -> ThreadDelay (SI.roundDiffTimeToMicroseconds d)
729+
(k ())
729730

730731
data Timeout s = Timeout !(TVar s TimeoutState) !TimeoutId
731732
-- ^ a timeout

0 commit comments

Comments
 (0)