lifted-async-0.10.2.7: Run lifted IO operations asynchronously and wait for their results

Copyright | Copyright (C) 2012-2018 Mitsutoshi Aoe
License | BSD-style (see the file LICENSE)
Maintainer | Mitsutoshi Aoe <maoe@foldr.in>
Stability | experimental
Safe Haskell | None
Language | Haskell2010

Control.Concurrent.Async.Lifted.Safe

Contents

- Asynchronous actions
  - Spawning
  - Spawning with automatic `cancel`ation
  - Quering `Async`s
  - STM operations
  - Waiting for multiple `Async`s
  - Waiting for multiple `Async`s in STM
  - Linking
- Convenient utilities

Description

This is a safe variant of `Control.Concurrent.Async.Lifted`.

This module assumes your monad stack to satisfy ``StM` m a ~ a` so you can't mess up monadic effects. If your monad stack is stateful, use `Control.Concurrent.Async.Lifted` with special care.

Synopsis

- data Async a
- class StM m a ~ a => Pure (m :: Type -> Type) a
- class (forall (a :: k). p a) => Forall (p :: k -> Constraint)
- async :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m (Async a)
- asyncBound :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m (Async a)
- asyncOn :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> m a -> m (Async a)
- asyncWithUnmask :: (MonadBaseControl IO m, Forall (Pure m)) => ((forall b. m b -> m b) -> m a) -> m (Async a)
- asyncOnWithUnmask :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> ((forall b. m b -> m b) -> m a) -> m (Async a)
- withAsync :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> (Async a -> m b) -> m b
- withAsyncBound :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> (Async a -> m b) -> m b
- withAsyncOn :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> m a -> (Async a -> m b) -> m b
- withAsyncWithUnmask :: (MonadBaseControl IO m, Forall (Pure m)) => ((forall c. m c -> m c) -> m a) -> (Async a -> m b) -> m b
- withAsyncOnWithUnmask :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> ((forall c. m c -> m c) -> m a) -> (Async a -> m b) -> m b
- wait :: (MonadBase IO m, Forall (Pure m)) => Async a -> m a
- poll :: (MonadBase IO m, Forall (Pure m)) => Async a -> m (Maybe (Either SomeException a))
- waitCatch :: (MonadBase IO m, Forall (Pure m)) => Async a -> m (Either SomeException a)
- cancel :: MonadBase IO m => Async a -> m ()
- uninterruptibleCancel :: MonadBase IO m => Async a -> m ()
- cancelWith :: (MonadBase IO m, Exception e) => Async a -> e -> m ()
- asyncThreadId :: Async a -> ThreadId
- data AsyncCancelled = AsyncCancelled
- waitSTM :: Async a -> STM a
- pollSTM :: Async a -> STM (Maybe (Either SomeException a))
- waitCatchSTM :: Async a -> STM (Either SomeException a)
- waitAny :: (MonadBase IO m, Forall (Pure m)) => [Async a] -> m (Async a, a)
- waitAnyCatch :: (MonadBase IO m, Forall (Pure m)) => [Async a] -> m (Async a, Either SomeException a)
- waitAnyCancel :: (MonadBase IO m, Forall (Pure m)) => [Async a] -> m (Async a, a)
- waitAnyCatchCancel :: (MonadBase IO m, Forall (Pure m)) => [Async a] -> m (Async a, Either SomeException a)
- waitEither :: (MonadBase IO m, Forall (Pure m)) => Async a -> Async b -> m (Either a b)
- waitEitherCatch :: (MonadBase IO m, Forall (Pure m)) => Async a -> Async b -> m (Either (Either SomeException a) (Either SomeException b))
- waitEitherCancel :: (MonadBase IO m, Forall (Pure m)) => Async a -> Async b -> m (Either a b)
- waitEitherCatchCancel :: (MonadBase IO m, Forall (Pure m)) => Async a -> Async b -> m (Either (Either SomeException a) (Either SomeException b))
- waitEither_ :: MonadBase IO m => Async a -> Async b -> m ()
- waitBoth :: (MonadBase IO m, Forall (Pure m)) => Async a -> Async b -> m (a, b)
- waitAnySTM :: [Async a] -> STM (Async a, a)
- waitAnyCatchSTM :: [Async a] -> STM (Async a, Either SomeException a)
- waitEitherSTM :: Async a -> Async b -> STM (Either a b)
- waitEitherCatchSTM :: Async a -> Async b -> STM (Either (Either SomeException a) (Either SomeException b))
- waitEitherSTM_ :: Async a -> Async b -> STM ()
- waitBothSTM :: Async a -> Async b -> STM (a, b)
- link :: MonadBase IO m => Async a -> m ()
- link2 :: MonadBase IO m => Async a -> Async b -> m ()
- data ExceptionInLinkedThread = ExceptionInLinkedThread (Async a) SomeException
- race :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m b -> m (Either a b)
- race_ :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m b -> m ()
- concurrently :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m b -> m (a, b)
- concurrently_ :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m b -> m ()
- mapConcurrently :: (Traversable t, MonadBaseControl IO m, Forall (Pure m)) => (a -> m b) -> t a -> m (t b)
- mapConcurrently_ :: (Foldable t, MonadBaseControl IO m, Forall (Pure m)) => (a -> m b) -> t a -> m ()
- forConcurrently :: (Traversable t, MonadBaseControl IO m, Forall (Pure m)) => t a -> (a -> m b) -> m (t b)
- forConcurrently_ :: (Foldable t, MonadBaseControl IO m, Forall (Pure m)) => t a -> (a -> m b) -> m ()
- replicateConcurrently :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> m a -> m [a]
- replicateConcurrently_ :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> m a -> m ()
- data Concurrently (m :: Type -> Type) a where
  - Concurrently :: forall (m :: Type -> Type) a. Forall (Pure m) => {..} -> Concurrently m a
