Push:
Original: top -> e -> e -> e -> ...
Objective: n -> top -> e -> e -> ...
Make n point to top
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.
This comment was marked helpful 0 times.
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.
This comment was marked helpful 0 times.
chaominy
int compare_and_swap (int* reg, int oldval, int newval)
The return value is the old_reg_val.
Push: Original: top -> e -> e -> e -> ... Objective: n -> top -> e -> e -> ...
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.
This comment was marked helpful 0 times.
The calls to
compare_and_swap
act to ensure that the stack has not been modified since the thread's entrance into thepush
orpop
functions.This comment was marked helpful 0 times.
int compare_and_swap (int* reg, int oldval, int newval)
The return value is the old_reg_val.
This comment was marked helpful 0 times.