Skip to content

Semaphore

1 min read

Import: import system.core;

Semaphore

Counting semaphore for limiting concurrent access to a shared resource.

shared public class Semaphore

constructor

Creates a semaphore with initial available permits.

public constructor(initial: int)

host_acquire

Waits until the synchronization object can be acquired.

public static extern fun host_acquire(borrow target: Semaphore): void

host_release

Releases the synchronization object after acquisition.

public static extern fun host_release(borrow target: Semaphore): void

host_try_acquire

Attempts acquisition without waiting; returns whether it succeeded.

public static extern fun host_try_acquire(borrow target: Semaphore): bool

host_try_acquire_for

Attempts acquisition for up to timeout_ms milliseconds.

public static extern fun host_try_acquire_for(borrow target: Semaphore, timeout_ms: int): bool

acquire

Blocks (busy-waiting) until a permit is available, then takes one.

public fun acquire(): void

try_acquire

One-shot acquire: returns true if a permit was taken, false if none were available.

public fun try_acquire(): bool

try_acquire_for

Waits up to timeout_ms milliseconds for a permit. timeout_ms <= 0 is a single try.

public fun try_acquire_for(timeout_ms: int): bool

release

Returns one permit.

public fun release(): void