Top Banner
Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit
108

Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Dec 14, 2015

Download

Documents

Colleen Knight
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Mutual Exclusion

Companion slides forThe Art of Multiprocessor Programming

by Maurice Herlihy & Nir Shavit

Page 2: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

5

Mutual Exclusion

• We will clarify our understanding of mutual exclusion

• We will also show you how to reason about various properties in an asynchronous concurrent setting

Page 3: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Mutual Exclusion

In his 1965 paper E. W. Dijkstra wrote:

"Given in this paper is a solution to a problem which, to the knowledge of the author, has been an open question since at least 1962, irrespective of the solvability. [...] Although the setting of the problem might seem somewhat academic at first, the author trusts that anyone familiar with the logical problems that arise in computer coupling will appreciate the significance of the fact that this problem indeed can be solved."

Art of Multiprocessor Programming

6

Page 4: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

7

Mutual Exclusion

• Formal problem definitions• Solutions for 2 threads• Solutions for n threads• Fair solutions• Inherent costs

Page 5: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

8

Warning

• You will never use these protocols– Get over it

• You are advised to understand them– The same issues show up everywhere– Except hidden and more complex

Page 6: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

9

Why is Concurrent Programming so Hard?

• Try preparing a seven-course banquet– By yourself– With one friend– With twenty-seven friends …

• Before we can talk about programs– Need a language– Describing time and concurrency

Page 7: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

10

• “Absolute, true and mathematical time, of itself and from its own nature, flows equably without relation to anything external.” (Isaac Newton, 1689)

• “Time is what keeps everything from happening at once.” (Ray Cummings, 1922)

Time

time

Page 8: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

11

time

• An event a0 of thread A is– Instantaneous– No simultaneous events (break ties)

a0

Events

Page 9: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

12

time

• A thread A is (formally) a sequence a0, a1, ... of events – “Trace” model– Notation: a0 a1 indicates order

a0

Threads

a1 a2 …

Page 10: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

13

• Assign to shared variable• Assign to local variable• Invoke method• Return from method• Lots of other things …

Example Thread Events

Page 11: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

14

Threads are State Machines

Events are transitions

a0

a1a2

a3

Page 12: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

15

States

• Thread State– Program counter– Local variables

• System state– Object fields (shared variables)– Union of thread states

Page 13: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

16

time

• Thread A

Concurrency

Page 14: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

17

time

time

• Thread A

• Thread B

Concurrency

Page 15: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

18

time

Interleavings

• Events of two or more threads– Interleaved– Not necessarily independent (why?)

Page 16: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

19

time

• An interval A0 =(a0,a1) is– Time between events a0 and a1

a0 a1

Intervals

A0

Page 17: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

20

time

Intervals may Overlap

a0 a1A0

b0 b1B0

Page 18: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

21

time

Intervals may be Disjoint

a0 a1A0

b0 b1B0

Page 19: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

22

time

Precedence

a0 a1A0

b0 b1B0

Interval A0 precedes interval B0

Page 20: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

23

Precedence

• Notation: A0 B0

• Formally,– End event of A0 before start event of B0

– Also called “happens before” or “precedes”

Page 21: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

24

Precedence Ordering

• Remark: A0 B0 is just like saying – 1066 AD 1492 AD, – Middle Ages Renaissance,

• Oh wait, – what about this week vs this month?

Page 22: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

25

Precedence Ordering

• Never true that A A

• If A B then not true that B A

• If A B & B C then A C

• Funny thing: A B & B A might both be false!

Page 23: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

26

Partial Orders(review)

• Irreflexive:– Never true that A A

• Antisymmetric:– If A B then not true that B A

• Transitive:– If A B & B C then A C

Page 24: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

27

Total Orders(review)

• Also– Irreflexive– Antisymmetric– Transitive

• Except that for every distinct A, B,– Either A B or B A

Page 25: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

28

Repeated Events

while (mumble) { a0; a1;}

a0k

k-th occurrence of event a0

A0k

k-th occurrence of interval A0 =(a0,a1)

Page 26: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

30

Locks (Mutual Exclusion)

public interface Lock {

public void lock();

public void unlock();}

Page 27: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

31

Locks (Mutual Exclusion)

public interface Lock {

public void lock();

public void unlock();}

acquire lock

Page 28: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

32

Locks (Mutual Exclusion)

public interface Lock {

public void lock();

public void unlock();} release lock

acquire lock

Page 29: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

33

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; }}

Page 30: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

34

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; }}

acquire Lock

Page 31: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

35

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; }}

Release lock(no matter what)

Page 32: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

36

Using Locks

