mutual exclusion - puc-rionoemi/pcp-10/aula2maths.pdf · 2010. 8. 17. · mutual exclusion •...

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

Upload: others

Post on 22-Sep-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Mutual Exclusion

Companion slides for The Art of Multiprocessor

Programming by Maurice Herlihy & Nir Shavit

Page 2: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

2

Mutual Exclusion

•  Today we will try to formalize our understanding of mutual exclusion

•  We will also use the opportunity to show you how to argue about and prove various properties in an asynchronous concurrent setting

Page 3: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

3

Mutual Exclusion

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

Page 4: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

4

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 5: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

5

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 6: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

6

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

•  “Time is, like, Nature’s way of making sure that everything doesn’t happen all at once.” (Anonymous, circa 1968)

Time

time

Page 7: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

7

time

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

a0

Events

Page 8: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

8

time

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

a0

Threads

a1 a2 …

Page 9: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

9

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

Example Thread Events

Page 10: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

10

Threads are State Machines

Events are transitions

a0

a1 a2

a3

Page 11: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

11

States

•  Thread State –  Program counter –  Local variables

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

Page 12: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

12

time

•  Thread A

Concurrency

Page 13: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

13

time

time

•  Thread A

•  Thread B

Concurrency

Page 14: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

14

time

Interleavings

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

Page 15: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

15

time

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

a0 a1

Intervals

A0

Page 16: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

16

time

Intervals may Overlap

a0 a1 A0

b0 b1 B0

Page 17: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

17

time

Intervals may be Disjoint

a0 a1 A0

b0 b1 B0

Page 18: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

18

time

Precedence

a0 a1 A0

b0 b1 B0

Interval A0 precedes interval B0

Page 19: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

19

Precedence

•  Notation: A0 B0 •  Formally,

–  End event of A0 before start event of B0

–  Also called “happens before” or “precedes”

Page 20: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

20

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 21: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

21

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 22: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

22

Partial Orders (you may know this already)

•  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 23: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

23

Total Orders (you may know this already)

•  Also –  Irreflexive –  Antisymmetric –  Transitive

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

Page 24: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

24

Repeated Events while (mumble) {

a0; a1;

}

a0k

k-th occurrence of event a0

A0k

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

Page 25: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

25

Implementing a Counter

public class Counter { private long value;

public long getAndIncrement() { temp = value; value = temp + 1; return temp; } }

Make these steps indivisible using

locks

Page 26: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

26

Locks (Mutual Exclusion)

public interface Lock {

public void lock();

public void unlock(); }

Page 27: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

27

Locks (Mutual Exclusion)

public interface Lock {

public void lock();

public void unlock(); }

acquire lock

Page 28: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

28

Locks (Mutual Exclusion)

public interface Lock {

public void lock();

public void unlock(); }

release lock

acquire lock

Page 29: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

29

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 - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

30

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 - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

31

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 - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

32

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 - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

33

Mutual Exclusion •  Let CSi

k be thread i’s k-th critical section execution

Page 34: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

34

Mutual Exclusion •  Let CSi

k be thread i’s k-th critical section execution

•  And CSjm be thread j’s m-th critical

section execution

Page 35: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

35

Mutual Exclusion •  Let CSi

k be thread i’s k-th critical section execution

•  And CSjm be j’s m-th execution

•  Then either –  or

Page 36: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

36

Mutual Exclusion •  Let CSi

k be thread i’s k-th critical section execution

•  And CSjm be j’s m-th execution

•  Then either –  or

CSik CSj

m

Page 37: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

37

Mutual Exclusion •  Let CSi

k be thread i’s k-th critical section execution

•  And CSjm be j’s m-th execution

•  Then either –  or

CSik CSj

m CSj

m CSik

Page 38: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

38

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 - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

39

Starvation-Free

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

•  Individual threads make progress

Page 40: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

40

Two-Thread vs n -Thread Solutions

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

•  Then n-Thread solutions

Page 41: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

41

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 - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

42

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 - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

43

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

Page 44: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

44

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

Set my flag

Page 45: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

45

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

LockOne

Wait for other flag to go false

Set my flag

Page 46: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

46

•  Assume CSAj overlaps CSB

k •  Consider each thread's last (j-th

and k-th) read and write in the lock() method before entering

•  Derive a contradiction