- compareAsyncs :: Async a -> Async b -> Ordering

# Asynchronous actions

data Async a Source #

An asynchronous action spawned by `async` or `withAsync`. Asynchronous actions are executed in a separate thread, and operations are provided for waiting for asynchronous actions to complete and obtaining their results (see e.g. `wait`).

#### Instances

Instances details

Functor Async |
Instance details Defined in Control.Concurrent.Async.Internal Methods fmap :: (a -> b) -> Async a -> Async b Source # (<$) :: a -> Async b -> Async a Source #
Eq (Async a) |
Instance details Defined in Control.Concurrent.Async.Internal Methods (==) :: Async a -> Async a -> Bool Source # (/=) :: Async a -> Async a -> Bool Source #
Ord (Async a) |
Instance details Defined in Control.Concurrent.Async.Internal Methods compare :: Async a -> Async a -> Ordering Source # (<) :: Async a -> Async a -> Bool Source # (<=) :: Async a -> Async a -> Bool Source # (>) :: Async a -> Async a -> Bool Source # (>=) :: Async a -> Async a -> Bool Source # max :: Async a -> Async a -> Async a Source # min :: Async a -> Async a -> Async a Source #
Hashable (Async a) |
Instance details Defined in Control.Concurrent.Async.Internal Methods hashWithSalt :: Int -> Async a -> Int hash :: Async a -> Int

class StM m a ~ a => Pure (m :: Type -> Type) a Source #

Most of the functions in this module have ``Forall` (`Pure` m)` in their constraints, which means they require the monad `m` satisfies ``StM` m a ~ a` for all `a`.

#### Instances

Instances details

StM m a ~ a => Pure m a Source # |
Instance details Defined in Control.Concurrent.Async.Lifted.Safe

class (forall (a :: k). p a) => Forall (p :: k -> Constraint) Source #

#### Instances

Instances details

(forall (a :: k). p a) => Forall (p :: k -> Constraint) |
Instance details Defined in Data.Constraint.Forall

## Spawning

async :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m (Async a) Source #

Generalized version of `async`.

asyncBound :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m (Async a) Source #

Generalized version of `asyncBound`.

asyncOn :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> m a -> m (Async a) Source #

