Documentation

CslibFrontier.Computability.Distributed.MutualExclusion.FilterLock

Peterson's Bounded-State Filter Lock and Single-Register Impossibility #

This module pairs two foundational results on shared-memory mutual exclusion over atomic read/write registers, complementing the unbounded-ticket safe-register Bakery algorithm in Bakery.lean:

  1. Peterson's (n - 1)-Level n-Process Filter Lock (filterLock, [Pet81], Fig. 3, p. 116): Using bounded 0-indexed Fin n-valued atomic read/write arrays level : Fin n → Fin n and victim : Fin (n - 1) → Fin n (evaluating each waiting room's test via single-register reads checkVictim and checkLevel without multi-word snapshots or read-modify-write primitives), each waiting room r : Fin (n - 1) acts as a filter admitting at most n - (r + 1) processes (card_at_level_le, proof infrastructure for Fig. 3). At the final level ℓ = n - 1, capacity n - (n - 1) = 1 yields mutual exclusion (filterLock_mutualExclusion). Inspecting the highest occupied waiting room (filterLock_step_progress) yields an enabled finite trying-only execution reaching CriticalSection (filterLock_exists_cs_execution, a finite progress witness; fair-execution progress across infinite runs with a sequenced room scan remains open in #7296).

  2. Single-Register Covering-Write Impossibility Step (not_deadlockFreeMutex_of_single_register, 1-register base step toward [BL93], Theorem 5.1, p. 177): Formalizes deterministic asynchronous multi-writer atomic read/write register protocols (MutexProtocol P Reg Val State, with .remainder, .trying, .cs, and .exit regions in MutexSection). Here MutexProtocol.DeadlockFree asks for some process to reach .cs from an initially trying state under MutexProtocol.WeakFair (scheduling .trying and .exit) and MutexProtocol.CSEventuallyExits, whereas [BL93] (§§2–3, pp. 173–175) asks for a region change in each nonnull fair history. Proves the Fintype.card Reg ≤ 1 (2 ≤ Fintype.card P) step via one blind write overwriting another process's writes (specializing Lemma 5.2's indistinguishability replay and Lemma 5.4's covering-write argument, pp. 177, 180); the full Fintype.card P ≤ Fintype.card Reg bound stays open under #7296.

Main definitions #

Main results #

References #

Part 1: Peterson's bounded-state n-process Filter Lock #

Per-process control phase of Peterson's n-process filter lock [Pet81].

  • remainder {n : ℕ} : FilterPhase n

    Noncritical remainder section (level[p] = 0).

  • ready {n : ℕ} (ℓ : Fin n) : FilterPhase n

    Ready at level ℓ (after requestCS at ℓ = 0, or after passing room ℓ - 1): poised to write level[p] := ℓ + 1 (if ℓ + 1 < n) or enter .cs (when n = 1).

  • writeVictim {n : ℕ} (r : Fin (n - 1)) : FilterPhase n

    Poised to write victim[r] := p after setting level[p] := r + 1.

  • waitRoom {n : ℕ} (r : Fin (n - 1)) (cleared : Finset (Fin n)) : FilterPhase n

    Waiting in room r (level[p] = r + 1), having confirmed level[q] < r + 1 for each q ∈ cleared.

  • cs {n : ℕ} : FilterPhase n

    Critical section (CS).

Boolean test for trying phases (outside .remainder and .cs).

Process has strictly passed waiting room r and has not exited .cs.

Global configuration of Peterson's n-process filter lock over bounded Fin n registers.

theorem Cslib.Distributed.MutualExclusion.FilterState.ext {n : ℕ} {x y : FilterState n} (phase : x.phase = y.phase) (level : x.level = y.level) (victim : x.victim = y.victim) :
x = y

Initial quiescent configuration with all processes in .remainder and all registers 0.

Process p is in its critical section (CS).

Instances For

    Process p is in its trying section.

    Instances For

      Processes that have reached filtered level ℓ (0 ≤ ℓ < n) and passed all wait rooms < ℓ.

      Global progress potential summing FilterPhase.potential across all n processes.

      Single-register atomic actions of Peterson's n-process filter lock.

      • requestCS {n : ℕ} (p : Fin n) : FilterAction n

        Process p leaves .remainder to acquire the lock.

      • enterCSOne {n : ℕ} (p : Fin n) : FilterAction n

        Single-process fast path (n = 1): enter .cs from .ready 0 when there are 0 rooms.

      • setLevel {n : ℕ} (p : Fin n) : FilterAction n

        Process p atomically writes level[p] := ℓ + 1 to enter waiting room ℓ.

      • setVictim {n : ℕ} (p : Fin n) : FilterAction n

        Process p atomically writes victim[r] := p in waiting room r.

      • checkVictim {n : ℕ} (p : Fin n) : FilterAction n

        Process p atomically reads victim[r] in room r (advancing if victim[r] ≠ p).

      • checkLevel {n : ℕ} (p q : Fin n) : FilterAction n

        Process p atomically reads level[q] (q ≠ p) in room r.

      • exitCS {n : ℕ} (p : Fin n) : FilterAction n

        Process p exits .cs, atomically resetting level[p] := 0.

      Phase entered upon passing waiting room r: .ready (r + 1) if higher rooms remain, or .cs directly when r is the final room n - 2.

      Computable single-step transition evaluator for Peterson's filter lock.

      Computable multi-step trace evaluator for filterStep?.

      Labelled transition system of Peterson's n-process filter lock [Pet81].

      Filter lock inductive invariants, capacity bound, mutual exclusion, and deadlock freedom #

      theorem Cslib.Distributed.MutualExclusion.atLevel_level_ge {n : ℕ} {s : FilterState n} (hr : s.Reachable) {ℓ : ℕ} {p : Fin n} (hp : p ∈ s.atLevel ℓ) :
      ℓ ≤ ↑(s.level p)

      Every process in s.atLevel ℓ has (s.level p).val ≥ ℓ.

      theorem Cslib.Distributed.MutualExclusion.card_at_level_le {n : ℕ} {s : FilterState n} (hr : s.Reachable) (ℓ : ℕ) (hℓ : ℓ < n) :
      (s.atLevel ℓ).card ≤ n - ℓ

      Inductive level-capacity invariant (proof infrastructure for Peterson's Fig. 3 protocol, [Pet81], p. 116): in every reachable state of filterLock n, for every level ℓ < n, at most n - ℓ processes belong to s.atLevel ℓ (having reached level ≥ ℓ and passed all waiting rooms < ℓ).

      theorem Cslib.Distributed.MutualExclusion.filterLock_mutualExclusion {n : ℕ} {s : FilterState n} (hr : s.Reachable) {p q : Fin n} (hp : s.inCS p) (hq : s.inCS q) :
      p = q

      Mutual Exclusion for Peterson's Filter Lock ([Pet81], Fig. 3, p. 116): in every reachable state of filterLock n, at most one process is in CriticalSection.

      Deadlock freedom of Peterson's filter lock #

      theorem Cslib.Distributed.MutualExclusion.filterLock_step_progress {n : ℕ} {s : FilterState n} (hr : s.Reachable) (htry : ∃ (p : Fin n), s.isTrying p) (hno_cs : ∀ (p : Fin n), ¬s.inCS p) :
      ∃ (μ : FilterAction n) (s' : FilterState n), s.isTrying μ.actor ∧ (filterLock n).Tr s μ s' ∧ s.potential < s'.potential

      Single-step potential-progress lemma (proof infrastructure for filterLock_exists_cs_execution): in any reachable state where the set of trying processes is nonempty and no process is in CriticalSection, some trying process has an enabled step (advancing from .ready/.writeVictim, passing the highest occupied waiting room r_max when not victim[r_max], or recording a lower-level witness at r_max when it is the unique occupant) that strictly increases FilterState.potential without waking any remainder process.

      theorem Cslib.Distributed.MutualExclusion.filterLock_exists_cs_execution {n : ℕ} {s : FilterState n} (hr : s.Reachable) (htry : ∃ (p : Fin n), s.isTrying p) :
      ∃ (μs : List (FilterAction n)) (s' : FilterState n), (filterLock n).MTr s μs s' ∧ (∀ μ ∈ μs, ∀ (p : Fin n), μ ≠ FilterAction.requestCS p ∧ μ ≠ FilterAction.exitCS p) ∧ ∃ (p : Fin n), s'.inCS p

      Finite Progress Witness for filterLock: from any reachable state where at least one process is trying (∃ p, s.isTrying p), there exists an enabled finite execution stepping only trying processes (never waking .remainder processes or exiting .cs) that reaches a state with a process in CriticalSection.

      This provides an existential finite execution witness (which may schedule several trying processes, can be empty if some process is already in .cs, and only promises that some process is in .cs at the end), rather than progress across all fair infinite executions (OmegaExecution), which requires sequencing the waiting-room scan (#7296).

      Part 2: General Multi-Writer Register Mutex Protocols & Single-Register Impossibility #

      Standard four-phase section partition of a shared-memory mutual-exclusion process.

      inductive Cslib.Distributed.MutualExclusion.RegStep (Reg : Type u_1) (Val : Type u_2) (State : Type u_3) :
      Type (max (max u_1 u_2) u_3)

      Elementary atomic step of a process over multi-writer atomic read/write registers Reg with value domain Val (no read-modify-write primitives).

      • localStep {Reg : Type u_1} {Val : Type u_2} {State : Type u_3} (next : State) : RegStep Reg Val State

        Internal local transition neither reading nor writing shared memory.

      • read {Reg : Type u_1} {Val : Type u_2} {State : Type u_3} (r : Reg) (cont : Val → State) : RegStep Reg Val State

        Atomic read of shared register r, branching to cont v on observed value v.

      • write {Reg : Type u_1} {Val : Type u_2} {State : Type u_3} (r : Reg) (val : Val) (next : State) : RegStep Reg Val State

        Atomic blind write r := val, transitioning to local state next.

      def Cslib.Distributed.MutualExclusion.RegStep.NextState {Reg : Type u_1} {Val : Type u_2} {State : Type u_3} :
      RegStep Reg Val State → State → Prop

      Whether st' is a possible successor local state of a RegStep.

      def Cslib.Distributed.MutualExclusion.RegStep.isWrite {Reg : Type u_1} {Val : Type u_2} {State : Type u_3} :
      RegStep Reg Val State → Bool

      Whether a RegStep is a shared-register write.

      structure Cslib.Distributed.MutualExclusion.MutexProtocol (P : Type u_1) (Reg : Type u_2) (Val : Type u_3) (State : Type u_4) :
      Type (max (max (max u_1 u_2) u_3) u_4)

      General deterministic asynchronous mutual-exclusion protocol over processes P, atomic multi-writer read/write registers Reg with values in Val, and local process states State.

      structure Cslib.Distributed.MutualExclusion.MutexConfig (P : Type u_1) (Reg : Type u_2) (Val : Type u_3) (State : Type u_4) :
      Type (max (max (max u_1 u_2) u_3) u_4)

      Global configuration of a MutexProtocol: shared register memory and local process states.

      • mem : Reg → Val

        Shared register contents.

      • localState : P → State

        Local state of each process.

      theorem Cslib.Distributed.MutualExclusion.MutexConfig.ext_iff {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} {x y : MutexConfig P Reg Val State} :
      theorem Cslib.Distributed.MutualExclusion.MutexConfig.ext {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} {x y : MutexConfig P Reg Val State} (mem : x.mem = y.mem) (localState : x.localState = y.localState) :
      x = y
      @[instance_reducible]
      instance Cslib.Distributed.MutualExclusion.instDecidableEqMutexConfigOfFintype {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [Fintype P] [Fintype Reg] [DecidableEq Val] [DecidableEq State] :
      DecidableEq (MutexConfig P Reg Val State)

      Transition labels of a MutexProtocol LTS.

      def Cslib.Distributed.MutualExclusion.MutexConfig.inCS {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} (c : MutexConfig P Reg Val State) (π : MutexProtocol P Reg Val State) (p : P) :

      Process p is in its critical section under π.

      Instances For
        def Cslib.Distributed.MutualExclusion.MutexConfig.isTrying {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} (c : MutexConfig P Reg Val State) (π : MutexProtocol P Reg Val State) (p : P) :

        Process p is in its trying section under π.

        Instances For
          def Cslib.Distributed.MutualExclusion.MutexConfig.isActive {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} (c : MutexConfig P Reg Val State) (π : MutexProtocol P Reg Val State) (p : P) :

          Process p has an active protocol step enabled (.trying or .exit).

          Instances For
            @[instance_reducible]
            instance Cslib.Distributed.MutualExclusion.MutexConfig.instDecidableInCS {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} (c : MutexConfig P Reg Val State) (π : MutexProtocol P Reg Val State) (p : P) :
            Decidable (c.inCS π p)
            @[instance_reducible]
            instance Cslib.Distributed.MutualExclusion.MutexConfig.instDecidableIsTrying {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} (c : MutexConfig P Reg Val State) (π : MutexProtocol P Reg Val State) (p : P) :
            @[instance_reducible]
            instance Cslib.Distributed.MutualExclusion.MutexConfig.instDecidableIsActive {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} (c : MutexConfig P Reg Val State) (π : MutexProtocol P Reg Val State) (p : P) :
            def Cslib.Distributed.MutualExclusion.MutexProtocol.initConfig {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} (π : MutexProtocol P Reg Val State) :
            MutexConfig P Reg Val State

            Initial configuration (initMem, initLocal) of π.

            def Cslib.Distributed.MutualExclusion.MutexProtocol.execRegStep {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] (π : MutexProtocol P Reg Val State) (c : MutexConfig P Reg Val State) (p : P) :
            MutexConfig P Reg Val State

            Execute p's atomic RegStep on configuration c.

            def Cslib.Distributed.MutualExclusion.MutexProtocol.step? {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] (π : MutexProtocol P Reg Val State) (c : MutexConfig P Reg Val State) (μ : MutexAction P) :
            Option (MutexConfig P Reg Val State)

            Computable single-step transition evaluator for π.

            def Cslib.Distributed.MutualExclusion.MutexProtocol.stepTrace? {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] (π : MutexProtocol P Reg Val State) (c : MutexConfig P Reg Val State) :
            List (MutexAction P) → Option (MutexConfig P Reg Val State)

            Computable multi-step trace evaluator for π.step?.

            def Cslib.Distributed.MutualExclusion.MutexProtocol.lts {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] (π : MutexProtocol P Reg Val State) :
            LTS (MutexConfig P Reg Val State) (MutexAction P)

            Labelled transition system of π.

            theorem Cslib.Distributed.MutualExclusion.MutexProtocol.stepTrace?_eq_some_iff {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] (π : MutexProtocol P Reg Val State) {c c' : MutexConfig P Reg Val State} {μs : List (MutexAction P)} :
            π.stepTrace? c μs = some c' ↔ π.lts.MTr c μs c'
            def Cslib.Distributed.MutualExclusion.MutexProtocol.Reachable {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] (π : MutexProtocol P Reg Val State) (c : MutexConfig P Reg Val State) :

            Reachability from π.initConfig in π.lts.

            theorem Cslib.Distributed.MutualExclusion.MutexProtocol.Reachable.init {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] (π : MutexProtocol P Reg Val State) :
            theorem Cslib.Distributed.MutualExclusion.MutexProtocol.Reachable.step_tr {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] {π : MutexProtocol P Reg Val State} {c c' : MutexConfig P Reg Val State} {μ : MutexAction P} (hr : π.Reachable c) (htr : π.lts.Tr c μ c') :
            π.Reachable c'
            theorem Cslib.Distributed.MutualExclusion.MutexProtocol.Reachable.mTr {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] {π : MutexProtocol P Reg Val State} {c c' : MutexConfig P Reg Val State} {μs : List (MutexAction P)} (hr : π.Reachable c) (hmtr : π.lts.MTr c μs c') :
            π.Reachable c'
            def Cslib.Distributed.MutualExclusion.MutexProtocol.MutuallyExclusive {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] (π : MutexProtocol P Reg Val State) :

            Mutual Exclusion: in every reachable configuration, at most one process is in .cs.

            def Cslib.Distributed.MutualExclusion.MutexProtocol.WeakFair {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} (π : MutexProtocol P Reg Val State) (ss : ωSequence (MutexConfig P Reg Val State)) (μs : ωSequence (MutexAction P)) :

            Weak step fairness for active processes (.trying or .exit); processes in .remainder may remain quiescent indefinitely.

            def Cslib.Distributed.MutualExclusion.MutexProtocol.CSEventuallyExits {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} (π : MutexProtocol P Reg Val State) (ss : ωSequence (MutexConfig P Reg Val State)) (μs : ωSequence (MutexAction P)) :

            Every process entering .cs eventually exits .cs.

            def Cslib.Distributed.MutualExclusion.MutexProtocol.DeadlockFree {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] (π : MutexProtocol P Reg Val State) :

            Deadlock Freedom (critical-section entry from an initially trying state): in every infinite execution from a reachable configuration with at least one trying process, assuming WeakFair (scheduling active processes in .trying or .exit) and CSEventuallyExits, some process eventually reaches .cs (∃ k q, (ss k).inCS π q).

            Whereas [BL93] (§3, pp. 174–175) defines global progress as some process changing regions in each nonnull fair history (stepping every process outside .remainder), this predicate asks for some process to reach .cs from an initially trying state under WeakFair and CSEventuallyExits.

            def Cslib.Distributed.MutualExclusion.MutexProtocol.DeadlockFreeMutex {P : Type u_1} {Reg : Type u_2} {Val : Type u_3} {State : Type u_4} [DecidableEq P] [DecidableEq Reg] (π : MutexProtocol P Reg Val State) :

            Combined specification: π achieves both MutuallyExclusive and DeadlockFree.

            Covering-write proof of the single-register impossibility step #

            theorem Cslib.Distributed.MutualExclusion.not_deadlockFreeMutex_of_single_register {P : Type u_5} {Reg : Type u_6} {Val : Type u_7} {State : Type u_8} [Fintype P] [Fintype Reg] [DecidableEq P] [DecidableEq Reg] (hP : 2 ≤ Fintype.card P) (hReg : Fintype.card Reg ≤ 1) (π : MutexProtocol P Reg Val State) :

            Intermediate 1-register covering-write base step toward the Fintype.card P ≤ Fintype.card Reg space lower bound of [BL93] (Theorem 5.1, p. 177; tracked in #7296): for 2 ≤ Fintype.card P processes and Fintype.card Reg ≤ 1 atomic multi-writer read/write registers, no deterministic asynchronous MutexProtocol satisfies DeadlockFreeMutex (MutuallyExclusive and DeadlockFree).

            Here MutexProtocol includes .remainder, .trying, .cs, and .exit (MutexSection), and MutexProtocol.DeadlockFree asks for some process to reach .cs from an initially trying state under MutexProtocol.WeakFair (scheduling .trying and .exit) and MutexProtocol.CSEventuallyExits, whereas [BL93] (§§2–3, pp. 173–175) asks for a region change in each nonnull fair history. The proof uses one poised blind write to erase the second process's writes, specializing the indistinguishability and covering-write arguments of [BL93], Lemmas 5.2 and 5.4 (pp. 177, 180).