System.Semaphore

semaphore-compat-1.0.0: Cross-platform abstraction for system semaphores

Safe Haskell | None
Language | Haskell2010

System.Semaphore

Contents

- System semaphores
- Abstract semaphores

Synopsis

- data Semaphore = Semaphore {
  - semaphoreName :: !SemaphoreName
  - semaphore :: !Semaphore
  }
- newtype SemaphoreName = SemaphoreName {
  - getSemaphoreName :: String
  }
- createSemaphore :: SemaphoreName -> Int -> IO Semaphore
- freshSemaphore :: String -> Int -> IO Semaphore
- openSemaphore :: SemaphoreName -> IO Semaphore
- waitOnSemaphore :: Semaphore -> IO ()
- tryWaitOnSemaphore :: Semaphore -> IO Bool
- data WaitId = WaitId {
  - waitingThreadId :: ThreadId
  }
- forkWaitOnSemaphoreInterruptible :: Semaphore -> (Either SomeException Bool -> IO ()) -> IO WaitId
- interruptWaitOnSemaphore :: WaitId -> IO ()
- getSemaphoreValue :: Semaphore -> IO Int
- releaseSemaphore :: Semaphore -> Int -> IO ()
- destroySemaphore :: Semaphore -> IO ()
- data AbstractSem = AbstractSem {
  - acquireSem :: IO ()
  - releaseSem :: IO ()
  }
- withAbstractSem :: AbstractSem -> IO b -> IO b

# System semaphores

data Semaphore Source #

A system semaphore (POSIX or Win32).

Constructors

Semaphore |
Fields semaphoreName :: !SemaphoreName semaphore :: !Semaphore

newtype SemaphoreName Source #

Constructors

SemaphoreName |
Fields getSemaphoreName :: String

#### Instances

Instances details

Eq SemaphoreName Source # |
Instance details Defined in System.Semaphore Methods (==) :: SemaphoreName -> SemaphoreName -> Bool # (/=) :: SemaphoreName -> SemaphoreName -> Bool #

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 |
Fields waitingThreadId :: ThreadId

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 acquireSem :: IO () releaseSem :: IO ()

withAbstractSem :: AbstractSem -> IO b -> IO b Source #