Documentation

CslibFrontier.Computability.Distributed.MutualExclusion.Bakery

Lamport's Bakery Algorithm over Safe Overlapping Reads #

This module formalizes Lamport's P-process Bakery mutual-exclusion algorithm ([Lam74], pp. 453–455) as a fine-grained labelled transition system (Cslib.LTS) over single-writer safe registers choosing : P → Bool and number : P → ℕ without atomic read-modify-write primitives or atomic multi-reader register hardware.

Every register write is split into an initiation step (entering a mid-write phase) and a completion step, and processes may also fail into a transient .failed phase ([Lam74], p. 454) before recovering to .remainder with choosing[p] = false and number[p] = 0:

Main definitions #

Main results #

References #

Control phase of a single process in Lamport's Bakery algorithm ([Lam74], p. 454).

  • remainder {P : Type u_1} : BakeryPhase P

    Noncritical remainder section (choosing[p] = false, number[p] = 0).

  • writeChoosingTrue {P : Type u_1} : BakeryPhase P

    Doorway (L1): mid-write setting choosing[p] := true.

  • readMax {P : Type u_1} (scanned : Finset P) (acc : ℕ) : BakeryPhase P

    Doorway (L1): reading number[j] in arbitrary order (scanned read so far, max acc).

  • writeNumber {P : Type u_1} (nextTicket : ℕ) : BakeryPhase P

    Doorway (L1): mid-write setting number[p] := nextTicket.

  • writeChoosingFalse {P : Type u_1} : BakeryPhase P

    Doorway (L1): mid-write resetting choosing[p] := false after number[p] is written.

  • waitL2 {P : Type u_1} (checked : Finset P) (target : P) : BakeryPhase P

    Waiting loop (L2): polling choosing[target] until a safe read returns false.

  • waitL3 {P : Type u_1} (checked : Finset P) (target : P) : BakeryPhase P

    Waiting loop (L3): polling number[target] until 0 or (number[p], p) has priority.

  • cs {P : Type u_1} : BakeryPhase P

    Critical section (CS).

  • writeNumberZero {P : Type u_1} : BakeryPhase P

    Exit section: mid-write resetting number[p] := 0.

  • failed {P : Type u_1} : BakeryPhase P

    Transient failure state ([Lam74], p. 454): concurrent reads of p's registers may return arbitrary values until recovery resets choosing[p] = false and number[p] = 0.

Boolean test for doorway phases (L1).

Boolean test for waiting-loop phases (L2/L3).

Boolean test for phases prior to starting the doorway ticket scan.

Global configuration of Lamport's P-process Bakery algorithm ([Lam74], p. 454).

  • phase : P → BakeryPhase P

    Per-process control phase.

  • choosing : P → Bool

    Last completed value of each process's doorway flag choosing[p].

  • number : P → ℕ

    Last completed value of each process's ticket register number[p].

theorem Cslib.Distributed.MutualExclusion.BakeryState.ext {P : Type u_1} {x y : BakeryState P} (phase : x.phase = y.phase) (choosing : x.choosing = y.choosing) (number : x.number = y.number) :
x = y

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

Whether p is mid-write (or transiently failing) on choosing[p].

Whether p is mid-write (or transiently failing) on number[p].

Safe-register read relation for choosing[j] ([Lam74], p. 454): returns s.choosing j when no write overlaps, and allows any Bool value during an overlapping write or failure.

