Docsity

Understanding Lock Algorithms: From Peterson's to Bakery Algorithm, Slides of Parallel Computing and Programming

Partial preview of the text

Download Understanding Lock Algorithms: From Peterson's to Bakery Algorithm and more Slides Parallel Computing and Programming in PDF only on Docsity!

Mutual Exclusion:

Classical Algorithms for Locks

2

Motivation

Ensure that a data structure is manipulated by only one thread at a time

  • Why? avoid conflicting accesses to shared data (data races) —read/write conflicts —write/write conflicts
  • Approach that we’ll consider today: enforce mutual exclusion
  • Mechanism: lock —operations: acquire, release
  • Usage —acquire lock before accessing the shared data —release lock when finished with the shared data

4

Lock Alternatives

- Transactional memory (TM) - support arbitrary atomic actions on multi-word shared data - transactions that don’t conflict run uninterrupted in parallel - transactions that conflict abort and retry - no need for programmer to worry about deadlock! - note: repeated aborts can waste resources and hurt performance + easy to use, well-understood metaphor - high overhead in software; BG/Q has first HTM (Spring 2012) ± **subject of much active research

  • Ad hoc non-blocking synchronization (NBS)**
    • thread failure/delay cannot prevent progress
    • can be faster than locks (stacks, queues)
    • difficult to write: every new algorithm is a publishable result
    • can be “canned” in libraries (e.g. java.util) atomic (entries > 0) { node *first = head; head = head->next; entries--; return first; }

5

Programmer Effort

System Performance

Coarse Locks Canned NBS Fine Lock Ad Hoc NBS HW TM Software TM (STM)

Synchronization Landscape

Figure credit: William Scherer

7

Topics for Today

Classical locking algorithms using load and store

  • Steps toward a two-thread solution —two partial solutions and their properties
  • Peterson’s algorithm: a two-thread solution
  • Filter lock: an^ n -thread solution
  • Lamport’s bakery lock

8

Classical Lock Algorithms

  • Use atomic load and store only, no stronger atomic primitives
  • Not used in practice —locks based on stronger atomic primitives are more efficient
  • Why study classical lock algorithms? —understand the principles underlying synchronization - ubiquitous in parallel programs —appreciate their subtlety —motivate the need for hardware support

10

Lock

class Lock1: public Lock { private: ! volatile bool flag[2]; ! public: void acquire() { int other_threadid = 1 - self_threadid; flag[self_threadid] = true; while (flag[other_threadid] == true); } void release() { flag[self_threadid] = false; } }

set my flag

wait until other flag

is false

11

Using Lock

flag[0] = true

while(flag[1] == true);

flag[1] = true

flag[0] = false

CS

0

CS

1

flag[1] = false

wait

thread 0 thread 1

while(flag[0] == true);

assume that initially both flags are false

13

Lock

class Lock1: public Lock { private: ! volatile bool flag[2]; ! public: void acquire() { int other_threadid = 1 - self_threadid; flag[self_threadid] = true; while (flag[other_threadid] == true); } void release() { flag[self_threadid] = false; } }

set my flag

wait until other flag

is false

14

Using Lock

flag[0] = true

while(flag[1] == true);

flag[1] = true

wait

thread 0 thread 1

while(flag[0] == true);

wait

deadlock!

16

Lock

class Lock2: public Lock { private: ! volatile int victim; ! public: void acquire() { victim = self_threadid; while (victim == self_threadid); // busy wait } void release() { } }

17

Using Lock

victim = 0

while(victim == 0);

victim = 1

wait

thread 0 thread 1

while(victim == 1);

victim = 0

while(victim == 0);

wait

19

Lock

class Lock2: public Lock { private: ! volatile int victim; ! public: void acquire() { victim = self_threadid; while (victim == self_threadid); // busy wait } void release() { } }

20

Using Lock

thread 0

wait

deadlock!

victim = 0

while(victim == 0);