ouroboros-consensus-0.1.0.0: Consensus layer for the Ouroboros blockchain protocol
Safe HaskellNone
LanguageHaskell2010

Ouroboros.Consensus.Util.MonadSTM.NormalForm

Contents

Synopsis

Documentation

checkInvariant :: HasCallStack => Maybe String -> a -> a Source #

Check invariant (if enabled) before continuing

checkInvariant mErr x is equal to x if mErr == Nothing, and throws an error err if mErr == Just err.

This is exported so that other code that wants to conditionally check invariants can reuse the same logic, rather than having to introduce new per-package flags.

isEmptyTMVar :: forall (m :: Type -> Type) a. MonadSTM m => StrictTMVar m a -> STM m Bool Source #

swapTMVar :: forall (m :: Type -> Type) a. MonadSTM m => StrictTMVar m a -> a -> STM m a Source #

tryReadTMVar :: forall (m :: Type -> Type) a. MonadSTM m => StrictTMVar m a -> STM m (Maybe a) Source #

readTMVar :: forall (m :: Type -> Type) a. MonadSTM m => StrictTMVar m a -> STM m a Source #

tryPutTMVar :: forall (m :: Type -> Type) a. MonadSTM m => StrictTMVar m a -> a -> STM m Bool Source #

putTMVar :: forall (m :: Type -> Type) a. MonadSTM m => StrictTMVar m a -> a -> STM m () Source #

tryTakeTMVar :: forall (m :: Type -> Type) a. MonadSTM m => StrictTMVar m a -> STM m (Maybe a) Source #

takeTMVar :: forall (m :: Type -> Type) a. MonadSTM m => StrictTMVar m a -> STM m a Source #

newEmptyTMVar :: forall (m :: Type -> Type) a. MonadSTM m => STM m (StrictTMVar m a) Source #

newTMVarM :: MonadSTM m => a -> m (StrictTMVar m a) Source #

castStrictTMVar :: forall (m :: Type -> Type) (n :: Type -> Type) a. LazyTMVar m ~ LazyTMVar n => StrictTMVar m a -> StrictTMVar n a Source #

updateTVar :: forall (m :: Type -> Type) a b. MonadSTM m => StrictTVar m a -> (a -> (a, b)) -> STM m b Source #

stateTVar :: forall (m :: Type -> Type) a b. MonadSTM m => StrictTVar m a -> (a -> (a, b)) -> STM m b Source #

modifyTVar :: forall (m :: Type -> Type) a. MonadSTM m => StrictTVar m a -> (a -> a) -> STM m () Source #

writeTVar :: forall (m :: Type -> Type) a. (MonadSTM m, HasCallStack) => StrictTVar m a -> a -> STM m () Source #

readTVar :: forall (m :: Type -> Type) a. MonadSTM m => StrictTVar m a -> STM m a Source #

newTVarWithInvariantM Source #

Arguments

:: (MonadSTM m, HasCallStack) 
=> (a -> Maybe String)

Invariant (expect Nothing)

-> a 
-> m (StrictTVar m a) 

newTVarM :: MonadSTM m => a -> m (StrictTVar m a) Source #

toLazyTVar :: forall (m :: Type -> Type) a. StrictTVar m a -> LazyTVar m a Source #

Get the underlying TVar

Since we obviously cannot guarantee that updates to this LazyTVar will be strict, this should be used with caution.

castStrictTVar :: forall (m :: Type -> Type) (n :: Type -> Type) a. LazyTVar m ~ LazyTVar n => StrictTVar m a -> StrictTVar n a Source #

type LazyTVar (m :: Type -> Type) = TVar m Source #

type LazyTMVar (m :: Type -> Type) = TMVar m Source #

data StrictTVar (m :: Type -> Type) a Source #

data StrictTMVar (m :: Type -> Type) a Source #

catchSTM :: (MonadSTMTx stm, MonadCatch stm, Exception e) => stm a -> (e -> stm a) -> stm a Source #

