Previous | Next --- Slide 15 of 24
Back to Lecture Thumbnails
Avesh

Push: Original: top -> e -> e -> e -> ... Objective: n -> top -> e -> e -> ...

  1. Make n point to top
  2. Try to have top point to n. If this fails, restart

This code is different from a spin lock because in locking code, a thread could acquire the lock and then sit for a while (perhaps there's some logic to compute, perhaps it got a pagefault, etc). Now all other threads cannot access the stack.

Here, if a thread freezes before modifying top, this won't halt progress.

gbarboza

The calls to compare_and_swap act to ensure that the stack has not been modified since the thread's entrance into the push or pop functions.

chaominy

int compare_and_swap (int* reg, int oldval, int newval)
The return value is the old_reg_val.