HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleasync-2.2.5Haskell2010

Control.Concurrent.Async.Internal

This module is an internal module. The public API is provided in Control.Concurrent.Async. Breaking changes to this module will not be reflected in a major bump, and using this module may break your code unless you are extremely careful.

  • 5 types
  • 61 values
  • Packageasync-2.2.5
  • Exports66
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceInternal.hs
datadata Async a
#

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).

Constructors

Instances4Functor, Eq, Ord, Hashable
  • Functor AsyncDefined in async-2.2.5 · Control.Concurrent.Async.Internal
  • Eq (Async a)Defined in async-2.2.5 · Control.Concurrent.Async.Internal
  • Ord (Async a)Defined in async-2.2.5 · Control.Concurrent.Async.Internal
  • Hashable (Async a)Defined in async-2.2.5 · Control.Concurrent.Async.Internal
valueasync :: IO a -> IO (Async a)
#

Spawn an asynchronous action in a separate thread.

Like for forkIO, the action may be left running unintentionally (see module-level documentation for details).

Use withAsync style functions wherever you can instead!

valuewithAsync :: IO a -> (Async a -> IO b) -> IO b
#

Spawn an asynchronous action in a separate thread, and pass its Async handle to the supplied function. When the function returns or throws an exception, uninterruptibleCancel is called on the Async.

withAsync action inner = mask $ \restore -> do
  a <- async (restore action)
  restore (inner a) `finally` uninterruptibleCancel a

This is a useful variant of async that ensures an Async is never left running unintentionally.

Note: a reference to the child thread is kept alive until the call to withAsync returns, so nesting many withAsync calls requires linear memory.

valuewait :: Async a -> IO a
#

Wait for an asynchronous action to complete, and return its value. If the asynchronous action threw an exception, then the exception is re-thrown by wait.

wait = atomically . waitSTM
valuewaitCatch :: Async a -> IO (Either SomeException a)
#

Wait for an asynchronous action to complete, and return either Left e if the action raised an exception e, or Right a if it returned a value a.

waitCatch = atomically . waitCatchSTM
valuepoll :: Async a -> IO (Maybe (Either SomeException a))
#

Check whether an Async has completed yet. If it has not completed yet, then the result is Nothing, otherwise the result is Just e where e is Left x if the Async raised an exception x, or Right a if it returned a value a.

poll = atomically . pollSTM
valuecancel :: Async a -> IO ()
#

Cancel an asynchronous action by throwing the AsyncCancelled exception to it, and waiting for the Async thread to quit. Has no effect if the Async has already completed.

cancel a = throwTo (asyncThreadId a) AsyncCancelled <* waitCatch a

Note that cancel will not terminate until the thread the Async refers to has terminated. This means that cancel will block for as long said thread blocks when receiving an asynchronous exception.

For example, it could block if:

  • It's executing a foreign call, and thus cannot receive the asynchronous exception;

  • It's executing some cleanup handler after having received the exception, and the handler is blocking.

valuecancelMany :: [Async a] -> IO ()
#

Cancel multiple asynchronous actions by throwing the AsyncCancelled exception to each of them in turn, then waiting for all the Async threads to complete.

valuecancelWith :: Exception e => Async a -> e -> IO ()
#

Cancel an asynchronous action by throwing the supplied exception to it.

cancelWith a x = throwTo (asyncThreadId a) x

The notes about the synchronous nature of cancel also apply to cancelWith.

valuewaitAnyCatch :: [Async a] -> IO (Async a, Either SomeException a)
#

Wait for any of the supplied asynchronous operations to complete. The value returned is a pair of the Async that completed, and the result that would be returned by wait on that Async. The input list must be non-empty.

If multiple Asyncs complete or have completed, then the value returned corresponds to the first completed Async in the list.

valuewaitAny :: [Async a] -> IO (Async a, a)
#

Wait for any of the supplied Asyncs to complete. If the first to complete throws an exception, then that exception is re-thrown by waitAny. The input list must be non-empty.

If multiple Asyncs complete or have completed, then the value returned corresponds to the first completed Async in the list.

valuewaitEither :: Async a -> Async b -> IO (Either a b)
#

Wait for the first of two Asyncs to finish. If the Async that finished first raised an exception, then the exception is re-thrown by waitEither.

valuewaitBoth :: Async a -> Async b -> IO (a, b)
#

Waits for both Asyncs to finish, but if either of them throws an exception before they have both finished, then the exception is re-thrown by waitBoth.

valuelinkOnly
  1. :: (SomeException -> Bool)

    return True if the exception should be propagated, False otherwise.

  2. -> Async a
  3. -> IO ()
#

Link the given Async to the current thread, such that if the Async raises an exception, that exception will be re-thrown in the current thread, wrapped in ExceptionInLinkedThread.

The supplied predicate determines which exceptions in the target thread should be propagated to the source thread.

valuelink2Only :: (SomeException -> Bool) -> Async a -> Async b -> IO ()
#

Link two Asyncs together, such that if either raises an exception, the same exception is re-thrown in the other Async, wrapped in ExceptionInLinkedThread.

The supplied predicate determines which exceptions in the target thread should be propagated to the source thread.

valuerace :: IO a -> IO b -> IO (Either a b)
#

Run two IO actions concurrently, and return the first to finish. The loser of the race is cancelled.

race left right =
  withAsync left $ \a ->
  withAsync right $ \b ->
  waitEither a b
valueconcurrently :: IO a -> IO b -> IO (a, b)
#

Run two IO actions concurrently, and return both results. If either action throws an exception at any time, then the other action is cancelled, and the exception is re-thrown by concurrently.

concurrently left right =
  withAsync left $ \a ->
  withAsync right $ \b ->
  waitBoth a b
valueconcurrentlyE :: IO (Either e a) -> IO (Either e b) -> IO (Either e (a, b))
#

Run two IO actions concurrently. If both of them end with Right, return both results. If one of then ends with Left, interrupt the other action and return the Left.

valuemapConcurrently :: Traversable t => (a -> IO b) -> t a -> IO (t b)
#

Maps an IO-performing function over any Traversable data type, performing all the IO actions concurrently, and returning the original data structure with the arguments replaced by the results.

If any of the actions throw an exception, then all other actions are cancelled and the exception is re-thrown.

For example, mapConcurrently works with lists:

pages <- mapConcurrently getURL ["url1", "url2", "url3"]

Take into account that async will try to immediately spawn a thread for each element of the Traversable, so running this on large inputs without care may lead to resource exhaustion (of memory, file descriptors, or other limited resources).

newtypenewtype Concurrently a
#

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

Calling runConcurrently on a value of type Concurrently a will execute the IO 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")
Instances5Functor, Applicative, Alternative, Semigroup, Monoid
newtypenewtype ConcurrentlyE e a
#

A value of type ConcurrentlyE e a is an IO operation that can be composed with other ConcurrentlyE values, using the Applicative instance.

Calling runConcurrentlyE on a value of type ConcurrentlyE e a will execute the IO operations it contains concurrently, before delivering either the result of type a, or an error of type e if one of the actions returns Left.

| @since 2.2.5

Instances5Bifunctor, Functor, Applicative, Semigroup, Monoid
valueforkRepeat :: IO a -> IO ThreadId
#

Fork a thread that runs the supplied action, and if it raises an exception, re-runs the action. The thread terminates only when the action runs to completion without raising an exception.