catch speclialized for an stm monad.

throwSTM :: (MonadSTMTx stm, MonadThrow stm, Exception e) => e -> stm a Source #

throwIO specialised to stm monad.

flushTBQueueDefault :: forall (m :: Type -> Type) a. MonadSTM m => TBQueueDefault m a -> STM m [a] Source #

lengthTBQueueDefault :: forall (m :: Type -> Type) a. MonadSTM m => TBQueueDefault m a -> STM m Natural Source #

isFullTBQueueDefault :: forall (m :: Type -> Type) a. MonadSTM m => TBQueueDefault m a -> STM m Bool Source #

isEmptyTBQueueDefault :: forall (m :: Type -> Type) a. MonadSTM m => TBQueueDefault m a -> STM m Bool Source #

writeTBQueueDefault :: forall (m :: Type -> Type) a. MonadSTM m => TBQueueDefault m a -> a -> STM m () Source #

tryReadTBQueueDefault :: forall (m :: Type -> Type) a. MonadSTMTx (STM m) => TBQueueDefault m a -> STM m (Maybe a) Source #

readTBQueueDefault :: forall (m :: Type -> Type) a. MonadSTM m => TBQueueDefault m a -> STM m a Source #

newTBQueueDefault :: forall (m :: Type -> Type) a. MonadSTM m => Natural -> STM m (TBQueueDefault m a) Source #

isEmptyTQueueDefault :: forall (m :: Type -> Type) a. MonadSTMTx (STM m) => TQueueDefault m a -> STM m Bool Source #

tryReadTQueueDefault :: forall (m :: Type -> Type) a. MonadSTMTx (STM m) => TQueueDefault m a -> STM m (Maybe a) Source #

readTQueueDefault :: forall (m :: Type -> Type) a. MonadSTM m => TQueueDefault m a -> STM m a Source #

writeTQueueDefault :: forall (m :: Type -> Type) a. MonadSTM m => TQueueDefault m a -> a -> STM m () Source #

newTQueueDefault :: forall (m :: Type -> Type) a. MonadSTM m => STM m (TQueueDefault m a) Source #

isEmptyTMVarDefault :: forall (m :: Type -> Type) a. MonadSTM m => TMVarDefault m a -> STM m Bool Source #

swapTMVarDefault :: forall (m :: Type -> Type) a. MonadSTM m => TMVarDefault m a -> a -> STM m a Source #

tryReadTMVarDefault :: forall (m :: Type -> Type) a. MonadSTM m => TMVarDefault m a -> STM m (Maybe a) Source #

readTMVarDefault :: forall (m :: Type -> Type) a. MonadSTM m => TMVarDefault m a -> STM m a Source #

tryPutTMVarDefault :: forall (m :: Type -> Type) a. MonadSTM m => TMVarDefault m a -> a -> STM m Bool Source #

putTMVarDefault :: forall (m :: Type -> Type) a. MonadSTM m => TMVarDefault m a -> a -> STM m () Source #

tryTakeTMVarDefault :: forall (m :: Type -> Type) a. MonadSTM m => TMVarDefault m a -> STM m (Maybe a) Source #

takeTMVarDefault :: forall (m :: Type -> Type) a. MonadSTM m => TMVarDefault m a -> STM m a Source #

newEmptyTMVarDefault :: forall (m :: Type -> Type) a. MonadSTM m => STM m (TMVarDefault m a) Source #

newTMVarDefault :: forall (m :: Type -> Type) a. MonadSTM m => a -> STM m (TMVarDefault m a) Source #

type family TBQueue_ (stm :: Type -> Type) :: Type -> Type Source #

Instances

Instances details
type TBQueue_ STM 
Instance details

Defined in Control.Monad.Class.MonadSTM

type TBQueue_ (WithEarlyExit stm) Source # 
Instance details

Defined in Ouroboros.Consensus.Util.EarlyExit

type family TQueue_ (stm :: Type -> Type) :: Type -> Type Source #

Instances

