Alambic.CountDown

A simple countdown latch implementation useful for simple fan in scenarios. It is initialized with a count and clients can wait on it to be signaled when the count reaches 0, decrement the count or increment the count.

It is implemented as a GenServer.

In the unlikely case you need to start a named CountDown you can directly use the GenServer.start/start_link functions passing the required initial count as argument.

Summary

Functions

Return the current count

Create a CountDown object with count initial count. count must be a positive integer

Create a CountDown with count initial count. It is linked to the current process

Destroy the countdown object, returning :error to all waiters

Increase the count by one

Reset the count to a new value

Decrease the count by one. Returns true if the count reached 0, false otherwise

Wait for the count to reach 0

Types

t :: %Alambic.CountDown{id: pid}

Functions

count(count_down)

Specs

count(t) :: integer

Return the current count.

create(count)

Specs

create(integer) :: t

Create a CountDown object with count initial count. count must be a positive integer.

iex> c = Alambic.CountDown.create(2)
iex> is_nil(c.id)
false
create_link(count)

Specs

create_link(integer) :: t

Create a CountDown with count initial count. It is linked to the current process.

iex> c = Alambic.CountDown.create_link(2)
iex> Alambic.CountDown.destroy(c)
:ok
destroy(count_down)

Specs

destroy(t) :: :ok

Destroy the countdown object, returning :error to all waiters.

increase(count_down)

Specs

increase(t) :: :ok | :error

Increase the count by one.

reset(count_down, count)

Specs

reset(t, integer) :: :ok

Reset the count to a new value.

signal(count_down)

Specs

signal(t) :: true | false

Decrease the count by one. Returns true if the count reached 0, false otherwise.

wait(count_down)

Specs

wait(t) :: :ok | :error

Wait for the count to reach 0.