Top Banner
Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit
193

Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Mar 27, 2015

Download

Documents

Abigail Perez
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: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Concurrent Objects

Companion slides forThe Art of Multiprocessor Programming

by Maurice Herlihy & Nir Shavit

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

Art of Multiprocessor Programming

2

Concurrent Computation

memory

object object

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

Art of Multiprocessor Programming

3

Objectivism

• What is a concurrent object?– How do we describe one?– How do we implement one?– How do we tell if we’re right?

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

Art of Multiprocessor Programming

4

Objectivism

• What is a concurrent object?– How do we describe one?

– How do we tell if we’re right?

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

Art of Multiprocessor Programming

5

FIFO Queue: Enqueue Method

q.enq( )

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

Art of Multiprocessor Programming

6

FIFO Queue: Dequeue Method

q.deq()/

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

Art of Multiprocessor Programming

7

Lock-Based Queue

headtail0

2

1

5 4

3

yx

capacity = 8

7

6

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

Art of Multiprocessor Programming

8

Lock-Based Queue

headtail0

2

1

5 4

3

yx

capacity = 8

7

6

Fields protected by single shared lock

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

class LockBasedQueue<T> { int head, tail; T[] items; Lock lock; public LockBasedQueue(int capacity) { head = 0; tail = 0; lock = new ReentrantLock(); items = (T[]) new Object[capacity]; }

Art of Multiprocessor Programming

9

A Lock-Based Queue0 1

capacity-12

head tail

y z

Fields protected by single shared lock

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

Art of Multiprocessor Programming

10

Lock-Based Queue

head

tail

0

2

1

5 4

3

Initially head = tail

7

6

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

class LockBasedQueue<T> { int head, tail; T[] items; Lock lock; public LockBasedQueue(int capacity) { head = 0; tail = 0; lock = new ReentrantLock(); items = (T[]) new Object[capacity]; }

Art of Multiprocessor Programming

11

A Lock-Based Queue0 1

capacity-12

head tail

y z

Initially head = tail

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

Art of Multiprocessor Programming

12

Lock-Based deq()

headtail0

2

5 4

7

36

yx

1

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

Art of Multiprocessor Programming

13

Acquire Lock

headtail0

2

5 4

7

36

yx

1

Waiting to enqueue…

My turn …

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

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Art of Multiprocessor Programming

14

Implementation: deq()

Acquire lock at method start

0 1capacity-1

2

head tail

y z

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

Art of Multiprocessor Programming

15

Check if Non-Empty

headtail0

2

5 4

7

36

yx

1

Waiting to enqueue…

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

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Art of Multiprocessor Programming

16

Implementation: deq()

If queue emptythrow exception

0 1capacity-1

2

head tail

y z

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

Art of Multiprocessor Programming

17

Modify the Queue

headtail0

2

1

5 4

7

36

yx

head

Waiting to enqueue…

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

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Art of Multiprocessor Programming

18

Implementation: deq()

Queue not empty?Remove item and update head

0 1capacity-1

2

head tail

y z

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

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Art of Multiprocessor Programming

19

Implementation: deq()

Return result

0 1capacity-1

2

head tail

y z

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

Art of Multiprocessor Programming

20

Release the Lock

tail0

2

1

5 4

7

36

y

x

head

My turn!

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

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Art of Multiprocessor Programming

21

Implementation: deq()

Release lock no matter what!

0 1capacity-1

2

head tail

y z

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

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Art of Multiprocessor Programming

22

Implementation: deq()

Should be correct because

modifications are mutually exclusive…

Should be correct because

modifications are mutually exclusive…

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

Art of Multiprocessor Programming

23

Now consider the following implementation

• The same thing without mutual exclusion

• For simplicity, only two threads – One thread enq only– The other deq only

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

Art of Multiprocessor Programming

24

Wait-free 2-Thread Queue

headtail0

2

1

5 4

7

36

yx

capacity = 8

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

Art of Multiprocessor Programming

25

Wait-free 2-Thread Queue

tail0

2

5 4

7

36

yx

1

enq(z)deq()

z

head

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

Art of Multiprocessor Programming

26

Wait-free 2-Thread Queuehead

tail0

2

5 4

7

36

y

1

queue[tail] = z

result = x

z

x

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

Art of Multiprocessor Programming

27

Wait-free 2-Thread Queue

tail0

2

5 4

7

36

y

1

tail--head++

z

head

x

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

public class WaitFreeQueue {

int head = 0, tail = 0; items = (T[]) new Object[capacity];

public void enq(Item x) { if (tail-head == capacity) throw new FullException(); items[tail % capacity] = x; tail++; } public Item deq() { if (tail == head) throw new EmptyException(); Item item = items[head % capacity]; head++; return item;}} Art of Multiprocessor

Programming28

Wait-free 2-Thread Queue

0 1capacity-1

2

head tail

y z

No lock needed !

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

Wait-free 2-Thread Queue

Art of Multiprocessor Programming

29

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

How do we define “correct” when

modifications are not mutually

exclusive? How do we define “correct” when

modifications are not mutually

exclusive?

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

Art of Multiprocessor Programming

30

What is a Concurrent Queue?

• Need a way to specify a concurrent queue object

• Need a way to prove that an algorithm implements the object’s specification

• Lets talk about object specifications …

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

Correctness and Progress

• In a concurrent setting, we need to specify both the safety and the liveness properties of an object

• Need a way to define – when an implementation is correct– the conditions under which it guarantees

progress

Art of Multiprocessor Programming

31

Lets begin with correctness

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

Art of Multiprocessor Programming

32

Sequential Objects

• Each object has a state– Usually given by a set of fields– Queue example: sequence of items

• Each object has a set of methods– Only way to manipulate state– Queue example: enq and deq methods

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

Art of Multiprocessor Programming

33

Sequential Specifications

• If (precondition) – the object is in such-and-such a state– before you call the method,

• Then (postcondition)– the method will return a particular value– or throw a particular exception.

• and (postcondition, con’t)– the object will be in some other state– when the method returns,

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

Art of Multiprocessor Programming

34

Pre and PostConditions for Dequeue

• Precondition:– Queue is non-empty

• Postcondition:– Returns first item in queue

• Postcondition:– Removes first item in queue

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

Art of Multiprocessor Programming

35

Pre and PostConditions for Dequeue

• Precondition:– Queue is empty

• Postcondition:– Throws Empty exception

• Postcondition:– Queue state unchanged

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

Art of Multiprocessor Programming

36

Why Sequential Specifications Totally Rock

• Interactions among methods captured by side-effects on object state– State meaningful between method calls

• Documentation size linear in number of methods– Each method described in isolation

• Can add new methods– Without changing descriptions of old methods

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

Art of Multiprocessor Programming

37

What About Concurrent Specifications ?

• Methods?

• Documentation?

• Adding new methods?

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

Art of Multiprocessor Programming

38

Methods Take Time

timetime

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

Art of Multiprocessor Programming

39

Methods Take Time

time

invocation 12:00

q.enq(...)

time

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

Art of Multiprocessor Programming

40

Methods Take Time

time

Method call

invocation 12:00

time

q.enq(...)

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

Art of Multiprocessor Programming

41

Methods Take Time

time

Method call

invocation 12:00

time

q.enq(...)

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

Art of Multiprocessor Programming

42

Methods Take Time

time

Method call

invocation 12:00

time

void

response 12:01

q.enq(...)

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

Art of Multiprocessor Programming

43

Sequential vs Concurrent

• Sequential– Methods take time? Who knew?

• Concurrent– Method call is not an event– Method call is an interval.

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

Art of Multiprocessor Programming

44

time

Concurrent Methods Take Overlapping Time

time

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

Art of Multiprocessor Programming

45

time

Concurrent Methods Take Overlapping Time

time

Method call

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

Art of Multiprocessor Programming

46

time

Concurrent Methods Take Overlapping Time

time

Method call

Method call

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

Art of Multiprocessor Programming

47

time

Concurrent Methods Take Overlapping Time

time

Method call Method call

Method call

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

Art of Multiprocessor Programming

48

Sequential vs Concurrent

• Sequential:– Object needs meaningful state only between

method calls

• Concurrent– Because method calls overlap, object might

never be between method calls

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

Art of Multiprocessor Programming

49

Sequential vs Concurrent

• Sequential:– Each method described in isolation

• Concurrent– Must characterize all possible interactions

with concurrent calls • What if two enqs overlap?• Two deqs? enq and deq? …

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

Art of Multiprocessor Programming

50

Sequential vs Concurrent

• Sequential:– Can add new methods without affecting older

methods

• Concurrent:– Everything can potentially interact with

everything else

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

Art of Multiprocessor Programming

51

Sequential vs Concurrent

• Sequential:– Can add new methods without affecting older

methods

• Concurrent:– Everything can potentially interact with

everything elsePanic!

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

Art of Multiprocessor Programming

52

The Big Question

• What does it mean for a concurrent object to be correct?– What is a concurrent FIFO queue?– FIFO means strict temporal order– Concurrent means ambiguous temporal order

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

Art of Multiprocessor Programming

53

Intuitively…

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

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

Art of Multiprocessor Programming

54

Intuitively…

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

All queue modifications are mutually exclusive

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

Art of Multiprocessor Programming

55

time

Intuitively

q.deq

q.enq

enq deq

lock() unlock()

lock() unlock() Behavior is “Sequential”

enq

deq

Lets capture the idea of describing the concurrent via the sequential

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

Art of Multiprocessor Programming

56

Linearizability

• Each method should– “take effect”– Instantaneously– Between invocation and response events

• Object is correct if this “sequential” behavior is correct

• Any such concurrent object is– Linearizable™

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

Art of Multiprocessor Programming

57

Is it really about the object?

• Each method should– “take effect”– Instantaneously– Between invocation and response events

• Sounds like a property of an execution…

• A linearizable object: one all of whose possible executions are linearizable

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

Art of Multiprocessor Programming

58

Example

timetime

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

Art of Multiprocessor Programming

59

Example

time

q.enq(x)

time

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

Art of Multiprocessor Programming

60

Example

time

q.enq(x)

q.enq(y)

time

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

Art of Multiprocessor Programming

61

Example

time

q.enq(x)

q.enq(y) q.deq(x)

time

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

Art of Multiprocessor Programming

62

Example

time

q.enq(x)

q.enq(y) q.deq(x)

q.deq(y)

time

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

Art of Multiprocessor Programming

63

Example

time

q.enq(x)

q.enq(y) q.deq(x)

q.deq(y)

linearizableq.enq(x)

q.enq(y) q.deq(x)

q.deq(y)

time

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

Art of Multiprocessor Programming

64

Example

time

q.enq(x)

q.enq(y) q.deq(x)

q.deq(y)

Valid?q.enq(x)

q.enq(y) q.deq(x)

q.deq(y)

time

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

Art of Multiprocessor Programming

65

Example

time

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

Art of Multiprocessor Programming

66

Example

time

q.enq(x)

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

Art of Multiprocessor Programming

67

Example

time

q.enq(x) q.deq(y)

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

Art of Multiprocessor Programming

68

Example

time

q.enq(x)

q.enq(y)

q.deq(y)

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

Art of Multiprocessor Programming

69

Example

time

q.enq(x)

q.enq(y)

q.deq(y)q.enq(x)

q.enq(y)

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

Art of Multiprocessor Programming

70

Example

time

q.enq(x)

q.enq(y)

q.deq(y)q.enq(x)

q.enq(y)

(5)

not linearizable

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

Art of Multiprocessor Programming

71

Example

timetime

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

Art of Multiprocessor Programming

72

Example

time

q.enq(x)

time

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

Art of Multiprocessor Programming

73

Example

time

q.enq(x)

q.deq(x)

time

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

Art of Multiprocessor Programming

74

Example

time

q.enq(x)

q.deq(x)

q.enq(x)

q.deq(x)

time

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

Art of Multiprocessor Programming

75

Example

time

q.enq(x)

q.deq(x)

q.enq(x)

q.deq(x)

linearizable

time

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

Art of Multiprocessor Programming

76

Example

time

q.enq(x)

time

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

Art of Multiprocessor Programming

77

Example

time

q.enq(x)

q.enq(y)

time

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

Art of Multiprocessor Programming

78

Example

time

q.enq(x)

q.enq(y)

q.deq(y)

time

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

Art of Multiprocessor Programming

79

Example

time

q.enq(x)

q.enq(y)

q.deq(y)

q.deq(x)

time

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

Art of Multiprocessor Programming

80

q.enq(x)

q.enq(y)

q.deq(y)

q.deq(x)

Comme ci Example

time

Comme ça multiple orders OK

linearizable

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

Art of Multiprocessor Programming

81

Read/Write Register Example

time

read(1)write(0)

write(1)

write(2)

time

read(0)

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

Art of Multiprocessor Programming

82

Read/Write Register Example

time

read(1)write(0)

write(1)

write(2)

time

read(0)

write(1) already happened

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

Art of Multiprocessor Programming

83

Read/Write Register Example

time

read(1)write(0)

write(1)

write(2)

time

read(0)write(1)

write(1) already happened

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

Art of Multiprocessor Programming

84

Read/Write Register Example

time

read(1)write(0)

write(1)

write(2)

time

read(0)write(1)

write(1) already happened

not linearizable

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

Art of Multiprocessor Programming

85

Read/Write Register Example

time

read(1)write(0)

write(1)

write(2)

time

read(1)

write(1) already happened

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

Art of Multiprocessor Programming

86

Read/Write Register Example

time

read(1)write(0)

write(1)

write(2)

time

read(1)write(1)

write(2)

write(1) already happened

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

Art of Multiprocessor Programming

87

Read/Write Register Example

time

read(1)write(0)

write(1)

write(2)

time

read(1)write(1)

write(2)

not linearizable

write(1) already happened

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

Art of Multiprocessor Programming

88

Read/Write Register Example

time

write(0)

write(1)

write(2)

time

read(1)

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

Art of Multiprocessor Programming

89

Read/Write Register Example

time

write(0)

write(1)

write(2)

time

read(1)write(1)

write(2)

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

Art of Multiprocessor Programming

90

Read/Write Register Example

time

write(0)

write(1)

write(2)

time

read(1)write(1)

write(2)

linearizable

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

Art of Multiprocessor Programming

91

Read/Write Register Example

time

read(1)write(0)

write(1)

write(2)

time

read(1)

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

Art of Multiprocessor Programming

92

Read/Write Register Example

time

read(1)write(0)

write(1)

write(2)

time

read(1)write(1)

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

Art of Multiprocessor Programming

93

Read/Write Register Example

time

read(1)write(0)

write(1)

write(2)

time

read(1)write(1)

write(2)

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

Art of Multiprocessor Programming

94

Read/Write Register Example

time

read(1)write(0)

write(1)

write(2)

time

read(2)write(1)

write(2)

Not linearizable

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

Art of Multiprocessor Programming

95

Talking About Executions

• Why?– Can’t we specify the linearization point of

each operation without describing an execution?

• Not Always– In some cases, linearization point depends on

the execution

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

Art of Multiprocessor Programming

96

Formal Model of Executions

• Define precisely what we mean– Ambiguity is bad when intuition is weak

• Allow reasoning– Formal– But mostly informal

• In the long run, actually more important• Ask me why!

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

Art of Multiprocessor Programming

97

Split Method Calls into Two Events

• Invocation– method name & args– q.enq(x)

• Response– result or exception– q.enq(x) returns void– q.deq() returns x– q.deq() throws empty

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

Art of Multiprocessor Programming

98

Invocation Notation

A q.enq(x)

(4)

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

Art of Multiprocessor Programming

99

Invocation Notation

A q.enq(x)

thread

(4)

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

Art of Multiprocessor Programming

100

Invocation Notation

A q.enq(x)

thread method

(4)

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

Art of Multiprocessor Programming

101

Invocation Notation

A q.enq(x)

thread

object(4)

method

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

Art of Multiprocessor Programming

102

Invocation Notation

A q.enq(x)

thread

object

method

arguments(4)

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

Art of Multiprocessor Programming

103

Response Notation

A q: void

(2)

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

Art of Multiprocessor Programming

104

Response Notation

A q: void

thread

(2)

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

Art of Multiprocessor Programming

105

Response Notation

A q: void

thread result

(2)

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

Art of Multiprocessor Programming

106

Response Notation

A q: void

thread

object

result

(2)

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

Art of Multiprocessor Programming

107

Response Notation

A q: void

thread

object

result

(2)

Met

hod is im

plicit

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

Art of Multiprocessor Programming

108

Response Notation

A q: empty()

thread

object(2)

Met

hod is im

plicit

exception

Page 109: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

109

History - Describing an Execution

A q.enq(3)A q:voidA q.enq(5)B p.enq(4)B p:voidB q.deq()B q:3

Sequence of invocations and

responses

H =

Page 110: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

110

Definition

• Invocation & response match if

A q.enq(3)

A q:void

Thread names agree

Object names agree

Method call

(1)

Page 111: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

111

Object Projections

A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3

H =

Page 112: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

112

Object Projections

A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3

H|q =

Page 113: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

113

Thread Projections

A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3

H =

Page 114: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

114

Thread Projections

A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3

H|B =

Page 115: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

115

Complete Subhistory

A q.enq(3)A q:voidA q.enq(5)B p.enq(4)B p:voidB q.deq()B q:3

An invocation is pending if it has no matching respnse

H =

Page 116: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

116

Complete Subhistory

A q.enq(3)A q:voidA q.enq(5)B p.enq(4)B p:voidB q.deq()B q:3

May or may not have taken effect

H =

Page 117: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

117

Complete Subhistory

A q.enq(3)A q:voidA q.enq(5)B p.enq(4)B p:voidB q.deq()B q:3

discard pending invocations

H =

Page 118: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

118

Complete Subhistory

A q.enq(3)A q:void B p.enq(4)B p:voidB q.deq()B q:3

Complete(H) =

Page 119: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

119

Sequential Histories

A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3A q:enq(5)

(4)

Page 120: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

120

Sequential Histories

A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3A q:enq(5)

match

(4)

Page 121: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

121

Sequential Histories

A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3A q:enq(5)

match

match

(4)

Page 122: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

122

Sequential Histories

A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3A q:enq(5)

match

match

match

(4)

Page 123: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

123

Sequential Histories

A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3A q:enq(5)

match

match

match

Final pending invocation OK

(4)

Page 124: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

124

Sequential Histories

A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3A q:enq(5)

match

match

match

Final pending invocation OK

(4)

Method calls of different

threads do not interleave

Page 125: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

125

Well-Formed Histories

H=

A q.enq(3)B p.enq(4)B p:voidB q.deq()A q:voidB q:3

Page 126: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

126

Well-Formed Histories

H=

A q.enq(3)B p.enq(4)B p:voidB q.deq()A q:voidB q:3

H|B=B p.enq(4)B p:voidB q.deq()B q:3

Per-thread projections sequential

Page 127: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

127

Well-Formed Histories

H=

A q.enq(3)B p.enq(4)B p:voidB q.deq()A q:voidB q:3

H|B=B p.enq(4)B p:voidB q.deq()B q:3

A q.enq(3)A q:void

H|A=

Per-thread projections sequential

Page 128: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

128

Equivalent Histories

H=

A q.enq(3)B p.enq(4)B p:voidB q.deq()A q:voidB q:3

Threads see the same thing in both

A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3

G=

H|A = G|AH|B = G|B

Page 129: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

129

Sequential Specifications

• A sequential specification is some way of telling whether a– Single-thread, single-object history– Is legal

• For example:– Pre and post-conditions– But plenty of other techniques exist …

Page 130: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

130

Legal Histories

• A sequential (multi-object) history H is legal if– For every object x– H|x is in the sequential spec for x

Page 131: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

131

Precedence

A q.enq(3)B p.enq(4)B p.voidA q:voidB q.deq()B q:3

A method call precedes another if response event

precedes invocation event

Method call Method call

(1)

Page 132: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

132

Non-Precedence

A q.enq(3)B p.enq(4)B p.voidB q.deq()A q:voidB q:3

Some method calls overlap one another

Method call

Method call

(1)

Page 133: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

133

Notation

• Given – History H– method executions m0 and m1 in H

• We say m0 Hm1, if– m0 precedes m1

• Relation m0 Hm1 is a– Partial order – Total order if H is sequential

m0 m1

Page 134: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

134

Linearizability

• History H is linearizable if it can be extended to G by– Appending zero or more responses to

pending invocations– Discarding other pending invocations

• So that G is equivalent to– Legal sequential history S – where G S

Page 135: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

135

Ensuring G S

time

a

b

time

(8)

G

S

cG

G = {ac,bc}

S = {ab,ac,bc}

A limita

tion on th

e

Choice of S!

Page 136: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

136

Remarks

• Some pending invocations– Took effect, so keep them– Discard the rest

• Condition G S

– Means that S respects “real-time order” of G

Page 137: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

137

A q.enq(3)B q.enq(4)B q:voidB q.deq()B q:4B q:enq(6)

Example

time

B.q.enq(4)

A. q.enq(3)

B.q.deq(4) B. q.enq(6)

Page 138: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

138

Example

Complete this pending

invocation

time

B.q.enq(4) B.q.deq(3) B. q.enq(6)

A q.enq(3)B q.enq(4)B q:voidB q.deq()B q:4B q:enq(6)

A. q.enq(3)

Page 139: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

139

Example

Complete this pending

invocation

time

B.q.enq(4) B.q.deq(4) B. q.enq(6)

A.q.enq(3)

A q.enq(3)B q.enq(4)B q:voidB q.deq()B q:4B q:enq(6)A q:void

Page 140: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

140

Example

time

B.q.enq(4) B.q.deq(4) B. q.enq(6)

A.q.enq(3)

A q.enq(3)B q.enq(4)B q:voidB q.deq()B q:4B q:enq(6)A q:void

discard this one

Page 141: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

141

Example

time

B.q.enq(4) B.q.deq(4)

A.q.enq(3)

A q.enq(3)B q.enq(4)B q:voidB q.deq()B q:4

A q:void

discard this one

Page 142: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

142

A q.enq(3)B q.enq(4)B q:voidB q.deq()B q:4A q:void

Example

time

B.q.enq(4) B.q.deq(4)

A.q.enq(3)

Page 143: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

143

A q.enq(3)B q.enq(4)B q:voidB q.deq()B q:4A q:void

Example

time

B q.enq(4)B q:voidA q.enq(3)A q:voidB q.deq()B q:4

B.q.enq(4) B.q.deq(4)

A.q.enq(3)

Page 144: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

144

B.q.enq(4) B.q.deq(4)

A.q.enq(3)

A q.enq(3)B q.enq(4)B q:voidB q.deq()B q:4A q:void

Example

time

B q.enq(4)B q:voidA q.enq(3)A q:voidB q.deq()B q:4

Equivalent sequential history

Page 145: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

150

Composability Theorem

• History H is linearizable if and only if– For every object x– H|x is linearizable

• We care about objects only!– (Materialism?)

Page 146: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

151

Why Does Composability Matter?

• Modularity

• Can prove linearizability of objects in isolation

• Can compose independently-implemented objects

Page 147: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

152

Reasoning About Linearizability: Locking

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

0 1capacity-1

2

head tail

y z

Page 148: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

153

Reasoning About Linearizability: Locking

public T deq() throws EmptyException { lock.lock(); try { if (tail == head) throw new EmptyException(); T x = items[head % items.length]; head++; return x; } finally { lock.unlock(); } }

Linearization pointsare when locks are

released

Page 149: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

154

More Reasoning: Wait-free

0 1capacity-1

2

head tail

y z

public class WaitFreeQueue {

int head = 0, tail = 0; items = (T[]) new Object[capacity];

public void enq(Item x) { if (tail-head == capacity) throw new FullException(); items[tail % capacity] = x; tail++; } public Item deq() { if (tail == head) throw new EmptyException(); Item item = items[head % capacity]; head++; return item;}}

0 1capacity-1

2

head tail

y z

Page 150: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

public class WaitFreeQueue {

int head = 0, tail = 0; items = (T[]) new Object[capacity];

public void enq(Item x) { if (tail-head == capacity) throw new FullException(); items[tail % capacity] = x; tail++; } public Item deq() { if (tail == head) throw new EmptyException(); Item item = items[head % capacity]; head++; return item;}} Art of Multiprocessor

Programming155

More Reasoning: Wait-free

0 1capacity-1

2

head tail

y zLinearization order is order head and tail

fields modified

Remember that t

here

is only one enqueuer

and only one dequeuer

Page 151: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

156

Strategy

• Identify one atomic step where method “happens”– Critical section– Machine instruction

• Doesn’t always work– Might need to define several different steps

for a given method

Page 152: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

157

Linearizability: Summary

• Powerful specification tool for shared objects

• Allows us to capture the notion of objects being “atomic”

• Don’t leave home without it

Page 153: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

158

Alternative: Sequential Consistency

• History H is Sequentially Consistent if it can be extended to G by– Appending zero or more responses to

pending invocations– Discarding other pending invocations

• So that G is equivalent to a– Legal sequential history S

– Where G S

Differs from linearizability

Page 154: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

159

Sequential Consistency

• No need to preserve real-time order– Cannot re-order operations done by the

same thread– Can re-order non-overlapping operations

done by different threads

• Often used to describe multiprocessor memory architectures

Page 155: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

160

Example

time

(5)

Page 156: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

161

Example

time

q.enq(x)

(5)

Page 157: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

162

Example

time

q.enq(x) q.deq(y)

(5)

Page 158: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

163

Example

time

q.enq(x)

q.enq(y)

q.deq(y)

(5)

Page 159: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

164

Example

time

q.enq(x)

q.enq(y)

q.deq(y)q.enq(x)

q.enq(y)

(5)

Page 160: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

165

Example

time

q.enq(x)

q.enq(y)

q.deq(y)q.enq(x)

q.enq(y)

(5)

not linearizable

Page 161: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

166

Example

time

q.enq(x)

q.enq(y)

q.deq(y)q.enq(x)

q.enq(y)

(5)

Yet Sequentially

Consistent

Page 162: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

167

Theorem

Sequential Consistency is not composable

Page 163: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

168

FIFO Queue Example

time

p.enq(x) p.deq(y)q.enq(x)

time

Page 164: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

169

FIFO Queue Example

time

p.enq(x) p.deq(y)q.enq(x)

q.enq(y) q.deq(x)p.enq(y)

time

Page 165: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

170

FIFO Queue Example

time

p.enq(x) p.deq(y)q.enq(x)

q.enq(y) q.deq(x)p.enq(y)

History H

time

Page 166: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

171

H|p Sequentially Consistent

time

p.enq(x) p.deq(y)

p.enq(y)

q.enq(x)

q.enq(y) q.deq(x)

time

Page 167: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

172

H|q Sequentially Consistent

time

p.enq(x) p.deq(y)q.enq(x)

q.enq(y) q.deq(x)p.enq(y)

time

Page 168: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

173

Ordering imposed by p

time

p.enq(x) p.deq(y)q.enq(x)

q.enq(y) q.deq(x)p.enq(y)

time

Page 169: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

174

Ordering imposed by q

time

p.enq(x) p.deq(y)q.enq(x)

q.enq(y) q.deq(x)p.enq(y)

time

Page 170: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

175

p.enq(x)

Ordering imposed by both

time

q.enq(x)

q.enq(y) q.deq(x)

time

p.deq(y)

p.enq(y)

Page 171: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

176

p.enq(x)

Combining orders

time

q.enq(x)

q.enq(y) q.deq(x)

time

p.deq(y)

p.enq(y)

Page 172: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

177

Fact

• Most hardware architectures don’t support sequential consistency

• Because they think it’s too strong

• Here’s another story …

Page 173: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

178

The Flag Example

time

x.write(1) y.read(0)

y.write(1) x.read(0)

time

Page 174: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

179

The Flag Example

time

x.write(1) y.read(0)

y.write(1) x.read(0)

• Each thread’s view is sequentially consistent– It went first

Page 175: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

180

The Flag Example

time

x.write(1) y.read(0)

y.write(1) x.read(0)

• Entire history isn’t sequentially consistent– Can’t both go first

Page 176: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

181

The Flag Example

time

x.write(1) y.read(0)

y.write(1) x.read(0)

• Is this behavior really so wrong?– We can argue either way …

Page 177: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

182

Opinion1: It’s Wrong

• This pattern– Write mine, read yours

• Is exactly the flag principle– Beloved of Alice and Bob– Heart of mutual exclusion

• Peterson• Bakery, etc.

• It’s non-negotiable!

Page 178: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

183

Opinion2: But It Feels So Right …

• Many hardware architects think that sequential consistency is too strong

• Too expensive to implement in modern hardware

• OK if flag principle– violated by default– Honored by explicit request

Page 179: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

184

Memory Hierarchy

• On modern multiprocessors, processors do not read and write directly to memory.

• Memory accesses are very slow compared to processor speeds,

• Instead, each processor reads and writes directly to a cache

Page 180: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

185

Memory Operations

• To read a memory location,– load data into cache.

• To write a memory location– update cached copy,– lazily write cached data back to memory

Page 181: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

186

While Writing to Memory

• A processor can execute hundreds, or even thousands of instructions

• Why delay on every memory write?

• Instead, write back in parallel with rest of the program.

Page 182: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

187

Revisionist History

• Flag violation history is actually OK– processors delay writing to memory– until after reads have been issued.

• Otherwise unacceptable delay between read and write instructions.

• Who knew you wanted to synchronize?

Page 183: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

188

Who knew you wanted to synchronize?

• Writing to memory = mailing a letter

• Vast majority of reads & writes– Not for synchronization– No need to idle waiting for post office

• If you want to synchronize– Announce it explicitly– Pay for it only when you need it

Page 184: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

189

Explicit Synchronization

• Memory barrier instruction– Flush unwritten caches– Bring caches up to date

• Compilers often do this for you– Entering and leaving critical sections

• Expensive

Page 185: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

190

Volatile

• In Java, can ask compiler to keep a variable up-to-date with volatile keyword

• Also inhibits reordering, removing from loops, & other “optimizations”

Page 186: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

191

Real-World Hardware Memory

• Weaker than sequential consistency

• But you can get sequential consistency at a price

• OK for expert, tricky stuff– assembly language, device drivers, etc.

• Linearizability more appropriate for high-level software

Page 187: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

192

Linearizability

• Linearizability– Operation takes effect instantaneously

between invocation and response– Uses sequential specification, locality implies

composablity– Good for high level objects

Page 188: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

193

Correctness: Linearizability

• Sequential Consistency– Not composable– Harder to work with– Good way to think about hardware models

• We will use linearizability as in the remainder of this course unless stated otherwise

Page 189: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Progress

• We saw an implementation whose methods were lock-based (deadlock-free)

• We saw an implementation whose methods did not use locks (lock-free)

• How do they relate?

Art of Multiprocessor Programming

194

Page 190: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Progress Conditions

• Deadlock-free: some thread trying to acquire the lock eventually succeeds.

• Starvation-free: every thread trying to acquire the lock eventually succeeds.

• Lock-free: some thread calling a method eventually returns.

• Wait-free: every thread calling a method eventually returns.

Art of Multiprocessor Programming

195

Page 191: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Progress Conditions

Art of Multiprocessor Programming

196

Wait-free

Lock-free

Starvation-free

Deadlock-free

Everyone makes progress

Non-Blocking Blocking

Someone makes progress

Page 192: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

197

Summary

• We will look at linearizable blocking and non-blocking implementations of objects.

Page 193: Concurrent Objects Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Art of Multiprocessor Programming

198

         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.