Instances details
type TQueue_ STM 
Instance details

Defined in Control.Monad.Class.MonadSTM

type TQueue_ (WithEarlyExit stm) Source # 
Instance details

Defined in Ouroboros.Consensus.Util.EarlyExit

type TQueue_ (WithEarlyExit stm) = TQueue_ stm

type family TMVar_ (stm :: Type -> Type) :: Type -> Type Source #

Instances

Instances details
type TMVar_ STM 
Instance details

Defined in Control.Monad.Class.MonadSTM

type TMVar_ (WithEarlyExit stm) Source # 
Instance details

Defined in Ouroboros.Consensus.Util.EarlyExit

type TMVar_ (WithEarlyExit stm) = TMVar_ stm

type family TVar_ (stm :: Type -> Type) :: Type -> Type Source #

Instances

Instances details
type TVar_ STM 
Instance details

Defined in Control.Monad.Class.MonadSTM

type TVar_ STM = TVar
type TVar_ (WithEarlyExit stm) Source # 
Instance details

Defined in Ouroboros.Consensus.Util.EarlyExit

type TVar_ (WithEarlyExit stm) = TVar_ stm

class (Monad stm, Alternative stm, MonadPlus stm) => MonadSTMTx (stm :: Type -> Type) where Source #

Associated Types

type TVar_ (stm :: Type -> Type) :: Type -> Type Source #

type TMVar_ (stm :: Type -> Type) :: Type -> Type Source #

type TQueue_ (stm :: Type -> Type) :: Type -> Type Source #

type TBQueue_ (stm :: Type -> Type) :: Type -> Type Source #

Methods

retry :: stm a Source #

orElse :: stm a -> stm a -> stm a Source #

modifyTVar' :: TVar_ stm a -> (a -> a) -> stm () Source #

check :: Bool -> stm () Source #

newTQueue :: stm (TQueue_ stm a) Source #

readTQueue :: TQueue_ stm a -> stm a Source #

tryReadTQueue :: TQueue_ stm a -> stm (Maybe a) Source #

writeTQueue :: TQueue_ stm a -> a -> stm () Source #

isEmptyTQueue :: TQueue_ stm a -> stm Bool Source #

newTBQueue :: Natural -> stm (TBQueue_ stm a) Source #

readTBQueue :: TBQueue_ stm a -> stm a Source #

tryReadTBQueue :: TBQueue_ stm a -> stm (Maybe a) Source #

flushTBQueue :: TBQueue_ stm a -> stm [a] Source #

writeTBQueue :: TBQueue_ stm a -> a -> stm () Source #

lengthTBQueue :: TBQueue_ stm a -> stm Natural Source #

Since: io-sim-classes-0.2.0.0

isEmptyTBQueue :: TBQueue_ stm a -> stm Bool Source #

isFullTBQueue :: TBQueue_ stm a -> stm Bool Source #

Instances

Instances details
MonadSTMTx STM 
Instance details

Defined in Control.Monad.Class.MonadSTM

Associated Types

type TVar_ STM :: Type -> Type Source #

type TMVar_ STM :: Type -> Type Source #

type TQueue_ STM :: Type -> Type Source #

type TBQueue_ STM :: Type -> Type Source #

Methods

newTVar :: a -> STM (TVar_ STM a) Source #

readTVar :: TVar_ STM a -> STM a Source #

writeTVar :: TVar_ STM a -> a -> STM () Source #

retry :: STM a Source #

orElse :: STM a -> STM a -> STM a Source #

modifyTVar :: TVar_ STM a -> (a -> a) -> STM () Source #

modifyTVar' :: TVar_ STM a -> (a -> a) -> STM () Source #

stateTVar :: TVar_ STM s -> (s -> (a, s)) -> STM a Source #

check :: Bool -> STM () Source #

newTMVar :: a -> STM (TMVar_ STM a) Source #

newEmptyTMVar :: STM (TMVar_ STM a) Source #

takeTMVar :: TMVar_ STM a -> STM a Source #

