| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
System.Semaphore
Synopsis
- data ClientSemaphore
- data ServerSemaphore
- data SemaphoreName = SemaphoreName {}
- clientSemaphoreName :: ClientSemaphore -> SemaphoreName
- semaphoreIdentifier :: SemaphoreName -> SemaphoreIdentifier
- serverClientSemaphore :: ServerSemaphore -> ClientSemaphore
- createSemaphore :: String -> Int -> IO (Either SemaphoreError ServerSemaphore)
- freshSemaphore :: String -> Int -> IO (Either SemaphoreError ServerSemaphore)
- data SemaphoreToken
- openSemaphore :: SemaphoreIdentifier -> IO (Either SemaphoreError ClientSemaphore)
- type SemaphoreIdentifier = String
- parseSemaphoreIdentifier :: SemaphoreIdentifier -> Maybe SemaphoreName
- newtype SemaphoreProtocolVersion = SemaphoreProtocolVersion {}
- semaphoreVersion :: SemaphoreProtocolVersion
- versionsAreCompatible :: SemaphoreProtocolVersion -> SemaphoreProtocolVersion -> Bool
- data SemaphoreError
- = SemaphoreAlreadyExists { }
- | SemaphoreDoesNotExist { }
- | SemaphoreIncompatibleVersion { }
- | SemaphoreOtherError { }
- waitOnSemaphore :: HasCallStack => ClientSemaphore -> IO SemaphoreToken
- tryWaitOnSemaphore :: HasCallStack => ClientSemaphore -> IO (Maybe SemaphoreToken)
- withSemaphoreToken :: ClientSemaphore -> (SemaphoreToken -> IO a) -> IO a
- getSemaphoreValue :: ServerSemaphore -> IO Int
- releaseSemaphoreToken :: HasCallStack => SemaphoreToken -> IO ()
- destroyClientSemaphore :: ClientSemaphore -> IO ()
- destroyServerSemaphore :: ServerSemaphore -> IO ()
- data AbstractSem = AbstractSem {
- acquireSem :: IO ()
- releaseSem :: IO ()
- withAbstractSem :: AbstractSem -> IO b -> IO b
System semaphores
data ClientSemaphore Source #
A semaphore identity (name + socket path). Each operation that needs a connection opens one internally.
data ServerSemaphore Source #
A server-side semaphore (owns the server thread, listen socket, and token pool).
data SemaphoreName Source #
A semaphore name: a protocol version and an unversioned name string.
Constructors
| SemaphoreName | |
Instances
| Show SemaphoreName Source # | |
Defined in System.Semaphore.Internal.Common Methods showsPrec :: Int -> SemaphoreName -> ShowS # show :: SemaphoreName -> String # showList :: [SemaphoreName] -> ShowS # | |
| Eq SemaphoreName Source # | |
Defined in System.Semaphore.Internal.Common Methods (==) :: SemaphoreName -> SemaphoreName -> Bool # (/=) :: SemaphoreName -> SemaphoreName -> Bool # | |
The name of a client semaphore
semaphoreIdentifier :: SemaphoreName -> SemaphoreIdentifier Source #
The serialised identifier of a SemaphoreName for transport
between processes.
For version 1 this is the bare name; for version N (N >= 2) the
name is prefixed with v<N>-.
Retrieve the client semaphore corresponding to a server semaphore
Creating a semaphore
Arguments
| :: String | label |
| -> Int | number of tokens on the semaphore |
| -> IO (Either SemaphoreError ServerSemaphore) |
Create a new semaphore with the given label and initial token count.
On POSIX, crash recovery is automatic: disconnected clients' tokens are returned to the pool. On Windows, tokens held by a crashed client are permanently lost.
Arguments
| :: String | label prefix |
| -> Int | number of tokens on the semaphore |
| -> IO (Either SemaphoreError ServerSemaphore) |
Create a fresh semaphore with a unique name and the given token count.
The name is derived from the given prefix with a random suffix.
Opening a semaphore
data SemaphoreToken Source #
A held semaphore token, bound to one acquired resource.
If all references to the SemaphoreToken are dropped without being
released, a finalizer closes the underlying connection and the server
returns the token to the pool. Use releaseSemaphoreToken or
withSemaphoreToken for prompt release rather than
relying on GC timing.
The fd is held in an internal MVar so releaseSemaphoreToken takes
ownership atomically: a second (erroneous) release is a safe no-op.
openSemaphore :: SemaphoreIdentifier -> IO (Either SemaphoreError ClientSemaphore) Source #
Open a semaphore from its SemaphoreIdentifier.
The identifier should normally begin with a version prefix v<N>-.
An unversioned identifier is treated as v1 for backwards
compatibility. Returns Left SemaphoreIncompatibleVersion if the
identifier's protocol version is not compatible with this build of
semaphore-compat.
type SemaphoreIdentifier = String Source #
The identifier string of a semaphore, as serialised for transport
between processes (e.g. on a command line via -jsem).
For version 1 this is a bare name; for version N (with N >= 2)
this is "v<N>-<name>".
parseSemaphoreIdentifier :: SemaphoreIdentifier -> Maybe SemaphoreName Source #
Parse a SemaphoreIdentifier into a SemaphoreName.
Returns Nothing for unversioned strings (which should be treated as
v1 by the caller's compatibility logic).
newtype SemaphoreProtocolVersion Source #
The protocol version of a semaphore.
Constructors
| SemaphoreProtocolVersion | |
Fields | |
Instances
semaphoreVersion :: SemaphoreProtocolVersion Source #
The protocol version on this platform.
The version tracks the IPC mechanism, not the library version:
- POSIX: 2 (domain sockets, replacing v1 system semaphores).
- Windows: 1 (Win32 named semaphores, unchanged from v1).
- Unsupported platforms: 0 (no compatible IPC backend).
Because the version is 1 on Windows, semaphoreIdentifier produces
a bare name (no v<N>- prefix), matching the v1 format.
versionsAreCompatible :: SemaphoreProtocolVersion -> SemaphoreProtocolVersion -> Bool Source #
Check whether two semaphore protocol versions are compatible. Only identical versions are compatible.
data SemaphoreError Source #
Errors that can occur when creating or opening a semaphore.
Constructors
| SemaphoreAlreadyExists | |
Fields | |
| SemaphoreDoesNotExist | |
Fields | |
| SemaphoreIncompatibleVersion | |
| SemaphoreOtherError | |
Fields | |
Instances
| Exception SemaphoreError Source # | |
Defined in System.Semaphore.Internal.Common Methods toException :: SemaphoreError -> SomeException # fromException :: SomeException -> Maybe SemaphoreError # displayException :: SemaphoreError -> String # backtraceDesired :: SemaphoreError -> Bool # | |
| Show SemaphoreError Source # | |
Defined in System.Semaphore.Internal.Common Methods showsPrec :: Int -> SemaphoreError -> ShowS # show :: SemaphoreError -> String # showList :: [SemaphoreError] -> ShowS # | |
| Eq SemaphoreError Source # | |
Defined in System.Semaphore.Internal.Common Methods (==) :: SemaphoreError -> SemaphoreError -> Bool # (/=) :: SemaphoreError -> SemaphoreError -> Bool # | |
Requesting a token
waitOnSemaphore :: HasCallStack => ClientSemaphore -> IO SemaphoreToken Source #
Acquire a token from the semaphore, blocking until one is available.
This operation is interruptible: it can be cancelled by
throwTo, killThread, etc. If
interrupted, any transiently acquired token is automatically returned to the
pool.
For prompt and predictable release of resources, callers should use
withSemaphoreToken or releaseSemaphoreToken.
tryWaitOnSemaphore :: HasCallStack => ClientSemaphore -> IO (Maybe SemaphoreToken) Source #
Try to acquire a token from the semaphore without blocking.
Returns Just token if a token was available, Nothing otherwise.
Not interruptible, but this shouldn't block for long as the server is supposed to respond immediately.
withSemaphoreToken :: ClientSemaphore -> (SemaphoreToken -> IO a) -> IO a Source #
Acquire a token, run an action, then release the token. Exception safe.
getSemaphoreValue :: ServerSemaphore -> 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.
Releasing resources
releaseSemaphoreToken :: HasCallStack => SemaphoreToken -> IO () Source #
Release a semaphore token, returning it to the pool.
Sends a release command on the token's connection, then closes it. Idempotent: a second call on the same token is a safe no-op.
Not interruptible; only returns when the release has succeeded.
destroyClientSemaphore :: ClientSemaphore -> IO () Source #
Destroy a client-side semaphore.
On POSIX this is a no-op: ClientSemaphore holds no live connection.
destroyServerSemaphore :: ServerSemaphore -> IO () Source #
Destroy a server-side semaphore.
Idempotent. Subsequent calls after the first one are no-ops Not interruptible. Only returns when the server and all resources have been cleaned up
Abstract semaphores
data AbstractSem Source #
Abstraction over the operations of a semaphore.
Constructors
| AbstractSem | |
Fields
| |
withAbstractSem :: AbstractSem -> IO b -> IO b Source #