Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Lifted Control.Concurrent.MVar.
Synopsis
- data Concurrent :: Effect
- runConcurrent :: (HasCallStack, IOE :> es) => Eff (Concurrent : es) a -> Eff es a
- data MVar a
- newEmptyMVar :: Concurrent :> es => Eff es (MVar a)
- newMVar :: Concurrent :> es => a -> Eff es (MVar a)
- takeMVar :: Concurrent :> es => MVar a -> Eff es a
- putMVar :: Concurrent :> es => MVar a -> a -> Eff es ()
- readMVar :: Concurrent :> es => MVar a -> Eff es a
- swapMVar :: Concurrent :> es => MVar a -> a -> Eff es a
- tryTakeMVar :: Concurrent :> es => MVar a -> Eff es (Maybe a)
- tryPutMVar :: Concurrent :> es => MVar a -> a -> Eff es Bool
- isEmptyMVar :: Concurrent :> es => MVar a -> Eff es Bool
- withMVar :: Concurrent :> es => MVar a -> (a -> Eff es b) -> Eff es b
- withMVarMasked :: Concurrent :> es => MVar a -> (a -> Eff es b) -> Eff es b
- modifyMVar :: Concurrent :> es => MVar a -> (a -> Eff es (a, b)) -> Eff es b
- modifyMVar_ :: Concurrent :> es => MVar a -> (a -> Eff es a) -> Eff es ()
- modifyMVarMasked :: Concurrent :> es => MVar a -> (a -> Eff es (a, b)) -> Eff es b
- modifyMVarMasked_ :: Concurrent :> es => MVar a -> (a -> Eff es a) -> Eff es ()
- tryReadMVar :: Concurrent :> es => MVar a -> Eff es (Maybe a)
- mkWeakMVar :: (HasCallStack, Concurrent :> es) => MVar a -> Eff es () -> Eff es (Weak (MVar a))
Effect
data Concurrent :: Effect Source #
Provide the ability to run Eff
computations concurrently in multiple
threads and communicate between them.
Warning: unless you stick to high level functions from the
withAsync
family, the Concurrent
effect makes
it possible to escape the scope of any scoped effect operation. Consider the
following:
>>>
import Effectful.Reader.Static qualified as R
>>>
printAsk msg = liftIO . putStrLn . (msg ++) . (": " ++) =<< R.ask
>>>
:{
runEff . R.runReader "GLOBAL" . runConcurrent $ do a <- R.local (const "LOCAL") $ do a <- async $ do printAsk "child (first)" threadDelay 20000 printAsk "child (second)" threadDelay 10000 printAsk "parent (inside)" pure a printAsk "parent (outside)" wait a :} child (first): LOCAL parent (inside): LOCAL parent (outside): GLOBAL child (second): LOCAL
Note that the asynchronous computation doesn't respect the scope of
local
, i.e. the child thread still behaves like
it's inside the local
block, even though the parent
thread already got out of it.
This is because the value provided by the Reader
effect is thread local, i.e. each thread manages its own version of it. For
the Reader
it is the only reasonable behavior, it
wouldn't be very useful if its "read only" value was affected by calls to
local
from its parent or child threads.
However, the cut isn't so clear if it comes to effects that provide access to
a mutable state. That's why statically dispatched State
and Writer
effects come in two flavors, local and shared:
>>>
import Effectful.State.Static.Local qualified as SL
>>>
:{
runEff . SL.execState "Hi" . runConcurrent $ do replicateConcurrently_ 3 $ SL.modify (++ "!") :} "Hi"
>>>
import Effectful.State.Static.Shared qualified as SS
>>>
:{
runEff . SS.execState "Hi" . runConcurrent $ do replicateConcurrently_ 3 $ SS.modify (++ "!") :} "Hi!!!"
In the first example state updates made concurrently are not reflected in the parent thread because the value is thread local, but in the second example they are, because the value is shared.
Instances
type DispatchOf Concurrent Source # | |
Defined in Effectful.Concurrent.Effect | |
data StaticRep Concurrent Source # | |
Defined in Effectful.Concurrent.Effect |
Handlers
runConcurrent :: (HasCallStack, IOE :> es) => Eff (Concurrent : es) a -> Eff es a Source #
Run the Concurrent
effect.
MVar
An MVar
(pronounced "em-var") is a synchronising variable, used
for communication between concurrent threads. It can be thought of
as a box, which may be empty or full.
Instances
NFData1 MVar | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
NFData (MVar a) | NOTE: Only strict in the reference and not the referenced value. Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
Eq (MVar a) | Since: base-4.1.0.0 |
newEmptyMVar :: Concurrent :> es => Eff es (MVar a) Source #
Lifted newEmptyMVar
.
tryTakeMVar :: Concurrent :> es => MVar a -> Eff es (Maybe a) Source #
Lifted tryTakeMVar
.
tryPutMVar :: Concurrent :> es => MVar a -> a -> Eff es Bool Source #
Lifted tryPutMVar
.
isEmptyMVar :: Concurrent :> es => MVar a -> Eff es Bool Source #
Lifted isEmptyMVar
.
withMVarMasked :: Concurrent :> es => MVar a -> (a -> Eff es b) -> Eff es b Source #
Lifted withMVarMasked
.
modifyMVar :: Concurrent :> es => MVar a -> (a -> Eff es (a, b)) -> Eff es b Source #
Lifted modifyMVar
.
modifyMVar_ :: Concurrent :> es => MVar a -> (a -> Eff es a) -> Eff es () Source #
Lifted modifyMVar_
.
modifyMVarMasked :: Concurrent :> es => MVar a -> (a -> Eff es (a, b)) -> Eff es b Source #
Lifted modifyMVarMasked
.
modifyMVarMasked_ :: Concurrent :> es => MVar a -> (a -> Eff es a) -> Eff es () Source #
Lifted modifyMVarMasked_
.
tryReadMVar :: Concurrent :> es => MVar a -> Eff es (Maybe a) Source #
Lifted tryReadMVar
.
mkWeakMVar :: (HasCallStack, Concurrent :> es) => MVar a -> Eff es () -> Eff es (Weak (MVar a)) Source #
Lifted mkWeakMVar
.
Note: the finalizer will run a cloned environment, so any changes it makes to thread local data will not be visible outside of it.