tryTakeTMVar :: TMVar_ STM a -> STM (Maybe a) Source #

putTMVar :: TMVar_ STM a -> a -> STM () Source #

tryPutTMVar :: TMVar_ STM a -> a -> STM Bool Source #

readTMVar :: TMVar_ STM a -> STM a Source #

tryReadTMVar :: TMVar_ STM a -> STM (Maybe a) Source #

swapTMVar :: TMVar_ STM a -> a -> STM a Source #

isEmptyTMVar :: TMVar_ STM a -> STM Bool Source #

newTQueue :: STM (TQueue_ STM a) Source #

readTQueue :: TQueue_ STM a -> STM a Source #

tryReadTQueue :: TQueue_ STM a -> STM (Maybe a) Source #

writeTQueue :: TQueue_ STM a -> a -> STM () Source #

isEmptyTQueue :: TQueue_ STM a -> STM Bool Source #

newTBQueue :: Natural -> STM (TBQueue_ STM a) Source #

readTBQueue :: TBQueue_ STM a -> STM a Source #

tryReadTBQueue :: TBQueue_ STM a -> STM (Maybe a) Source #

flushTBQueue :: TBQueue_ STM a -> STM [a] Source #

writeTBQueue :: TBQueue_ STM a -> a -> STM () Source #

lengthTBQueue :: TBQueue_ STM a -> STM Natural Source #

isEmptyTBQueue :: TBQueue_ STM a -> STM Bool Source #

isFullTBQueue :: TBQueue_ STM a -> STM Bool Source #

MonadSTMTx stm => MonadSTMTx (WithEarlyExit stm) Source # 
Instance details

Defined in Ouroboros.Consensus.Util.EarlyExit

Associated Types

type TVar_ (WithEarlyExit stm) :: Type -> Type Source #

type TMVar_ (WithEarlyExit stm) :: Type -> Type Source #

type TQueue_ (WithEarlyExit stm) :: Type -> Type Source #

type TBQueue_ (WithEarlyExit stm) :: Type -> Type Source #

Methods

newTVar :: a -> WithEarlyExit stm (TVar_ (WithEarlyExit stm) a) Source #

readTVar :: TVar_ (WithEarlyExit stm) a -> WithEarlyExit stm a Source #

writeTVar :: TVar_ (WithEarlyExit stm) a -> a -> WithEarlyExit stm () Source #

retry :: WithEarlyExit stm a Source #

orElse :: WithEarlyExit stm a -> WithEarlyExit stm a -> WithEarlyExit stm a Source #

modifyTVar :: TVar_ (WithEarlyExit stm) a -> (a -> a) -> WithEarlyExit stm () Source #

modifyTVar' :: TVar_ (WithEarlyExit stm) a -> (a -> a) -> WithEarlyExit stm () Source #

stateTVar :: TVar_ (WithEarlyExit stm) s -> (s -> (a, s)) -> WithEarlyExit stm a Source #

check :: Bool -> WithEarlyExit stm () Source #

newTMVar :: a -> WithEarlyExit stm (TMVar_ (WithEarlyExit stm) a) Source #

newEmptyTMVar :: WithEarlyExit stm (TMVar_ (WithEarlyExit stm) a) Source #

takeTMVar :: TMVar_ (WithEarlyExit stm) a -> WithEarlyExit stm a Source #

tryTakeTMVar :: TMVar_ (WithEarlyExit stm) a -> WithEarlyExit stm (Maybe a) Source #

putTMVar :: TMVar_ (WithEarlyExit stm) a -> a -> WithEarlyExit stm () Source #

tryPutTMVar :: TMVar_ (WithEarlyExit stm) a -> a -> WithEarlyExit stm Bool Source #

readTMVar :: TMVar_ (WithEarlyExit stm) a -> WithEarlyExit stm a Source #

tryReadTMVar :: TMVar_ (WithEarlyExit stm) a -> WithEarlyExit stm (Maybe a) Source #

