Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- data Queue d :: Effect
- data QueueResult d
- interpretQueueTBM :: forall d r. Members [Race, Embed IO] r => Int -> InterpreterFor (Queue d) r
- interpretQueueTB :: forall d r. Members [Race, Embed IO] r => Natural -> InterpreterFor (Queue d) r
- data Sync d :: Effect
- interpretSync :: forall d r. Members [Race, Embed IO] r => InterpreterFor (Sync d) r
- data Race :: Effect
- race :: forall a b r. Member Race r => Sem r a -> Sem r b -> Sem r (Either a b)
- race_ :: Member Race r => Sem r a -> Sem r a -> Sem r a
- timeout :: forall a b u r. TimeUnit u => Member Race r => a -> u -> Sem r b -> Sem r (Either a b)
- timeout_ :: TimeUnit u => Member Race r => a -> u -> Sem r a -> Sem r a
- interpretRace :: Member (Final IO) r => InterpreterFor Race r
- data Interrupt :: Effect
- interpretInterrupt :: Members [Critical, Race, Async, Embed IO] r => InterpreterFor Interrupt r
Introduction
This library provides an assortment of tools for concurrency-related tasks:
Queues
data Queue d :: Effect Source #
Abstracts queues like TBQueue
.
For documentation on the constructors, see the module Polysemy.Conc.Data.Queue.
import Polysemy.Conc (Queue, QueueResult) import Polysemy.Conc.Data.Queue as Queue prog :: Member (Queue Int) r => Sem r (QueueResult Int) prog = do Queue.write 5 Queue.write 10 Queue.read >>= \case QueueResult.Success i -> fmap (i +) <$> Queue.read r -> pure r
Instances
type DefiningModule Queue Source # | |
Defined in Polysemy.Conc.Data.Queue |
data QueueResult d Source #
Encodes failure reasons for queues.
For documentation on the constructors, see the module Polysemy.Conc.Data.QueueResult.
import qualified Polysemy.Conc.Data.QueueResult as QueueResult
Instances
Interpreters
Interpret Queue
with a TBMQueue
.
MVars
data Sync d :: Effect Source #
Abstracts an MVar
.
For documentation on the constructors, see the module Polysemy.Conc.Data.Sync.
import Polysemy.Conc (Sync) import qualified Polysemy.Conc.Data.Sync as Sync prog :: Member (Sync Int) r => Sem r Int prog = do Sync.putTry 5 Sync.takeBlock
Instances
type DefiningModule Sync Source # | |
Defined in Polysemy.Conc.Data.Sync |
Interpreters
interpretSync :: forall d r. Members [Race, Embed IO] r => InterpreterFor (Sync d) r Source #
Racing
prog = Polysemy.Conc.race (httpRequest "hackage.haskell.org") (readFile "/path/to/file") >>= \case Left _ -> putStrLn "hackage was faster" Right _ -> putStrLn "file was faster"
When the first thunk finishes, the other will be killed.
Abstract the concept of running two programs concurrently, aborting the other when one terminates.
Timeout
is a simpler variant, where one thread just sleeps for a given interval.
Instances
type DefiningModule Race Source # | |
Defined in Polysemy.Conc.Data.Race |
race :: forall a b r. Member Race r => Sem r a -> Sem r b -> Sem r (Either a b) Source #
Run both programs concurrently, returning the result of the faster one.
timeout :: forall a b u r. TimeUnit u => Member Race r => a -> u -> Sem r b -> Sem r (Either a b) Source #
Return the fallback value if the given program doesn't finish within the specified interval.
Interpreters
interpretRace :: Member (Final IO) r => InterpreterFor Race r Source #
Signal Handling
data Interrupt :: Effect Source #
The interrupt handler effect allows three kinds of interaction for interrupt signals:
- Execute a callback when a signal is received
- Block a thread until a signal is received
- Kill a thread when a signal is received
For documentation on the constructors, see the module Polysemy.Conc.Data.Interrupt.
import qualified Polysemy.Conc.Data.Interrupt as Interrupt prog = do Interrupt.register "task 1" (putStrLn "interrupted") Interrupt.killOnQuit $ forever do doSomeWork
Instances
type DefiningModule Interrupt Source # | |
Defined in Polysemy.Conc.Data.Interrupt |