Skip to content

l0.DependencyControl.NamedSemaphore

NamedSemaphore = require "l0.DependencyControl.NamedSemaphore"
local NamedSemaphore = require("l0.DependencyControl.NamedSemaphore")

NamedSemaphore

A non-reentrant binary semaphore identified by a name. Usable as a per-process or cross-process lock primitive.

Constructor

namedSemaphore = NamedSemaphore token, unlinkOnClose
local namedSemaphore = NamedSemaphore(token, unlinkOnClose)

Gets a handle to the named semaphore for the given token, creating it if it doesn't exist. this instance is collected — that would break a name another holder still owns). Use true for process-private names so a reused PID can't inherit a stale semaphore. Use false for cross-process usage to prevent an exiting process from removing a name others still hold. No effect on Windows, where names are cleaned up automatically when the last handle closes.

param type description
token string A name token restricted to [A-Za-z0-9_].
unlinkOnClose? boolean POSIX-only: remove the OS name when this Lua state tears down (not when

Instance methods

tryLock

namedSemaphore\tryLock!
namedSemaphore:tryLock()

Attempts to acquire without blocking.

Returns:

  • acquired boolean — True if the semaphore was acquired.

lock

namedSemaphore\lock!
namedSemaphore:lock()

Blocks until the semaphore is acquired.

Returns:

  • issued boolean — True if a wait was issued (false only when unavailable).

unlock

namedSemaphore\unlock!
namedSemaphore:unlock()

Releases one unit of the semaphore. Safe to call only by the current holder.

Returns:

  • issued boolean — True if a release was issued.

Fields

field type description
isAvailable boolean
pid integer