Module sleeplocks

BEAM friendly spinlocks for Elixir/Erlang.

Description

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.

Function Index

acquire/1Acquires a lock for the current process.
attempt/1Attempts to acquire a lock for the current process.
execute/2Executes a function when a lock can be acquired.
handle_call/3
init/1
new/1Creates a new lock with a provided concurrency factor.
new/2Creates a new lock with a provided concurrency factor.
release/1Releases a lock held by the current process.

Function Details

acquire/1

acquire(ServerName) -> ok

Acquires a lock for the current process.

This will block until a lock can be acquired.

attempt/1

attempt(ServerName) -> Result

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.

execute/2

execute(ServerName, Exec) -> ok

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.

handle_call/3

handle_call(X1, Caller, Lock) -> any()

init/1

init(Slots) -> any()

new/1

new(Slots::pos_integer()) -> {ok, pid()} | ignore | {error, term()}

Creates a new lock with a provided concurrency factor.

new/2

new(Slots::pos_integer(), Args::list()) -> {ok, pid()} | ignore | {error, term()}

Creates a new lock with a provided concurrency factor.

release/1

release(ServerName) -> ok

Releases a lock held by the current process.


Generated by EDoc