{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
module Control.Concurrent.Classy.Async
(
Async
, async
, asyncN
, asyncBound
, asyncBoundN
, asyncOn
, asyncOnN
, asyncWithUnmask
, asyncWithUnmaskN
, asyncOnWithUnmask
, asyncOnWithUnmaskN
, withAsync
, withAsyncN
, withAsyncBound
, withAsyncBoundN
, withAsyncOn
, withAsyncOnN
, withAsyncWithUnmask
, withAsyncWithUnmaskN
, withAsyncOnWithUnmask
, withAsyncOnWithUnmaskN
, wait, waitSTM
, poll, pollSTM
, waitCatch, waitCatchSTM
, cancel
, uninterruptibleCancel
, cancelWith
, asyncThreadId
, waitAny, waitAnySTM
, waitAnyCatch, waitAnyCatchSTM
, waitAnyCancel
, waitAnyCatchCancel
, waitEither, waitEitherSTM
, waitEitherCatch, waitEitherCatchSTM
, waitEitherCancel
, waitEitherCatchCancel
, waitEither_, waitEitherSTM_
, waitBoth, waitBothSTM
, link
, link2
, race
, race_
, concurrently, concurrently_
, mapConcurrently, mapConcurrently_
, forConcurrently, forConcurrently_
, replicateConcurrently, replicateConcurrently_
, Concurrently(..)
) where
import Control.Applicative
import Control.Concurrent.Classy.STM.TMVar (newEmptyTMVar, putTMVar,
readTMVar)
import Control.Exception (AsyncException(ThreadKilled),
BlockedIndefinitelyOnSTM(..),
Exception, SomeException)
import Control.Monad
import Control.Monad.Catch (finally, onException, try)
import Control.Monad.Conc.Class
import Control.Monad.STM.Class
import Data.Foldable (foldMap)
import Data.Semigroup (Semigroup(..))
data Async m a = Async
{ forall (m :: * -> *) a. Async m a -> ThreadId m
asyncThreadId :: !(ThreadId m)
, forall (m :: * -> *) a. Async m a -> STM m (Either SomeException a)
_asyncWait :: STM m (Either SomeException a)
}
instance MonadConc m => Eq (Async m a) where
Async ThreadId m
t1 STM m (Either SomeException a)
_ == :: Async m a -> Async m a -> Bool
== Async ThreadId m
t2 STM m (Either SomeException a)
_ = ThreadId m
t1 forall a. Eq a => a -> a -> Bool
== ThreadId m
t2
instance MonadConc m => Functor (Async m) where
fmap :: forall a b. (a -> b) -> Async m a -> Async m b
fmap a -> b
f (Async ThreadId m
t STM m (Either SomeException a)
w) = forall (m :: * -> *) a.
ThreadId m -> STM m (Either SomeException a) -> Async m a
Async ThreadId m
t forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM m (Either SomeException a)
w
newtype Concurrently m a = Concurrently { forall (m :: * -> *) a. Concurrently m a -> m a
runConcurrently :: m a }
instance MonadConc m => Functor (Concurrently m) where
fmap :: forall a b. (a -> b) -> Concurrently m a -> Concurrently m b
fmap a -> b
f (Concurrently m a
a) = forall (m :: * -> *) a. m a -> Concurrently m a
Concurrently forall a b. (a -> b) -> a -> b
$ a -> b
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
a
instance MonadConc m => Applicative (Concurrently m) where
pure :: forall a. a -> Concurrently m a
pure = forall (m :: * -> *) a. m a -> Concurrently m a
Concurrently forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
Concurrently m (a -> b)
fs <*> :: forall a b.
Concurrently m (a -> b) -> Concurrently m a -> Concurrently m b
<*> Concurrently m a
as =
forall (m :: * -> *) a. m a -> Concurrently m a
Concurrently forall a b. (a -> b) -> a -> b
$ (\(a -> b
f, a
a) -> a -> b
f a
a) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b. MonadConc m => m a -> m b -> m (a, b)
concurrently m (a -> b)
fs m a
as
instance MonadConc m => Alternative (Concurrently m) where
empty :: forall a. Concurrently m a
empty = forall (m :: * -> *) a. m a -> Concurrently m a
Concurrently forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Applicative f => f a -> f b
forever forall (m :: * -> *). MonadConc m => m ()
yield
Concurrently m a
as <|> :: forall a. Concurrently m a -> Concurrently m a -> Concurrently m a
<|> Concurrently m a
bs =
forall (m :: * -> *) a. m a -> Concurrently m a
Concurrently forall a b. (a -> b) -> a -> b
$ forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. a -> a
id forall a. a -> a
id forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b.
MonadConc m =>
m a -> m b -> m (Either a b)
race m a
as m a
bs
instance (MonadConc m, Semigroup a) => Semigroup (Concurrently m a) where
<> :: Concurrently m a -> Concurrently m a -> Concurrently m a
(<>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Semigroup a => a -> a -> a
(<>)
instance (MonadConc m, Monoid a) => Monoid (Concurrently m a) where
mempty :: Concurrently m a
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty
#if MIN_VERSION_base(4,11,0)
mappend :: Concurrently m a -> Concurrently m a -> Concurrently m a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
#else
mappend = liftA2 mappend
#endif
async :: MonadConc m => m a -> m (Async m a)
async :: forall (m :: * -> *) a. MonadConc m => m a -> m (Async m a)
async = forall (m :: * -> *) a.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> m (Async m a)
asyncUsing forall (m :: * -> *). MonadConc m => m () -> m (ThreadId m)
fork
asyncN :: MonadConc m => String -> m a -> m (Async m a)
asyncN :: forall (m :: * -> *) a.
MonadConc m =>
String -> m a -> m (Async m a)
asyncN String
name = forall (m :: * -> *) a.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> m (Async m a)
asyncUsing (forall (m :: * -> *).
MonadConc m =>
String -> m () -> m (ThreadId m)
forkN String
name)
asyncBound :: MonadConc m => m a -> m (Async m a)
asyncBound :: forall (m :: * -> *) a. MonadConc m => m a -> m (Async m a)
asyncBound = forall (m :: * -> *) a.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> m (Async m a)
asyncUsing forall (m :: * -> *). MonadConc m => m () -> m (ThreadId m)
forkOS
asyncBoundN :: MonadConc m => String -> m a -> m (Async m a)
asyncBoundN :: forall (m :: * -> *) a.
MonadConc m =>
String -> m a -> m (Async m a)
asyncBoundN String
name = forall (m :: * -> *) a.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> m (Async m a)
asyncUsing (forall (m :: * -> *).
MonadConc m =>
String -> m () -> m (ThreadId m)
forkOSN String
name)
asyncOn :: MonadConc m => Int -> m a -> m (Async m a)
asyncOn :: forall (m :: * -> *) a. MonadConc m => Int -> m a -> m (Async m a)
asyncOn = forall (m :: * -> *) a.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> m (Async m a)
asyncUsing forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadConc m => Int -> m () -> m (ThreadId m)
forkOn
asyncOnN :: MonadConc m => String -> Int -> m a -> m (Async m a)
asyncOnN :: forall (m :: * -> *) a.
MonadConc m =>
String -> Int -> m a -> m (Async m a)
asyncOnN String
name = forall (m :: * -> *) a.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> m (Async m a)
asyncUsing forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadConc m =>
String -> Int -> m () -> m (ThreadId m)
forkOnN String
name
asyncWithUnmask :: MonadConc m => ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncWithUnmask :: forall (m :: * -> *) a.
MonadConc m =>
((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncWithUnmask = forall (m :: * -> *) a.
MonadConc m =>
(((forall b. m b -> m b) -> m ()) -> m (ThreadId m))
-> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncUnmaskUsing forall (m :: * -> *).
MonadConc m =>
((forall a. m a -> m a) -> m ()) -> m (ThreadId m)
forkWithUnmask
asyncWithUnmaskN :: MonadConc m => String -> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncWithUnmaskN :: forall (m :: * -> *) a.
MonadConc m =>
String -> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncWithUnmaskN String
name = forall (m :: * -> *) a.
MonadConc m =>
(((forall b. m b -> m b) -> m ()) -> m (ThreadId m))
-> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncUnmaskUsing (forall (m :: * -> *).
MonadConc m =>
String -> ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)
forkWithUnmaskN String
name)
asyncOnWithUnmask :: MonadConc m => Int -> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncOnWithUnmask :: forall (m :: * -> *) a.
MonadConc m =>
Int -> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncOnWithUnmask Int
i = forall (m :: * -> *) a.
MonadConc m =>
(((forall b. m b -> m b) -> m ()) -> m (ThreadId m))
-> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncUnmaskUsing (forall (m :: * -> *).
MonadConc m =>
Int -> ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)
forkOnWithUnmask Int
i)
asyncOnWithUnmaskN :: MonadConc m => String -> Int -> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncOnWithUnmaskN :: forall (m :: * -> *) a.
MonadConc m =>
String -> Int -> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncOnWithUnmaskN String
name Int
i = forall (m :: * -> *) a.
MonadConc m =>
(((forall b. m b -> m b) -> m ()) -> m (ThreadId m))
-> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncUnmaskUsing (forall (m :: * -> *).
MonadConc m =>
String -> Int -> ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)
forkOnWithUnmaskN String
name Int
i)
asyncUsing :: MonadConc m => (m () -> m (ThreadId m)) -> m a -> m (Async m a)
asyncUsing :: forall (m :: * -> *) a.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> m (Async m a)
asyncUsing m () -> m (ThreadId m)
doFork m a
action = do
TMVar (STM m) (Either SomeException a)
var <- forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall (stm :: * -> *) a. MonadSTM stm => stm (TMVar stm a)
newEmptyTMVar
ThreadId m
tid <- forall (m :: * -> *) b.
MonadConc m =>
((forall a. m a -> m a) -> m b) -> m b
mask forall a b. (a -> b) -> a -> b
$ \forall a. m a -> m a
restore -> m () -> m (ThreadId m)
doFork forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
try (forall a. m a -> m a
restore m a
action) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (stm :: * -> *) a.
MonadSTM stm =>
TMVar stm a -> a -> stm ()
putTMVar TMVar (STM m) (Either SomeException a)
var
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall (m :: * -> *) a.
ThreadId m -> STM m (Either SomeException a) -> Async m a
Async ThreadId m
tid (forall (stm :: * -> *) a. MonadSTM stm => TMVar stm a -> stm a
readTMVar TMVar (STM m) (Either SomeException a)
var))
asyncUnmaskUsing :: MonadConc m => (((forall b. m b -> m b) -> m ()) -> m (ThreadId m)) -> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncUnmaskUsing :: forall (m :: * -> *) a.
MonadConc m =>
(((forall b. m b -> m b) -> m ()) -> m (ThreadId m))
-> ((forall b. m b -> m b) -> m a) -> m (Async m a)
asyncUnmaskUsing ((forall b. m b -> m b) -> m ()) -> m (ThreadId m)
doFork (forall b. m b -> m b) -> m a
action = do
TMVar (STM m) (Either SomeException a)
var <- forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall (stm :: * -> *) a. MonadSTM stm => stm (TMVar stm a)
newEmptyTMVar
ThreadId m
tid <- ((forall b. m b -> m b) -> m ()) -> m (ThreadId m)
doFork forall a b. (a -> b) -> a -> b
$ \forall b. m b -> m b
restore -> forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
try ((forall b. m b -> m b) -> m a
action forall b. m b -> m b
restore) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (stm :: * -> *) a.
MonadSTM stm =>
TMVar stm a -> a -> stm ()
putTMVar TMVar (STM m) (Either SomeException a)
var
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall (m :: * -> *) a.
ThreadId m -> STM m (Either SomeException a) -> Async m a
Async ThreadId m
tid (forall (stm :: * -> *) a. MonadSTM stm => TMVar stm a -> stm a
readTMVar TMVar (STM m) (Either SomeException a)
var))
withAsync :: MonadConc m => m a -> (Async m a -> m b) -> m b
withAsync :: forall (m :: * -> *) a b.
MonadConc m =>
m a -> (Async m a -> m b) -> m b
withAsync = forall (m :: * -> *) a b.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> (Async m a -> m b) -> m b
withAsyncUsing forall (m :: * -> *). MonadConc m => m () -> m (ThreadId m)
fork
withAsyncN :: MonadConc m => String -> m a -> (Async m a -> m b) -> m b
withAsyncN :: forall (m :: * -> *) a b.
MonadConc m =>
String -> m a -> (Async m a -> m b) -> m b
withAsyncN String
name = forall (m :: * -> *) a b.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> (Async m a -> m b) -> m b
withAsyncUsing (forall (m :: * -> *).
MonadConc m =>
String -> m () -> m (ThreadId m)
forkN String
name)
withAsyncBound :: MonadConc m => m a -> (Async m a -> m b) -> m b
withAsyncBound :: forall (m :: * -> *) a b.
MonadConc m =>
m a -> (Async m a -> m b) -> m b
withAsyncBound = forall (m :: * -> *) a b.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> (Async m a -> m b) -> m b
withAsyncUsing forall (m :: * -> *). MonadConc m => m () -> m (ThreadId m)
forkOS
withAsyncBoundN :: MonadConc m => String -> m a -> (Async m a -> m b) -> m b
withAsyncBoundN :: forall (m :: * -> *) a b.
MonadConc m =>
String -> m a -> (Async m a -> m b) -> m b
withAsyncBoundN String
name = forall (m :: * -> *) a b.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> (Async m a -> m b) -> m b
withAsyncUsing (forall (m :: * -> *).
MonadConc m =>
String -> m () -> m (ThreadId m)
forkOSN String
name)
withAsyncOn :: MonadConc m => Int -> m a -> (Async m a -> m b) -> m b
withAsyncOn :: forall (m :: * -> *) a b.
MonadConc m =>
Int -> m a -> (Async m a -> m b) -> m b
withAsyncOn = forall (m :: * -> *) a b.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> (Async m a -> m b) -> m b
withAsyncUsing forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadConc m => Int -> m () -> m (ThreadId m)
forkOn
withAsyncOnN :: MonadConc m => String -> Int -> m a -> (Async m a -> m b) -> m b
withAsyncOnN :: forall (m :: * -> *) a b.
MonadConc m =>
String -> Int -> m a -> (Async m a -> m b) -> m b
withAsyncOnN String
name Int
i = forall (m :: * -> *) a b.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> (Async m a -> m b) -> m b
withAsyncUsing (forall (m :: * -> *).
MonadConc m =>
String -> Int -> m () -> m (ThreadId m)
forkOnN String
name Int
i)
withAsyncWithUnmask :: MonadConc m => ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncWithUnmask :: forall (m :: * -> *) a b.
MonadConc m =>
((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncWithUnmask = forall (m :: * -> *) a b.
MonadConc m =>
(((forall x. m x -> m x) -> m ()) -> m (ThreadId m))
-> ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncUnmaskUsing forall (m :: * -> *).
MonadConc m =>
((forall a. m a -> m a) -> m ()) -> m (ThreadId m)
forkWithUnmask
withAsyncWithUnmaskN :: MonadConc m => String -> ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncWithUnmaskN :: forall (m :: * -> *) a b.
MonadConc m =>
String
-> ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncWithUnmaskN String
name = forall (m :: * -> *) a b.
MonadConc m =>
(((forall x. m x -> m x) -> m ()) -> m (ThreadId m))
-> ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncUnmaskUsing (forall (m :: * -> *).
MonadConc m =>
String -> ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)
forkWithUnmaskN String
name)
withAsyncOnWithUnmask :: MonadConc m => Int -> ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncOnWithUnmask :: forall (m :: * -> *) a b.
MonadConc m =>
Int -> ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncOnWithUnmask Int
i = forall (m :: * -> *) a b.
MonadConc m =>
(((forall x. m x -> m x) -> m ()) -> m (ThreadId m))
-> ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncUnmaskUsing (forall (m :: * -> *).
MonadConc m =>
Int -> ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)
forkOnWithUnmask Int
i)
withAsyncOnWithUnmaskN :: MonadConc m
=> String -> Int -> ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncOnWithUnmaskN :: forall (m :: * -> *) a b.
MonadConc m =>
String
-> Int
-> ((forall x. m x -> m x) -> m a)
-> (Async m a -> m b)
-> m b
withAsyncOnWithUnmaskN String
name Int
i = forall (m :: * -> *) a b.
MonadConc m =>
(((forall x. m x -> m x) -> m ()) -> m (ThreadId m))
-> ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncUnmaskUsing (forall (m :: * -> *).
MonadConc m =>
String -> Int -> ((forall a. m a -> m a) -> m ()) -> m (ThreadId m)
forkOnWithUnmaskN String
name Int
i)
withAsyncUsing :: MonadConc m => (m () -> m (ThreadId m)) -> m a -> (Async m a -> m b) -> m b
withAsyncUsing :: forall (m :: * -> *) a b.
MonadConc m =>
(m () -> m (ThreadId m)) -> m a -> (Async m a -> m b) -> m b
withAsyncUsing m () -> m (ThreadId m)
doFork m a
action Async m a -> m b
inner = do
TMVar (STM m) (Either SomeException a)
var <- forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall (stm :: * -> *) a. MonadSTM stm => stm (TMVar stm a)
newEmptyTMVar
ThreadId m
tid <- forall (m :: * -> *) b.
MonadConc m =>
((forall a. m a -> m a) -> m b) -> m b
mask forall a b. (a -> b) -> a -> b
$ \forall a. m a -> m a
restore -> m () -> m (ThreadId m)
doFork forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
try (forall a. m a -> m a
restore m a
action) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (stm :: * -> *) a.
MonadSTM stm =>
TMVar stm a -> a -> stm ()
putTMVar TMVar (STM m) (Either SomeException a)
var
forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> (Async m a -> m b) -> m b
withAsyncDo (forall (m :: * -> *) a.
ThreadId m -> STM m (Either SomeException a) -> Async m a
Async ThreadId m
tid (forall (stm :: * -> *) a. MonadSTM stm => TMVar stm a -> stm a
readTMVar TMVar (STM m) (Either SomeException a)
var)) Async m a -> m b
inner
withAsyncUnmaskUsing :: MonadConc m => (((forall x. m x -> m x) -> m ()) -> m (ThreadId m)) -> ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncUnmaskUsing :: forall (m :: * -> *) a b.
MonadConc m =>
(((forall x. m x -> m x) -> m ()) -> m (ThreadId m))
-> ((forall x. m x -> m x) -> m a) -> (Async m a -> m b) -> m b
withAsyncUnmaskUsing ((forall x. m x -> m x) -> m ()) -> m (ThreadId m)
doFork (forall x. m x -> m x) -> m a
action Async m a -> m b
inner = do
TMVar (STM m) (Either SomeException a)
var <- forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall (stm :: * -> *) a. MonadSTM stm => stm (TMVar stm a)
newEmptyTMVar
ThreadId m
tid <- ((forall x. m x -> m x) -> m ()) -> m (ThreadId m)
doFork forall a b. (a -> b) -> a -> b
$ \forall x. m x -> m x
restore -> forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
try ((forall x. m x -> m x) -> m a
action forall x. m x -> m x
restore) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (stm :: * -> *) a.
MonadSTM stm =>
TMVar stm a -> a -> stm ()
putTMVar TMVar (STM m) (Either SomeException a)
var
forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> (Async m a -> m b) -> m b
withAsyncDo (forall (m :: * -> *) a.
ThreadId m -> STM m (Either SomeException a) -> Async m a
Async ThreadId m
tid (forall (stm :: * -> *) a. MonadSTM stm => TMVar stm a -> stm a
readTMVar TMVar (STM m) (Either SomeException a)
var)) Async m a -> m b
inner
withAsyncDo :: MonadConc m => Async m a -> (Async m a -> m b) -> m b
withAsyncDo :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> (Async m a -> m b) -> m b
withAsyncDo Async m a
a Async m a -> m b
inner = do
b
res <- Async m a -> m b
inner Async m a
a forall (m :: * -> *) a.
MonadConc m =>
m a -> (SomeException -> m a) -> m a
`catchAll` (\SomeException
e -> forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
uninterruptibleCancel Async m a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) e a. (MonadConc m, Exception e) => e -> m a
throw SomeException
e)
forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
cancel Async m a
a
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
res
catchAll :: MonadConc m => m a -> (SomeException -> m a) -> m a
catchAll :: forall (m :: * -> *) a.
MonadConc m =>
m a -> (SomeException -> m a) -> m a
catchAll = forall (m :: * -> *) e a.
(MonadConc m, Exception e) =>
m a -> (e -> m a) -> m a
catch
wait :: MonadConc m => Async m a -> m a
wait :: forall (m :: * -> *) a. MonadConc m => Async m a -> m a
wait = forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadConc m => Async m a -> STM m a
waitSTM
waitSTM :: MonadConc m => Async m a -> STM m a
waitSTM :: forall (m :: * -> *) a. MonadConc m => Async m a -> STM m a
waitSTM Async m a
a = do
Either SomeException a
r <- forall (m :: * -> *) a.
MonadConc m =>
Async m a -> STM m (Either SomeException a)
waitCatchSTM Async m a
a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (stm :: * -> *) e a.
(MonadSTM stm, Exception e) =>
e -> stm a
throwSTM forall (f :: * -> *) a. Applicative f => a -> f a
pure Either SomeException a
r
poll :: MonadConc m => Async m a -> m (Maybe (Either SomeException a))
poll :: forall (m :: * -> *) a.
MonadConc m =>
Async m a -> m (Maybe (Either SomeException a))
poll = forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
MonadConc m =>
Async m a -> STM m (Maybe (Either SomeException a))
pollSTM
pollSTM :: MonadConc m => Async m a -> STM m (Maybe (Either SomeException a))
pollSTM :: forall (m :: * -> *) a.
MonadConc m =>
Async m a -> STM m (Maybe (Either SomeException a))
pollSTM (Async ThreadId m
_ STM m (Either SomeException a)
w) = (forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> STM m (Either SomeException a)
w) forall (stm :: * -> *) a. MonadSTM stm => stm a -> stm a -> stm a
`orElse` forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
waitCatch :: MonadConc m => Async m a -> m (Either SomeException a)
waitCatch :: forall (m :: * -> *) a.
MonadConc m =>
Async m a -> m (Either SomeException a)
waitCatch = forall {m :: * -> *} {a}. MonadConc m => m a -> m a
tryAgain forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
MonadConc m =>
Async m a -> STM m (Either SomeException a)
waitCatchSTM where
tryAgain :: m a -> m a
tryAgain m a
f = m a
f forall (m :: * -> *) e a.
(MonadConc m, Exception e) =>
m a -> (e -> m a) -> m a
`catch` \BlockedIndefinitelyOnSTM
BlockedIndefinitelyOnSTM -> m a
f
waitCatchSTM :: MonadConc m => Async m a -> STM m (Either SomeException a)
waitCatchSTM :: forall (m :: * -> *) a.
MonadConc m =>
Async m a -> STM m (Either SomeException a)
waitCatchSTM (Async ThreadId m
_ STM m (Either SomeException a)
w) = STM m (Either SomeException a)
w
cancel :: MonadConc m => Async m a -> m ()
cancel :: forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
cancel a :: Async m a
a@(Async ThreadId m
tid STM m (Either SomeException a)
_) = forall (m :: * -> *) e.
(MonadConc m, Exception e) =>
ThreadId m -> e -> m ()
throwTo ThreadId m
tid AsyncException
ThreadKilled forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (m :: * -> *) a.
MonadConc m =>
Async m a -> m (Either SomeException a)
waitCatch Async m a
a
uninterruptibleCancel :: MonadConc m => Async m a -> m ()
uninterruptibleCancel :: forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
uninterruptibleCancel = forall (m :: * -> *) a. MonadMask m => m a -> m a
uninterruptibleMask_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
cancel
cancelWith :: (MonadConc m, Exception e) => Async m a -> e -> m ()
cancelWith :: forall (m :: * -> *) e a.
(MonadConc m, Exception e) =>
Async m a -> e -> m ()
cancelWith (Async ThreadId m
tid STM m (Either SomeException a)
_) = forall (m :: * -> *) e.
(MonadConc m, Exception e) =>
ThreadId m -> e -> m ()
throwTo ThreadId m
tid
waitAny :: MonadConc m => [Async m a] -> m (Async m a, a)
waitAny :: forall (m :: * -> *) a.
MonadConc m =>
[Async m a] -> m (Async m a, a)
waitAny = forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
MonadConc m =>
[Async m a] -> STM m (Async m a, a)
waitAnySTM
waitAnySTM :: MonadConc m => [Async m a] -> STM m (Async m a, a)
waitAnySTM :: forall (m :: * -> *) a.
MonadConc m =>
[Async m a] -> STM m (Async m a, a)
waitAnySTM = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall (stm :: * -> *) a. MonadSTM stm => stm a -> stm a -> stm a
orElse forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\Async m a
a -> do a
r <- forall (m :: * -> *) a. MonadConc m => Async m a -> STM m a
waitSTM Async m a
a; forall (f :: * -> *) a. Applicative f => a -> f a
pure (Async m a
a, a
r))) forall (stm :: * -> *) a. MonadSTM stm => stm a
retry
waitAnyCatch :: MonadConc m => [Async m a] -> m (Async m a, Either SomeException a)
waitAnyCatch :: forall (m :: * -> *) a.
MonadConc m =>
[Async m a] -> m (Async m a, Either SomeException a)
waitAnyCatch = forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
MonadConc m =>
[Async m a] -> STM m (Async m a, Either SomeException a)
waitAnyCatchSTM
waitAnyCatchSTM :: MonadConc m => [Async m a] -> STM m (Async m a, Either SomeException a)
waitAnyCatchSTM :: forall (m :: * -> *) a.
MonadConc m =>
[Async m a] -> STM m (Async m a, Either SomeException a)
waitAnyCatchSTM = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall (stm :: * -> *) a. MonadSTM stm => stm a -> stm a -> stm a
orElse forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\Async m a
a -> do Either SomeException a
r <- forall (m :: * -> *) a.
MonadConc m =>
Async m a -> STM m (Either SomeException a)
waitCatchSTM Async m a
a; forall (f :: * -> *) a. Applicative f => a -> f a
pure (Async m a
a, Either SomeException a
r))) forall (stm :: * -> *) a. MonadSTM stm => stm a
retry
waitAnyCancel :: MonadConc m => [Async m a] -> m (Async m a, a)
waitAnyCancel :: forall (m :: * -> *) a.
MonadConc m =>
[Async m a] -> m (Async m a, a)
waitAnyCancel [Async m a]
asyncs = forall (m :: * -> *) a.
MonadConc m =>
[Async m a] -> m (Async m a, a)
waitAny [Async m a]
asyncs forall (m :: * -> *) a b. MonadMask m => m a -> m b -> m a
`finally` forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
cancel [Async m a]
asyncs
waitAnyCatchCancel :: MonadConc m => [Async m a] -> m (Async m a, Either SomeException a)
waitAnyCatchCancel :: forall (m :: * -> *) a.
MonadConc m =>
[Async m a] -> m (Async m a, Either SomeException a)
waitAnyCatchCancel [Async m a]
asyncs = forall (m :: * -> *) a.
MonadConc m =>
[Async m a] -> m (Async m a, Either SomeException a)
waitAnyCatch [Async m a]
asyncs forall (m :: * -> *) a b. MonadMask m => m a -> m b -> m a
`finally` forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
cancel [Async m a]
asyncs
waitEither :: MonadConc m => Async m a -> Async m b -> m (Either a b)
waitEither :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> m (Either a b)
waitEither Async m a
left Async m b
right = forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> STM m (Either a b)
waitEitherSTM Async m a
left Async m b
right
waitEitherSTM :: MonadConc m => Async m a -> Async m b -> STM m (Either a b)
waitEitherSTM :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> STM m (Either a b)
waitEitherSTM Async m a
left Async m b
right =
(forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. MonadConc m => Async m a -> STM m a
waitSTM Async m a
left) forall (stm :: * -> *) a. MonadSTM stm => stm a -> stm a -> stm a
`orElse` (forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. MonadConc m => Async m a -> STM m a
waitSTM Async m b
right)
waitEitherCatch :: MonadConc m => Async m a -> Async m b
-> m (Either (Either SomeException a) (Either SomeException b))
waitEitherCatch :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a
-> Async m b
-> m (Either (Either SomeException a) (Either SomeException b))
waitEitherCatch Async m a
left Async m b
right = forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
MonadConc m =>
Async m a
-> Async m b
-> STM m (Either (Either SomeException a) (Either SomeException b))
waitEitherCatchSTM Async m a
left Async m b
right
waitEitherCatchSTM :: MonadConc m => Async m a -> Async m b
-> STM m (Either (Either SomeException a) (Either SomeException b))
waitEitherCatchSTM :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a
-> Async m b
-> STM m (Either (Either SomeException a) (Either SomeException b))
waitEitherCatchSTM Async m a
left Async m b
right =
(forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
MonadConc m =>
Async m a -> STM m (Either SomeException a)
waitCatchSTM Async m a
left) forall (stm :: * -> *) a. MonadSTM stm => stm a -> stm a -> stm a
`orElse` (forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
MonadConc m =>
Async m a -> STM m (Either SomeException a)
waitCatchSTM Async m b
right)
waitEitherCancel :: MonadConc m => Async m a -> Async m b -> m (Either a b)
waitEitherCancel :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> m (Either a b)
waitEitherCancel Async m a
left Async m b
right =
forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> m (Either a b)
waitEither Async m a
left Async m b
right forall (m :: * -> *) a b. MonadMask m => m a -> m b -> m a
`finally` (forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
cancel Async m a
left forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
cancel Async m b
right)
waitEitherCatchCancel :: MonadConc m => Async m a -> Async m b
-> m (Either (Either SomeException a) (Either SomeException b))
waitEitherCatchCancel :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a
-> Async m b
-> m (Either (Either SomeException a) (Either SomeException b))
waitEitherCatchCancel Async m a
left Async m b
right =
forall (m :: * -> *) a b.
MonadConc m =>
Async m a
-> Async m b
-> m (Either (Either SomeException a) (Either SomeException b))
waitEitherCatch Async m a
left Async m b
right forall (m :: * -> *) a b. MonadMask m => m a -> m b -> m a
`finally` (forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
cancel Async m a
left forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
cancel Async m b
right)
waitEither_ :: MonadConc m => Async m a -> Async m b -> m ()
waitEither_ :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> m ()
waitEither_ Async m a
left Async m b
right = forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> STM m ()
waitEitherSTM_ Async m a
left Async m b
right
waitEitherSTM_:: MonadConc m => Async m a -> Async m b -> STM m ()
waitEitherSTM_ :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> STM m ()
waitEitherSTM_ Async m a
left Async m b
right = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> STM m (Either a b)
waitEitherSTM Async m a
left Async m b
right
waitBoth :: MonadConc m => Async m a -> Async m b -> m (a, b)
waitBoth :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> m (a, b)
waitBoth Async m a
left Async m b
right = forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> STM m (a, b)
waitBothSTM Async m a
left Async m b
right
waitBothSTM :: MonadConc m => Async m a -> Async m b -> STM m (a, b)
waitBothSTM :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> STM m (a, b)
waitBothSTM Async m a
left Async m b
right = do
a
a <- forall (m :: * -> *) a. MonadConc m => Async m a -> STM m a
waitSTM Async m a
left forall (stm :: * -> *) a. MonadSTM stm => stm a -> stm a -> stm a
`orElse` (forall (m :: * -> *) a. MonadConc m => Async m a -> STM m a
waitSTM Async m b
right forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (stm :: * -> *) a. MonadSTM stm => stm a
retry)
b
b <- forall (m :: * -> *) a. MonadConc m => Async m a -> STM m a
waitSTM Async m b
right
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
a, b
b)
link :: MonadConc m => Async m a -> m ()
link :: forall (m :: * -> *) a. MonadConc m => Async m a -> m ()
link (Async ThreadId m
_ STM m (Either SomeException a)
w) = do
ThreadId m
me <- forall (m :: * -> *). MonadConc m => m (ThreadId m)
myThreadId
forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadConc m => m a -> m (ThreadId m)
forkRepeat forall a b. (a -> b) -> a -> b
$ do
Either SomeException a
r <- forall (m :: * -> *) a. MonadConc m => STM m a -> m a
atomically STM m (Either SomeException a)
w
case Either SomeException a
r of
Left SomeException
e -> forall (m :: * -> *) e.
(MonadConc m, Exception e) =>
ThreadId m -> e -> m ()
throwTo ThreadId m
me SomeException
e
Either SomeException a
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
link2 :: MonadConc m => Async m a -> Async m b -> m ()
link2 :: forall (m :: * -> *) a b.
MonadConc m =>
Async m a -> Async m b -> m ()
link2 left :: Async m a
left@(Async ThreadId m
tl STM m (Either SomeException a)
_) right :: Async m b
right@(Async ThreadId m
tr STM m (Either SomeException b)
_) =
forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadConc m => m a -> m (ThreadId m)
forkRepeat forall a b. (a -> b) -> a -> b
$ do
Either (Either SomeException a) (Either SomeException b)
r <- forall (m :: * -> *) a b.
MonadConc m =>
Async m a
-> Async m b
-> m (Either (Either SomeException a) (Either SomeException b))
waitEitherCatch Async m a
left Async m b
right
case Either (Either SomeException a) (Either SomeException b)
r of
Left (Left SomeException
e) -> forall (m :: * -> *) e.
(MonadConc m, Exception e) =>
ThreadId m -> e -> m ()
throwTo ThreadId m
tr SomeException
e
Right (Left SomeException
e) -> forall (m :: * -> *) e.
(MonadConc m, Exception e) =>
ThreadId m -> e -> m ()
throwTo ThreadId m
tl SomeException
e
Either (Either SomeException a) (Either SomeException b)
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
forkRepeat :: MonadConc m => m a -> m (ThreadId m)
forkRepeat :: forall (m :: * -> *) a. MonadConc m => m a -> m (ThreadId m)
forkRepeat m a
action = forall (m :: * -> *) b.
MonadConc m =>
((forall a. m a -> m a) -> m b) -> m b
mask forall a b. (a -> b) -> a -> b
$ \forall a. m a -> m a
restore ->
let go :: m ()
go = do
Either SomeException a
r <- (forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
try :: MonadConc m => m a -> m (Either SomeException a)) forall a b. (a -> b) -> a -> b
$ forall a. m a -> m a
restore m a
action
case Either SomeException a
r of
Left SomeException
_ -> m ()
go
Either SomeException a
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
in forall (m :: * -> *). MonadConc m => m () -> m (ThreadId m)
fork m ()
go
race :: MonadConc m => m a -> m b -> m (Either a b)
race :: forall (m :: * -> *) a b.
MonadConc m =>
m a -> m b -> m (Either a b)
race m a
left m b
right = forall (m :: * -> *) a b r.
MonadConc m =>
m a
-> m b
-> (MVar m (Either SomeException (Either a b)) -> m r)
-> m r
concurrently' m a
left m b
right forall {m :: * -> *} {e} {b}.
(MonadConc m, Exception e) =>
MVar m (Either e b) -> m b
collect where
collect :: MVar m (Either e b) -> m b
collect MVar m (Either e b)
m = do
Either e b
e <- forall (m :: * -> *) a. MonadConc m => MVar m a -> m a
takeMVar MVar m (Either e b)
m
case Either e b
e of
Left e
ex -> forall (m :: * -> *) e a. (MonadConc m, Exception e) => e -> m a
throw e
ex
Right b
r -> forall (f :: * -> *) a. Applicative f => a -> f a
pure b
r
race_ :: MonadConc m => m a -> m b -> m ()
race_ :: forall (m :: * -> *) a b. MonadConc m => m a -> m b -> m ()
race_ m a
a m b
b = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
MonadConc m =>
m a -> m b -> m (Either a b)
race m a
a m b
b
concurrently :: MonadConc m => m a -> m b -> m (a, b)
concurrently :: forall (m :: * -> *) a b. MonadConc m => m a -> m b -> m (a, b)
concurrently m a
left m b
right = forall (m :: * -> *) a b r.
MonadConc m =>
m a
-> m b
-> (MVar m (Either SomeException (Either a b)) -> m r)
-> m r
concurrently' m a
left m b
right (forall {f :: * -> *} {e} {a} {b}.
(MonadConc f, Exception e) =>
[Either a b] -> MVar f (Either e (Either a b)) -> f (a, b)
collect []) where
collect :: [Either a b] -> MVar f (Either e (Either a b)) -> f (a, b)
collect [Left a
a, Right b
b] MVar f (Either e (Either a b))
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
a, b
b)
collect [Right b
b, Left a
a] MVar f (Either e (Either a b))
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
a, b
b)
collect [Either a b]
xs MVar f (Either e (Either a b))
m = do
Either e (Either a b)
e <- forall (m :: * -> *) a. MonadConc m => MVar m a -> m a
takeMVar MVar f (Either e (Either a b))
m
case Either e (Either a b)
e of
Left e
ex -> forall (m :: * -> *) e a. (MonadConc m, Exception e) => e -> m a
throw e
ex
Right Either a b
r -> [Either a b] -> MVar f (Either e (Either a b)) -> f (a, b)
collect (Either a b
rforall a. a -> [a] -> [a]
:[Either a b]
xs) MVar f (Either e (Either a b))
m
concurrently_ :: MonadConc m => m a -> m b -> m ()
concurrently_ :: forall (m :: * -> *) a b. MonadConc m => m a -> m b -> m ()
concurrently_ m a
left m b
right = forall (m :: * -> *) a b r.
MonadConc m =>
m a
-> m b
-> (MVar m (Either SomeException (Either a b)) -> m r)
-> m r
concurrently' m a
left m b
right (forall {f :: * -> *} {e} {b}.
(MonadConc f, Exception e) =>
Int -> MVar f (Either e b) -> f ()
collect Int
0) where
collect :: Int -> MVar f (Either e b) -> f ()
collect Int
2 MVar f (Either e b)
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
collect Int
i MVar f (Either e b)
m = do
Either e b
e <- forall (m :: * -> *) a. MonadConc m => MVar m a -> m a
takeMVar MVar f (Either e b)
m
case Either e b
e of
Left e
ex -> forall (m :: * -> *) e a. (MonadConc m, Exception e) => e -> m a
throw e
ex
Right b
_ -> Int -> MVar f (Either e b) -> f ()
collect (Int
iforall a. Num a => a -> a -> a
+Int
1::Int) MVar f (Either e b)
m
concurrently' :: MonadConc m => m a -> m b
-> (MVar m (Either SomeException (Either a b)) -> m r)
-> m r
concurrently' :: forall (m :: * -> *) a b r.
MonadConc m =>
m a
-> m b
-> (MVar m (Either SomeException (Either a b)) -> m r)
-> m r
concurrently' m a
left m b
right MVar m (Either SomeException (Either a b)) -> m r
collect = do
MVar m (Either SomeException (Either a b))
done <- forall (m :: * -> *) a. MonadConc m => m (MVar m a)
newEmptyMVar
forall (m :: * -> *) b.
MonadConc m =>
((forall a. m a -> m a) -> m b) -> m b
mask forall a b. (a -> b) -> a -> b
$ \forall a. m a -> m a
restore -> do
ThreadId m
lid <- forall (m :: * -> *). MonadConc m => m () -> m (ThreadId m)
fork forall a b. (a -> b) -> a -> b
$ forall a. m a -> m a
restore (m a
left forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m (Either SomeException (Either a b))
done forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left)
forall (m :: * -> *) e a.
(MonadConc m, Exception e) =>
m a -> (e -> m a) -> m a
`catch` (forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m (Either SomeException (Either a b))
done forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left)
ThreadId m
rid <- forall (m :: * -> *). MonadConc m => m () -> m (ThreadId m)
fork forall a b. (a -> b) -> a -> b
$ forall a. m a -> m a
restore (m b
right forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m (Either SomeException (Either a b))
done forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right)
forall (m :: * -> *) e a.
(MonadConc m, Exception e) =>
m a -> (e -> m a) -> m a
`catch` (forall (m :: * -> *) a. MonadConc m => MVar m a -> a -> m ()
putMVar MVar m (Either SomeException (Either a b))
done forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left)
let stop :: m ()
stop = forall (m :: * -> *). MonadConc m => ThreadId m -> m ()
killThread ThreadId m
rid forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *). MonadConc m => ThreadId m -> m ()
killThread ThreadId m
lid
r
r <- forall a. m a -> m a
restore (MVar m (Either SomeException (Either a b)) -> m r
collect MVar m (Either SomeException (Either a b))
done) forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`onException` m ()
stop
m ()
stop
forall (f :: * -> *) a. Applicative f => a -> f a
pure r
r
mapConcurrently :: (Traversable t, MonadConc m) => (a -> m b) -> t a -> m (t b)
mapConcurrently :: forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, MonadConc m) =>
(a -> m b) -> t a -> m (t b)
mapConcurrently a -> m b
f = forall (m :: * -> *) a. Concurrently m a -> m a
runConcurrently forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (forall (m :: * -> *) a. m a -> Concurrently m a
Concurrently forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m b
f)
forConcurrently :: (Traversable t, MonadConc m) => t a -> (a -> m b)-> m (t b)
forConcurrently :: forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, MonadConc m) =>
t a -> (a -> m b) -> m (t b)
forConcurrently = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, MonadConc m) =>
(a -> m b) -> t a -> m (t b)
mapConcurrently
mapConcurrently_ :: (Foldable f, MonadConc m) => (a -> m b) -> f a -> m ()
mapConcurrently_ :: forall (f :: * -> *) (m :: * -> *) a b.
(Foldable f, MonadConc m) =>
(a -> m b) -> f a -> m ()
mapConcurrently_ a -> m b
f = forall (m :: * -> *) a. Concurrently m a -> m a
runConcurrently forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (forall (m :: * -> *) a. m a -> Concurrently m a
Concurrently forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m b
f)
forConcurrently_ :: (Foldable f, MonadConc m) => f a -> (a -> m b) -> m ()
forConcurrently_ :: forall (f :: * -> *) (m :: * -> *) a b.
(Foldable f, MonadConc m) =>
f a -> (a -> m b) -> m ()
forConcurrently_ = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) (m :: * -> *) a b.
(Foldable f, MonadConc m) =>
(a -> m b) -> f a -> m ()
mapConcurrently_
replicateConcurrently :: MonadConc m => Int -> m a -> m [a]
replicateConcurrently :: forall (m :: * -> *) a. MonadConc m => Int -> m a -> m [a]
replicateConcurrently Int
i = forall (m :: * -> *) a. Concurrently m a -> m a
runConcurrently forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. m a -> Concurrently m a
Concurrently
replicateConcurrently_ :: MonadConc m => Int -> m a -> m ()
replicateConcurrently_ :: forall (m :: * -> *) a. MonadConc m => Int -> m a -> m ()
replicateConcurrently_ Int
i = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadConc m => Int -> m a -> m [a]
replicateConcurrently Int
i