-
Notifications
You must be signed in to change notification settings - Fork 86
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
Generalize StrictT(M)Var invariant #1040
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE CPP #-} | ||
{-# LANGUAGE DuplicateRecordFields #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
module Control.Monad.Class.MonadSTM.Strict | ||
|
@@ -31,7 +32,6 @@ module Control.Monad.Class.MonadSTM.Strict | |
, isEmptyTMVar | ||
) where | ||
|
||
import Control.Exception (assert) | ||
import Control.Monad.Class.MonadSTM as X hiding (LazyTMVar, LazyTVar, | ||
isEmptyTMVar, modifyTVar, newEmptyTMVar, newEmptyTMVarM, | ||
newTMVar, newTMVarM, newTVar, newTVarM, putTMVar, | ||
|
@@ -45,9 +45,8 @@ import GHC.Stack | |
-------------------------------------------------------------------------------} | ||
|
||
data StrictTVar m a = StrictTVar | ||
{ invariant :: !(a -> Bool) | ||
-- ^ Invariant checked in an 'assert' whenever storing an @a@ in the | ||
-- 'StrictTVar'. | ||
{ invariant :: !(a -> Maybe String) | ||
-- ^ Invariant checked whenever updating the 'StrictTVar'. | ||
, tvar :: !(Lazy.LazyTVar m a) | ||
} | ||
|
||
|
@@ -56,25 +55,25 @@ toLazyTVar :: StrictTVar m a -> Lazy.LazyTVar m a | |
toLazyTVar StrictTVar { tvar } = tvar | ||
|
||
newTVar :: MonadSTM m => a -> STM m (StrictTVar m a) | ||
newTVar !a = StrictTVar (const True) <$> Lazy.newTVar a | ||
newTVar !a = StrictTVar (const Nothing) <$> Lazy.newTVar a | ||
|
||
newTVarM :: MonadSTM m => a -> m (StrictTVar m a) | ||
newTVarM = newTVarWithInvariantM (const True) | ||
newTVarM = newTVarWithInvariantM (const Nothing) | ||
|
||
newTVarWithInvariantM :: MonadSTM m | ||
=> (a -> Bool) -- ^ Invariant | ||
newTVarWithInvariantM :: (MonadSTM m, HasCallStack) | ||
=> (a -> Maybe String) -- ^ Invariant (expect 'Nothing') | ||
-> a | ||
-> m (StrictTVar m a) | ||
newTVarWithInvariantM invariant !a = | ||
assert (invariant a) $ | ||
checkInvariant (invariant a) $ | ||
StrictTVar invariant <$> Lazy.newTVarM a | ||
|
||
readTVar :: MonadSTM m => StrictTVar m a -> STM m a | ||
readTVar StrictTVar { tvar } = Lazy.readTVar tvar | ||
|
||
writeTVar :: MonadSTM m => StrictTVar m a -> a -> STM m () | ||
writeTVar :: (MonadSTM m, HasCallStack) => StrictTVar m a -> a -> STM m () | ||
writeTVar StrictTVar { tvar, invariant } !a = | ||
assert (invariant a) $ | ||
checkInvariant (invariant a) $ | ||
Lazy.writeTVar tvar a | ||
|
||
modifyTVar :: MonadSTM m => StrictTVar m a -> (a -> a) -> STM m () | ||
|
@@ -92,34 +91,33 @@ updateTVar v f = do | |
-------------------------------------------------------------------------------} | ||
|
||
data StrictTMVar m a = StrictTMVar | ||
{ invariant :: !(a -> Bool) | ||
-- ^ Used in an 'assert' to check whether the given @a@ is in normal form | ||
-- whenever storing an @a@ in the 'StrictTMVar'. | ||
{ invariant :: !(a -> Maybe String) | ||
-- ^ Invariant checked whenever updating the 'StrictTMVar'. | ||
, tmvar :: !(Lazy.LazyTMVar m a) | ||
} | ||
|
||
newTMVar :: MonadSTM m => a -> STM m (StrictTMVar m a) | ||
newTMVar !a = StrictTMVar (const True) <$> Lazy.newTMVar a | ||
newTMVar !a = StrictTMVar (const Nothing) <$> Lazy.newTMVar a | ||
|
||
newTMVarM :: MonadSTM m => a -> m (StrictTMVar m a) | ||
newTMVarM = newTMVarWithInvariantM (const True) | ||
newTMVarM = newTMVarWithInvariantM (const Nothing) | ||
|
||
newTMVarWithInvariantM :: (MonadSTM m, HasCallStack) | ||
=> (a -> Bool) -- ^ Invariant | ||
=> (a -> Maybe String) -- ^ Invariant (expect 'Nothing') | ||
-> a | ||
-> m (StrictTMVar m a) | ||
newTMVarWithInvariantM invariant !a = | ||
assert (invariant a) $ | ||
checkInvariant (invariant a) $ | ||
StrictTMVar invariant <$> Lazy.newTMVarM a | ||
|
||
newEmptyTMVar :: MonadSTM m => STM m (StrictTMVar m a) | ||
newEmptyTMVar = StrictTMVar (const True) <$> Lazy.newEmptyTMVar | ||
newEmptyTMVar = StrictTMVar (const Nothing) <$> Lazy.newEmptyTMVar | ||
|
||
newEmptyTMVarM :: MonadSTM m => m (StrictTMVar m a) | ||
newEmptyTMVarM = newEmptyTMVarWithInvariantM (const True) | ||
newEmptyTMVarM = newEmptyTMVarWithInvariantM (const Nothing) | ||
|
||
newEmptyTMVarWithInvariantM :: MonadSTM m | ||
=> (a -> Bool) -- ^ Invariant | ||
=> (a -> Maybe String) -- ^ Invariant (expect 'Nothing') | ||
-> m (StrictTMVar m a) | ||
newEmptyTMVarWithInvariantM invariant = | ||
StrictTMVar invariant <$> Lazy.newEmptyTMVarM | ||
|
@@ -131,14 +129,14 @@ takeTMVar StrictTMVar { tmvar } = Lazy.takeTMVar tmvar | |
tryTakeTMVar :: MonadSTM m => StrictTMVar m a -> STM m (Maybe a) | ||
tryTakeTMVar StrictTMVar { tmvar } = Lazy.tryTakeTMVar tmvar | ||
|
||
putTMVar :: MonadSTM m => StrictTMVar m a -> a -> STM m () | ||
putTMVar :: (MonadSTM m, HasCallStack) => StrictTMVar m a -> a -> STM m () | ||
putTMVar StrictTMVar { tmvar, invariant } !a = | ||
assert (invariant a) $ | ||
checkInvariant (invariant a) $ | ||
Lazy.putTMVar tmvar a | ||
|
||
tryPutTMVar :: MonadSTM m => StrictTMVar m a -> a -> STM m Bool | ||
tryPutTMVar :: (MonadSTM m, HasCallStack) => StrictTMVar m a -> a -> STM m Bool | ||
tryPutTMVar StrictTMVar { tmvar, invariant } !a = | ||
assert (invariant a) $ | ||
checkInvariant (invariant a) $ | ||
Lazy.tryPutTMVar tmvar a | ||
|
||
readTMVar :: MonadSTM m => StrictTMVar m a -> STM m a | ||
|
@@ -147,10 +145,22 @@ readTMVar StrictTMVar { tmvar } = Lazy.readTMVar tmvar | |
tryReadTMVar :: MonadSTM m => StrictTMVar m a -> STM m (Maybe a) | ||
tryReadTMVar StrictTMVar { tmvar } = Lazy.tryReadTMVar tmvar | ||
|
||
swapTMVar :: MonadSTM m => StrictTMVar m a -> a -> STM m a | ||
swapTMVar :: (MonadSTM m, HasCallStack) => StrictTMVar m a -> a -> STM m a | ||
swapTMVar StrictTMVar { tmvar, invariant } !a = | ||
assert (invariant a) $ | ||
checkInvariant (invariant a) $ | ||
Lazy.swapTMVar tmvar a | ||
|
||
isEmptyTMVar :: MonadSTM m => StrictTMVar m a -> STM m Bool | ||
isEmptyTMVar StrictTMVar { tmvar } = Lazy.isEmptyTMVar tmvar | ||
|
||
{------------------------------------------------------------------------------- | ||
Dealing with invariants | ||
-------------------------------------------------------------------------------} | ||
|
||
checkInvariant :: HasCallStack => Maybe String -> m x -> m x | ||
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. Would it be useful to reuse this function to check other invariants than this one? I mean instead of |
||
#if CHECK_TVAR_INVARIANT | ||
checkInvariant Nothing k = k | ||
checkInvariant (Just err) _ = error $ "Invariant violation: " ++ err | ||
#else | ||
checkInvariant _err k = k | ||
#endif |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
When/where do we enable this flag?
Thinking out loud: we want to enable it always (?) for
test-consensus
andtest-storage
. We don't want to have to pass it manually via the cmd line.