# Semaphore

`import { Semaphore } from "destack:sync";`

Counting semaphore.

```ds title="Semaphore"
export class Semaphore {
    state: Atomic<isize>;
    constructor(permits: usize);
    get availablePermits(): usize;
    acquire(): SemaphorePermit;
    tryAcquire(): SemaphorePermit | undefined;
    acquireMany(count: usize): SemaphorePermit;
    tryAcquireMany(count: usize): SemaphorePermit | undefined;
    release(count?: usize): void;
}
```

[language/library/src/sync/semaphore.ds:7:45](https://github.com/destack-sh/destack/blob/main/language/library/src/sync/semaphore.ds#L7-L45)

## Members

### `state: Atomic<isize>`

The available permit count.

### `constructor(permits: usize)`

Create a semaphore with `permits` available permits.

### `get availablePermits(): usize`

Return the number of immediately available permits.

### `acquire(): SemaphorePermit`

Acquire one permit, blocking the current Worker if needed.

### `tryAcquire(): SemaphorePermit | undefined`

Try to acquire one permit without blocking.

### `acquireMany(count: usize): SemaphorePermit`

Acquire `count` permits, blocking the current Worker if needed.

### `tryAcquireMany(count: usize): SemaphorePermit | undefined`

Try to acquire `count` permits without blocking.

### `release(count?: usize): void`

Release `count` permits.