Instances For

    Safe-register read relation for number[j] ([Lam74], p. 454): returns s.number j when no write overlaps, and allows any ℕ value during an overlapping write or failure.

    Instances For
      @[instance_reducible]

      Process p is in the doorway (L1, prior to completing choosing[p] := false).

      Instances For

        Process p is in the L2/L3 waiting loop.

        Instances For

          Process p is in its critical section (CS).

          Instances For

            Process p is in the bakery ([Lam74], p. 454): from completing choosing[p] := false until leaving its critical section or failing.

            Instances For

              Process p is trying to enter its critical section (in the doorway or waiting loop).

              Instances For

                Process q has not yet started scanning tickets in its current doorway (remainder, writeChoosingTrue, writeNumberZero, or failed).

                Instances For

                  Lexicographic pair (s.number p, p) ordered as in [Lam74], p. 454.

                  @[simp]
                  theorem Cslib.Distributed.MutualExclusion.BakeryState.ticket_lt_iff {P : Type u_1} [LT P] (s₁ s₂ : BakeryState P) (p q : P) :
                  s₁.ticket p < s₂.ticket q ↔ s₁.number p < s₂.number q ∨ s₁.number p = s₂.number q ∧ p < q

                  Transition labels of Lamport's Bakery LTS, carrying the acting process and any safe-read value observed during an overlapping read.

                  • enterDoorway {P : Type u_1} (p : P) : BakeryAction P

                    Process p leaves .remainder and begins writing choosing[p] := true.

                  • finishChoosingTrue {P : Type u_1} (p : P) : BakeryAction P

                    Process p completes writing choosing[p] := true and begins scanning number.

                  • readTicket {P : Type u_1} (p j : P) (val : ℕ) : BakeryAction P

                    Process p reads number[j] (observing safe-read value val) during the doorway max.

                  • beginWriteNumber {P : Type u_1} (p : P) : BakeryAction P

                    Process p finishes scanning all j : P and begins writing number[p] := acc + 1.

                  • finishWriteNumber {P : Type u_1} (p : P) : BakeryAction P

                    Process p completes writing number[p] and begins writing choosing[p] := false.

                  • finishChoosingFalse {P : Type u_1} (p : P) : BakeryAction P

                    Process p completes writing choosing[p] := false and enters the waiting loop (L2).

                  • pollChoosing {P : Type u_1} (p : P) (val : Bool) : BakeryAction P

                    Process p polls choosing[target] at L2, observing safe-read value val.

                  • pollNumber {P : Type u_1} (p : P) (val : ℕ) : BakeryAction P

                    Process p polls number[target] at L3, observing safe-read value val.

                  • exitCS {P : Type u_1} (p : P) : BakeryAction P

                    Process p leaves CriticalSection and begins resetting number[p] := 0.

                  • finishResetNumber {P : Type u_1} (p : P) : BakeryAction P

                    Process p completes resetting number[p] := 0 and returns to .remainder.

                  • fail {P : Type u_1} (p : P) : BakeryAction P

                    Process p fails into transient .failed state ([Lam74], p. 454).

                  • recover {P : Type u_1} (p : P) : BakeryAction P

                    Process p recovers from .failed to .remainder with zeroed registers.

                  • stutter {P : Type u_1} : BakeryAction P

                    Stuttering step leaving the global state unchanged.

                  Bounded-failure condition ([Lam74], Assertion 3, p. 455): no .fail action occurs at or after step index N.

                  Computable single-step transition function of Lamport's Bakery algorithm over safe registers.

                  Computable multi-step trace evaluator for bakeryStep?.

                  Labelled transition system of Lamport's P-process Bakery algorithm ([Lam74]).

                  Weak per-process step fairness: every process that remains continuously outside .remainder eventually executes a step (completing writes/recoveries, exiting CS, or polling in waiting).

                  Internal transition structure and inductive invariants #

                  Main safety and ordering theorems #

                  theorem Cslib.Distributed.MutualExclusion.bakery_cs_lt {P : Type u_1} [Fintype P] [LinearOrder P] {s : BakeryState P} (hr : s.Reachable) {p q : P} (hcs : s.inCS p) (hbak : s.inBakery q) (hne : p ≠ q) :
                  s.ticket p < s.ticket q

                  Critical-Section Ticket Ordering ([Lam74], Assertion 2, pp. 454–455): in any reachable state s, if p is in its critical section, q is in the bakery, and p ≠ q, then p's ticket strictly precedes q's ticket lexicographically.

                  theorem Cslib.Distributed.MutualExclusion.bakery_mutualExclusion {P : Type u_1} [Fintype P] [LinearOrder P] {s : BakeryState P} (hr : s.Reachable) {p q : P} (hp : s.inCS p) (hq : s.inCS q) :
                  p = q

                  Mutual Exclusion ([Lam74], p. 455): in every reachable state of Lamport's Bakery algorithm — even under arbitrary nondeterministic safe-register overlapping reads and mid-entry failures or halts — at most one process is in CriticalSection.

                  Doorway ordering and First-Come-First-Served #

                  theorem Cslib.Distributed.MutualExclusion.bakery_doorway_lt {P : Type u_1} [Fintype P] [LinearOrder P] {s₁ s₂ : BakeryState P} {μs : List (BakeryAction P)} {sl : List (BakeryState P)} (hex : (bakeryLTS P).Execution s₁ μs s₂ sl) {p q : P} (hstay : ∀ s ∈ sl, s.inBakery p) (hbef : s₁.beforeDoorwayScan q) (hq_bak : s₂.inBakery q) :
                  s₂.ticket p < s₂.ticket q

                  Doorway Ordering Lemma (lexicographic variant of [Lam74], Assertion 1, p. 454): if p has entered the bakery (s₁.inBakery p) before q begins its doorway ticket scan (s₁.beforeDoorwayScan q, in particular when s₁.phase q = .remainder), and p remains in the bakery throughout an execution to s₂ where q is also in the bakery (s₂.inBakery q), then s₂.ticket p < s₂.ticket q.

                  Whereas Assertion 1 in [Lam74] (p. 454) starts before q enters the doorway and states the numeric ticket inequality number[p] < number[q], this lemma starts before q's ticket scan and states the induced lexicographic inequality s₂.ticket p < s₂.ticket q.

                  theorem Cslib.Distributed.MutualExclusion.bakery_fcfs {P : Type u_1} [Fintype P] [LinearOrder P] {s₁ s₂ : BakeryState P} {μs : List (BakeryAction P)} {sl : List (BakeryState P)} (hr₁ : s₁.Reachable) (hex : (bakeryLTS P).Execution s₁ μs s₂ sl) {p q : P} (hwait : ∀ s ∈ sl, s.inWaiting p) (hbef : s₁.beforeDoorwayScan q) :
                  ¬s₂.inCS q

                  First-Come-First-Served ([Lam74], p. 455): if p has completed its doorway and remains in its waiting section throughout a reachable execution starting before q begins its doorway scan (s₁.beforeDoorwayScan q), then q cannot enter CriticalSection.

                  Starvation-free liveness #

                  theorem Cslib.Distributed.MutualExclusion.bakery_starvationFree {P : Type u_1} [Fintype P] [LinearOrder P] {ss : ωSequence (BakeryState P)} {μs : ωSequence (BakeryAction P)} (hreach : (ss 0).Reachable) (hexec : (bakeryLTS P).OmegaExecution ss μs) (hfair : BakeryState.WeakFair ss μs) {N : ℕ} (hbound : BakeryAction.NoFailAfter μs N) {p : P} (hp_nofail : ∀ (k : ℕ), μs k ≠ BakeryAction.fail p) (htry : (ss 0).isTrying p) :
                  ∃ (k : ℕ), (ss k).inCS p

                  Per-Process Starvation Freedom (weak-fairness corollary following [Lam74], Assertion 3, p. 455): in any infinite execution from a reachable state under weak per-process step fairness (BakeryState.WeakFair, under which processes in CriticalSection eventually exit and in-progress writes/recoveries complete) and bounded total failures (BakeryAction.NoFailAfter), every trying process p that never fails (∀ k, μs k ≠ .fail p) eventually enters CriticalSection.

                  Whereas Assertion 3 in [Lam74] (p. 455) states bounded-failure progress for some process in the bakery, this theorem formalizes the ensuing per-process lockout-freedom consequence under explicit weak step fairness and p's no-failure premise.