Control.Concurrent.SSem

SafeSemaphore-0.10.1: Much safer replacement for QSemN, QSem, and SampleVar

Copyright | (c) Chris Kuklewicz, 2012
License | BSD-style
Maintainer | haskell@list.mightyreason.com
Stability | experimental
Portability | non-portable (concurrency)
Safe Haskell | Safe
Language | Haskell98

Control.Concurrent.SSem

Description

Very simple quantity semaphore.

Synopsis

- data SSem
- new :: Int -> IO SSem
- withSem :: SSem -> IO a -> IO a
- wait :: SSem -> IO ()
- signal :: SSem -> IO ()
- tryWait :: SSem -> IO (Maybe Int)
- withSemN :: SSem -> Int -> IO a -> IO a
- waitN :: SSem -> Int -> IO ()
- signalN :: SSem -> Int -> IO ()
- tryWaitN :: SSem -> Int -> IO (Maybe Int)
- getValue :: SSem -> IO Int

# Documentation

data SSem

Instances

Eq SSem |

new :: Int -> IO SSem

Create a new semaphore with the given argument as the initially available quantity. This allows new semaphores to start with a negative, zero, or positive quantity.

withSem :: SSem -> IO a -> IO a

It is recommended that all paired uses of `wait` and `signal` use the `with` bracketed form to ensure exceptions safety.

wait :: SSem -> IO ()

Try to take a unit of value from the semaphore. This succeeds when the current quantity is positive, and then reduces the quantity by one. Otherwise this will block and `retry` until it succeeds or is killed. This will never result in a negative quantity. If several threads are retying then which one succeeds next is undefined -- an unlucky thread might starve.

signal :: SSem -> IO ()

Signal that single unit of the semaphore is available. This increases the available quantity by one.

tryWait :: SSem -> IO (Maybe Int)

Non-waiting version of wait. `tryWait s` is defined as `tryWaitN s 1`

withSemN :: SSem -> Int -> IO a -> IO a

It is recommended that all paired uses of `waitN` and `signalN` use the `withN` bracketed form to ensure exceptions safety.

waitN :: SSem -> Int -> IO ()

Try to take the given value from the semaphore. This succeeds when the quantity is greater or equal to the given value, and then subtracts the given value from the quantity. Otherwise this will block and `retry` until it succeeds or is killed. This will never result in a negative quantity. If several threads are retrying then which one succeeds next is undefined -- an unlucky thread might starve.

signalN :: SSem -> Int -> IO ()

Signal that many units of the semaphore are available. This changes the available quantity by adding the passed size.

tryWaitN :: SSem -> Int -> IO (Maybe Int)

Non-waiting version of waitN. It either takes the quantity from the semaphore like waitN and returns `Just value taken` or finds insufficient quantity to take and returns Nothing

getValue :: SSem -> IO Int

This returns the current quantity in the semaphore. This is diffucult to use due to race conditions.