public class Counter { private long value; private Lock lock; public long getAndIncrement() { lock.lock(); try { int temp = value; value = value + 1; } finally { lock.unlock(); } return temp; }}

critical section

Page 33: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

37

Mutual Exclusion

• Let CSik be thread i's k-th critical

section execution

Page 34: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

38

Mutual Exclusion

• Let CSik be thread i's k-th critical

section execution• And CSj

m be thread j's m-th critical section execution

Page 35: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

39

Mutual Exclusion

• Let CSik be thread i's k-th critical

section execution• And CSj

m be j's m-th execution

• Then either– or

Page 36: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

40

Mutual Exclusion

• Let CSik be thread i's k-th critical

section execution• And CSj

m be j's m-th execution

• Then either– or

CSik CSj

m

Page 37: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

41

Mutual Exclusion

• Let CSik be thread i's k-th critical

section execution• And CSj

m be j's m-th execution

• Then either– or

CSik CSj

m

CSjm CSi

k

Page 38: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

42

Deadlock-Free

• If some thread calls lock()– And never returns– Then other threads must complete lock()

and unlock() calls infinitely often• System as a whole makes progress

– Even if individuals starve

Page 39: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

43

Starvation-Free

• If some thread calls lock()– It will eventually return

• Individual threads make progress

Page 40: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

44

Two-Thread vs n-Thread Solutions

• 2-thread solutions first– Illustrate most basic ideas– Fits on one slide

• Then n-thread solutions

Page 41: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

45

class … implements Lock { … // thread-local index, 0 or 1 public void lock() { int i = ThreadID.get(); int j = 1 - i; … }}

Two-Thread Conventions

Page 42: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

46

class … implements Lock { … // thread-local index, 0 or 1 public void lock() { int i = ThreadID.get(); int j = 1 - i; … } }

Two-Thread Conventions

Henceforth: i is current thread, j is other thread

Page 43: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

LockOneclass LockOne implements Lock {private boolean[] flag = new boolean[2];public void lock() { flag[i] = true; while (flag[j]) {} }

Page 44: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

LockOneclass LockOne implements Lock {private boolean[] flag = new boolean[2];public void lock() { flag[i] = true; while (flag[j]) {} } Each thread has flag

Page 45: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

LockOneclass LockOne implements Lock {private boolean[] flag = new boolean[2];public void lock() { flag[i] = true; while (flag[j]) {} } Set my flag

Page 46: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

LockOneclass LockOne implements Lock {private boolean[] flag = new boolean[2];public void lock() { flag[i] = true; while (flag[j]) {} }

Wait for other flag to become false

Page 47: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

51

• Assume CSAj overlaps CSB

k

• Consider each thread's last– (jth and kth) read and write …– in lock() before entering

• Derive a contradiction

LockOne Satisfies Mutual Exclusion

Page 48: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

52

• writeA(flag[A]=true) readA(flag[B]==false) CSA

• writeB(flag[B]=true) readB(flag[A]==false) CSB

From the Code

class LockOne implements Lock {… public void lock() { flag[i] = true; while (flag[j]) {} }

Page 49: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

53

• readA(flag[B]==false) writeB(flag[B]=true)

• readB(flag[A]==false) writeA(flag[A]=true)

From the Assumption

Page 50: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

54

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 51: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

55

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 52: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

56

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 53: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

57

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 54: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

58

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 55: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

59

• Assumptions:– readA(flag[B]==false) writeB(flag[B]=true)

– readB(flag[A]==false) writeA(flag[A]=true)

• From the code– writeA(flag[A]=true) readA(flag[B]==false)

– writeB(flag[B]=true) readB(flag[A]==false)

Combining

Page 56: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

60

Cycle!

Impossible in

a

partial o

rder

Page 57: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

61

Deadlock Freedom

• LockOne Fails deadlock-freedom– Concurrent execution can deadlock

– Sequential executions OK

flag[i] = true; flag[j] = true; while (flag[j]){} while (flag[i]){}

Page 58: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

62

LockTwopublic class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; }

public void unlock() {}}

Page 59: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

63

LockTwopublic class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; }

public void unlock() {}}

Let other go first

Page 60: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

64

LockTwopublic class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; }

public void unlock() {}}

Wait for permission

Page 61: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

65

LockTwopublic class Lock2 implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {}; }

public void unlock() {}}

Nothing to do

Page 62: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

66

public void LockTwo() { victim = i; while (victim == i) {}; }

LockTwo Claims

• Satisfies mutual exclusion– If thread i in CS– Then victim == j– Cannot be both 0 and 1

• Not deadlock free– Sequential execution

deadlocks– Concurrent execution does not

Page 63: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

67

