CS 152 Computer Architecture and Engineering
CS252A Graduate Computer Architecture Lecture 24 - Synchronization

Chris Fletcher
UC Berkeley, Spring 2026

Synchronization

Hardware Atomics

A putsLock wasA getsLock becomesMeaning
1001A owns the lock
1111Held by someone else; retry
0110Unlocked

Load Linked / Store Conditional

Building Atomics with LL/SC

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

Check Your Understanding

1. Why can't two cores implement a mutex with a plain load followed by a store?
Both can load 0 at the same time, then both store 1 and both believe they hold the lock.
2. Name the two conditions that make SC fail.
The LL'd location is modified (or loses ownership) before SC, or there's a context switch between them.

Spin Locks

        addi R2, R0, #1
lockit: EXCH R2, 0(R1)     ; atomic exchange
        bnez R2, lockit    ; already locked -> retry

Test and Test-and-Set

lockit: 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

Trace: Three Processors Contending

StepP0P1P2Line state
1holds lockspin on ld (sees 1)spin on ld (sees 1)Shared
2writes 0 (release)invalidatedinvalidatedExclusive @ P0
3read miss (waits)read miss served firstShared
4waits for bussees 0Shared
5read miss served, sees 0Shared
6EXCH (waits)EXCH wins: gets 0, writes 1Exclusive @ P2
7EXCH: gets 1, writes 1in critical sectionExclusive @ P1
8R2 ≠ 0: back to spinning

Spin Locks Don't Scale

Exponential Backoff

        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:
  • After a failure, wait, doubling the delay each time
  • (+) Far less bus and network contention
  • (−) Unfair: a newcomer with delay 1 can keep stealing the lock from a thread sleeping a long backoff (starvation)

Ticket Locks

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)++;
}
  • P0 gets ticket 0, enters
  • P1 gets 1, P2 gets 2; both spin locally on now_serving
  • P0 releases → now_serving = 1: only P1 enters
  • Then P2: strict FIFO, no stampede, no starvation

Check Your Understanding

1. Why does test-and-test-and-set cut bus traffic vs. spinning on EXCH?
The ld hits a local Shared copy; EXCH is a write that forces an S → M invalidation every iteration.
2. What flaw of backoff locks do ticket locks fix, and how?
Unfairness/starvation. Each arrival takes a sequential ticket via fetch-and-increment and is served strictly in order via now_serving.

Barriers

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);
}

The Naive Barrier Deadlocks in a Loop

  1. Last thread sets count = 0; release = 1
  2. Before a slow thread sees release == 1, a fast thread finishes the next phase and re-enters the barrier
  3. It sees count == 0 and sets release = 0
  4. The slow thread is stuck in barrier 1 forever
  5. Barrier 2's count never reaches total: deadlock

Sense-Reversing Barrier

local_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);
}

Scaling Barriers

Fetch-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.

Check Your Understanding

1. Explain the race that deadlocks the naive barrier in a loop.
A fast thread leaves barrier 1, finishes its work, and re-enters before a slow thread sees release == 1; it resets release = 0, trapping the slow thread in barrier 1.
2. How does a combining tree with fan-in k reduce contention?
Groups of k threads count on local nodes and only the k-th moves up, instead of all N hitting one counter.