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:
- While
pis mid-write onchoosing[p](.writeChoosingTrue,.writeChoosingFalse) or in.failed, a concurrent read ofchoosing[p]may nondeterministically return anyBoolvalue (BakeryState.SafeReadChoosing). - While
pis mid-write onnumber[p](.writeNumber,.writeNumberZero) or in.failed, a concurrent read ofnumber[p]may nondeterministically return anyℕvalue (BakeryState.SafeReadNumber). - During the doorway maximum calculation (
L1), a process scansnumber[j]acrossj : Pin any order (BakeryPhase.readMax).
Main definitions #
Cslib.Distributed.MutualExclusion.BakeryPhase: per-process control state covering remainder, split-phase doorway writes and arbitrary-order ticket reads,L2/L3waiting polls, critical section, split-phase exit reset, and transient failure.Cslib.Distributed.MutualExclusion.BakeryState: global configuration(phase, choosing, number)with state predicatesisWritingChoosing,isWritingNumber,SafeReadChoosing,SafeReadNumber,inDoorway,inWaiting,inCS,inBakery,isTrying,beforeDoorwayScan,ticket,Reachable, andWeakFair.Cslib.Distributed.MutualExclusion.BakeryAction: transition labels carrying the acting process and any nondeterministic safe-read value, plusactorandNoFailAfter.Cslib.Distributed.MutualExclusion.bakeryStep?,Cslib.Distributed.MutualExclusion.bakeryStepTrace?: computable single-step and multi-step transition evaluators.Cslib.Distributed.MutualExclusion.bakeryLTS: theCslib.LTSof Lamport's Bakery algorithm.
Main results #
Cslib.Distributed.MutualExclusion.bakery_doorway_lt: Lexicographic Doorway Ordering (variant of [Lam74], Assertion 1, p. 454) — ifpis in the bakery beforeqbegins its doorway ticket scan (s₁.beforeDoorwayScan q, whereas Assertion 1 starts before doorway entry and concludes the numeric inequalitynumber[p] < number[q]) andpremains in the bakery untilqenters the bakery, thens₂.ticket p < s₂.ticket q.Cslib.Distributed.MutualExclusion.bakery_cs_lt: Critical-Section Ticket Ordering ([Lam74], Assertion 2, pp. 454–455) — ifpis inCriticalSectionandq ≠ pis in the bakery in any reachable state, thens.ticket p < s.ticket q.Cslib.Distributed.MutualExclusion.bakery_mutualExclusion: Mutual Exclusion ([Lam74], p. 455) — in every reachable state (under arbitrary overlapping safe reads and mid-entry failures/halts),s.inCS p → s.inCS q → p = q.Cslib.Distributed.MutualExclusion.bakery_fcfs: First-Come-First-Served ([Lam74], p. 455) — ifpcompletes its doorway beforeqbegins its doorway scan,qcannot enterCriticalSectionwhilepremains in its waiting section.Cslib.Distributed.MutualExclusion.bakery_starvationFree: Per-Process Starvation Freedom (per-process weak-fairness corollary following [Lam74], Assertion 3, p. 455) — whereas Assertion 3 states some-process progress under bounded failures, this theorem derives per-processCriticalSectionentry under explicit weak per-process step fairness (BakeryState.WeakFair), bounded total failures (BakeryAction.NoFailAfter), and the target's no-failure premise.
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 settingchoosing[p] := true. - readMax {P : Type u_1} (scanned : Finset P) (acc : ℕ) : BakeryPhase P
Doorway (
L1): readingnumber[j]in arbitrary order (scannedread so far, maxacc). - writeNumber {P : Type u_1} (nextTicket : ℕ) : BakeryPhase P
Doorway (
L1): mid-write settingnumber[p] := nextTicket. - writeChoosingFalse {P : Type u_1} : BakeryPhase P
Doorway (
L1): mid-write resettingchoosing[p] := falseafternumber[p]is written. - waitL2 {P : Type u_1} (checked : Finset P) (target : P) : BakeryPhase P
Waiting loop (
L2): pollingchoosing[target]until a safe read returnsfalse. - waitL3 {P : Type u_1} (checked : Finset P) (target : P) : BakeryPhase P
Waiting loop (
L3): pollingnumber[target]until0or(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 resetschoosing[p] = falseandnumber[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].
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
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.
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
pleaves.remainderand begins writingchoosing[p] := true. - finishChoosingTrue {P : Type u_1} (p : P) : BakeryAction P
Process
pcompletes writingchoosing[p] := trueand begins scanningnumber. - readTicket {P : Type u_1} (p j : P) (val : ℕ) : BakeryAction P
Process
preadsnumber[j](observing safe-read valueval) during the doorwaymax. - beginWriteNumber {P : Type u_1} (p : P) : BakeryAction P
Process
pfinishes scanning allj : Pand begins writingnumber[p] := acc + 1. - finishWriteNumber {P : Type u_1} (p : P) : BakeryAction P
Process
pcompletes writingnumber[p]and begins writingchoosing[p] := false. - finishChoosingFalse {P : Type u_1} (p : P) : BakeryAction P
Process
pcompletes writingchoosing[p] := falseand enters the waiting loop (L2). - pollChoosing {P : Type u_1} (p : P) (val : Bool) : BakeryAction P
Process
ppollschoosing[target]atL2, observing safe-read valueval. - pollNumber {P : Type u_1} (p : P) (val : ℕ) : BakeryAction P
Process
ppollsnumber[target]atL3, observing safe-read valueval. - exitCS {P : Type u_1} (p : P) : BakeryAction P
Process
pleavesCriticalSectionand begins resettingnumber[p] := 0. - finishResetNumber {P : Type u_1} (p : P) : BakeryAction P
Process
pcompletes resettingnumber[p] := 0and returns to.remainder. - fail {P : Type u_1} (p : P) : BakeryAction P
- recover {P : Type u_1} (p : P) : BakeryAction P
- stutter {P : Type u_1} : BakeryAction P
Stuttering step leaving the global state unchanged.
Acting process of a BakeryAction (none for .stutter).
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]).
A BakeryState is reachable if it is reachable from BakeryState.init P in bakeryLTS P.
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 #
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.
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 #
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.
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 #
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.