Skip to content

Commit 363ad29

Browse files
committed
io-sim-classes: StrictTVar representation
if the cabal flag 'checktvarinvariant' is not set, we can use a newtype for representing a `StrictTVar`.
1 parent 4d57942 commit 363ad29

File tree

1 file changed

+25
-4
lines changed
  • io-sim-classes/src/Control/Monad/Class/MonadSTM

1 file changed

+25
-4
lines changed

io-sim-classes/src/Control/Monad/Class/MonadSTM/Strict.hs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,17 @@ type LazyTMVar m = Lazy.TMVar m
7676
Strict TVar
7777
-------------------------------------------------------------------------------}
7878

79+
#if CHECK_TVAR_INVARIANT
7980
data StrictTVar m a = StrictTVar
8081
{ invariant :: !(a -> Maybe String)
8182
-- ^ Invariant checked whenever updating the 'StrictTVar'.
8283
, tvar :: !(LazyTVar m a)
8384
}
85+
#else
86+
newtype StrictTVar m a = StrictTVar
87+
{ tvar :: LazyTVar m a
88+
}
89+
#endif
8490

8591
labelTVar :: MonadLabelledSTM m => StrictTVar m a -> String -> STM m ()
8692
labelTVar StrictTVar { tvar } = Lazy.labelTVar tvar
@@ -90,7 +96,11 @@ labelTVarIO v = atomically . labelTVar v
9096

9197
castStrictTVar :: LazyTVar m ~ LazyTVar n
9298
=> StrictTVar m a -> StrictTVar n a
99+
#if CHECK_TVAR_INVARIANT
93100
castStrictTVar StrictTVar{invariant, tvar} = StrictTVar{invariant, tvar}
101+
#else
102+
castStrictTVar StrictTVar{ tvar} = StrictTVar{ tvar}
103+
#endif
94104

95105
-- | Get the underlying @TVar@
96106
--
@@ -100,7 +110,11 @@ toLazyTVar :: StrictTVar m a -> LazyTVar m a
100110
toLazyTVar StrictTVar { tvar } = tvar
101111

102112
newTVar :: MonadSTM m => a -> STM m (StrictTVar m a)
113+
#if CHECK_TVAR_INVARIANT
103114
newTVar !a = StrictTVar (const Nothing) <$> Lazy.newTVar a
115+
#else
116+
newTVar !a = StrictTVar <$> Lazy.newTVar a
117+
#endif
104118

105119
newTVarIO :: MonadSTM m => a -> m (StrictTVar m a)
106120
newTVarIO = newTVarWithInvariantIO (const Nothing)
@@ -113,9 +127,14 @@ newTVarWithInvariantIO :: (MonadSTM m, HasCallStack)
113127
=> (a -> Maybe String) -- ^ Invariant (expect 'Nothing')
114128
-> a
115129
-> m (StrictTVar m a)
116-
newTVarWithInvariantIO invariant !a =
130+
#if CHECK_TVAR_INVARIANT
131+
newTVarWithInvariantIO invariant !a =
117132
checkInvariant (invariant a) $
118133
StrictTVar invariant <$> Lazy.newTVarIO a
134+
#else
135+
newTVarWithInvariantIO _invariant !a =
136+
StrictTVar <$> Lazy.newTVarIO a
137+
#endif
119138

120139
newTVarWithInvariantM :: (MonadSTM m, HasCallStack)
121140
=> (a -> Maybe String) -- ^ Invariant (expect 'Nothing')
@@ -131,9 +150,11 @@ readTVarIO :: MonadSTM m => StrictTVar m a -> m a
131150
readTVarIO StrictTVar { tvar } = Lazy.readTVarIO tvar
132151

133152
writeTVar :: (MonadSTM m, HasCallStack) => StrictTVar m a -> a -> STM m ()
134-
writeTVar StrictTVar { tvar, invariant } !a =
135-
checkInvariant (invariant a) $
136-
Lazy.writeTVar tvar a
153+
writeTVar v !a =
154+
#if CHECK_TVAR_INVARIANT
155+
checkInvariant (invariant v a) $
156+
#endif
157+
Lazy.writeTVar (tvar v) a
137158

138159
modifyTVar :: MonadSTM m => StrictTVar m a -> (a -> a) -> STM m ()
139160
modifyTVar v f = readTVar v >>= writeTVar v . f

0 commit comments

Comments
 (0)