Generalized version of `asyncOn`.

asyncWithUnmask :: (MonadBaseControl IO m, Forall (Pure m)) => ((forall b. m b -> m b) -> m a) -> m (Async a) Source #

Generalized version of `asyncWithUnmask`.

asyncOnWithUnmask :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> ((forall b. m b -> m b) -> m a) -> m (Async a) Source #

Generalized version of `asyncOnWithUnmask`.

## Spawning with automatic `cancel`ation

withAsync :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> (Async a -> m b) -> m b Source #

Generalized version of `withAsync`.

withAsyncBound :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> (Async a -> m b) -> m b Source #

Generalized version of `withAsyncBound`.

withAsyncOn :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> m a -> (Async a -> m b) -> m b Source #

Generalized version of `withAsyncOn`.

withAsyncWithUnmask :: (MonadBaseControl IO m, Forall (Pure m)) => ((forall c. m c -> m c) -> m a) -> (Async a -> m b) -> m b Source #

Generalized version of `withAsyncWithUnmask`.

withAsyncOnWithUnmask :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> ((forall c. m c -> m c) -> m a) -> (Async a -> m b) -> m b Source #

Generalized version of `withAsyncOnWithUnmask`.

## Quering `Async`s

wait :: (MonadBase IO m, Forall (Pure m)) => Async a -> m a Source #

Generalized version of `wait`.

poll :: (MonadBase IO m, Forall (Pure m)) => Async a -> m (Maybe (Either SomeException a)) Source #

Generalized version of `poll`.

waitCatch :: (MonadBase IO m, Forall (Pure m)) => Async a -> m (Either SomeException a) Source #

Generalized version of `waitCatch`.

cancel :: MonadBase IO m => Async a -> m () Source #

Generalized version of `cancel`.

uninterruptibleCancel :: MonadBase IO m => Async a -> m () Source #

Generalized version of `uninterruptibleCancel`.

cancelWith :: (MonadBase IO m, Exception e) => Async a -> e -> m () Source #

Generalized version of `cancelWith`.

asyncThreadId :: Async a -> ThreadId Source #

Returns the `ThreadId` of the thread running the given `Async`.

data AsyncCancelled Source #

The exception thrown by `cancel` to terminate a thread.

Constructors

AsyncCancelled |

#### Instances

Instances details

Exception AsyncCancelled |
Instance details Defined in Control.Concurrent.Async.Internal Methods toException :: AsyncCancelled -> SomeException Source # fromException :: SomeException -> Maybe AsyncCancelled Source # displayException :: AsyncCancelled -> String Source # backtraceDesired :: AsyncCancelled -> Bool Source #
Show AsyncCancelled |
Instance details Defined in Control.Concurrent.Async.Internal Methods showsPrec :: Int -> AsyncCancelled -> ShowS Source # show :: AsyncCancelled -> String Source # showList :: [AsyncCancelled] -> ShowS Source #
Eq AsyncCancelled |
Instance details Defined in Control.Concurrent.Async.Internal Methods (==) :: AsyncCancelled -> AsyncCancelled -> Bool Source # (/=) :: AsyncCancelled -> AsyncCancelled -> Bool Source #

## STM operations

waitSTM :: Async a -> STM a Source #

A version of `wait` that can be used inside an STM transaction.

pollSTM :: Async a -> STM (Maybe (Either SomeException a)) Source #

A version of `poll` that can be used inside an STM transaction.

waitCatchSTM :: Async a -> STM (Either SomeException a) Source #

A version of `waitCatch` that can be used inside an STM transaction.

## Waiting for multiple `Async`s

waitAny :: (MonadBase IO m, Forall (Pure m)) => [Async a] -> m (Async a, a) Source #

Generalized version of `waitAny`.

waitAnyCatch :: (MonadBase IO m, Forall (Pure m)) => [Async a] -> m (Async a, Either SomeException a) Source #

Generalized version of `waitAnyCatch`.

waitAnyCancel :: (MonadBase IO m, Forall (Pure m)) => [Async a] -> m (Async a, a) Source #

