misha_cafex v0.0.5 Cafex.Lock behaviour

A general, backend-pluggable lock implementation.

It’s an FSM.

Summary

Types

Arguments for locker

Functions

Non-blocking function to acquire a lock

Types

args :: [term]

Arguments for locker

locker :: atom

Cafex.Lock module

state :: term

Functions

acquire(locker, args, timeout \\ :infinity)

Specs

acquire(locker, args, timeout) ::
  {:ok, pid} |
  {:error, term}

Non-blocking function to acquire a lock

The args is pass to locker.init(args).

If at first it hold the lock, this function will return {:ok, pid}.

Or else, it will return {:error, :locked} and stop the FSM if timeout is 0. Or it will return {:wait, pid} if timeout is other then 0, and the FSM will wait for the lock until timeout.

The caller will receive a message {:lock, :ok} if it get the lock before timeout, or {:lock, :timeout} and then stop.

NOTE: The caller process is linked to the FSM process.

release(pid)

Callbacks

handle_acquire(state)

Specs

handle_acquire(state) ::
  {:ok | :wait, state} |
  {:error, term}

Handle acquire callback

Non-blocking function, return {:ok, state} if acquired the lock. Or {:wait, state} if waiting the lock, continue waiting in a asynchronized way(i.e. a process), then if lock changed, a :lock_changed message will send to this process.

handle_release(state)

Specs

handle_release(state) ::
  {:ok, state} |
  {:error, term}
init(term)

Specs

init(term) :: {:ok, state} | {:error, term}
terminate(state)

Specs

terminate(state) :: :ok