Skip to content

Commit ace5350

Browse files
committed
Provide MonadTraceMVar
1 parent 5863917 commit ace5350

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

io-classes/src/Control/Concurrent/Class/MonadMVar.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Control.Concurrent.Class.MonadMVar
77
( MonadMVar (..)
88
-- * non-standard extensions
99
, MonadInspectMVar (..)
10+
, MonadTraceMVar (..)
1011
, MonadLabelledMVar (..)
1112
) where
1213

@@ -16,6 +17,7 @@ import Control.Monad.Class.MonadThrow
1617
import Control.Monad.Reader (ReaderT (..))
1718
import Control.Monad.Trans (lift)
1819

20+
import Control.Concurrent.Class.MonadSTM (TraceValue)
1921
import Data.Kind (Type)
2022

2123

@@ -205,6 +207,15 @@ instance MonadInspectMVar IO where
205207
type InspectMVarMonad IO = IO
206208
inspectMVar _ = tryReadMVar
207209

210+
class MonadTraceMVar m where
211+
traceMVarIO :: proxy
212+
-> MVar m a
213+
-> (Maybe (Maybe a) -> Maybe a -> InspectMVarMonad m TraceValue)
214+
-> m ()
215+
216+
instance MonadTraceMVar IO where
217+
traceMVarIO = \_ _ _ -> pure ()
218+
208219
-- | Labelled `MVar`s
209220
--
210221
-- The `IO` instances is no-op, the `IOSim` instance enhances simulation trace.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,16 @@ instance MonadInspectMVar (IOSim s) where
615615
MVarEmpty _ _ -> pure Nothing
616616
MVarFull x _ -> pure (Just x)
617617

618+
instance MonadTraceMVar (IOSim s) where
619+
traceMVarIO _ (MVar mvar) f = traceTVarIO mvar traceMVarAsTVar
620+
where
621+
traceMVarAsTVar Nothing (MVarEmpty _ _) = f Nothing Nothing
622+
traceMVarAsTVar Nothing (MVarFull a _) = f Nothing (Just a)
623+
traceMVarAsTVar (Just (MVarEmpty _ _)) (MVarEmpty _ _) = f (Just Nothing) Nothing
624+
traceMVarAsTVar (Just (MVarEmpty _ _)) (MVarFull a _) = f (Just Nothing) (Just a)
625+
traceMVarAsTVar (Just (MVarFull a _)) (MVarEmpty _ _) = f (Just (Just a)) Nothing
626+
traceMVarAsTVar (Just (MVarFull a _)) (MVarFull a' _) = f (Just (Just a)) (Just a')
627+
618628
instance MonadLabelledMVar (IOSim s) where
619629
labelMVar = labelMVarDefault
620630

0 commit comments

Comments
 (0)