Peterson's Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}public void unlock() { flag[i] = false;}

Page 64: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

68

Peterson's Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}public void unlock() { flag[i] = false;}

Announce I'm interested

Page 65: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

69

Peterson's Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}public void unlock() { flag[i] = false;}

Announce I'm interested

Defer to other

Page 66: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

70

Peterson's Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}public void unlock() { flag[i] = false;}

Announce I'm interested

Defer to other

Wait while other interested & I'm

the victim

Page 67: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

71

Peterson's Algorithm

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {}; }public void unlock() { flag[i] = false;}

No longer interested

Announce I'm interested

Defer to other

Wait while other interested & I'm

the victim

Page 68: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

72

Mutual Exclusion

(1) writeB(Flag[B]=true)writeB(victim=B)

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}

From the Code

Page 69: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

73

Also from the Code

(2) writeA(victim=A)readA(flag[B]) readA(victim)

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}

Page 70: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

74

Assumption

W.L.O.G. assume A is the last thread to write victim

(3) writeB(victim=B)writeA(victim=A)

Page 71: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

75

Combining Observations

(1) writeB(flag[B]=true)writeB(victim=B)

(3) writeB(victim=B)writeA(victim=A)

(2) writeA(victim=A)readA(flag[B]) readA(victim)

Page 72: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

76

Combining Observations

(1) writeB(flag[B]=true)writeB(victim=B)

(3) writeB(victim=B)writeA(victim=A)

(2) writeA(victim=A)readA(flag[B]) readA(victim)

Page 73: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

77

Combining Observations

(1) writeB(flag[B]=true)writeB(victim=B)

(3) writeB(victim=B)writeA(victim=A)

(2) writeA(victim=A)readA(flag[B]) readA(victim)

A read flag[B] == true and victim == A, so it could not have entered the CS (QED)

Page 74: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

78

Deadlock Free

• Thread blocked – only at while loop– only if other's flag is true– only if it is the victim

• Solo: other's flag is false• Both: one or the other not the victim

public void lock() { … while (flag[j] && victim == i) {};

Page 75: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

79

Starvation Free

• Thread i blocked only if j repeatedly re-enters so that

flag[j] == true and victim == i

• When j re-enters– it sets victim to j.– So i gets in

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}

public void unlock() { flag[i] = false; }

Page 76: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

98

Bounded Waiting

• Want stronger fairness guarantees• Thread not “overtaken” too much• If A starts before B, then A enters

before B?• But what does “start” mean?• Need to adjust definitions ….

Page 77: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

99

Bounded Waiting

• Divide lock() method into 2 parts:– Doorway interval:

• Written DA

• always finishes in finite steps

– Waiting interval:• Written WA

• may take unbounded steps

Page 78: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

100

• For threads A and B:– If DA

k DB j

• A's k-th doorway precedes B's j-th doorway

– Then CSAk CSB

j+r

• A's k-th critical section precedes B's j+r-th critical section

• B cannot overtake A more than r times

• First-come-first-served r = 0

r-Bounded Waiting

Page 79: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

101

What is “r” for Peterson's Algorithm?

public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {};}public void unlock() { flag[i] = false;}

Answer: r = 0

Page 80: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

103

• For threads A and B:– If DA

k DB j

• A's k-th doorway precedes B's j-th doorway

– Then CSAk CSB

j

• A's k-th critical section precedes B's j-th critical section

• B cannot overtake A

First-Come-First-Served

Page 81: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

105

Bakery Algorithm

• Provides First-Come-First-Served for n threads

• How?– Take a “number”– Wait until lower numbers have been served

• Lexicographic order– (a,i) > (b,j)

• If a > b, or a = b and i > j

Page 82: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

106

Bakery Algorithmclass Bakery implements Lock { boolean[] flag; Label[] label; public Bakery (int n) { flag = new boolean[n]; label = new Label[n]; for (int i = 0; i < n; i++) { flag[i] = false; label[i] = 0; } } …

Page 83: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

107

Bakery Algorithmclass Bakery implements Lock { boolean[] flag; Label[] label; public Bakery (int n) { flag = new boolean[n]; label = new Label[n]; for (int i = 0; i < n; i++) { flag[i] = false; label[i] = 0; } } …

n-10

f f f f t ft

2

f

0 0 0 0 5 04 0

6

CS

Page 84: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

108

Bakery Algorithm

class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while ($k flag[k] && (label[i],i) > (label[k],k)); }

Page 85: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

109

Bakery Algorithm

class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while ($k flag[k] && (label[i],i) > (label[k],k)); }

Doorway

Page 86: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

110

Bakery Algorithm

