Alambic.Semaphore
A simple semaphore implementation, useful when you need quick control around resource access and do not want to resort to the full OTP artillery or complex process pooling.
This semaphore is implemented as a GenServer.
If you need to start a named Semaphore as part of a supervision
tree, you should use directly the GenServer.start/start_link
functions passing the required max as argument.
Summary
Functions
Acquire a slot in the semaphore. Will block until a slot is available or the semaphore is destroyed
Create a semaphore with max slots
Create a semaphore with max slots. The semaphore is linked to
the current process
Destroy a semaphore. Clients waiting on acquire will receive
an :error response
Return true if no slot is available, false otherwise
Release a slot from the semaphore. :error is returned if no slot
is currently acquired
Try to acquire a slot in the semaphore but does not block if no slot
is available. Returns true if a slot was acquired, false otherwise
Types
t :: %Alambic.Semaphore{id: pid}
Functions
Specs
acquire(t) :: :ok | :error
Acquire a slot in the semaphore. Will block until a slot is available or the semaphore is destroyed.
Specs
create(integer) :: t
Create a semaphore with max slots.
iex> s = Alambic.Semaphore.create(3)
iex> is_nil(s.id)
false
Specs
create_link(integer) :: t
Create a semaphore with max slots. The semaphore is linked to
the current process.
iex> s = Alambic.Semaphore.create_link(3)
iex> Alambic.Semaphore.destroy(s)
:ok
Specs
destroy(t) :: :ok
Destroy a semaphore. Clients waiting on acquire will receive
an :error response.
Specs
full?(t) :: true | false
Return true if no slot is available, false otherwise.
Specs
release(t) :: :ok | :error
Release a slot from the semaphore. :error is returned if no slot
is currently acquired.
Specs
try_acquire(t) :: true | false
Try to acquire a slot in the semaphore but does not block if no slot
is available. Returns true if a slot was acquired, false otherwise.