LockOne Satisfies Mutual Exclusion

Page 47: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

47

•  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 48: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

48

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

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

From the Assumption

Page 49: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

49

•  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 50: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

50

•  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 - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

51

•  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 - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

52

•  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 - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

53

•  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 - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

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 55: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

55

Cycle!

Page 56: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

56

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 57: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

57

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

public void unlock() {} }

Page 58: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

58

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

public void unlock() {} }

Let other go first

Page 59: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

59

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

public void unlock() {} }

Wait for permission

Page 60: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

60

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

public void unlock() {} }

Nothing to do

Page 61: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

61

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 62: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

62

Peterson’s Algorithm

1 public void lock() { 2 flag[i] = true; 3 victim = i; 4 while (flag[j] && victim == i) {}; 5 } 6 public void unlock() { 7 flag[i] = false; 8 }

Page 63: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

63

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 64: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

64

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 65: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

65

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 66: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

66

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 No longer interested

Page 67: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

67

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

Mutual Exclusion

•  If thread 1 in critical section, –  flag[1]=true, –  victim = 0

•  If thread 0 in critical section, –  flag[0]=true, –  victim = 1

Cannot both be true

Page 68: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

68

Deadlock Free

•  Thread blocked –  only at while loop –  only if it is the victim

•  One or the other must not be the victim

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

Page 69: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

69

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 70: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

70

The Filter Algorithm for n Threads

There are n-1 “waiting rooms” called levels

•  At each level –  At least one enters level –  At least one blocked if many try

•  Only one thread makes it through

ncs

cs

Page 71: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

71

Filter class Filter implements Lock { volatile int[] level; // level[i] for thread i volatile int[] victim; // victim[L] for level L

public Filter(int n) {

level = new int[n];

victim = new int[n];

for (int i = 1; i < n; i++) {

level[i] = 0;

}}

}

n-1

n-1

0

1

0 0 0 0 0 0 4

2

2

Thread 2 at level 4

0

4

level

victim

Page 72: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

72

Filter class Filter implements Lock { …

public void lock(){ for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((∃ k != i level[k] >= L) && victim[L] == i ); }} public void unlock() { level[i] = 0; }}

Page 73: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

73

class Filter implements Lock { …

public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((∃ k != i) level[k] >= L) && victim[L] == i); }} public void release(int i) { level[i] = 0; }}

Filter

One level at a time

Page 74: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

74

class Filter implements Lock { …

public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((∃ k != i) level[k] >= L) && victim[L] == i); // busy wait }} public void release(int i) { level[i] = 0; }}

Filter

Announce intention to enter level L

Page 75: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

75

class Filter implements Lock { int level[n]; int victim[n]; public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((∃ k != i) level[k] >= L) && victim[L] == i); }} public void release(int i) { level[i] = 0; }}

Filter

Give priority to anyone but me

Page 76: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

76

class Filter implements Lock { int level[n]; int victim[n]; public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((∃ k != i) level[k] >= L) && victim[L] == i); }} public void release(int i) { level[i] = 0; }}

Filter Wait as long as someone else is at same or

higher level, and I’m designated victim

Page 77: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

77

class Filter implements Lock { int level[n]; int victim[n]; public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((∃ k != i) level[k] >= L) && victim[L] == i); }} public void release(int i) { level[i] = 0; }}

Filter

Thread enters level L when it completes the loop

Page 78: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

78

Claim •  Start at level L=0 •  At most n-L threads enter level L •  Mutual exclusion at level L=n-1

ncs

cs L=n-1

L=1

L=n-2

L=0

Page 79: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

79

Induction Hypothesis

•  Assume all at level L-1 enter level L

•  A last to write victim[L]

•  B is any other thread at level L

•  No more than n-L+1 at level L-1 •  Induction step: by contradiction

ncs

cs

L-1 has n-L+1 L has n-L

assume

prove

Page 80: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

80

Proof Structure ncs

cs

Assumed to enter L-1

By way of contradiction all enter L

n-L+1 = 4 n-L+1 = 4

A B

Last to write victim[L]

Show that A must have seen B at level L and since victim[L] == A could not have entered

Page 81: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

81

From the Code

(1) writeB(level[B]=L)writeB(victim[L]=B)

public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((∃ k != i) level[k] >= L) && victim[L] == i) {}; }}

