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.
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).
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.
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.
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.
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.
Cancel multiple asynchronous actions by throwing the AsyncCancelled
exception to each of them in turn, then waiting for all the Async threads
to complete.
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.
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.
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.
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.
link ignores AsyncCancelled exceptions thrown in the other thread,
so that it's safe to cancel a thread you're linked to. If you want
different behaviour, use linkOnly.
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.
Link two Asyncs together, such that if either raises an
exception, the same exception is re-thrown in the other Async,
wrapped in ExceptionInLinkedThread.
link2 ignores AsyncCancelled exceptions, so that it's possible
to cancel either thread without cancelling the other. If you
want different behaviour, use link2Only.
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.
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
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.
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.
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).
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.
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.
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.