class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while ($k flag[k] && (label[i],i) > (label[k],k)); }

I'm interested

Page 87: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

111

Bakery Algorithm

class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while ($k flag[k] && (label[i],i) > (label[k],k)); }

Take increasing label (read labels in some arbitrary

order)

Page 88: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

112

Bakery Algorithm

class Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while ($k flag[k] && (label[i],i) > (label[k],k)); }

Someone is interested

Page 89: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

113

Bakery Algorithmclass Bakery implements Lock { boolean flag[n]; int label[n];

public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while ($k flag[k] && (label[i],i) > (label[k],k)); }

Someone is interested …

… whose (label,i) in lexicographic order is lower

Page 90: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

114

Bakery Algorithm

class Bakery implements Lock { …

public void unlock() { flag[i] = false; }}

Page 91: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

115

Bakery Algorithm

class Bakery implements Lock { …

public void unlock() { flag[i] = false; }}

No longer interested

labels are always increasing

Page 92: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

116

No Deadlock

• There is always one thread with earliest label

• Ties are impossible (why?)

Page 93: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

117

First-Come-First-Served

• If DA DB then– A's label is smaller

• And:– writeA(label[A])

– readB(label[A])

– writeB(label[B]) readB(flag[A])

• So B sees– smaller label for A – locked out while flag[A] is

true

class Bakery implements Lock {

public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1; while ($k flag[k] && (label[i],i) > (label[k],k));

}

Page 94: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

118

Mutual Exclusion

• Suppose A and B in CS together• Suppose A has earlier label• When B entered, it must have seen

– flag[A] is false, or– label[A] > label[B]

class Bakery implements Lock { public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1; while ($k flag[k] && (label[i],i) > (label[k],k));

}

Page 95: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

119

Mutual Exclusion

• Labels are strictly increasing so

• B must have seen flag[A] == false

Page 96: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

120

Mutual Exclusion

• Labels are strictly increasing so

• B must have seen flag[A] == false• LabelingB readB(flag[A])

writeA(flag[A]) LabelingA

Page 97: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

121

Mutual Exclusion

• Labels are strictly increasing so

• B must have seen flag[A] == false• LabelingB readB(flag[A])

writeA(flag[A]) LabelingA

• Which contradicts the assumption that A has an earlier label

Page 98: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

122

Bakery Y232K Bugclass Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while ($k flag[k] && (label[i],i) > (label[k],k)); }

Page 99: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

123

Bakery Y232K Bugclass Bakery implements Lock { … public void lock() { flag[i] = true; label[i] = max(label[0], …,label[n-1])+1;

while ($k flag[k] && (label[i],i) > (label[k],k)); }

Mutex breaks if label[i] overflows

Page 100: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

124

Does Overflow Actually Matter?

• Yes– Y2K– 18 January 2038 (Unix time_t rollover)– 16-bit counters

• No– 64-bit counters

• Maybe– 32-bit counters

Page 101: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

145

Deep Philosophical Question

• The Bakery Algorithm is– Succinct,– Elegant, and– Fair.

• Q: So why isn't it practical?• A: Well, you have to read N distinct

variables

Page 102: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

146

Shared Memory

• Shared read/write memory locations called Registers (historical reasons)

• Come in different flavors– Multi-Reader-Single-Writer (flag[])– Multi-Reader-Multi-Writer (victim[])– Not that interesting: SRMW and SRSW

Page 103: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

147

Theorem

At least N MRSW (multi-reader/single-writer) registers are needed to solve deadlock-free mutual exclusion.

N registers such as flag[]…

Page 104: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

157

Theorem

Deadlock-free mutual exclusion for 3 threads requires at least 3 multi-reader multi-writer registers

Page 105: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

158

Theorem

Deadlock-free mutual exclusion for n threads requires at least n multi-reader multi-writer registers

Page 106: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

169

Summary of Lecture

• In the 1960's several incorrect solutions to starvation-free mutual exclusion using RW-registers were published…

• Today we know how to solve FIFO N thread mutual exclusion using 2N RW-Registers

Page 107: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

170

Summary of Lecture

• N RW-Registers inefficient– Because writes “cover” older writes

• Need stronger hardware operations – that do not have the “covering problem”

• In next lectures - understand what these operations are…

Page 108: Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

171

         This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.

• You are free:– to Share — to copy, distribute and transmit the work – to Remix — to adapt the work

• Under the following conditions:– Attribution. You must attribute the work to “The Art of

Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work).

– Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.

• For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to– http://creativecommons.org/licenses/by-sa/3.0/.

• Any of the above conditions can be waived if you get permission from the copyright holder.

• Nothing in this license impairs or restricts the author's moral rights.