Page 82: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

82

From the Code

(2) writeA(victim[L]=A)readA(level[B])

public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((∃ k != i) level[k] >= L) && victim[L] == i) {}; }}

Page 83: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

83

By Assumption

By assumption, A is the last thread to write victim[L]

(3) writeB(victim[L]=B)writeA(victim[L]=A)

Page 84: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

84

Combining Observations

(1) writeB(level[B]=L)writeB(victim[L]=B) (3) writeB(victim[L]=B)writeA(victim[L]=A) (2) writeA(victim[L]=A)readA(level[B])

Page 85: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

85

public void lock() { for (int L = 1; L < n; L++) { level[i] = L; victim[L] = i;

while ((∃ k != i) level[k] >= L) && victim[L] == i) {}; }}

Combining Observations

(1) writeB(level[B]=L)writeB(victim[L]=B) (3) writeB(victim[L]=B)writeA(victim[L]=A) (2) writeA(victim[L]=A)readA(level[B])

Page 86: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

86

Combining Observations

(1) writeB(level[B]=L)writeB(victim[L]=B) (3) writeB(victim[L]=B)writeA(victim[L]=A) (2) writeA(victim[L]=A)readA(level[B])

Thus, A read level[B] ≥ L, A was last to write victim[L], so it could not have entered level L!

Page 87: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

87

No Starvation

•  Filter Lock satisfies properties: –  Just like Peterson Alg at any level –  So no one starves

•  But what about fairness? –  Threads can be overtaken by others

Page 88: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

88

Bounded Waiting

•  Want stronger fairness guarantees •  Thread not “overtaken” too much •  Need to adjust definitions ….

Page 89: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

89

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 90: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

90

Bakery Algorithm

•  Provides First-Come-First-Served •  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 91: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

91

