Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential:...

144
Programming Paradigms for Concurrency Lecture 3 – Concurrent Objects Based on companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by Thomas Wies New York University

Transcript of Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential:...

Page 1: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

Programming Paradigms for Concurrency Lecture 3 – Concurrent Objects

Based on companion slides for The Art of Multiprocessor Programming

by Maurice Herlihy & Nir Shavit

Modified by Thomas Wies

New York University

TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAAA

Page 2: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

2

Concurrent Computation

memory

object object

Page 3: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

4

Objectivism

• What is a concurrent object?

– How do we describe one?

– How do we tell if we’re right?

Page 5: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

5

FIFO Queue: Enqueue Method

q.enq( )

Page 6: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

6

FIFO Queue: Dequeue Method

q.deq()/

Page 7: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

7

Lock-Based Queue

head tail 0

2

1

5 4

3

y x

capacity = 8

7

6

Page 8: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

8

Lock-Based Queue

head tail 0

2

1

5 4

3

y x

capacity = 8

7

6

Fields protected by

single shared lock

Page 9: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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];

}

9

A Lock-Based Queue

0 1

capacity-1 2

head tail

y z

Fields protected by

single shared lock

Page 10: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

10

Lock-Based Queue

head

tail

0

2

1

5 4

3

Initially head = tail

7

6

Page 11: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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];

}

11

A Lock-Based Queue

0 1

capacity-1 2

head tail

y z

Initially head = tail

Page 12: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

12

Lock-Based deq()

head tail 0

2

5 4

7

3 6

y x

1

Page 13: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

13

Acquire Lock

head tail 0

2

5 4

7

3 6

y x

1

Waiting to

enqueue…

My turn …

Page 14: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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();

}

}

14

Implementation: deq()

Acquire lock at

method start

0 1

capacity-1 2

head tail

y z

Page 15: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

15

Check if Non-Empty

head tail 0

2

5 4

7

3 6

y x

1

Waiting to

enqueue…

Page 16: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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();

}

}

16

Implementation: deq()

If queue empty

throw exception

0 1

capacity-1 2

head tail

y z

Page 17: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

17

Modify the Queue

head tail 0

2

1

5 4

7

3 6

y x

head

Waiting to

enqueue…

Page 18: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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();

}

}

18

Implementation: deq()

Queue not empty?

Remove item and update head

0 1

capacity-1 2

head tail

y z

Page 19: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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();

}

}

19

Implementation: deq()

Return result

0 1

capacity-1 2

head tail

y z

Page 20: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

20

Release the Lock

tail 0

2

1

5 4

7

3 6

y

x

head

My turn!

Page 21: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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();

}

}

21

Implementation: deq()

Release lock no

matter what!

0 1

capacity-1 2

head tail

y z

Page 22: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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();

}

}

22

Implementation: deq()

Page 23: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

24

Wait-free 2-Thread Queue

head tail 0

2

1

5 4

7

3 6

y x

capacity = 8

Page 25: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

25

Wait-free 2-Thread Queue

tail 0

2

5 4

7

3 6

y x

1

enq(z) deq()

z

head

Page 26: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

26

Wait-free 2-Thread Queue head

tail 0

2

5 4

7

3 6

y

1

queue[tail]

= z

result = x

z

x

Page 27: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

27

Wait-free 2-Thread Queue

tail 0

2

5 4

7

3 6

y

1

tail-- head++

z

head

x

Page 28: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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;

}} 28

Wait-free 2-Thread Queue

0 1

capacity-1 2

head tail

y z

No lock needed !

Page 29: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

Wait-free 2-Thread Queue

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();

}

}

Page 30: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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

31

Lets begin with correctness

Page 32: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

35

Pre and PostConditions for

Dequeue

• Precondition:

– Queue is empty

• Postcondition:

– Throws Empty exception

• Postcondition:

– Queue state unchanged

Page 36: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

37

What About Concurrent

