Alambic.CountDown (alambic v1.1.0)
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.
Link to this section Summary
Functions
Returns a specification to start this module under a supervisor.
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.
Callback implementation for GenServer.init/1
.
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.
Link to this section Types
Specs
t() :: %Alambic.CountDown{id: pid()}
Link to this section Functions
child_spec(init_arg)
Returns a specification to start this module under a supervisor.
See Supervisor
.
count(_)
Specs
Return the current count.
Example
iex> c = Alambic.CountDown.create(10)
iex> Alambic.CountDown.count(c)
10
create(count)
Specs
Create a CountDown object with count
initial count.
count
must be a positive integer.
Example
iex> c = Alambic.CountDown.create(2)
iex> is_nil(c.id)
false
create_link(count)
Specs
Create a CountDown with count
initial count. It is linked
to the current process.
Example
iex> c = Alambic.CountDown.create_link(2)
iex> Alambic.CountDown.destroy(c)
:ok
destroy(_)
Specs
destroy(t()) :: :ok
Destroy the countdown object, returning :error
to all waiters.
Example
iex> c = Alambic.CountDown.create(2)
iex> Alambic.CountDown.destroy(c)
:ok
increase(_)
Specs
increase(t()) :: :ok | :error
Increase the count by one.
Example
iex> c = Alambic.CountDown.create(0)
iex> Alambic.CountDown.increase(c)
iex> Alambic.CountDown.signal(c)
true
init(count)
Callback implementation for GenServer.init/1
.
reset(_, count)
Specs
Reset the count to a new value.
Example
iex> c = Alambic.CountDown.create(10)
iex> false = Alambic.CountDown.signal(c)
iex> Alambic.CountDown.reset(c, 1)
iex> Alambic.CountDown.signal(c)
true
signal(_)
Specs
signal(t()) :: true | false
Decrease the count by one. Returns true if the count reached 0, false otherwise.
Example
iex> c = Alambic.CountDown.create(1)
iex> Alambic.CountDown.signal(c)
true
wait(_)
Specs
wait(t()) :: :ok | :error
Wait for the count to reach 0.