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.

Data Types

name()

name() = atom() | {local, Name::atom()} | {global, GlobalName::any()} | {via, Module::atom(), ViaName::any()}

start_ret()

start_ret() = {ok, pid()} | ignore | {error, term()}

Function Index

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

Function Details

acquire/1

acquire(Name::name()) -> ok

Acquires a lock for the current process.

This will block until a lock can be acquired.

attempt/1

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.

execute/2

execute(Name::name(), Exec::fun(() -> any())) -> 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.

new/1

new(Slots::pos_integer()) -> start_ret()

Creates a new lock with a provided concurrency factor.

new/2

new(Slots::pos_integer(), Args::list()) -> start_ret()

Creates a new lock with a provided concurrency factor.

release/1

release(Name::name()) -> ok

Releases a lock held by the current process.


Generated by EDoc