sync_primitives v0.2.0 SyncPrimitives.CyclicBarrier View Source

A CyclicBarrier expects a predefined number of parties to await/2 before all calls to await/2 can continue.

Parties arrive at the barrier by calling await/2.

When all parties have arrived, all calls to await/2 unblock, and parties may proceed.

Thereafter, the barrier resets (which is what makes it cyclic).

Although this fully describes the happy path, documentation for time outs, participating parties that exit, and other sad paths is currently lacking.

Example

iex> barrier = SyncPrimitives.CyclicBarrier.start(2, fn -> IO.puts("barrier action") end)
{SyncPrimitives.CyclicBarrier, #PID<0.149.0>}
iex> spawn_link(fn ->
...>  IO.puts("process 1, before wait")
...>  SyncPrimitives.CyclicBarrier.await(barrier)
...>  IO.puts("process 1, after wait")
...> end)
process 1, before wait
#PID<0.155.0>
iex> spawn_link(fn ->
...>  IO.puts("process 2, before wait")
...>  SyncPrimitives.CyclicBarrier.await(barrier)
...>  IO.puts("process 2, after wait")
...> end)
process 2, before wait
#PID<0.161.0>
barrier action
process 1, after wait
process 2, after wait
iex> SyncPrimitives.CyclicBarrier.stop(barrier)

Link to this section Summary

Functions

Returns true, if any of the parties waiting for the barrier timed out or, exited since construction or the last reset, false otherwise.

Returns the number of parties currently waiting for the barrier.

Returns the number of parties required to trip this barrier.

Resets the barrier to its initial state. If any parties are currently waiting at the barrier, the await/1 or await/2 calls will return :broken.

Starts a new CyclicBarrier that expects parties processes to call await/1 or await/2 before it releases. Calls to await/1 block until all expected parties have called await/1. Thereafter, the barrier resets (which is what makes it cyclic).

Link to this section Types

Link to this type

barrier()

View Source
barrier() :: {SyncPrimitives.CyclicBarrier, pid :: pid()}

Link to this section Functions

Link to this function

await(arg, timeout \\ :infinity)

View Source
await(barrier(), :infinity | integer()) :: :fulfilled | :broken

Returns true, if any of the parties waiting for the barrier timed out or, exited since construction or the last reset, false otherwise.

Link to this function

number_waiting(arg)

View Source
number_waiting(barrier()) :: false | integer()

Returns the number of parties currently waiting for the barrier.

Link to this function

parties(arg)

View Source
parties(barrier()) :: false | integer()

Returns the number of parties required to trip this barrier.

Resets the barrier to its initial state. If any parties are currently waiting at the barrier, the await/1 or await/2 calls will return :broken.

Link to this function

start(parties, action \\ nil)

View Source
start(pos_integer(), nil | (() -> any())) :: barrier()

Starts a new CyclicBarrier that expects parties processes to call await/1 or await/2 before it releases. Calls to await/1 block until all expected parties have called await/1. Thereafter, the barrier resets (which is what makes it cyclic).

Link to this function

stop(barrier)

View Source
stop(barrier()) :: :ok