wasm_storage_lock (wasm_storage v0.1.0)

View Source

Byte-range locks over one shared storage, for the guests that open it.

redb lets one writing handle and several reading handles share a file by locking byte ranges of it. Inside one node those handles are guests, each in its own process, so this process plays the part of the OS lock table. The lock import of wasm_storage_host calls it for every device registered with it (wasm_storage_guest's locks option). Like wasm_storage_owner, it is advisory and BEAM-local: it does not lock the file against other OS processes.

{ok, L} = wasm_storage_lock:start_link(),
ok = wasm_storage_lock:request(L, {self(), 0}, shared, 0, 319),
busy = wasm_storage_lock:request(L, {Other, 0}, try_exclusive, 0, 0).

The rules are those of POSIX record locks between different owners:

  • Shared locks over overlapping ranges coexist. An exclusive lock conflicts with any other owner's lock over an overlapping range.
  • An owner's locks never conflict with each other. Locking the exact range it already holds changes the kind of that lock.
  • unlock releases the lock over exactly that range.
  • A waiting request is granted in the order of arrival: a waiting exclusive lock is not passed by shared locks that ask after it. A try_ request never waits and only looks at the locks held.
  • When an owner's process exits, its locks and its waiting requests go.

An owner is {Pid, Tag}; Pid is monitored. Ranges are inclusive First..Last, unsigned 64-bit.

Summary

Functions

Drop every lock and waiting request of the owners whose process is Pid. Call it after killing a guest and before starting its replacement: the replacement may ask for the same locks before this process has handled the dead one's exit.

The locks held, for tests and diagnosis.

Release every lock and waiting request of Owner.

One lock operation for Owner over First..Last.

Start a lock table, linked to the caller.

Types

kind()

-type kind() :: shared | exclusive.

op()

-type op() :: try_exclusive | try_shared | exclusive | shared | unlock | query.

owner()

-type owner() :: {pid(), term()}.

Functions

forget(Server, Pid)

-spec forget(pid(), pid()) -> ok.

Drop every lock and waiting request of the owners whose process is Pid. Call it after killing a guest and before starting its replacement: the replacement may ask for the same locks before this process has handled the dead one's exit.

locks(Server)

-spec locks(pid()) -> [{owner(), kind(), non_neg_integer(), non_neg_integer()}].

The locks held, for tests and diagnosis.

release(Server, Owner)

-spec release(pid(), owner()) -> ok.

Release every lock and waiting request of Owner.

request/5

-spec request(pid(), owner(), op(), non_neg_integer(), non_neg_integer()) ->
                 ok | busy | {error, einval | enoent}.

One lock operation for Owner over First..Last.

  • try_exclusive, try_shared: ok when granted, busy on a conflict.
  • exclusive, shared: wait until granted, then ok.
  • unlock: ok, or {error, enoent} when the owner holds no lock over exactly that range.
  • query: busy when an exclusive lock over the range would conflict with another owner's lock, ok otherwise.

start_link()

-spec start_link() -> {ok, pid()}.

Start a lock table, linked to the caller.