swapTMVar :: TMVar_ (WithEarlyExit stm) a -> a -> WithEarlyExit stm a Source #

isEmptyTMVar :: TMVar_ (WithEarlyExit stm) a -> WithEarlyExit stm Bool Source #

newTQueue :: WithEarlyExit stm (TQueue_ (WithEarlyExit stm) a) Source #

readTQueue :: TQueue_ (WithEarlyExit stm) a -> WithEarlyExit stm a Source #

tryReadTQueue :: TQueue_ (WithEarlyExit stm) a -> WithEarlyExit stm (Maybe a) Source #

writeTQueue :: TQueue_ (WithEarlyExit stm) a -> a -> WithEarlyExit stm () Source #

isEmptyTQueue :: TQueue_ (WithEarlyExit stm) a -> WithEarlyExit stm Bool Source #

newTBQueue :: Natural -> WithEarlyExit stm (TBQueue_ (WithEarlyExit stm) a) Source #

readTBQueue :: TBQueue_ (WithEarlyExit stm) a -> WithEarlyExit stm a Source #

tryReadTBQueue :: TBQueue_ (WithEarlyExit stm) a -> WithEarlyExit stm (Maybe a) Source #

flushTBQueue :: TBQueue_ (WithEarlyExit stm) a -> WithEarlyExit stm [a] Source #

writeTBQueue :: TBQueue_ (WithEarlyExit stm) a -> a -> WithEarlyExit stm () Source #

lengthTBQueue :: TBQueue_ (WithEarlyExit stm) a -> WithEarlyExit stm Natural Source #

isEmptyTBQueue :: TBQueue_ (WithEarlyExit stm) a -> WithEarlyExit stm Bool Source #

isFullTBQueue :: TBQueue_ (WithEarlyExit stm) a -> WithEarlyExit stm Bool Source #

type TQueue (m :: Type -> Type) = TQueue_ (STM m) Source #

type TBQueue (m :: Type -> Type) = TBQueue_ (STM m) Source #

type family STM (m :: Type -> Type) :: Type -> Type Source #

Instances

Instances details
type STM IO 
Instance details

Defined in Control.Monad.Class.MonadSTM

type STM IO = STM
type STM (WithEarlyExit m) Source # 
Instance details

Defined in Ouroboros.Consensus.Util.EarlyExit

type STM (ReaderT r m) 
Instance details

Defined in Control.Monad.Class.MonadSTM

type STM (ReaderT r m) = STM m
type STM (WithTempRegistry st m) Source # 
Instance details

Defined in Ouroboros.Consensus.Util.ResourceRegistry

type STM (WithTempRegistry st m)

class (Monad m, MonadSTMTx (STM m)) => MonadSTM (m :: Type -> Type) where Source #

Minimal complete definition

atomically

Associated Types

type STM (m :: Type -> Type) :: Type -> Type Source #

Methods

atomically :: HasCallStack => STM m a -> m a Source #

newTBQueueIO :: Natural -> m (TBQueue m a) Source #

Instances

Instances details
MonadSTM IO 
Instance details

Defined in Control.Monad.Class.MonadSTM

Associated Types

type STM IO :: Type -> Type Source #

MonadSTM m => MonadSTM (WithEarlyExit m) Source # 
Instance details

Defined in Ouroboros.Consensus.Util.EarlyExit

Associated Types

type STM (WithEarlyExit m) :: Type -> Type Source #

MonadSTM m => MonadSTM (ReaderT r m) 
Instance details

Defined in Control.Monad.Class.MonadSTM

Associated Types

type STM (ReaderT r m) :: Type -> Type Source #

Methods

atomically :: HasCallStack => STM (ReaderT r m) a -> ReaderT r m a Source #

newTVarIO :: a -> ReaderT r m (TVar (ReaderT r m) a) Source #

newTMVarIO :: a -> ReaderT r m (TMVar (ReaderT r m) a) Source #

newEmptyTMVarIO :: ReaderT r m (TMVar (ReaderT r m) a) Source #

