immortal-0.3: Spawn threads that never die (unless told to do so)

Control.Immortal

Description

This module is designed to be imported qualified, e.g.

import qualified Control.Immortal as Immortal

Synopsis

# Documentation

Immortal thread identifier (including its underlying ThreadId)

Spawn a new immortal thread running the given computation.

If the computation ever finishes (either normally or due to an exception), it will be restarted (in the same thread).

Like create, but also apply the given label to the thread (using labelThread).

mortalize :: Thread -> IO () Source #

Make a thread mortal. Next time a mortal thread attempts to finish, nothing will prevent it from doing so.

If a thread was mortalized, this will make it immortal again. However, if it finished while being in the mortal state, it won't be resurrected.

Calling this on an immortal thread has no effect.

stop :: Thread -> IO () Source #

This is equivalent to making it mortal, and then killing it with an exception.

Note that if the thread has installed its own exception handlers, it may not be killed immediately.

wait :: Thread -> IO () Source #

Wait for the thread to stop. Use stop to stop the thread

waitSTM :: Thread -> STM () Source #

An STM version of wait

Get the ThreadId of the immortal thread.

The ThreadId can be used to throw asynchronous exception to interrupt the computation. This won't kill the thread, however — even if the exception is not handled, the computation will be simply restarted.

onFinish :: MonadUnliftIO m => (Either SomeException () -> m ()) -> m () -> m () Source #

Run a callback every time the action finishes. This can be used e.g. to log exceptions or attempts to exit when such attempts are not expected. Example usage:

Immortal.create \$ \_ -> Immortal.onFinish print myAction

This is nothing more than a simple wrapper around try.

onUnexpectedFinish :: MonadUnliftIO m => Thread -> (Either SomeException () -> m ()) -> m () -> m () Source #

Like onFinish, but the callback does not run when the thread is mortalized (i.e. when the exit is expected).

The Thread argument is used to find out the mortality of the thread.