semaphore-compat-2.0.1: Cross-platform abstraction for system semaphores
Safe HaskellSafe-Inferred
LanguageHaskell2010

System.Semaphore.Internal.Posix.Server

Description

The Unix domain socket server backing the POSIX semaphore implementation.

The server manages a shared token pool and accepts multiple client connections, each served on its own thread.

Protocol (SOCK_STREAM, one byte per command):

"-" Wait (blocking acquire). Decrements semaphore; replies ".". "?" Try-wait. Decrements if positive and replies "."; otherwise replies "!". "+" Release. Increments pool; replies ".". Rejected with "!" if myCount <= 0 (client has not acquired any tokens on this connection).

Unrecognised bytes are rejected with RspFail.

Per-connection token tracking: the server counts tokens held by each connection (myCount). On disconnect (EOF / ResourceVanished) held tokens are returned to the pool, so a crashing client cannot leak tokens.

Connections are tied to a token, so each token (obtained by waitOnSemaphore) has its own connection.

Synopsis

Documentation

serverLoop Source #

Arguments

:: TVar Int

shared token pool

-> Fd

listening socket

-> Fd

read end of the cancel pipe

-> IO () 

Run the server accept loop on a listening socket until cancellation is signalled via the cancel pipe. All accepted connections are served on child threads, which are cleaned up before this returns.

pattern CmdWait :: Word8 Source #

pattern RspOk :: Word8 Source #

pattern RspFail :: Word8 Source #