Overview
ThePetersonLock class implements Peterson’s algorithm for mutual exclusion between exactly two threads (robots). It’s a classic software-based synchronization algorithm that guarantees mutual exclusion, progress, and bounded waiting without hardware support.
Constructor
Creates a new Peterson lock for two participants (indexed 0 and 1).Initial State:
flag:[false, false]turn:0owner:null
Properties
Two-element array where
flag[i] is true when robot i wants to enter the critical section.Indicates who yields in case of conflict (0 or 1). The robot that sets
turn to the other robot’s ID is willing to wait.The robot ID (0 or 1) currently in the critical section, or
null if no owner.Methods
lock(i)
Attempts to acquire the lock for roboti.
The robot ID (0 or 1) attempting to acquire the lock.
true: Robot successfully acquired the lock and can enter critical sectionfalse: Robot must wait (other robot has priority)
- Sets
flag[i] = trueto indicate intent to enter - Sets
turn = otherto give the other robot priority - Checks Peterson’s wait condition:
flag[other] && turn === other - If condition is true, must wait (return
false) - Otherwise, acquires lock and returns
true
petersonLock.js:13
unlock(i)
Releases the lock for roboti.
The robot ID (0 or 1) releasing the lock.
- Sets
flag[i] = falseto indicate robot no longer wants to enter - Clears
ownerif this robot was the owner - No return value (void)
petersonLock.js:28
Usage Example
Peterson’s Algorithm Explained
The algorithm ensures mutual exclusion through two mechanisms:1. Intent Declaration (flag)
Each robot declares intent before trying to enter:2. Conflict Resolution (turn)
If both robots want to enter simultaneously,turn determines who waits:
3. Wait Condition
A robot must wait if:- The other robot wants to enter (
flag[other] === true) - AND it’s the other robot’s turn (
turn === other)
Correctness Properties
Mutual Exclusion
Only one robot can be in the critical section at a time. Proof sketch: If both robots are in CS, both must have seenflag[other] === false or turn === self. But the last robot to set turn set it to other, so it would have waited.
Progress
If no robot is in the critical section and one wants to enter, it will eventually enter. Proof sketch: If only one robot wants to enter (flag[0] = true, flag[1] = false), the wait condition flag[other] && turn === other is false, so it enters immediately.
Bounded Waiting
A robot waiting to enter will enter before the other robot enters twice. Proof sketch: After robot 0 enters and exits, it setsflag[0] = false. If robot 1 is waiting, it will enter next. Even if robot 0 wants to re-enter, it will set turn = 1, giving robot 1 priority.
Timeline Example
Race Conditions Handled
Both try to enter simultaneously:
turn last gets to enter (its competitor sees the updated turn value).
Limitations
- Two threads only: Peterson’s algorithm works for exactly 2 participants
- Busy waiting: The waiting robot repeatedly checks the condition (no blocking)
- Memory model assumptions: Requires sequential consistency (modern CPUs may need memory barriers)
Busy-Wait Pattern
In a real implementation, the calling code would retry in a loop:Comparison with Mutex
| Feature | PetersonLock | Mutex |
|---|---|---|
| Participants | Exactly 2 | Any number |
| Waiting | Busy-wait (active) | Queue (passive) |
| Hardware support | None required | May use atomic operations |
| Fairness | Alternation guaranteed | FIFO queue |
| Use case | Educational/embedded | General purpose |
Educational Value
Peterson’s algorithm demonstrates:- Software-only synchronization is possible
- The importance of memory ordering
- How to reason about concurrent algorithms
- Classic mutual exclusion problem solving