Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
strict-mvar
dependency and StrictMVar
with NoThunks invariants.
We depend on an unreleased version of `strict-mvar` and we create a new module for `StrictMVar`s with `NoThunks` invariants. We also add a flag `checkmvarnothunks` that acts as a switch for whether we want to run the invariant checks or not. By default, these checks are off, since we do not want to perform these checks in production.
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...s-consensus/src/ouroboros-consensus/Control/Concurrent/Class/MonadMVar/Strict/NoThunks.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{-# LANGUAGE CPP #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
|
||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
|
||
module Control.Concurrent.Class.MonadMVar.Strict.NoThunks ( | ||
-- * StrictMVars with NoThunks invariants | ||
newEmptyMVar | ||
, newEmptyMVarWithInvariant | ||
, newMVar | ||
, newMVarWithInvariant | ||
-- * Re-exports | ||
, module StrictMVar | ||
) where | ||
|
||
#if CHECK_MVAR_NOTHUNKS | ||
import qualified Control.Concurrent.Class.MonadMVar.Strict.Checked as StrictMVar | ||
import Control.Concurrent.Class.MonadMVar.Strict.Checked as StrictMVar hiding (newMVar, newMVarWithInvariant, newEmptyMVarWithInvariant, newEmptyMVar) | ||
#else | ||
import qualified Control.Concurrent.Class.MonadMVar.Strict as StrictMVar | ||
import Control.Concurrent.Class.MonadMVar.Strict as StrictMVar hiding (newMVar, newMVarWithInvariant, newEmptyMVarWithInvariant, newEmptyMVar) | ||
#endif | ||
|
||
import Control.Applicative ((<|>)) | ||
import NoThunks.Class (NoThunks (..), unsafeNoThunks) | ||
|
||
noThunksInvariant :: NoThunks a => a -> Maybe String | ||
noThunksInvariant = fmap show . unsafeNoThunks | ||
|
||
newMVar :: (MonadMVar m, NoThunks a) => a -> m (StrictMVar m a) | ||
newMVar = StrictMVar.newMVarWithInvariant noThunksInvariant | ||
|
||
newMVarWithInvariant :: | ||
(MonadMVar m, NoThunks a) | ||
=> (a -> Maybe String) | ||
-> a | ||
-> m (StrictMVar m a) | ||
newMVarWithInvariant inv = | ||
StrictMVar.newMVarWithInvariant (\x -> inv x <|> noThunksInvariant x) | ||
|
||
newEmptyMVar :: (MonadMVar m, NoThunks a) => m (StrictMVar m a) | ||
newEmptyMVar = StrictMVar.newEmptyMVarWithInvariant noThunksInvariant | ||
|
||
newEmptyMVarWithInvariant :: | ||
(MonadMVar m, NoThunks a) | ||
=> (a -> Maybe String) | ||
-> m (StrictMVar m a) | ||
newEmptyMVarWithInvariant inv = | ||
StrictMVar.newEmptyMVarWithInvariant (\x -> inv x <|> noThunksInvariant x) | ||
|
||
instance NoThunks a => NoThunks (StrictMVar IO a) where | ||
showTypeOf _ = "StrictMVar IO" | ||
wNoThunks ctxt mvar = do | ||
a <- StrictMVar.readMVar mvar | ||
noThunks ctxt a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters