Documentation

CslibFrontier.Computability.Distributed.SharedMemory.Hierarchy

Herlihy's wait-free shared-memory consensus hierarchy (1 < 2 < ∞) #

Different shared-memory synchronization primitives have strictly different wait-free consensus numbers — the maximum number of asynchronous crash-prone processes for which they can solve wait-free consensus ([Her91], §3, Definition 1, p. 130 & Figure 1, p. 126; see also prior shared-register impossibility results in [LAA87] noted in [Her91], §3.1, p. 132). Building on the bivalence and critical-configuration engine in CslibFrontier/Computability/Distributed/SharedMemory/Basic.lean, this module establishes the three fundamental levels 1 < 2 < ∞ of Herlihy's hierarchy:

Main definitions #

Main results #

References #

Shared helpers for critical-step analysis and 1-step termination #

Level 1: Atomic read/write registers #

Operations on an atomic multi-reader multi-writer read/write register storing values of type α: either read the current value or write a new value (returning no information about the overwritten state).

  • read {α : Type u_1} : RegOp α

    Read the register's current value.

  • write {α : Type u_1} (v : α) : RegOp α

    Overwrite the register with v.

@[instance_reducible]
def Cslib.Distributed.SharedMemory.instDecidableEqRegOp.decEq {α✝ : Type u_1} [DecidableEq α✝] (x✝ x✝¹ : RegOp α✝) :
Decidable (x✝ = x✝¹)
def Cslib.Distributed.SharedMemory.registerObj {α : Type u_1} (init : α) :
ObjSpec (RegOp α) (Option α) α

Specification of an atomic multi-reader multi-writer read/write register initialized to init: read leaves the state unchanged and returns some s; write v sets the state to v and returns none.

def Cslib.Distributed.SharedMemory.oneProcProtocol {P : Type u_1} {I : Type u_2} {V : Type u_3} {Op : I → Type u_4} {Resp : I → Type u_5} {ObjState : I → Type u_6} (objs : (i : I) → ObjSpec (Op i) (Resp i) (ObjState i)) (i₀ : I) (op₀ : Op i₀) :
Protocol P objs V V

Trivial 1-step protocol for at most one process (Fintype.card P ≤ 1): each process stores its own initial input in local state, performs one dummy invocation op₀ on i₀, and decides its own input.

theorem Cslib.Distributed.SharedMemory.solvesWaitFreeConsensus_one {P : Type u_1} {I : Type u_2} {V : Type u_3} {Op : I → Type u_4} {Resp : I → Type u_5} {ObjState : I → Type u_6} [Fintype P] [DecidableEq P] [DecidableEq I] (objs : (i : I) → ObjSpec (Op i) (Resp i) (ObjState i)) (i₀ : I) (op₀ : Op i₀) (hcard : Fintype.card P ≤ 1) :

1-process baseline: when Fintype.card P ≤ 1, oneProcProtocol solves wait-free consensus over any value type V and any nonempty shared-object family.

theorem Cslib.Distributed.SharedMemory.not_waitFreeConsensus_registers {P : Type u_1} {I : Type u_2} {S : Type u_3} {Val : I → Type u_4} [Fintype P] [DecidableEq P] [DecidableEq I] {init : (i : I) → Val i} (π : Protocol P (fun (i : I) => registerObj (init i)) S Bool) (hcard : 2 ≤ Fintype.card P) :

Herlihy Level-1 register impossibility ([Her91], §3.1, Theorem 2 & Figures 3–5, pp. 131–133): no wait-free deterministic shared-memory protocol using only atomic multi-reader multi-writer read/write registers solves binary consensus for 2 ≤ Fintype.card P processes.

theorem Cslib.Distributed.SharedMemory.exists_register_consensus_iff {P : Type u_1} [Fintype P] [DecidableEq P] :
(∃ (I : Type) (x : DecidableEq I) (Val : I → Type) (init : (i : I) → Val i) (S : Type) (π : Protocol P (fun (i : I) => registerObj (init i)) S Bool), π.SolvesWaitFreeConsensus) ↔ Fintype.card P ≤ 1

Sharp Level-1 consensus characterization: wait-free binary consensus (SolvesWaitFreeConsensus) is solvable using atomic multi-reader multi-writer read/write registers if and only if Fintype.card P ≤ 1 (trivially solvable for 0 or 1 processes via oneProcProtocol, and impossible for 2 ≤ Fintype.card P by not_waitFreeConsensus_registers).

Level 2: Commuting-or-overwriting read-modify-write objects #

def Cslib.Distributed.SharedMemory.IsCommutingOrOverwriting {Op : Type u_1} {Resp : Type u_2} {ObjState : Type u_3} (obj : ObjSpec Op Resp ObjState) :

A deterministic shared object obj is commuting-or-overwriting ("interfering" in [Her91], §3.2, p. 134) if for every pair of operations op₁, op₂ and every object state s, either op₁ and op₂ drive s to the same object state in both orders, op₂ overwrites op₁'s effect on the object state, or op₁ overwrites op₂'s effect on the object state. Formulated pointwise on (obj.step _ _).1 for arbitrary ObjSpecs, this uniformly covers both read-modify-write primitives (testAndSetObj, swapObj, fetchAndAddObj) and atomic read/write registers (registerObj).

Every atomic read/write register is commuting-or-overwriting.

Atomic TestAndSet object: starts at false; every invocation sets the state to true and returns the previous boolean value (false for the unique winner, true for all subsequent callers).

def Cslib.Distributed.SharedMemory.swapObj {α : Type u_1} (init : α) :
ObjSpec α α α

Atomic Swap (read-and-write / exchange) object: starts at init; step v s atomically replaces the state with v and returns the previous state s.

swapObj is commuting-or-overwriting (every operation overwrites the object state).

Atomic FetchAndAdd object over an additive type G: starts at init; step d s atomically updates the state to s + d and returns the previous state s.

fetchAndAddObj over any AddCommSemigroup (such as ℕ, ℤ, or ZMod n) is commuting-or-overwriting because increments commute on the object state.

Explicit 3-step 2-process TestAndSet consensus protocol #

Descriptor for a shared object in a TestAndSet-plus-registers system: either an atomic read/write register of type α initialized to init, or a TestAndSet object.

  • reg (α : Type u) (init : α) : TASRegKind

    Atomic multi-reader multi-writer read/write register storing α.

  • tas : TASRegKind

    Single-bit TestAndSet object initialized to false.

@[reducible, inline]

Operation type of a TASRegKind object.

@[reducible, inline]

Response type of a TASRegKind object.

@[reducible, inline]

State type of a TASRegKind object.

Shared-object addresses for the 2-process TestAndSet consensus protocol: one atomic read/write proposal register propReg r per register slot r : R, plus one shared TestAndSet object tas.

  • propReg {R : Type u_1} (r : R) : TASObjId R

    Proposal register indexed by r.

  • tas {R : Type u_1} : TASObjId R

    Single TestAndSet object arbitrating the winning proposer.

@[reducible, inline]
abbrev Cslib.Distributed.SharedMemory.tasKind (R : Type u_1) {V : Type u} (v₀ : V) :

TASRegKind assignment for TASObjId R: proposal registers initialized to v₀ and one TestAndSet object.

@[reducible, inline]
abbrev Cslib.Distributed.SharedMemory.tasObjFamily (R : Type u_1) {V : Type u} (v₀ : V) (i : TASObjId R) :
ObjSpec (tasKind R v₀ i).Op (tasKind R v₀ i).Resp (tasKind R v₀ i).State

Shared-object family for 2-process TestAndSet consensus: proposal registers initialized to v₀ : V together with TestAndSet.

inductive Cslib.Distributed.SharedMemory.TASState (R : Type u_1) (V : Type u_2) :
Type (max u_1 u_2)

Local control state of a process in the 3-step TestAndSet consensus protocol:

  1. writeProp v: write own input v to own proposal register;
  2. invokeTAS: invoke TestAndSet to determine whether this process (false) or its peer other p (true) won;
  3. readWinner r: read the winning process's proposal register propReg r and decide;
  4. done: post-decision terminal state.
  • writeProp {R : Type u_1} {V : Type u_2} (v : V) : TASState R V

    Step 1: write own input v into own proposal register.

  • invokeTAS {R : Type u_1} {V : Type u_2} : TASState R V

    Step 2: invoke TestAndSet to arbitrate the winner.

  • readWinner {R : Type u_1} {V : Type u_2} (r : R) : TASState R V

    Step 3: read proposal register r of the winning process and decide.

  • done {R : Type u_1} {V : Type u_2} : TASState R V

    Post-decision local state.

@[instance_reducible]
def Cslib.Distributed.SharedMemory.instDecidableEqTASState.decEq {R✝ : Type u_1} {V✝ : Type u_2} [DecidableEq R✝] [DecidableEq V✝] (x✝ x✝¹ : TASState R✝ V✝) :
Decidable (x✝ = x✝¹)
def Cslib.Distributed.SharedMemory.testAndSetProtocol {P : Type u_1} {R : Type u_2} {V : Type u} (regOf : P → R) (v₀ : V) (other : P → P) :
Protocol P (tasObjFamily R v₀) (TASState R V) V

Explicit 3-step wait-free consensus protocol using one proposal register per process (regOf) and one TestAndSet object, parameterized by a peer selector other : P → P (other p names the unique other process when Fintype.card P = 2). Completely kernel-computable when regOf and other are computable.

theorem Cslib.Distributed.SharedMemory.solvesWaitFreeConsensus_testAndSetProtocol {P : Type u_1} {R : Type u_2} {V : Type u} [Finite P] [DecidableEq P] [DecidableEq R] (regOf : P → R) (hreg : Function.Injective regOf) (v₀ : V) (other : P → P) (hother : ∀ (p q : P), p ≠ q → other p = q) :

Correctness of testAndSetProtocol: whenever regOf : P → R is injective and other p names the unique peer q ≠ p (∀ p q, p ≠ q → other p = q, which holds whenever Fintype.card P ≤ 2), testAndSetProtocol solves wait-free consensus.

theorem Cslib.Distributed.SharedMemory.solvesWaitFreeConsensus_testAndSet_two {P : Type u_1} {V : Type u} [Fintype P] [DecidableEq P] (v₀ : V) (hcard : Fintype.card P ≤ 2) :
∃ (other : P → P), (testAndSetProtocol id v₀ other).SolvesWaitFreeConsensus

2-process TestAndSet sufficiency (test&set instance of [Her91], §3.2, Figure 7, p. 134): whenever Fintype.card P ≤ 2, there is a 3-step testAndSetProtocol solving wait-free consensus over P and V (Theorem 3's general 2-process lower bound over arbitrary nontrivial RMW functions is outside this module).

theorem Cslib.Distributed.SharedMemory.not_waitFreeConsensus_rmw_of_three_le {P : Type u_1} {I : Type u_2} {S : Type u_3} {Op : I → Type u_4} {Resp : I → Type u_5} {ObjState : I → Type u_6} [Fintype P] [DecidableEq P] [DecidableEq I] {objs : (i : I) → ObjSpec (Op i) (Resp i) (ObjState i)} (hobj : ∀ (i : I), IsCommutingOrOverwriting (objs i)) (π : Protocol P objs S Bool) (hcard : 3 ≤ Fintype.card P) :

Herlihy's Level-2 impossibility ([Her91], §3.2, Theorem 4, p. 134): when 3 ≤ Fintype.card P, no wait-free deterministic shared-memory protocol over any indexed family of commuting-or-overwriting shared objects (including any combination of atomic read/write registers, testAndSetObj, swapObj, and fetchAndAddObj) solves binary consensus.

theorem Cslib.Distributed.SharedMemory.exists_testAndSet_consensus_iff {P : Type u_1} [Fintype P] [DecidableEq P] :
(∃ (I : Type) (x : DecidableEq I) (kind : I → TASRegKind) (S : Type) (π : Protocol P (fun (i : I) => (kind i).toObjSpec) S Bool), π.SolvesWaitFreeConsensus) ↔ Fintype.card P ≤ 2

Sharp Level-2 TestAndSet consensus characterization ([Her91], §3.2, Figure 7 & Theorem 4, p. 134): wait-free binary consensus (SolvesWaitFreeConsensus) is solvable using TestAndSet objects and atomic read/write registers if and only if Fintype.card P ≤ 2 (solvable for 0, 1, or 2 processes via testAndSetProtocol, and impossible for 3 ≤ Fintype.card P by not_waitFreeConsensus_rmw_of_three_le).

Level ∞: CompareAndSwap #

Operation of an atomic CompareAndSwap (CAS) object over values of type α: compare the current state against expected, and if equal replace it with desired.

  • expected : α

    Expected current value required for the update to take effect.

  • desired : α

    Desired replacement value written when the state equals expected.

def Cslib.Distributed.SharedMemory.instDecidableEqCASOp.decEq {α✝ : Type u_1} [DecidableEq α✝] (x✝ x✝¹ : CASOp α✝) :
Decidable (x✝ = x✝¹)
@[instance_reducible]
def Cslib.Distributed.SharedMemory.casObj {α : Type u_1} [DecidableEq α] (init : α) :
ObjSpec (CASOp α) α α

Atomic value-returning CompareAndSwap (compare&swap in [Her91]): starts at init; step ⟨expected, desired⟩ s replaces s with desired iff s = expected, and returns the previous value s.

theorem Cslib.Distributed.SharedMemory.not_isCommutingOrOverwriting_casObj {α : Type u_1} [DecidableEq α] (init : α) {a b c : α} (hab : a ≠ b) (hac : a ≠ c) (hbc : b ≠ c) :

On any state domain containing three pairwise distinct values a, b, c, casObj is not commuting-or-overwriting: from state a, CAS(a, b) and CAS(a, c) neither commute (b ≠ c) nor overwrite one another (b ≠ c).

casObj over Option V (for any [Nontrivial V], including Option Bool) is not commuting-or-overwriting.

Herlihy's 1-step n-process wait-free consensus protocol using a single CompareAndSwap object initialized to none : Option V: each process p stores its input s = inp p and invokes CAS(none, some s). If CAS returns none, p won and decides s; if CAS returns some w, p adopts and decides the winner's value w.

n-process CompareAndSwap consensus ([Her91], §3.2, Theorem 5 & Figures 8–9, p. 135): casConsensusProtocol solves wait-free consensus for every finite process set P (consensus number = ∞) and every decidable value type V.