HORIZON HASKELLDocslts/ghc-10.0.x8174fa42026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8174fa4 · 2026-10-11

Modulesemaphore-compat-2.0.1Haskell2010

System.Semaphore

This library provides a cross-platform implementation of semaphores.

Its main role is to provide a cross-platform notion of semaphore that can be used to implement the jobserver/jobclient model of GHC proposal #540.

Typical usage:

NB: the above usage outline deliberately omits any mention of implicit semaphore tokens: such a notion does not exist in this library, as they are purely an abstraction layer used by the jobserver implementation. See destroyClientSemaphore for more details.

  • 8 types
  • 17 values

System semaphores

7 declarations

Since version 2 of semaphore-compat, the library distinguishes between two kinds of semaphores:

When the jobserver wants to also act as a jobclient, it can use serverClientSemaphore to obtain the ClientSemaphore corresponding to its ServerSemaphore.

This architecture allows the jobserver to keep full accounting of semaphore resources held by all clients.

newtypenewtype ClientSemaphore
#

A client-side semaphore: the handle a jobclient uses to acquire and release tokens from a semaphore created by a jobserver.

Retrieve the underlying name with clientSemaphoreName.

The identifier string of a semaphore: a serialised SemaphoreName used for inter-process communication (e.g. as a command line argument).

The specific format of this name string depends on the protocol version:

  • For version 1, this is the unadorned semaphore name.

  • For version N >= 2, the semaphore name is prefixed with v<N>-.

Creating a semaphore

Opening a semaphore

newtypenewtype SemaphoreToken
#

A held semaphore token: evidence of one unit of resource acquired from a semaphore.

Use releaseSemaphoreToken or withSemaphoreToken to ensure prompt release of semaphore tokens.

Platform-dependent behaviour:

  • On POSIX, if all references to a SemaphoreToken are dropped without being released, a finalizer returns the token to the semaphore.

  • On Windows, tokens held by a crashed client are permanently lost.

newtypenewtype SemaphoreProtocolVersion
#

The semaphore protocol version currently being used.

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

Instances3Eq, Ord, Show
datadata SemaphoreError
#

Errors that can occur when creating or opening a semaphore.

Constructors

Instances3Eq, Show, Exception

Requesting a token

Acquire a token from the semaphore, blocking until one is available.

The returned SemaphoreToken must be released with releaseSemaphoreToken. For prompt and predictable release of resources, callers should use withSemaphoreToken or releaseSemaphoreToken.

This operation is interruptible: it can be cancelled by Control.Concurrent.throwTo, killThread, etc. If interrupted, any transiently acquired token is automatically returned.

Try to acquire a token from the semaphore without blocking.

Returns Just token if a token was available, Nothing otherwise.

This is not an interruptible operation, but that should not be a problem: this function is not expected to block for long, as the server is supposed to respond immediately.

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

The GHC jobserver protocol described in GHC proposal #540 specifies the notion of implicit semaphore tokens.

  1. To invoke a jobclient, the jobserver must be in possession of at least one semaphore token. We consider that the jobserver passes on ownership of this token to the jobclient, even though no interaction with the semaphore takes place. This is the implicit token.

  2. Each jobclient thus starts with one available token (the implicit token), and can request more tokens by waiting on the semaphore.

A jobclient calling destroyClientSemaphore signals to the jobserver that the client is done doing work. The jobserver thus regains control of the implicit token, which it can now use to spawn other jobclients.

Implicit tokens are purely an accounting abstraction that we superimpose on top of semaphore token ownership for the purposes of implementing the jobserver protocol. They are not part of this library.

Abstract semaphores

2 declarations
valuewithAbstractSem :: AbstractSem -> IO b -> IO b
#

Acquire/release bracket pattern: acquire a token, perform an action, and release the token.

Guarantees that the token is released in case that the inner action throws an exception.