Specifications ?

• Methods?

• Documentation?

• Adding new methods?

Page 38: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

38

Methods Take Time

time time

Page 39: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

39

Methods Take Time

time

invocation 12:00

q.enq(...)

time

Page 40: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

40

Methods Take Time

time

Method call

invocation 12:00

time

q.enq(...)

Page 41: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

41

Methods Take Time

time

Method call

invocation 12:00

time

q.enq(...)

Page 42: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

42

Methods Take Time

time

Method call

invocation 12:00

time

void

response 12:01

q.enq(...)

Page 43: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

43

Sequential vs Concurrent

• Sequential

– Methods take time? Who knew?

• Concurrent

– Method call is not an event

– Method call is an interval.

Page 44: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

44

time

Concurrent Methods Take

Overlapping Time

time

Page 45: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

45

time

Concurrent Methods Take

Overlapping Time

time

Method call

Page 46: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

46

time

Concurrent Methods Take

Overlapping Time

time

Method call

Method call

Page 47: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

47

time

Concurrent Methods Take

Overlapping Time

time

Method call Method call

Method call

Page 48: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

50

Sequential vs Concurrent

• Sequential:

– Can add new methods without affecting older

methods

• Concurrent:

– Everything can potentially interact with

everything else

Page 51: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

51

Sequential vs Concurrent

• Sequential:

– Can add new methods without affecting older

methods

• Concurrent:

– Everything can potentially interact with

everything else

Page 52: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

58

Example

time time

Page 59: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

59

Example

time

q.enq(x)

time

Page 60: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

60

Example

time

q.enq(x)

q.enq(y)

time

Page 61: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

61

Example

time

q.enq(x)

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

time

Page 62: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

62

Example

time

q.enq(x)

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

q.deq(y)

time

Page 63: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

63

Example

time

q.enq(x)

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

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

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

q.deq(y)

time

Page 64: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

64

Example

time

q.enq(x)

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

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

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

q.deq(y)

time

Page 65: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

65

Example

time

Page 66: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

66

Example

time

q.enq(x)

Page 67: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

67

Example

time

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

Page 68: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

68

Example

time

q.enq(x)

q.enq(y)

q.deq(y)

Page 69: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

69

Example

time

q.enq(x)

q.enq(y)

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

q.enq(y)

Page 70: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

70

Example

time

q.enq(x)

q.enq(y)

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

q.enq(y)

Page 71: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

71

Example

time time

Page 72: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

72

Example

time

q.enq(x)

time

Page 73: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

73

Example

time

q.enq(x)

q.deq(x)

time

Page 74: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

74

Example

time

q.enq(x)

q.deq(x)

q.enq(x)

q.deq(x)

time

Page 75: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

75

Example

time

q.enq(x)

q.deq(x)

q.enq(x)

q.deq(x)

time

Page 76: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

76

Example

time

q.enq(x)

time

Page 77: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

77

Example

time

q.enq(x)

q.enq(y)

time

Page 78: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

78

Example

time

q.enq(x)

q.enq(y)

q.deq(y)

time

Page 79: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

79

Example

time

q.enq(x)

q.enq(y)

q.deq(y)

q.deq(x)

time

Page 80: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

80

q.enq(x)

q.enq(y)

q.deq(y)

q.deq(x)

Comme ci Example

time

Comme ça

Page 81: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

81

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(0)

Page 82: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

82

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(0)

write(1) already

happened

Page 83: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

84

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(0) write(1)

write(1) already

happened

Page 85: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

85

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(1)

write(1) already

happened

Page 86: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

87

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 88: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

88

Read/Write Register Example

time

write(0)

write(1)

write(2)

time

read(1)

Page 89: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

89

Read/Write Register Example

time

write(0)

write(1)

write(2)

time

read(1) write(1)

write(2)

Page 90: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

90

Read/Write Register Example

time

write(0)

write(1)

write(2)

time

read(1) write(1)