Bakery Algorithm class Bakery implements Lock {

volatile boolean[] flag;

volatile 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 92: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

92

Bakery Algorithm class Bakery implements Lock {

volatile boolean[] flag;

volatile 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-1 0 f f f f t f t

2

f

0 0 0 0 5 0 4 0

6

CS

Page 93: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

93

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 94: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

94

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 95: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

95

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 96: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

96

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 97: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

97

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 98: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

98

Bakery Algorithm class 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

With lower (label,i) in lexicographic order

Page 99: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

99

Bakery Algorithm

class Bakery implements Lock {

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

Page 100: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

100

Bakery Algorithm

class Bakery implements Lock {

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

No longer interested

labels are always increasing

Page 101: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

101

No Deadlock

•  There is always one thread with earliest label

•  Ties are impossible (why?)

Page 102: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

102

First-Come-First-Served •  If DA DBthen A’s

label is earlier –  writeA(label[A])

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

•  So B is 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 103: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

103

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 104: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

104

Mutual Exclusion

•  Labels are strictly increasing so •  B must have seen flag[A] == false

Page 105: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

105

Mutual Exclusion

•  Labels are strictly increasing so •  B must have seen flag[A] == false •  LabelingB readB(flag[A]) write

A(flag[A]) LabelingA

Page 106: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

106

Mutual Exclusion

•  Labels are strictly increasing so •  B must have seen flag[A] == false •  LabelingB readB(flag[A]) write

A(flag[A]) LabelingA •  Which contradicts the assumption

that A has an earlier label

Page 107: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

107

Bakery Y232K Bug 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 108: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

108

Bakery Y232K Bug 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)); }

Mutex breaks if label[i] overflows

Page 109: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

109

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 110: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

... spin locks e desempenho

•  material cap 7 livro Herlihy

Art of Multiprocessor Programming

110

Page 111: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

111

Revisit Mutual Exclusion...

•  Think of performance, not just correctness and progress

•  Begin to understand how performance depends on our software properly utilizing the multiprocessor machine’s hardware

•  And get to know a collection of locking algorithms…

(1)

Page 112: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

112

What Should you do if you can’t get a lock?

•  Keep trying –  “spin” or “busy-wait” –  Good if delays are short

•  Give up the processor –  Good if delays are long –  Always good on uniprocessor

(1)

Page 113: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

113

What Should you do if you can’t get a lock?

•  Keep trying –  “spin” or “busy-wait” –  Good if delays are short

•  Give up the processor –  Good if delays are long –  Always good on uniprocessor

our focus

Page 114: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

114

Basic Spin-Lock

CS

Resets lock upon exit

spin lock

critical section

. . .

Page 115: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

115

Basic Spin-Lock

CS

Resets lock upon exit

spin lock

critical section

. . .

…lock introduces sequential bottleneck

Page 116: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

116

Basic Spin-Lock

CS

Resets lock upon exit

spin lock

critical section

. . .

…lock suffers from contention

Page 117: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

117

Basic Spin-Lock

CS

Resets lock upon exit

spin lock

critical section

. . . Notice: these are distinct phenomena

…lock suffers from contention

Page 118: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

118

Basic Spin-Lock

CS

Resets lock upon exit

spin lock

critical section

. . .

…lock suffers from contention

Seq Bottleneck no parallelism

Page 119: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

119

Basic Spin-Lock

CS

Resets lock upon exit

spin lock

critical section

. . . Contention ???

…lock suffers from contention

Page 120: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

120

Review: Test-and-Set

•  Boolean value •  Test-and-set (TAS)

–  Swap true with current value –  Return value tells if prior value was true

or false •  Can reset just by writing false •  TAS aka “getAndSet”

Page 121: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

121

Review: Test-and-Set public class AtomicBoolean { boolean value;

public synchronized boolean getAndSet(boolean newValue) {

boolean prior = value; value = newValue; return prior; } }

(5)

Page 122: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

122

Review: Test-and-Set public class AtomicBoolean { boolean value;

public synchronized boolean getAndSet(boolean newValue) {

boolean prior = value; value = newValue; return prior; } }

Package java.util.concurrent.atomic

Page 123: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

123

Review: Test-and-Set public class AtomicBoolean { boolean value;

public synchronized boolean getAndSet(boolean newValue) {

boolean prior = value; value = newValue; return prior; } }

Swap old and new values

Page 124: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

124

Review: Test-and-Set AtomicBoolean lock = new AtomicBoolean(false) … boolean prior = lock.getAndSet(true)

Page 125: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

125

Review: Test-and-Set AtomicBoolean lock = new AtomicBoolean(false) … boolean prior = lock.getAndSet(true)

(5)

Swapping in true is called “test-and-set” or TAS

Page 126: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

126

Test-and-Set Locks

•  Locking –  Lock is free: value is false –  Lock is taken: value is true

•  Acquire lock by calling TAS –  If result is false, you win –  If result is true, you lose

•  Release lock by writing false

Page 127: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

127

Test-and-set Lock class TASlock { AtomicBoolean state = new AtomicBoolean(false);

void lock() { while (state.getAndSet(true)) {} }

void unlock() { state.set(false); }}

Page 128: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

128

Test-and-set Lock class TASlock { AtomicBoolean state = new AtomicBoolean(false);

void lock() { while (state.getAndSet(true)) {} }

void unlock() { state.set(false); }}

Lock state is AtomicBoolean

Page 129: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

129

Test-and-set Lock class TASlock { AtomicBoolean state = new AtomicBoolean(false);

void lock() { while (state.getAndSet(true)) {} }

void unlock() { state.set(false); }}

Keep trying until lock acquired

Page 130: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

130

Test-and-set Lock class TASlock { AtomicBoolean state = new AtomicBoolean(false);

void lock() { while (state.getAndSet(true)) {} }

void unlock() { state.set(false); }}

Release lock by resetting state to false

Page 131: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

131

Space Complexity

•  TAS spin-lock has small “footprint” •  N thread spin-lock uses O(1) space •  As opposed to O(n) Peterson/Bakery •  How did we overcome the Ω(n) lower

bound? •  We used a RMW operation…

Page 132: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

132

Performance

•  Experiment –  n threads –  Increment shared counter 1 million times

•  How long should it take? •  How long does it take?

Page 133: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

133

Graph

ideal tim

e

threads

no speedup because of sequential bottleneck

Page 134: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

134

Mystery #1

tim

e

threads

TAS lock

Ideal

(1)

What is going on?

Page 135: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

135

Test-and-Test-and-Set Locks

•  Lurking stage – Wait until lock “looks” free –  Spin while read returns true (lock taken)

•  Pouncing state –  As soon as lock “looks” available –  Read returns false (lock free) –  Call TAS to acquire lock –  If TAS loses, back to lurking

Page 136: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

136

Test-and-test-and-set Lock class TTASlock { AtomicBoolean state = new AtomicBoolean(false);

void lock() { while (true) { while (state.get()) {} if (!state.getAndSet(true)) return; } }

Page 137: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

137

Test-and-test-and-set Lock class TTASlock { AtomicBoolean state = new AtomicBoolean(false);

void lock() { while (true) { while (state.get()) {} if (!state.getAndSet(true)) return; } } Wait until lock looks free

Page 138: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

138

Test-and-test-and-set Lock class TTASlock { AtomicBoolean state = new AtomicBoolean(false);

void lock() { while (true) { while (state.get()) {} if (!state.getAndSet(true)) return; } }

Then try to acquire it

Page 139: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

139

Mystery #2 TAS lock

TTAS lock

Ideal tim

e

threads

Page 140: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

140

Mystery

•  Both –  TAS and TTAS –  Do the same thing (in our model)

•  Except that –  TTAS performs much better than TAS – Neither approaches ideal

Page 141: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

141

Opinion

•  Our memory abstraction is broken •  TAS & TTAS methods

–  Are provably the same (in our model)

–  Except they aren’t (in field tests)

•  Need a more detailed model …

Page 142: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

142

Bus-Based Architectures

Bus

cache

memory

cache cache

Page 143: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

143

Bus-Based Architectures

Bus

cache

memory

cache cache

Random access memory (10s of cycles)

Page 144: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

144

Bus-Based Architectures

cache

memory

cache cache

Shared Bus • broadcast medium • One broadcaster at a time • Processors and memory all “snoop”

Bus

Page 145: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

145

Bus-Based Architectures

Bus

cache

memory

cache cache

Per-Processor Caches • Small • Fast: 1 or 2 cycles • Address & state information

Page 146: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

146

Jargon Watch

•  Cache hit –  “I found what I wanted in my cache” –  Good Thing™

Page 147: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

147

Jargon Watch

•  Cache hit –  “I found what I wanted in my cache” –  Good Thing™

•  Cache miss –  “I had to shlep all the way to memory for

that data” –  Bad Thing™

Page 148: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

148

Cave Canem

•  This model is still a simplification –  But not in any essential way –  Illustrates basic principles

•  Will discuss complexities later

Page 149: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

149

Bus

Processor Issues Load Request

cache

memory

cache cache

data

Page 150: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

150

Bus

Processor Issues Load Request

Bus

cache

memory

cache cache

data

Gimme data

Page 151: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

151

cache

Bus

Memory Responds

Bus

memory

cache cache

data

Got your data right here data

Page 152: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

152

Bus

Processor Issues Load Request

memory

cache cache data

data

Gimme data

Page 153: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

153

Bus

Processor Issues Load Request

Bus

memory

cache cache data

data

Gimme data

Page 154: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

154

Bus

Processor Issues Load Request

Bus

memory

cache cache data

data

I got data

Page 155: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

155

Bus

Other Processor Responds

memory

cache cache

data

I got data

data data Bus

Page 156: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

156

Bus

Other Processor Responds

memory

cache cache

data

data data Bus

Page 157: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

157

Modify Cached Data

Bus

data

memory

cache data

data

(1)

Page 158: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

158

Modify Cached Data

Bus

data

memory

cache data

data

data

(1)

Page 159: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

159

memory

Bus

data

Modify Cached Data

cache data

data

Page 160: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

160

memory

Bus

data

Modify Cached Data

cache

What’s up with the other copies?

data

data

Page 161: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

161

Cache Coherence

•  We have lots of copies of data – Original copy in memory –  Cached copies at processors

•  Some processor modifies its own copy – What do we do with the others? – How to avoid confusion?

Page 162: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

162

Write-Back Caches

•  Accumulate changes in cache •  Write back when needed

– Need the cache for something else –  Another processor wants it

•  On first modification –  Invalidate other entries –  Requires non-trivial protocol …

Page 163: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

163

Write-Back Caches

•  Cache entry has three states –  Invalid: contains raw seething bits –  Valid: I can read but I can’t write –  Dirty: Data has been modified

•  Intercept other load requests • Write back to memory before using cache

Page 164: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

164

Bus

Invalidate

memory

cache data data

data

Page 165: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

165

Bus

Invalidate

Bus

memory

cache data data

data

Mine, all mine!

Page 166: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

166

Bus

Invalidate

Bus

memory

cache data data

data

cache

Uh,oh

Page 167: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

167

cache Bus

Invalidate

memory

cache data

data

Other caches lose read permission

Page 168: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

168

cache Bus

Invalidate

memory

cache data

data

Other caches lose read permission

This cache acquires write permission

Page 169: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

169

cache Bus

Invalidate

memory

cache data

data

Memory provides data only if not present in any cache, so no need to

change it now (expensive)

(2)

Page 170: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

170

cache Bus

Another Processor Asks for Data

memory

cache data

data

(2)

Bus

Page 171: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

171

cache data Bus

Owner Responds

memory

cache data

data

(2)

Bus

Here it is!

Page 172: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

172

Bus

End of the Day …

memory

cache data

data

(1)

Reading OK, no writing

data data

Page 173: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

173

Mutual Exclusion

•  What do we want to optimize? –  Bus bandwidth used by spinning threads –  Release/Acquire latency –  Acquire latency for idle lock

Page 174: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

174

Simple TASLock

•  TAS invalidates cache lines •  Spinners

– Miss in cache –  Go to bus

•  Thread wants to release lock –  delayed behind spinners

Page 175: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

175

Test-and-test-and-set

•  Wait until lock “looks” free –  Spin on local cache – No bus use while lock busy

•  Problem: when lock is released –  Invalidation storm …

Page 176: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

176

Local Spinning while Lock is Busy

Bus

memory

busy busy busy

busy

Page 177: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

177

Bus

On Release

memory

free invalid invalid

free

Page 178: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

178

On Release

Bus

memory

free invalid invalid

free

miss miss

Everyone misses, rereads

(1)

Page 179: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

179

On Release

Bus

memory

free invalid invalid

free

TAS(…) TAS(…)

Everyone tries TAS

(1)

Page 180: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

180

Problems

•  Everyone misses –  Reads satisfied sequentially

•  Everyone does TAS –  Invalidates others’ caches

•  Eventually quiesces after lock acquired – How long does this take?

Page 181: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

181

Mystery Explained TAS lock

TTAS lock

Ideal tim

e

threads Better than

TAS but still not as good as

ideal

Page 182: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

182

Solution: Introduce Delay

spin lock time d r1d r2d

•  If the lock looks free •  But I fail to get it

•  There must be lots of contention •  Better to back off than to collide again

Page 183: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

183

Dynamic Example: Exponential Backoff

time d 2d 4d spin lock

If I fail to get lock –  wait random duration before retry –  Each subsequent failure doubles expected wait

Page 184: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

184

Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}}

Page 185: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

185

Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}} Fix minimum delay

