class (Monad m, MonadCatch m, MonadThrow m, MonadMask m, MonadSTM (STM m), Ord (ThreadId m), Show (ThreadId m)) => MonadConc (m :: Type -> Type) whereMonadConc is an abstraction over GHC's typical concurrency
abstraction. It captures the interface of concurrency monads in
terms of how they can operate on shared state and in the presence
of exceptions.
Every MonadConc has an associated MonadSTM, transactions of
which can be run atomically.
Deriving instances: If you have a newtype wrapper around a type
with an existing MonadConc instance, you should be able to derive
an instance for your type automatically, in simple cases.
For example:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
data Env = Env
newtype MyMonad m a = MyMonad { runMyMonad :: ReaderT Env m a }
deriving (Functor, Applicative, Monad)
deriving instance MonadThrow m => MonadThrow (MyMonad m)
deriving instance MonadCatch m => MonadCatch (MyMonad m)
deriving instance MonadMask m => MonadMask (MyMonad m)
deriving instance MonadConc m => MonadConc (MyMonad m)Do not be put off by the use of UndecidableInstances, it is safe
here.
Associated types
type family STM (m :: Type -> Type) :: Type -> TypeThe associated MonadSTM for this class.
type family MVar (m :: Type -> Type) :: Type -> TypeThe mutable reference type, like MVars. This may contain one value at a time, attempting to read or take from an "empty"
MVarwill block until it is full, and attempting to put to a "full"MVarwill block until it is empty.type family IORef (m :: Type -> Type) :: Type -> TypeThe mutable non-blocking reference type. These may suffer from relaxed memory effects if functions outside the set
newIORef,readIORef,atomicModifyIORef, andatomicWriteIORefare used.type family Ticket (m :: Type -> Type) :: Type -> TypeWhen performing compare-and-swap operations on
IORefs, aTicketis a proof that a thread observed a specific previous value.type family ThreadId (m :: Type -> Type)An abstract handle to a thread.
Methods
forkWithUnmask :: ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)Like fork, but the child thread is passed a function that can be used to unmask asynchronous exceptions. This function should not be used within a mask or uninterruptibleMask.
forkWithUnmask = forkWithUnmaskN ""forkWithUnmaskN :: String -> ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)Like forkWithUnmask, but the thread is given a name which may be used to present more useful debugging information.
forkWithUnmaskN _ = forkWithUnmaskforkOnWithUnmask :: Int -> ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)Like forkWithUnmask, but the child thread is pinned to the given CPU, as with forkOn.
forkOnWithUnmask = forkOnWithUnmaskN ""forkOnWithUnmaskN :: String -> Int -> ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)Like forkWithUnmaskN, but the child thread is pinned to the given CPU, as with forkOn.
forkOnWithUnmaskN _ = forkOnWithUnmaskforkOSWithUnmask :: ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)Like forkOS, but the child thread is passed a function that can be used to unmask asynchronous exceptions. This function should not be used within a mask or uninterruptibleMask.
forkOSWithUnmask = forkOSWithUnmaskN ""forkOSWithUnmaskN :: String -> ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)Like forkOSWithUnmask, but the thread is given a name which may be used to present more useful debugging information.
forkOSWithUnmaskN _ = forkOSWithUnmasksupportsBoundThreads :: m BoolReturns True if bound threads can be forked. If False, isCurrentThreadBound will always return False and both forkOS and runInBoundThread will fail.
isCurrentThreadBound :: m BoolReturns True if the calling thread is bound, that is, if it is safe to use foreign libraries that rely on thread-local state from the calling thread.
This will always be false if your program is not compiled with the threaded runtime.
getNumCapabilities :: m IntGet the number of Haskell threads that can run simultaneously.
setNumCapabilities :: Int -> m ()Set the number of Haskell threads that can run simultaneously.
myThreadId :: m (ThreadId m)Get the
ThreadIdof the current thread.yield :: m ()Allows a context-switch to any other unblocked thread (if any).
threadDelay :: Int -> m ()Yields the current thread, and optionally suspends the current thread for a given number of microseconds.
If suspended, there is no guarantee that the thread will be rescheduled promptly when the delay has expired, but the thread will never continue to run earlier than specified.
threadDelay _ = yieldnewEmptyMVar :: m (MVar m a)Create a new empty
MVar.newEmptyMVar = newEmptyMVarN ""newEmptyMVarN :: String -> m (MVar m a)Create a new empty
MVar, but it is given a name which may be used to present more useful debugging information.newEmptyMVarN _ = newEmptyMVarputMVar :: MVar m a -> a -> m ()Put a value into a
MVar. If there is already a value there, this will block until that value has been taken, at which point the value will be stored.tryPutMVar :: MVar m a -> a -> m BoolreadMVar :: MVar m a -> m aBlock until a value is present in the
MVar, and then return it. This does not "remove" the value, multiple reads are possible.tryReadMVar :: MVar m a -> m (Maybe a)takeMVar :: MVar m a -> m aTake a value from a
MVar. This "empties" theMVar, allowing a new value to be put in. This will block if there is no value in theMVaralready, until one has been put.tryTakeMVar :: MVar m a -> m (Maybe a)newIORef :: a -> m (IORef m a)Create a new reference.
newIORef = newIORefN ""newIORefN :: String -> a -> m (IORef m a)Create a new reference, but it is given a name which may be used to present more useful debugging information.
newIORefN _ = newIORefreadIORef :: IORef m a -> m aRead the current value stored in a reference.
readIORef ioref = readForCAS ioref >>= peekTicketatomicModifyIORef :: IORef m a -> (a -> (a, b)) -> m bAtomically modify the value stored in a reference. This imposes a full memory barrier.
writeIORef :: IORef m a -> a -> m ()Write a new value into an
IORef, without imposing a memory barrier. This means that relaxed memory effects can be observed.atomicWriteIORef :: IORef m a -> a -> m ()Replace the value stored in a reference, with the barrier-to-reordering property that atomicModifyIORef has.
atomicWriteIORef r a = atomicModifyIORef r $ const (a, ())readForCAS :: IORef m a -> m (Ticket m a)Read the current value stored in a reference, returning a
Ticket, for use in future compare-and-swap operations.peekTicket' :: Proxy m -> Ticket m a -> aExtract the actual Haskell value from a
Ticket.The
Proxy mis to determine themin theTickettype.casIORef :: IORef m a -> Ticket m a -> a -> m (Bool, Ticket m a)Perform a machine-level compare-and-swap (CAS) operation on a
IORef. Returns an indication of success and aTicketfor the most current value in theIORef.This is strict in the "new" value argument.
modifyIORefCAS :: IORef m a -> (a -> (a, b)) -> m bA replacement for atomicModifyIORef using a compare-and-swap.
This is strict in the "new" value argument.
modifyIORefCAS_ :: IORef m a -> (a -> a) -> m ()A variant of modifyIORefCAS which doesn't return a result.
modifyIORefCAS_ ioref f = modifyIORefCAS ioref (\a -> (f a, ()))atomically :: STM m a -> m aPerform an STM transaction atomically.
newTVarConc :: a -> m (TVar (STM m) a)Create a
TVar. This may be implemented differently for speed.newTVarConc = atomically . newTVarreadTVarConc :: TVar (STM m) a -> m aRead the current value stored in a
TVar. This may be implemented differently for speed.readTVarConc = atomically . readTVarthrowTo :: Exception e => ThreadId m -> e -> m ()Throw an exception to the target thread. This blocks until the exception is delivered, and it is just as if the target thread had raised it with throw. This can interrupt a blocked action.
getMaskingState :: m MaskingStateReturn the MaskingState for the current thread.
unsafeUnmask :: m a -> m aSet the MaskingState for the current thread to MaskedUninterruptible.
Instances10MonadConc, …
MonadConc IODefined in concurrency-1.11.0.3 · Control.Monad.Conc.ClassMonadConc m => MonadConc (IsConc m)Defined in concurrency-1.11.0.3 · Control.Monad.Conc.ClassMonadConc m => MonadConc (IdentityT m)Defined in concurrency-1.11.0.3 · Control.Monad.Conc.ClassMonadConc m => MonadConc (ReaderT r m)Defined in concurrency-1.11.0.3 · Control.Monad.Conc.ClassNew threads inherit the reader state of their parent, but do not communicate results back.
MonadConc m => MonadConc (StateT s m)Defined in concurrency-1.11.0.3 · Control.Monad.Conc.ClassNew threads inherit the state of their parent, but do not communicate results back.
MonadConc m => MonadConc (StateT s m)Defined in concurrency-1.11.0.3 · Control.Monad.Conc.ClassNew threads inherit the state of their parent, but do not communicate results back.
(MonadConc m, Monoid w) => MonadConc (WriterT w m)Defined in concurrency-1.11.0.3 · Control.Monad.Conc.ClassNew threads inherit the writer state of their parent, but do not communicate results back.
(MonadConc m, Monoid w) => MonadConc (WriterT w m)Defined in concurrency-1.11.0.3 · Control.Monad.Conc.ClassNew threads inherit the writer state of their parent, but do not communicate results back.
(MonadConc m, Monoid w) => MonadConc (RWST r w s m)Defined in concurrency-1.11.0.3 · Control.Monad.Conc.ClassNew threads inherit the states of their parent, but do not communicate results back.
(MonadConc m, Monoid w) => MonadConc (RWST r w s m)Defined in concurrency-1.11.0.3 · Control.Monad.Conc.ClassNew threads inherit the states of their parent, but do not communicate results back.