Docsity

Algorithms for Scalable Locks: Synchronization on Shared-memory Multiprocessors, Slides of Parallel Computing and Programming

Partial preview of the text

Download Algorithms for Scalable Locks: Synchronization on Shared-memory Multiprocessors and more Slides Parallel Computing and Programming in PDF only on Docsity!

Algorithms for Scalable Lock

Synchronization on

Shared-memory Multiprocessors

2

Summary from Last Class

Locks using only load and store

**- O(n) words for one lock for mutual exclusion among n threads

  • O(n) operations required to acquire lock in uncontended case
  • Need more hardware support for better protocols
  • Important issues for lock design** — space — time — properties - provide mutual exclusion fairness - avoid deadlock starvation

4

Load-Linked & Store Conditional

  • load_linked(Word &M) — sets a mark bit for M’s cache line — returns M’s value
  • store_conditional(Word &M, Word V) — if mark bit is set for M’s cache line, store V into M, otherwise fail — condition code indicates success or failure — may spuriously fail if - context switch, another load-link, cache line eviction
  • Arbitrary read-modify-write operations with LL / SC loop forever load linked on M returns V V’ = f (V, ...) // V’ = arbitrary function of V and other values store conditional of V’ into M if store conditional succeeded exit loop
  • Supported on^ Alpha,^ Power,^ MIPS, and^ ARM

5

Test & Set Lock

type Lock = (unlocked, locked)

procedure acquire_lock(Lock *L)

loop

// NOTE: test and set returns old value

if test_and_set(L) = unlocked

return

procedure release_lock(Lock *L)

*L := unlocked

7

Test & Set Lock with Exponential Backoff

type Lock = (unlocked, locked)

procedure acquire_lock(Lock *L)

delay : integer := 1

! // NOTE: test and set returns old value

while test_and_set(L) = locked

pause(delay) // wait this many units of time

delay := delay * 2 // double delay each time

procedure release_lock(Lock *L)

*L := unlocked

Tom Anderson, IEEE TPDS , January 1990

8

Test & Set Lock with Exp. Backoff Notes

  • Grants requests in unpredictable order
  • Starvation is theoretically possible, but unlikely in practice
  • Spins (with backoff) on remote locations
  • Atomic primitives: test_and_set
  • Pragmatics: need to cap probe delay to some maximum

IEEE TPDS , January 1990

10

Ticket Lock Notes

  • Grants requests in FIFO order
  • Spins (with backoff) on remote locations
  • Atomic primitives: fetch_and_increment

11

Anderson’s Array-based Queue Lock

type Lock = record slots: array [0..numprocs -1] of (has_lock, must_wait) := (has_lock, must_wait, must_wait, ..., must_wait) // each element of slots should lie in a different memory module or cache line int next_slot := 0 // parameter my_place, below, points to a private variable in an enclosing scope procedure acquire_lock (Lock *L, int *my_place) *my_place := fetch_and_increment (&L->next_slot) if *my_place mod numprocs = 0 // decrement to avoid problems with overflow; ignore return value atomic_add(&L->next_slot,-numprocs) my_place := my_place mod numprocs repeat while L->slots[my_place] = must_wait // spin L->slots[my_place] := must_wait // init for next time procedure release_lock (Lock *L, int my_place) L->slots[(my_place + 1) mod numprocs] := has_lock

13

Anderson’s Lock Notes

  • Grants requests in FIFO order
  • Space:^ O(pn)^ space for^ p^ processes and^ n^ locks
  • Spins only on local locations on a cache-coherent machine
  • Atomic primitives: fetch_and_increment and atomic_add IEEE TPDS , January 1990

14

The MCS List-based Queue Lock

type qnode = record qnode *next bool locked type qnode *Lock // initialized to nil // parameter I, below, points to a qnode record allocated (in an enclosing scope) in // shared memory locally-accessible to the invoking processor procedure acquire_lock (Lock *L, qnode *I) I->next := nil qnode *predecessor := fetch_and_store (L, I) if predecessor != nil // queue was non-empty I->locked := true predecessor->next := I repeat while I->locked // spin procedure release_lock (Lock *L, qnode *I) if I->next = nil // no known successor if compare_and_swap (L, I, nil) return // compare_and_swap returns true iff it stored repeat while I->next = nil // spin I->next->locked := false

16 tail 1 2 3 4 run spin spin

MCS Lock In Action - II

  • Process 4 swaps self into tail pointer
  • Acquires pointer to predecessor (3) from swap on tail
  • 3 can’t leave without noticing that one or more successors

will link in behind it because the tail no longer points to 3

arriving

17 tail 1 2 3 4 run spin spin

MCS Lock In Action - III

4 links behind predecessor (3)

arriving

19 tail 1 2 3 4 leaving spin spin

MCS Lock In Action - V

  • Process 1 prepares to release lock — if it’s next field is set, signal successor directly — suppose 1’s next pointer is still null - attempt a compare_and_swap on the tail pointer - finds that tail no longer points to self - waits until successor pointer is valid (already points to 2 in diagram) - signal successor (process 2) spin

20 tail 2 3 4 run spin

MCS Lock In Action - VI

leaving (^) spin