-
Notifications
You must be signed in to change notification settings - Fork 23
si-timers: round difftime to microseconds in IOSim #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ module Control.Monad.Class.MonadTimer.SI | |||||||||||||
| -- * Auxiliary functions | ||||||||||||||
| , diffTimeToMicrosecondsAsInt | ||||||||||||||
| , microsecondsAsIntToDiffTime | ||||||||||||||
| , roundDiffTimeToMicroseconds | ||||||||||||||
| -- * Re-exports | ||||||||||||||
| , DiffTime | ||||||||||||||
| , MonadFork | ||||||||||||||
|
|
@@ -54,9 +55,24 @@ diffTimeToMicrosecondsAsInt d = | |||||||||||||
| microsecondsAsIntToDiffTime :: Int -> DiffTime | ||||||||||||||
| microsecondsAsIntToDiffTime = (/ 1_000_000) . fromIntegral | ||||||||||||||
|
|
||||||||||||||
| -- | Round to microseconds. | ||||||||||||||
| -- | ||||||||||||||
| -- For negative diff times it rounds towards negative infinity, which is | ||||||||||||||
| -- desirable for `MonadTimer` API. | ||||||||||||||
| -- | ||||||||||||||
| roundDiffTimeToMicroseconds :: DiffTime -> DiffTime | ||||||||||||||
| roundDiffTimeToMicroseconds d = fromIntegral usec / 1_000_000 | ||||||||||||||
| where | ||||||||||||||
| -- microseconds | ||||||||||||||
| usec :: Integer | ||||||||||||||
| usec = diffTimeToPicoseconds d `div` 1_000_000 | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| class ( MonadTimer.MonadDelay m | ||||||||||||||
| , MonadMonotonicTime m | ||||||||||||||
| ) => MonadDelay m where | ||||||||||||||
| -- | All instances SHOULD round delays down to the nearest microsecond so the | ||||||||||||||
| -- behaviour matches the `IO` instance. | ||||||||||||||
| threadDelay :: DiffTime -> m () | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of leaving the type as more generous than the canonical implementation's (ie This would make everything very explicit. The disadvantage is that people would sometimes be forced to call
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we change the API in The question is, how surprised will one be with
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
My pick would be: 1. long-term, 2. or 3. short-term. |
||||||||||||||
|
|
||||||||||||||
| -- | Thread delay. This implementation will not over- or underflow. | ||||||||||||||
|
|
@@ -68,6 +84,9 @@ class ( MonadTimer.MonadDelay m | |||||||||||||
| -- For delays smaller than `minBound :: Int` seconds, `minBound :: Int` will be | ||||||||||||||
| -- used instead. | ||||||||||||||
| -- | ||||||||||||||
| -- NOTE: since `MonadTimer.threadDelay` uses microsecond precision (as does | ||||||||||||||
| -- GHC), so does this instance. | ||||||||||||||
| -- | ||||||||||||||
| instance MonadDelay IO where | ||||||||||||||
| threadDelay :: forall m. | ||||||||||||||
| MonadDelay m | ||||||||||||||
|
|
@@ -103,6 +122,11 @@ instance MonadDelay IO where | |||||||||||||
| instance MonadDelay m => MonadDelay (ReaderT r m) where | ||||||||||||||
| threadDelay = lift . threadDelay | ||||||||||||||
|
|
||||||||||||||
| -- | `MonadTimer` API based on SI units (seconds). | ||||||||||||||
| -- | ||||||||||||||
| -- NOTE: all instances SHOULD round delays down to the nearest microsecond so | ||||||||||||||
| -- the behaviour matches the `IO` instance. | ||||||||||||||
| -- | ||||||||||||||
| class ( MonadTimer.MonadTimer m | ||||||||||||||
| , MonadMonotonicTime m | ||||||||||||||
| ) => MonadTimer m where | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -725,7 +725,8 @@ instance MonadDelay (IOSim s) where | |
|
|
||
| instance SI.MonadDelay (IOSim s) where | ||
| threadDelay d = | ||
| IOSim $ oneShot $ \k -> ThreadDelay d (k ()) | ||
| IOSim $ oneShot $ \k -> ThreadDelay (SI.roundDiffTimeToMicroseconds d) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Evaluate the rounding calculation strictly?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same question applies several other places in this diff. |
||
| (k ()) | ||
|
|
||
| data Timeout s = Timeout !(TVar s TimeoutState) !TimeoutId | ||
| -- ^ a timeout | ||
|
|
@@ -765,11 +766,15 @@ instance SI.MonadTimer (IOSim s) where | |
| timeout d action | ||
| | d < 0 = Just <$> action | ||
| | d == 0 = return Nothing | ||
| | otherwise = IOSim $ oneShot $ \k -> StartTimeout d (runIOSim action) k | ||
| | otherwise = IOSim $ oneShot $ \k -> | ||
| StartTimeout (SI.roundDiffTimeToMicroseconds d) | ||
| (runIOSim action) | ||
| k | ||
|
|
||
| registerDelay d = IOSim $ oneShot $ \k -> RegisterDelay d k | ||
| registerDelay d = IOSim $ oneShot $ \k -> | ||
| RegisterDelay (SI.roundDiffTimeToMicroseconds d) k | ||
| registerDelayCancellable d = do | ||
| t <- newTimeout d | ||
| t <- newTimeout (SI.roundDiffTimeToMicroseconds d) | ||
| return (readTimeout t, cancelTimeout t) | ||
|
|
||
| newtype TimeoutException = TimeoutException TimeoutId deriving Eq | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
threadDelay xforx<0have a different behavior thanthreadDelay 0?If not, then this comment seems like a distraction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
threadDelaydoesn't, buttimeoutdoes, I am not sure aboutregisterDelay, probably not, I'll check.