write(2)

Page 91: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

91

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(1)

Page 92: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

92

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(1) write(1)

Page 93: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

93

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(1) write(1)

write(2)

Page 94: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

94

Read/Write Register Example

time

read(1) write(0)

write(1)

write(2)

time

read(2) write(1)

write(2)

Page 95: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

101

Linearizable Objects are Composable

• Modularity

• Can prove linearizability of objects in

isolation

• Can compose independently-implemented

objects

Page 97: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

102

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 1

capacity-1 2

head tail

y z

Page 98: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

103

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 points

are when locks are

released

Page 99: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

104

More Reasoning: Wait-free

0 1

capacity-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 1

capacity-1 2

head tail

y z

Page 100: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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;

}} 105

More Reasoning: Wait-free

0 1

capacity-1 2

head tail

y z Linearization order is

order head and tail

fields modified

Page 101: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

106

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 102: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

107

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 103: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

108

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 104: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

109

Example

time

Page 105: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

110

Example

time

q.enq(x)

Page 106: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

111

Example

time

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

Page 107: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

112

Example

time

q.enq(x)

q.enq(y)

q.deq(y)

Page 108: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

113

Example

time

q.enq(x)

q.enq(y)

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

q.enq(y)

Page 109: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

114

Example

time

q.enq(x)

q.enq(y)

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

q.enq(y)

Page 110: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

115

Example

time

q.enq(x)

q.enq(y)

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

q.enq(y)

Page 111: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

116

Theorem

Sequential Consistency is not

composable

Page 112: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

117

FIFO Queue Example

time

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

time

Page 113: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

118

FIFO Queue Example

time

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

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

time

Page 114: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

119

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 115: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

120

Sequentially Consistent

time

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

p.enq(y)

q.enq(x)

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

time

Page 116: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

121

Sequentially Consistent

time

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

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

time

Page 117: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

Art of

Multiprocessor

Programming

122

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 118: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

Art of

Multiprocessor

Programming

123

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 119: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

Art of

Multiprocessor

Programming

124

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 120: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

125

p.enq(x)

Combining orders

time

q.enq(x)

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

time

p.deq(y)

p.enq(y)

Page 121: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

126

Fact

• Most hardware architectures don’t support

sequential consistency

• Because they think it’s too strong

• Here’s another story …

Page 122: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

127

The Flag Example

time

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

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

time

Page 123: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

128

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 124: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

129

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 125: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

130

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 126: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

131

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 127: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

132

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 128: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

133

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 129: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

134

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 130: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

135

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 131: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

136

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 132: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

137

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 133: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

138

Double-Checked Locking

0 1

capacity-1 2

head tail

y z

public class Singleton {

private static Singleton instance;

public static Singleton getInstance() {

if (instance == null) {

synchronized(Singleton.class) {

if (instance == null) {

instance = new Singleton();

}

}

}

return instance;

}

}

Page 134: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

139

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 135: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

140

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 136: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

141

Bakery Algorithm revisited

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 137: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

142

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 138: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

143

Linearizability

• Linearizability

– Operation takes effect instantaneously

between invocation and response

– Uses sequential specification, locality implies

composablity

– Good for high level objects

Page 139: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

144

Correctness: Linearizability

• Sequential Consistency

– Not composable

– Harder to work with

– Good way to think about hardware models

• We will use linearizability in the remainder

of this course unless stated otherwise

Page 140: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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?

145

Page 141: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

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.

146

Page 142: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

Progress Conditions

147

Wait-free

Lock-free

Starvation-free

Deadlock-free

Everyone

makes

progress

Non-Blocking Blocking

Someone

makes

progress

Page 143: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

148

Summary

• We will look at linearizable blocking and

non-blocking implementations of objects.

Page 144: Programming Paradigms for Concurrency Lecture 3 …Sequential vs Concurrent • Sequential: –Object needs meaningful state only between method calls • Concurrent –Because method

149

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.