Generalized version of `waitAnyCancel`.

waitAnyCatchCancel :: (MonadBase IO m, Forall (Pure m)) => [Async a] -> m (Async a, Either SomeException a) Source #

Generalized version of `waitAnyCatchCancel`.

waitEither :: (MonadBase IO m, Forall (Pure m)) => Async a -> Async b -> m (Either a b) Source #

Generalized version of `waitEither`.

waitEitherCatch :: (MonadBase IO m, Forall (Pure m)) => Async a -> Async b -> m (Either (Either SomeException a) (Either SomeException b)) Source #

Generalized version of `waitEitherCatch`.

waitEitherCancel :: (MonadBase IO m, Forall (Pure m)) => Async a -> Async b -> m (Either a b) Source #

Generalized version of `waitEitherCancel`.

waitEitherCatchCancel :: (MonadBase IO m, Forall (Pure m)) => Async a -> Async b -> m (Either (Either SomeException a) (Either SomeException b)) Source #

Generalized version of `waitEitherCatchCancel`.

waitEither_ :: MonadBase IO m => Async a -> Async b -> m () Source #

Generalized version of `waitEither_`

waitBoth :: (MonadBase IO m, Forall (Pure m)) => Async a -> Async b -> m (a, b) Source #

Generalized version of `waitBoth`.

## Waiting for multiple `Async`s in STM

waitAnySTM :: [Async a] -> STM (Async a, a) Source #

A version of `waitAny` that can be used inside an STM transaction.

Since: async-2.1.0

waitAnyCatchSTM :: [Async a] -> STM (Async a, Either SomeException a) Source #

A version of `waitAnyCatch` that can be used inside an STM transaction.

Since: async-2.1.0

waitEitherSTM :: Async a -> Async b -> STM (Either a b) Source #

A version of `waitEither` that can be used inside an STM transaction.

Since: async-2.1.0

waitEitherCatchSTM :: Async a -> Async b -> STM (Either (Either SomeException a) (Either SomeException b)) Source #

A version of `waitEitherCatch` that can be used inside an STM transaction.

Since: async-2.1.0

waitEitherSTM_ :: Async a -> Async b -> STM () Source #

A version of `waitEither_` that can be used inside an STM transaction.

Since: async-2.1.0

waitBothSTM :: Async a -> Async b -> STM (a, b) Source #

A version of `waitBoth` that can be used inside an STM transaction.

Since: async-2.1.0

## Linking

link :: MonadBase IO m => Async a -> m () Source #

Generalized version of `link`.

link2 :: MonadBase IO m => Async a -> Async b -> m () Source #

Generalized version of `link2`.

data ExceptionInLinkedThread Source #

Constructors

ExceptionInLinkedThread (Async a) SomeException |

#### Instances

Instances details

Exception ExceptionInLinkedThread |
Instance details Defined in Control.Concurrent.Async.Internal Methods toException :: ExceptionInLinkedThread -> SomeException Source # fromException :: SomeException -> Maybe ExceptionInLinkedThread Source # displayException :: ExceptionInLinkedThread -> String Source # backtraceDesired :: ExceptionInLinkedThread -> Bool Source #
Show ExceptionInLinkedThread |
Instance details Defined in Control.Concurrent.Async.Internal Methods showsPrec :: Int -> ExceptionInLinkedThread -> ShowS Source # show :: ExceptionInLinkedThread -> String Source # showList :: [ExceptionInLinkedThread] -> ShowS Source #

# Convenient utilities

race :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m b -> m (Either a b) Source #

Generalized version of `race`.

race_ :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m b -> m () Source #

Generalized version of `race_`.

concurrently :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m b -> m (a, b) Source #

Generalized version of `concurrently`.

concurrently_ :: (MonadBaseControl IO m, Forall (Pure m)) => m a -> m b -> m () Source #

Generalized version of `concurrently_`.

mapConcurrently :: (Traversable t, MonadBaseControl IO m, Forall (Pure m)) => (a -> m b) -> t a -> m (t b) Source #

