View Source sleeplocks (sleeplocks v1.1.3)
BEAM friendly spinlocks for Elixir/Erlang.
This module provides a very simple API for managing locks inside a BEAM instance. It's modeled on spinlocks, but works through message passing rather than loops. Locks can have multiple slots to enable arbitrary numbers of associated processes. The moment a slot is freed, the next awaiting process acquires the lock.
All of this is done in a simple Erlang process so there's very little dependency, and management is extremely simple.Link to this section Summary
Functions
Acquires a lock for the current process.
Attempts to acquire a lock for the current process.
Executes a function when a lock can be acquired.
Creates a new lock with a provided concurrency factor.
Creates a new lock with a provided concurrency factor.
Releases a lock held by the current process.
Link to this section Types
-type name() ::
atom() |
{local, Name :: atom()} |
{global, GlobalName :: any()} |
{via, Module :: atom(), ViaName :: any()}.
-type start_ret() :: {ok, pid()} | ignore | {error, term()}.
Link to this section Functions
-spec acquire(Name :: name()) -> ok.
Acquires a lock for the current process.
This will block until a lock can be acquired.-spec attempt(Name :: name()) -> ok | {error, unavailable}.
Attempts to acquire a lock for the current process.
In the case there are no slots available, an error will be returned immediately rather than waiting.-spec execute(Name :: name(), Exec :: fun(() -> any())) -> any().
Executes a function when a lock can be acquired.
The lock is automatically released after the function has completed execution; there's no need to manually release.-spec new(Slots :: pos_integer()) -> start_ret().
-spec new(Slots :: pos_integer(), Args :: list()) -> start_ret().
-spec release(Name :: name()) -> ok.
-spec start_link(Slots :: pos_integer()) -> start_ret().
-spec start_link(Slots :: pos_integer(), Args :: list()) -> start_ret().