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

System.Semaphore

Synopsis

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.

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

createSemaphore Source #

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.

freshSemaphore Source #

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).

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.

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