Page 186: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

186

Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}} Wait until lock looks free

Page 187: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

187

Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}} If we win, return

Page 188: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

188

Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}}

Back off for random duration

Page 189: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

189

Exponential Backoff Lock public class Backoff implements lock { public void lock() { int delay = MIN_DELAY; while (true) { while (state.get()) {} if (!lock.getAndSet(true)) return; sleep(random() % delay); if (delay < MAX_DELAY) delay = 2 * delay; }}}

Double max delay, within reason

Page 190: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

190

Spin-Waiting Overhead

TTAS Lock

Backoff lock tim

e

threads

Page 191: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming© Herlihy-Shavit

2007

191

Backoff: Other Issues

•  Good –  Easy to implement –  Beats TTAS lock

•  Bad – Must choose parameters carefully – Not portable across platforms

Page 192: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

Art of Multiprocessor Programming

192

          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.

Page 193: Mutual Exclusion - PUC-Rionoemi/pcp-10/aula2matHS.pdf · 2010. 8. 17. · Mutual Exclusion • Today we will try to formalize our understanding of mutual exclusion • We will also

exercícios – para 23/8

•  analisar o código no slide 62 (alg Peterson) e discutir se há diferença se trocarmos as linhas 2 e 3;

•  fazer um programa com threads, com pthreads+c ou Java, que tenha comportamento diferente do esperado quando se usam n threads;

•  implementar um dos algoritmos de lock vistos na aula de hoje no mesmo programa.

193 mandar por email para [email protected]