mstate-0.2.8: MState: A consistent State monad for concurrent applications.
Copyright(c) Nils Schweinsberg 2010
LicenseBSD3-style (see LICENSE)
Maintainermail@n-sch.de
Stabilityunstable
Portabilityportable
Safe HaskellSafe
LanguageHaskell98

Control.Concurrent.MState

Description

 
Synopsis

The MState Monad

data MState t m a Source #

The MState monad is a state monad for concurrent applications. To create a new thread sharing the same (modifiable) state use the forkM function.

Instances

Instances details
MonadIO m => MonadState t (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

get :: MState t m t

put :: t -> MState t m ()

state :: (t -> (a, t)) -> MState t m a

MonadError e m => MonadError e (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

throwError :: e -> MState t m a

catchError :: MState t m a -> (e -> MState t m a) -> MState t m a

MonadReader r m => MonadReader r (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

ask :: MState t m r

local :: (r -> r) -> MState t m a -> MState t m a

reader :: (r -> a) -> MState t m a

MonadWriter w m => MonadWriter w (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

writer :: (a, w) -> MState t m a

tell :: w -> MState t m ()

listen :: MState t m a -> MState t m (a, w)

pass :: MState t m (a, w -> w) -> MState t m a

MonadTransPeel (MState t) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

peel :: forall m (n :: Type -> Type) (o :: Type -> Type) a. (Monad m, Monad n, Monad o) => MState t n (MState t m a -> m (MState t o a)) #

MonadTrans (MState t) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

lift :: Monad m => m a -> MState t m a

Monad m => Monad (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

(>>=) :: MState t m a -> (a -> MState t m b) -> MState t m b

(>>) :: MState t m a -> MState t m b -> MState t m b

return :: a -> MState t m a

Functor f => Functor (MState t f) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

fmap :: (a -> b) -> MState t f a -> MState t f b

(<$) :: a -> MState t f b -> MState t f a

MonadFix m => MonadFix (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

mfix :: (a -> MState t m a) -> MState t m a

MonadFail m => MonadFail (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

fail :: String -> MState t m a

(Applicative m, Monad m) => Applicative (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

pure :: a -> MState t m a

(<*>) :: MState t m (a -> b) -> MState t m a -> MState t m b

liftA2 :: (a -> b -> c) -> MState t m a -> MState t m b -> MState t m c

(*>) :: MState t m a -> MState t m b -> MState t m b

(<*) :: MState t m a -> MState t m b -> MState t m a

MonadPeelIO m => MonadPeelIO (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

peelIO :: MState t m (MState t m a -> IO (MState t m a)) #

(Alternative m, Monad m) => Alternative (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

empty :: MState t m a

(<|>) :: MState t m a -> MState t m a -> MState t m a

some :: MState t m a -> MState t m [a]

many :: MState t m a -> MState t m [a]

MonadPlus m => MonadPlus (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

mzero :: MState t m a

mplus :: MState t m a -> MState t m a -> MState t m a

MonadIO m => MonadIO (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

liftIO :: IO a -> MState t m a

MonadCont m => MonadCont (MState t m) Source # 
Instance details

Defined in Control.Concurrent.MState

Methods

callCC :: ((a -> MState t m b) -> MState t m a) -> MState t m a

runMState Source #

Arguments

:: MonadPeelIO m 
=> MState t m a

Action to run

-> t

Initial state value

-> m (a, t) 

Run a MState application, returning both, the function value and the final state. Note that this function has to wait for all threads to finish before it can return the final state.

evalMState Source #

Arguments

:: MonadPeelIO m 
=> Bool

Wait for all threads to finish?

-> MState t m a

Action to evaluate

-> t

Initial state value

-> m a 

Run a MState application, ignoring the final state. If the first argument is True this function will wait for all threads to finish before returning the final result, otherwise it will return the function value as soon as its acquired.

execMState Source #

Arguments

:: MonadPeelIO m 
=> MState t m a

Action to execute

-> t

Initial state value

-> m t 

Run a MState application, ignoring the function value. This function will wait for all threads to finish before returning the final state.

mapMState :: (MonadIO m, MonadIO n) => (m (a, t) -> n (b, t)) -> MState t m a -> MState t n b Source #

Map a stateful computation from one (return value, state) pair to another. See Control.Monad.State.Lazy for more information. Be aware that both MStates still share the same state.

mapMState_ :: MonadIO n => (m a -> n b) -> MState t m a -> MState t n b Source #

modifyM :: MonadIO m => (t -> (a, t)) -> MState t m a Source #

Modify the MState, block all other threads from accessing the state in the meantime (using atomically from the Control.Concurrent.STM library).

modifyM_ :: MonadIO m => (t -> t) -> MState t m () Source #

Concurrency

forkM :: MonadPeelIO m => MState t m () -> MState t m ThreadId Source #

Start a new stateful thread.

forkM_ :: MonadPeelIO m => MState t m () -> MState t m () Source #

killMState :: MonadPeelIO m => MState t m () Source #

Kill all threads in the current MState application.

waitM :: MonadPeelIO m => ThreadId -> MState t m () Source #

Wait for a thread to finish

Example

Example usage:

import Control.Concurrent
import Control.Concurrent.MState
import Control.Monad.State

type MyState a = MState Int IO a

-- Expected state value: 2
main :: IO ()
main = print =<< execMState incTwice 0

incTwice :: MyState ()
incTwice = do
    -- increase in the current thread
    inc
    -- This thread should get killed before it can "inc" our state:
    t_id <- forkM $ do
        delay 2
        inc
    -- Second increase with a small delay in a forked thread, killing the
    -- thread above
    forkM $ do
        delay 1
        inc
        kill t_id
    return ()
  where
    inc   = modifyM (+1)
    kill  = liftIO . killThread
    delay = liftIO . threadDelay . (*1000000) -- in seconds