b: 0 = free, 1 = locked; with plain loads and stores:
load b → 0load b → 0store b, 1: both think they own itEXCH): swap register and memory| A puts | Lock was | A gets | Lock becomes | Meaning |
|---|---|---|---|---|
| 1 | 0 | 0 | 1 | A owns the lock |
| 1 | 1 | 1 | 1 | Held by someone else; retry |
| 0 | 1 | 1 | 0 | Unlocked |
cas R1, R2, M: if R1 == M, swap R2 and MLR/SC) split it into two:
Atomic exchange (R4 ↔ 0(R1))
try: mov R3, R4 # SC clobbers R3
ll R2, 0(R1) # old value
sc R3, 0(R1) # 1 ok / 0 fail
beqz R3, try
mov R4, R2
mov R3, R4 sits inside the loop because SC overwrites R3 with its status
Fetch-and-increment
try: ll R2, 0(R1)
addi R3, R2, #1
sc R3, 0(R1)
beqz R3, try
addi R2, R0, #1
lockit: EXCH R2, 0(R1) ; atomic exchange
bnez R2, lockit ; already locked -> retry
EXCH is a write: needs S → M every iterationlockit: ld R2, 0(R1) ; read in Shared state
bnez R2, lockit ; spin locally, no bus traffic
addi R2, R0, #1
EXCH R2, 0(R1) ; looked free: try to grab
bnez R2, lockit ; lost the race: back to reading
ll; bnez; addi; sc; beqzld until 0, then cas expecting 0 → 1| Step | P0 | P1 | P2 | Line state |
|---|---|---|---|---|
| 1 | holds lock | spin on ld (sees 1) | spin on ld (sees 1) | Shared |
| 2 | writes 0 (release) | invalidated | invalidated | Exclusive @ P0 |
| 3 | read miss (waits) | read miss served first | Shared | |
| 4 | waits for bus | sees 0 | Shared | |
| 5 | read miss served, sees 0 | Shared | ||
| 6 | EXCH (waits) | EXCH wins: gets 0, writes 1 | Exclusive @ P2 | |
| 7 | EXCH: gets 1, writes 1 | in critical section | Exclusive @ P1 | |
| 8 | R2 ≠ 0: back to spinning |
EXCH li R3, 1 ; delay
lockit: ll R2, 0(R1)
bnez R2, lockit
addi R2, R2, #1
sc R2, 0(R1)
bnez R2, gotit ; success
sll R3, R3, 1 ; double delay
pause R3
j lockit
gotit:
void ticketLock_acquire(int *next_ticket,
int *now_serving) {
int my_ticket = fetch_and_inc(next_ticket);
while (*now_serving != my_ticket) {}
}
void ticketLock_release(int *now_serving) {
(*now_serving)++;
}
now_servingnow_serving = 1: only P1 entersEXCH?
ld hits a local Shared copy; EXCH is a write that forces an S → M invalidation every iteration.lock(counterlock);
if (count == 0) release = 0; /* first: reset flag */
count++;
unlock(counterlock);
if (count == total) { /* last arrival */
count = 0;
release = 1;
} else {
spin(release == 1);
}
count = 0; release = 1release == 1, a fast thread finishes the next phase and re-enters the barriercount == 0 and sets release = 0local_sense = !local_sense; /* private, toggles each barrier */
lock(counterlock);
count++;
int my_count = count;
unlock(counterlock);
if (my_count == total) {
count = 0;
release = local_sense;
} else {
spin(release == local_sense);
}
release == 0release == 1 and never touches release until all arriveFetch-and-increment: no counter lock
local_sense = !local_sense;
int my_count = fetch_and_inc(count);
if (my_count + 1 == total) {
count = 0;
release = local_sense;
} else {
spin(release == local_sense);
}
Combining tree, fan-in k
void Barrier(int mynode) {
lock(tree[mynode].counterlock);
tree[mynode].count++;
unlock(tree[mynode].counterlock);
if (tree[mynode].count == k) {
tree[mynode].count = 0;
if (tree[mynode].parent >= 0)
Barrier(tree[mynode].parent);
else
release = local_sense;
} else {
spin(release == local_sense);
}
}
Each node absorbs k arrivals and sends one signal up: contention drops k× per level.