time-manager-0.3.2: Scalable timer
Safe HaskellNone
LanguageHaskell2010

System.ThreadManager

Description

A thread manager including a time manager. The manager has responsibility to kill managed threads.

Because this is based on the accompanying System.TimeManager module, the same caveats apply:

  • Only works for GHC.
  • Only works with a threaded runtime.
  • Users of older versions should check the current semantics.
  • Using 32-bit systems means the max timeout is maxBound :: Int (2147483647) microseconds, which is less than 36 minutes.
  • Using the same Handle in different threads might cause issues in some edge cases. (i.e. using cancel/pause in one thread, and resume in another)
Synopsis

Documentation

data ThreadManager Source #

Manager to manage the thread and the timer.

newThreadManager :: Manager -> IO ThreadManager Source #

Create a thread manager.

To create a ThreadManager, you will first have to create a Manager from the System.TimeManager module.

You can use either initialize or withManager.

stopAfter :: ThreadManager -> IO a -> (Maybe SomeException -> IO ()) -> IO a Source #

Stopping the manager.

stopAfter threadManager action cleanup

The action is run in the scope of an exception handler that catches all exceptions (including asynchronous ones); this allows the cleanup handler to cleanup in all circumstances. If an exception is caught, it is rethrown after the cleanup is complete.

stopAfterWithResult :: ThreadManager -> IO a -> (Either SomeException a -> IO b) -> IO b Source #

Generalization of stopAfter where the cleanup handler is allowed to construct the final result

Unlike in stopAfter, if an exception is thrown, it is not re-thrown after the cleanup handler completes; the cleanup handler itself can decide to rethrow it or compute a result.

Since: 0.3.2

Fork

forkManaged Source #

Arguments

:: ThreadManager 
-> String

Thread name

-> IO ()

Action

-> IO () 

Fork a managed thread.

This guarantees that the thread ID is added to the manager's queue before the thread starts, and is removed again when the thread terminates (normally or abnormally).

forkManagedFinally Source #

Arguments

:: ThreadManager 
-> String

Thread name

-> IO ()

Action

-> IO ()

Cleanup function

-> IO () 

Fork a managed thread with a cleanup function.

forkManagedUnmask Source #

Arguments

:: ThreadManager 
-> String

Thread name

-> ((forall x. IO x -> IO x) -> IO ())

Action with unmask argument

-> IO () 

Like forkManaged, but run action with exceptions masked

forkManagedTimeout Source #

Arguments

:: ThreadManager 
-> String

Thread name

-> (Handle -> IO ())

Action with timeout handle

-> IO () 

Fork a managed thread with a handle created by a timeout manager.

forkManagedTimeoutFinally Source #

Arguments

:: ThreadManager 
-> String

Thread name

-> (Handle -> IO ())

Action with timeout handle

-> IO ()

Cleanup function

-> IO () 

Fork a managed thread with a handle created by a timeout manager and with a cleanup function.

Synchronization

waitUntilAllGone :: ThreadManager -> IO () Source #

Wait until all managed threads are finished.

isAllGone :: ThreadManager -> STM Bool Source #

STM action that checks if all managed threads are finished.

Re-exports

data Manager Source #

A timeout manager

withHandle :: ThreadManager -> TimeoutAction -> (Handle -> IO a) -> IO a Source #

Registering a TimeoutAction and unregister its Handle when the body action is finished.

data Handle Source #

A handle used by a timeout manager.

tickle :: Handle -> IO () Source #

Extending the timeout.

Careful: this does NOT reactivate an already paused Handle!

pause :: Handle -> IO () Source #

This is identical to cancel. To resume timeout with the same Handle, resume MUST be called. Don't call tickle for resumption.

resume :: Handle -> IO () Source #

Resuming the timeout.

Works like tickle if the Handle wasn't paused or canceled.