package granary

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Granary_store.RwlockSource

Writer-mutex + reader-counter for Lwt.

Semantics (intentionally NOT a classic shared/exclusive RwLock):

  • Many readers run concurrently and never block.
  • Writers serialise against other writers (one at a time).
  • Readers do NOT block writers. Writers do NOT block readers.

Rationale: under snapshot isolation (see Store.ro_snapshot and Pager.read ~snapshot_frames) a reader's view at WAL frame N is invariant under a concurrent writer's appends, so reader/writer mutual exclusion is not needed for correctness. This module only serialises writers and tracks active readers for the checkpoint coordinator.

Re-entrancy is NOT supported — a fiber that holds the writer lock must not call acquire_write again.

Sourcetype t
Sourceval create : unit -> t

Create a fresh lock with no readers and no active writer.

Sourceval acquire_read : t -> unit Lwt.t

acquire_read increments the reader counter and returns immediately. Never blocks.

Sourceval release_read : t -> unit

release_read decrements the counter; broadcasts when it reaches zero (used by the checkpoint coordinator).

Sourceval acquire_write : t -> unit Lwt.t

acquire_write blocks while another writer is active. Does NOT block on active readers.

Sourceval release_write : t -> unit

release_write releases the writer lock, waking the next waiter.

Sourceval with_read : t -> (unit -> 'a Lwt.t) -> 'a Lwt.t

Acquire shared (reader) access for the duration of f.

Sourceval with_write : t -> (unit -> 'a Lwt.t) -> 'a Lwt.t

Acquire exclusive (writer) access for the duration of f.

Sourceval readers : t -> int

Number of currently-held read locks.

Sourceval writer_pending : t -> bool

True iff a writer is currently holding (or queued for) the lock. Diagnostic only — not used by Store for any control flow now that readers and writers don't mutually exclude.

Sourceval writer_active : t -> bool

True iff a writer is currently holding the lock.