Generalized version of `mapConcurrently`.

mapConcurrently_ :: (Foldable t, MonadBaseControl IO m, Forall (Pure m)) => (a -> m b) -> t a -> m () Source #

Generalized version of `mapConcurrently_`.

forConcurrently :: (Traversable t, MonadBaseControl IO m, Forall (Pure m)) => t a -> (a -> m b) -> m (t b) Source #

Generalized version of `forConcurrently`.

forConcurrently_ :: (Foldable t, MonadBaseControl IO m, Forall (Pure m)) => t a -> (a -> m b) -> m () Source #

Generalized version of `forConcurrently_`.

replicateConcurrently :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> m a -> m [a] Source #

Generalized version of `replicateConcurrently`.

replicateConcurrently_ :: (MonadBaseControl IO m, Forall (Pure m)) => Int -> m a -> m () Source #

Generalized version of `replicateConcurrently_`.

data Concurrently (m :: Type -> Type) a where Source #

Generalized version of `Concurrently`.

A value of type ``Concurrently` m a` is an IO-based operation that can be composed with other `Concurrently` values, using the `Applicative` and `Alternative` instances.

Calling `runConcurrently` on a value of type ``Concurrently` m a` will execute the IO-based lifted operations it contains concurrently, before delivering the result of type `a`.

For example

      (page1, page2, page3) <- runConcurrently $ (,,)
        <$> Concurrently (getURL "url1")
        <*> Concurrently (getURL "url2")
        <*> Concurrently (getURL "url3")

Constructors

Concurrently |

Fields

- :: forall (m :: Type -> Type) a. Forall (Pure m)
- => { runConcurrently :: m a
- } -> Concurrently m a

#### Instances

Instances details

(MonadBaseControl IO m, Forall (Pure m)) => Alternative (Concurrently m) Source # |
Instance details Defined in Control.Concurrent.Async.Lifted.Safe Methods empty :: Concurrently m a Source # (<|>) :: Concurrently m a -> Concurrently m a -> Concurrently m a Source # some :: Concurrently m a -> Concurrently m [a] Source # many :: Concurrently m a -> Concurrently m [a] Source #
(MonadBaseControl IO m, Forall (Pure m)) => Applicative (Concurrently m) Source # |

Instance details

Defined in Control.Concurrent.Async.Lifted.Safe

Methods

pure :: a -> Concurrently m a Source #

(<*>) :: Concurrently m (a -> b) -> Concurrently m a -> Concurrently m b Source #

liftA2 :: (a -> b -> c) -> Concurrently m a -> Concurrently m b -> Concurrently m c Source #

(*>) :: Concurrently m a -> Concurrently m b -> Concurrently m b Source #

(<*) :: Concurrently m a -> Concurrently m b -> Concurrently m a Source #

Functor m => Functor (Concurrently m) Source # |
Instance details Defined in Control.Concurrent.Async.Lifted.Safe Methods fmap :: (a -> b) -> Concurrently m a -> Concurrently m b Source # (<$) :: a -> Concurrently m b -> Concurrently m a Source #
(MonadBaseControl IO m, Semigroup a, Monoid a, Forall (Pure m)) => Monoid (Concurrently m a) Source # |
Instance details Defined in Control.Concurrent.Async.Lifted.Safe Methods mempty :: Concurrently m a Source # mappend :: Concurrently m a -> Concurrently m a -> Concurrently m a Source # mconcat :: [Concurrently m a] -> Concurrently m a Source #
(MonadBaseControl IO m, Semigroup a, Forall (Pure m)) => Semigroup (Concurrently m a) Source # |
Instance details Defined in Control.Concurrent.Async.Lifted.Safe Methods (<>) :: Concurrently m a -> Concurrently m a -> Concurrently m a Source # sconcat :: NonEmpty (Concurrently m a) -> Concurrently m a Source # stimes :: Integral b => b -> Concurrently m a -> Concurrently m a Source #

compareAsyncs :: Async a -> Async b -> Ordering Source #

Compare two Asyncs that may have different types by their `ThreadId`.