newTBQueueIO :: Natural -> ReaderT r m (TBQueue (ReaderT r m) a) Source #

MonadSTM m => MonadSTM (WithTempRegistry st m) Source # 
Instance details

Defined in Ouroboros.Consensus.Util.ResourceRegistry

Associated Types

type STM (WithTempRegistry st m) :: Type -> Type Source #

data TMVarDefault (m :: Type -> Type) a Source #

data TQueueDefault (m :: Type -> Type) a Source #

Constructors

TQueue !(TVar m [a]) !(TVar m [a]) 

data TBQueueDefault (m :: Type -> Type) a Source #

Constructors

TBQueue !(TVar m Natural) !(TVar m [a]) !(TVar m Natural) !(TVar m [a]) !Natural 

data StrictMVar m a Source #

Strict MVar (modelled using a lazy TMVar under the hood)

The StrictMVar API is slightly stronger than the usual MVar one, as we offer a primitive to read the value of the MVar even if it is empty (in which case we will return the oldest known stale one). See readMVarSTM.

There is a weaker invariant for a StrictMVar than for a StrictTVar: although all functions that modify the StrictMVar check the invariant, we do not guarantee that the value inside the StrictMVar always satisfies the invariant. Instead, we do guarantee that if the StrictMVar is updated with a value that does not satisfy the invariant, an exception is thrown. The reason for this weaker guarantee is that leaving an MVar empty can lead to very hard to debug "blocked indefinitely" problems.

This is also the reason we do not offer support for an invariant in StrictTMVar: if we throw an exception from an STM transaction, the STM transaction is not executed, and so we would not even be able to provide the weaker guarantee that we provide for StrictMVar.

Constructors

StrictMVar 

Fields

  • invariant :: !(a -> Maybe String)

    Invariant checked whenever updating the StrictMVar.

  • tmvar :: !(TMVar m a)

    The main TMVar supporting this StrictMVar

  • tvar :: !(TVar m a)

    TVar for supporting readMVarSTM

    This TVar is always kept up to date with the TMVar, but holds on the old value of the TMVar when it is empty. This is very useful to support single writer/many reader scenarios.

    NOTE: We should always update the tmvar before the tvar so that if the update to the tmvar fails, the 'tvar is left unchanged.

castStrictMVar :: (TMVar m ~ TMVar n, TVar m ~ TVar n) => StrictMVar m a -> StrictMVar n a Source #

takeMVar :: MonadSTM m => StrictMVar m a -> m a Source #

putMVar :: (MonadSTM m, HasCallStack) => StrictMVar m a -> a -> m () Source #

readMVar :: MonadSTM m => StrictMVar m a -> m a Source #

readMVarSTM :: MonadSTM m => StrictMVar m a -> STM m a Source #

Read the possibly-stale value of the MVar

Will return the current value of the MVar if it non-empty, or the last known value otherwise.

swapMVar :: (MonadSTM m, HasCallStack) => StrictMVar m a -> a -> m a Source #

Swap value of a StrictMVar

NOTE: Since swapping the value can't leave the StrictMVar empty, we could check the invariant first and only then swap. We nonetheless swap first and check the invariant after to keep the semantics the same with putMVar, otherwise it will be difficult to understand when a StrictMVar is updated and when it is not.

updateMVar :: (MonadSTM m, HasCallStack) => StrictMVar m a -> (a -> (a, b)) -> m b Source #

updateMVar_ :: (MonadSTM m, HasCallStack) => StrictMVar m a -> (a -> a) -> m () Source #

modifyMVar :: (MonadSTM m, MonadCatch m, HasCallStack) => StrictMVar m a -> (a -> m (a, b)) -> m b Source #

modifyMVar_ :: (MonadSTM m, MonadCatch m, HasCallStack) => StrictMVar m a -> (a -> m a) -> m () Source #

newEmptyMVar :: (MonadSTM m, NoThunks a) => a -> m (StrictMVar m a) Source #

Temporary