semaphore-compat-1.0.0: Cross-platform abstraction for system semaphores
Safe HaskellNone
LanguageHaskell2010

System.Semaphore

Synopsis

System semaphores

data Semaphore Source #

A system semaphore (POSIX or Win32).

newtype SemaphoreName Source #

Constructors

SemaphoreName 

Instances

Instances details
Eq SemaphoreName Source # 
Instance details

Defined in System.Semaphore

createSemaphore Source #

Arguments

:: SemaphoreName 
-> Int

number of tokens on the semaphore

-> IO Semaphore 

Create a new semaphore with the given name and initial amount of available resources.

Throws an error if a semaphore by this name already exists.

freshSemaphore Source #

Arguments

:: String

prefix

-> Int

number of tokens on the semaphore

-> IO Semaphore 

Create a fresh semaphore with the given amount of tokens.

Its name will start with the given prefix, but will have a random suffix appended to it.

openSemaphore :: SemaphoreName -> IO Semaphore Source #

Open a semaphore with the given name.

If no such semaphore exists, throws an error.

waitOnSemaphore :: Semaphore -> IO () Source #

Indefinitely wait on a semaphore.

If you want to be able to cancel a wait operation, use forkWaitOnSemaphoreInterruptible instead.

tryWaitOnSemaphore :: Semaphore -> IO Bool Source #

Try to obtain a token from the semaphore, without blocking.

Immediately returns False if no resources are available.

data WaitId Source #

WaitId stores the information we need to cancel a thread which is waiting on a semaphore.

See forkWaitOnSemaphoreInterruptible and interruptWaitOnSemaphore.

Constructors

WaitId 

forkWaitOnSemaphoreInterruptible Source #

Arguments

:: Semaphore 
-> (Either SomeException Bool -> IO ())

wait result action

-> IO WaitId 

Spawn a thread that waits on the given semaphore.

In this thread, asynchronous exceptions will be masked.

The waiting operation can be interrupted using the interruptWaitOnSemaphore function.

This implements a similar pattern to the forkFinally function.

interruptWaitOnSemaphore :: WaitId -> IO () Source #

Interrupt a semaphore wait operation initiated by forkWaitOnSemaphoreInterruptible.

getSemaphoreValue :: Semaphore -> IO Int Source #

Query the current semaphore value (how many tokens it has available).

This is mainly for debugging use, as it is easy to introduce race conditions when nontrivial program logic depends on the value returned by this function.

releaseSemaphore :: Semaphore -> Int -> IO () Source #

Release a semaphore: add n to its internal counter.

No-op when `n <= 0`.

destroySemaphore :: Semaphore -> IO () Source #

Destroy the given semaphore.

Abstract semaphores

data AbstractSem Source #

Abstraction over the operations of a semaphore.

Constructors

AbstractSem 

Fields