Skip to content

IOSim Timeouts API refactor and optimisation #9

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

Merged
merged 5 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions io-classes/src/Control/Monad/Class/MonadAsync.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}
-- MonadAsync's ReaderT instance is undecidable.
{-# LANGUAGE UndecidableInstances #-}
Expand Down
10 changes: 10 additions & 0 deletions io-classes/src/Control/Monad/Class/MonadSTM.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
-- | This module corresponds to `Control.Monad.STM` in "stm" package
--
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- undecidable instances needed for 'WrappedSTM' instances of 'MonadThrow' and
-- 'MonadCatch' type classes.
{-# LANGUAGE UndecidableInstances #-}
module Control.Monad.Class.MonadSTM
( MonadSTM (STM, atomically, retry, orElse, check)
, throwSTM
Expand Down
3 changes: 3 additions & 0 deletions io-classes/src/Control/Monad/Class/MonadTimer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class Monad m => MonadDelay m where
threadDelay d = void . atomically . awaitTimeout =<< newTimeout d

class (MonadSTM m, MonadDelay m) => MonadTimer m where
-- | The type of the timeout handle, used with 'newTimeout', 'readTimeout',
-- 'updateTimeout' and 'cancelTimeout'.
--
data Timeout m :: Type

-- | Create a new timeout which will fire at the given time duration in
Expand Down
20 changes: 19 additions & 1 deletion io-sim/bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Main (main) where

import Control.Concurrent.Class.MonadSTM
import Control.Monad (replicateM)
import Control.Monad (replicateM, forever)
import Control.Monad.Class.MonadAsync
import Control.Monad.Class.MonadFork
import Control.Monad.Class.MonadSay
Expand Down Expand Up @@ -67,6 +67,9 @@ prop_timeout_fail = timeout 1 (threadDelay 2)
prop_timeout_succeed :: forall m. MonadTimer m => m (Maybe ())
prop_timeout_succeed = timeout 2 (threadDelay 1)

prop_timeout_race :: forall m. MonadTimer m => m (Maybe ())
prop_timeout_race = timeout 1 (threadDelay 1)


--
-- threads, async
Expand All @@ -88,6 +91,13 @@ prop_async n = do
)
traverse_ wait threads

prop_threadDelay_bottleneck :: forall m. (MonadTimer m, MonadSay m)
=> m (Maybe ())
prop_threadDelay_bottleneck =
timeout 1000000 $ do
forever $ do
threadDelay 1
say ""

main :: IO ()
main = defaultMain
Expand Down Expand Up @@ -117,6 +127,8 @@ main = defaultMain
whnf id (runSimOrThrow prop_timeout_fail)
, bench "succeed" $
whnf id (runSimOrThrow prop_timeout_succeed)
, bench "race" $
whnf id (runSimOrThrow prop_timeout_race)
]
]
,
Expand All @@ -127,6 +139,8 @@ main = defaultMain
whnf id (runSimOrThrow (prop_async n))
, bench "forkIO silent" $
whnf id (runSimOrThrow (prop_threads n))
, bench "threadDelay bottleneck silent" $
whnf id (runSimOrThrow prop_threadDelay_bottleneck)
, bench "async say" $
nf id ( selectTraceEventsSay
$ runSimTrace
Expand All @@ -135,6 +149,10 @@ main = defaultMain
nf id ( selectTraceEventsSay
$ runSimTrace
$ prop_threads n)
, bench "threadDelay bottleneck say" $
nf id ( selectTraceEventsSay
$ runSimTrace
$ prop_threadDelay_bottleneck)
]
, env (pure 250) $ \n ->
bgroup "250"
Expand Down
1 change: 0 additions & 1 deletion io-sim/src/Control/Monad/IOSim/CommonTypes.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}

-- | Common types shared between `IOSim` and `IOSimPOR`.
--
Expand Down
Loading