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

System.Semaphore.Internal.Posix

Synopsis

Documentation

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

Constructors

ServerSemaphore 

Fields

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

Constructors

SemaphoreToken 

Fields

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.

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

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.