Foundation.Semaphore.Counting (foundation v0.2.1)

Copy Markdown View Source

ETS-backed counting semaphore.

Summary

Functions

Acquire a semaphore in the default registry.

Acquire a semaphore in the provided registry.

Acquire a semaphore, retrying with backoff until available.

Get the current count for a semaphore.

Return the default registry (anonymous ETS table).

Create a new registry. Use name: :my_table for a named ETS table.

Release a semaphore in the default registry.

Release a semaphore in the provided registry.

Attempt to acquire a semaphore without blocking.

Execute a function while holding a semaphore.

Types

name()

@type name() :: term()

registry()

@type registry() :: :ets.tid() | atom()

Functions

acquire(name, max)

@spec acquire(name(), pos_integer()) :: boolean()

Acquire a semaphore in the default registry.

acquire(registry, name, max)

@spec acquire(registry(), name(), pos_integer()) :: boolean()

Acquire a semaphore in the provided registry.

acquire_blocking(registry, name, max, backoff, opts \\ [])

@spec acquire_blocking(
  registry(),
  name(),
  pos_integer(),
  Foundation.Backoff.Policy.t(),
  keyword()
) ::
  :ok

Acquire a semaphore, retrying with backoff until available.

count(registry, name)

@spec count(registry(), name()) :: non_neg_integer()

Get the current count for a semaphore.

default_registry()

@spec default_registry() :: registry()

Return the default registry (anonymous ETS table).

new_registry(opts \\ [])

@spec new_registry(keyword()) :: registry()

Create a new registry. Use name: :my_table for a named ETS table.

release(name)

@spec release(name()) :: :ok

Release a semaphore in the default registry.

release(registry, name)

@spec release(registry(), name()) :: :ok

Release a semaphore in the provided registry.

try_acquire(registry, name, max)

@spec try_acquire(registry(), name(), pos_integer()) :: boolean()

Attempt to acquire a semaphore without blocking.

with_acquire(registry, name, max, fun)

@spec with_acquire(registry(), name(), pos_integer(), (-> result)) ::
  {:ok, result} | {:error, :max}
when result: any()

Execute a function while holding a semaphore.