Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrent Objects MIT 6.05s by Maurice Herlihy & Nir Shavit.

Similar presentations


Presentation on theme: "Concurrent Objects MIT 6.05s by Maurice Herlihy & Nir Shavit."— Presentation transcript:

1 Concurrent Objects MIT 6.05s by Maurice Herlihy & Nir Shavit

2 Art of Multiprocessor Programming 2 Concurrent Computaton memory object

3 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?

4 Art of Multiprocessor Programming 4 Objectivism What is a concurrent object? –How do we describe one? –How do we tell if we ’ re right? (later)

5 Art of Multiprocessor Programming 5 FIFO Queue: Enqueue Method q.enq ( )

6 Art of Multiprocessor Programming 6 FIFO Queue: Dequeue Method q.deq()/

7 Art of Multiprocessor Programming 7 A Lock-Based Queue class LockBasedQueue { int head, tail; T[] items; Lock lock; public LockBasedQueue(int capacity) { head = 0; tail = 0; lock = new ReentrantLock(); items = (T[]) new Object[capacity]; }

8 Art of Multiprocessor Programming 8 A Lock-Based Queue class LockBasedQueue { int head, tail; T[] items; Lock lock; public LockBasedQueue(int capacity) { head = 0; tail = 0; lock = new ReentrantLock(); items = (T[]) new Object[capacity]; } Queue fields protected by single shared lock headtail y z

9 Art of Multiprocessor Programming 9 A Lock-Based Queue class LockBasedQueue { int head, tail; T[] items; Lock lock; public LockBasedQueue(int capacity) { head = 0; tail = 0; lock = new ReentrantLock(); items = (T[]) new Object[capacity]; } Initially head = tail headtail y z

10 Art of Multiprocessor Programming 10 Implementation: Deq 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(); } headtail y z

11 Art of Multiprocessor Programming 11 Implementation: Deq 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(); } Method calls mutually exclusive headtail y z

12 Art of Multiprocessor Programming 12 Implementation: Deq 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(); } If queue empty throw exception headtail y z

13 Art of Multiprocessor Programming 13 Implementation: Deq 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(); } Queue not empty: remove item and update head tail y z

14 Art of Multiprocessor Programming 14 Implementation: Deq 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(); } Return result headtail y z

15 Art of Multiprocessor Programming 15 Implementation: Deq 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(); } Release lock no matter what! headtail y z

16 Art of Multiprocessor Programming 16 Implementation: Deq 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(); } Should be correct because modifications are mutually exclusive…

17 Art of Multiprocessor Programming 17 Now consider the following implementation The same thing without mutual exclusion For simplicity, only two threads –One thread enq only –The other deq only

18 Art of Multiprocessor Programming 18 Lock-free 2-Thread Queue public class WaitFreeQueue { int head = 0, tail = 0; items = (T[]) new Object[capacity]; public void enq(Item x) { while (tail-head == capacity); // busy-wait items[tail % capacity] = x; tail++; } public Item deq() { while (tail == head); // busy-wait Item item = items[head % capacity]; head++; return item; }}

19 Art of Multiprocessor Programming 19 Lock-free 2-Thread Queue public class WaitFreeQueue { int head = 0, tail = 0; items = (T[]) new Object[capacity]; public void enq(Item x) { while (tail-head == capacity); // busy-wait items[tail % capacity] = x; tail++; } public Item deq() { while (tail == head); // busy-wait Item item = items[head % capacity]; head++; return item; }} headtail y z

20 Art of Multiprocessor Programming 20 Lock-free 2-Thread Queue public class WaitFreeQueue { int head = 0, tail = 0; items = (T[]) new Object[capacity]; public void enq(Item x) { while (tail-head == capacity); // busy-wait items[tail % capacity] = x; tail++; } public Item deq() { while (tail == head); // busy-wait Item item = items[head % capacity]; head++; return item; }} Queue is updated without a lock! headtail y z How do we define “correct” when modifications are not mutually exclusive?

21 Art of Multiprocessor Programming 21 Defining concurrent queue implementations 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 …

22 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 22 Lets begin with correctness

23 Art of Multiprocessor Programming 23 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

24 Art of Multiprocessor Programming 24 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,

25 Art of Multiprocessor Programming 25 Pre and PostConditions for Dequeue Precondition: –Queue is non-empty Postcondition: –Returns first item in queue Postcondition: –Removes first item in queue

26 Art of Multiprocessor Programming 26 Pre and PostConditions for Dequeue Precondition: –Queue is empty Postcondition: –Throws Empty exception Postcondition: –Queue state unchanged

27 Art of Multiprocessor Programming 27 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

28 Art of Multiprocessor Programming 28 What About Concurrent Specifications ? Methods? Documentation? Adding new methods?

29 Art of Multiprocessor Programming 29 Methods Take Time time

30 Art of Multiprocessor Programming 30 Methods Take Time time invocation 12:00 q.enq(... ) time

31 Art of Multiprocessor Programming 31 Methods Take Time time Method call invocation 12:00 q.enq(... ) time

32 Art of Multiprocessor Programming 32 Methods Take Time time Method call invocation 12:00 q.enq(... ) time

33 Art of Multiprocessor Programming 33 Methods Take Time time Method call invocation 12:00 q.enq(... ) time void response 12:01

34 Art of Multiprocessor Programming 34 Sequential vs Concurrent Sequential –Methods take time? Who knew? Concurrent –Method call is not an event –Method call is an interval.

35 Art of Multiprocessor Programming 35 time Concurrent Methods Take Overlapping Time time

36 Art of Multiprocessor Programming 36 time Concurrent Methods Take Overlapping Time time Method call

37 Art of Multiprocessor Programming 37 time Concurrent Methods Take Overlapping Time time Method call

38 Art of Multiprocessor Programming 38 time Concurrent Methods Take Overlapping Time time Method call

39 Art of Multiprocessor Programming 39 Sequential vs Concurrent Sequential: –Object needs meaningful state only between method calls Concurrent –Because method calls overlap, object might never be between method calls

40 Art of Multiprocessor Programming 40 Sequential vs Concurrent Sequential: –Each method described in isolation Concurrent –Must characterize all possible interactions with concurrent calls What if two enq s overlap? Two deq s? enq and deq ? …

41 Art of Multiprocessor Programming 41 Sequential vs Concurrent Sequential: –Can add new methods without affecting older methods Concurrent: –Everything can potentially interact with everything else

42 Art of Multiprocessor Programming 42 Sequential vs Concurrent Sequential: –Can add new methods without affecting older methods Concurrent: –Everything can potentially interact with everything else Panic!

43 Art of Multiprocessor Programming 43 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

44 Art of Multiprocessor Programming 44 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(); }

45 Art of Multiprocessor Programming 45 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 modifications of queue are done mutually exclusive

46 Art of Multiprocessor Programming 46 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

47 Art of Multiprocessor Programming 47 Linearizability Each method should –“ Take effect ” –Instantaneously –Between invocation and response events

48 Art of Multiprocessor Programming 48 Example time (6)

49 Art of Multiprocessor Programming 49 Example time q.enq(x) time (6)

50 Art of Multiprocessor Programming 50 Example time q.enq(x) q.enq(y) time (6)

51 Art of Multiprocessor Programming 51 Example time q.enq(x) q.enq(y)q.deq(x) time (6)

52 Art of Multiprocessor Programming 52 Example time q.enq(x) q.enq(y)q.deq(x) q.deq(y) time (6)

53 Art of Multiprocessor Programming 53 Example time q.enq(x) q.enq(y)q.deq(x) q.deq(y) linearizable q.enq(x) q.enq(y)q.deq(x) q.deq(y) time (6)

54 Art of Multiprocessor Programming 54 Example time (5)

55 Art of Multiprocessor Programming 55 Example time q.enq(x) (5)

56 Art of Multiprocessor Programming 56 Example time q.enq(x)q.deq(y) (5)

57 Art of Multiprocessor Programming 57 Example time q.enq(x) q.enq(y) q.deq(y) (5)

58 Art of Multiprocessor Programming 58 Example time q.enq(x) q.enq(y) q.deq(y) q.enq(x) q.enq(y) (5)

59 Art of Multiprocessor Programming 59 Example time q.enq(x) q.enq(y) q.deq(y) q.enq(x) q.enq(y) (5) not linearizable

60 Art of Multiprocessor Programming 60 Example time (4)

61 Art of Multiprocessor Programming 61 Example time q.enq(x) time (4)

62 Art of Multiprocessor Programming 62 Example time q.enq(x) q.deq(x) time (4)

63 Art of Multiprocessor Programming 63 Example time q.enq(x) q.deq(x) q.enq(x) q.deq(x) time (4)

64 Art of Multiprocessor Programming 64 Example time q.enq(x) q.deq(x) q.enq(x) q.deq(x) linearizable time (4)

65 Art of Multiprocessor Programming 65 Example time q.enq(x) time (8)

66 Art of Multiprocessor Programming 66 Example time q.enq(x) q.enq(y) time (8)

67 Art of Multiprocessor Programming 67 Example time q.enq(x) q.enq(y) q.deq(y) time (8)

68 Art of Multiprocessor Programming 68 Example time q.enq(x) q.enq(y) q.deq(y) q.deq(x) time (8)

69 Art of Multiprocessor Programming 69 q.enq(x) q.enq(y) q.deq(y) q.deq(x) Comme ci Example time Comme ça multiple orders OK linearizable

70 Art of Multiprocessor Programming 70 Read/Write Register Example time read(1)write(0) write(1) write(2) time read(0) (4)

71 Art of Multiprocessor Programming 71 Read/Write Register Example time read(1)write(0) write(1) write(2) time read(0) write(1) already happened (4)

72 Art of Multiprocessor Programming 72 Read/Write Register Example time read(1)write(0) write(1) write(2) time read(0) write(1) write(1) already happened (4)

73 Art of Multiprocessor Programming 73 Read/Write Register Example time read(1)write(0) write(1) write(2) time read(0) write(1) write(1) already happened (4) not linearizable

74 Art of Multiprocessor Programming 74 Read/Write Register Example time read(1)write(0) write(1) write(2) time read(1) write(1) already happened (4)

75 Art of Multiprocessor Programming 75 Read/Write Register Example time read(1)write(0) write(1) write(2) time read(1) write(1)write(2) (4) write(1) already happened

76 Art of Multiprocessor Programming 76 Read/Write Register Example time read(1)write(0) write(1) write(2) time read(1) write(1)write(2) not linearizable (4) write(1) already happened

77 Art of Multiprocessor Programming 77 Read/Write Register Example time write(0) write(1) write(2) time read(1) (4)

78 Art of Multiprocessor Programming 78 Read/Write Register Example time write(0) write(1) write(2) time read(1) write(1) write(2) (4)

79 Art of Multiprocessor Programming 79 Read/Write Register Example time write(0) write(1) write(2) time read(1) write(1) write(2) linearizable (4)

80 Art of Multiprocessor Programming 80 Read/Write Register Example time read(1)write(0) write(1) write(2) time read(1) (2)

81 Art of Multiprocessor Programming 81 Read/Write Register Example time read(1)write(0) write(1) write(2) time read(1) write(1) (2)

82 Art of Multiprocessor Programming 82 Read/Write Register Example time read(1)write(0) write(1) write(2) time read(1) write(1)write(2) (2)

83 Art of Multiprocessor Programming 83 Read/Write Register Example time read(1)write(0) write(1) write(2) time read(2) write(1)write(2) Not linearizable (2)

84 Art of Multiprocessor Programming 84 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

85 Art of Multiprocessor Programming 85 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

86 Art of Multiprocessor Programming 86 Invocation Notation A q.enq(x) (4)

87 Art of Multiprocessor Programming 87 Invocation Notation A q.enq(x) thread (4)

88 Art of Multiprocessor Programming 88 Invocation Notation A q.enq(x) thread method (4)

89 Art of Multiprocessor Programming 89 Invocation Notation A q.enq(x) thread object (4) method

90 Art of Multiprocessor Programming 90 Invocation Notation A q.enq(x) thread object method arguments (4)

91 Art of Multiprocessor Programming 91 Response Notation A q: void (2)

92 Art of Multiprocessor Programming 92 Response Notation A q: void thread (2)

93 Art of Multiprocessor Programming 93 Response Notation A q: void thread result (2)

94 Art of Multiprocessor Programming 94 Response Notation A q: void thread object result (2)

95 Art of Multiprocessor Programming 95 Response Notation A q: void thread object result (2) Method is implicit

96 Art of Multiprocessor Programming 96 Response Notation A q: empty() thread object (2) Method is implicit exception

97 Art of Multiprocessor Programming 97 History - Describing an Execution A q.enq(3) A q:void A q.enq(5) B p.enq(4) B p:void B q.deq() B q:3 Sequence of invocations and responses H =

98 Art of Multiprocessor Programming 98 Definition Invocation & response match if A q.enq(3) A q:void Thread names agree Object names agree Method call (1)

99 Art of Multiprocessor Programming 99 Object Projections A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 H =

100 Art of Multiprocessor Programming 100 Object Projections A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 H|q =

101 Art of Multiprocessor Programming 101 Thread Projections A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 H =

102 Art of Multiprocessor Programming 102 Thread Projections A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 H|B =

103 Art of Multiprocessor Programming 103 Complete Subhistory A q.enq(3) A q:void A q.enq(5) B p.enq(4) B p:void B q.deq() B q:3 An invocation is pending if it has no matching respnse H =

104 Art of Multiprocessor Programming 104 Complete Subhistory A q.enq(3) A q:void A q.enq(5) B p.enq(4) B p:void B q.deq() B q:3 May or may not have taken effect H =

105 Art of Multiprocessor Programming 105 Complete Subhistory A q.enq(3) A q:void A q.enq(5) B p.enq(4) B p:void B q.deq() B q:3 discard pending invocations H =

106 Art of Multiprocessor Programming 106 Complete Subhistory A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 Complete(H) =

107 Art of Multiprocessor Programming 107 Sequential Histories A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5) (4)

108 Art of Multiprocessor Programming 108 Sequential Histories A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5) match (4)

109 Art of Multiprocessor Programming 109 Sequential Histories A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5) match (4)

110 Art of Multiprocessor Programming 110 Sequential Histories A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5) match (4)

111 Art of Multiprocessor Programming 111 Sequential Histories A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5) match Final pending invocation OK (4)

112 Art of Multiprocessor Programming 112 Sequential Histories A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 A q:enq(5) match Final pending invocation OK (4) Method calls of different threads do not interleave

113 Art of Multiprocessor Programming 113 Well-Formed Histories H= A q.enq(3) B p.enq(4) B p:void B q.deq() A q:void B q:3

114 Art of Multiprocessor Programming 114 Well-Formed Histories H= A q.enq(3) B p.enq(4) B p:void B q.deq() A q:void B q:3 H|B= B p.enq(4) B p:void B q.deq() B q:3 Per-thread projections sequential

115 Art of Multiprocessor Programming 115 Well-Formed Histories H= A q.enq(3) B p.enq(4) B p:void B q.deq() A q:void B q:3 H|B= B p.enq(4) B p:void B q.deq() B q:3 A q.enq(3) A q:void H|A= Per-thread projections sequential

116 Art of Multiprocessor Programming 116 Equivalent Histories H= A q.enq(3) B p.enq(4) B p:void B q.deq() A q:void B q:3 Threads see the same thing in both A q.enq(3) A q:void B p.enq(4) B p:void B q.deq() B q:3 G= H|A = G|A H|B = G|B

117 Art of Multiprocessor Programming 117 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 …

118 Art of Multiprocessor Programming 118 Legal Histories A sequential (multi-object) history H is legal if –For every object x –H|x is in the sequential spec for x

119 Art of Multiprocessor Programming 119 Precedence A q.enq(3) B p.enq(4) B p.void A q:void B q.deq() B q:3 A method call precedes another if response event precedes invocation event Method call (1)

120 Art of Multiprocessor Programming 120 Non-Precedence A q.enq(3) B p.enq(4) B p.void B q.deq() A q:void B q:3 Some method calls overlap one another Method call (1)

121 Art of Multiprocessor Programming 121 Notation Given –History H –method executions m 0 and m 1 in H We say m 0  H  m 1, if –m 0 precedes m 1 Relation m 0  H  m 1 is a –Partial order –Total order if H is sequential m0m0 m1m1

122 Art of Multiprocessor Programming 122 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

123 Art of Multiprocessor Programming 123 What is  G   S time a b (8) GG SS c GG  G = {a  c,b  c}  S = {a  b,a  c,b  c} A limitation on the Choice of S!

124 Art of Multiprocessor Programming 124 Remarks Some pending invocations –Took effect, so keep them –Discard the rest Condition  G   S –Means that S respects “ real-time order ” of G

125 Art of Multiprocessor Programming 125 A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 B q:enq(6) Example time B.q.enq(4 ) A. q.enq(3) B.q.deq(4 ) B. q.enq(6)

126 Art of Multiprocessor Programming 126 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:void B q.deq() B q:4 B q:enq(6) A. q.enq(3)

127 Art of Multiprocessor Programming 127 Example Complete this pending invocation time B.q.enq(4 ) B.q.deq(4 ) B. q.enq(6) B.q.enq(3 ) A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 B q:enq(6) A q:void

128 Art of Multiprocessor Programming 128 Example time B.q.enq(4 ) B.q.deq(4 ) B. q.enq(6) B.q.enq(3 ) A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 B q:enq(6) A q:void discard this one

129 Art of Multiprocessor Programming 129 Example time B.q.enq(4 ) B.q.deq(4 ) B.q.enq(3 ) A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 A q:void discard this one

130 Art of Multiprocessor Programming 130 A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 A q:void Example time B.q.enq(4 ) B.q.deq(4 ) B.q.enq(3 )

131 Art of Multiprocessor Programming 131 A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 A q:void Example time B q.enq(4) B q:void A q.enq(3) A q:void B q.deq() B q:4 B.q.enq(4 ) B.q.deq(4 ) B.q.enq(3 )

132 Art of Multiprocessor Programming 132 B.q.enq(4 ) B.q.deq(4 ) B.q.enq(3 ) A q.enq(3) B q.enq(4) B q:void B q.deq() B q:4 A q:void Example time B q.enq(4) B q:void A q.enq(3) A q:void B q.deq() B q:4 Equivalent sequential history

133 Art of Multiprocessor Programming 133 Linearization Points Identify one atomic step where method “ happens ” –Critical section? –Machine instruction? Usually straightforward Sometimes not –E.g., Successful vs unsuccessful search … –Stay tuned …

134 Art of Multiprocessor Programming 134 Linearization Points : 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(); }

135 Art of Multiprocessor Programming 135 Linearization Points: 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 0 1 capacity-1 2 head tail y z

136 Art of Multiprocessor Programming 136 Linearization Points : Lock-free public class LockFreeQueue { int head = 0, tail = 0; items = (T[]) new Object[capacity]; public void enq(Item x) { while (tail-head == capacity); // busy-wait items[tail % capacity] = x; tail++; } public Item deq() { while (tail == head); // busy-wait Item item = items[head % capacity]; head++; return item; }} 0 1 capacity-1 2 head tail y z

137 Art of Multiprocessor Programming 137 public class LockFreeQueue { int head = 0, tail = 0; items = (T[]) new Object[capacity]; public void enq(Item x) { while (tail-head == capacity); // busy-wait items[tail % capacity] = x; tail++; } public Item deq() { while (tail == head); // busy-wait Item item = items[head % capacity]; head++; return item; }} Linearization order is order head and tail fields modified Remember that there is only one enqueuer and only one dequeuer Linearization Points : Lock-free

138 Art of Multiprocessor Programming 138 Concurrency How much concurrency does linearizability allow? When must a method invocation block?

139 Art of Multiprocessor Programming 139 Technicality Focus on total methods –Defined for every state Example: –deq() that throws EmptyException –Versus deq() that waits … Why? –Otherwise, blocking unrelated to synchronization

140 Art of Multiprocessor Programming 140 Concurrency Question: –When does linearizability require a method invocation to block? Answer: –never. –Linearizability is non-blocking

141 Radical, Dude! Linearizability permits lock-free implementations … Where no thread is ever blocked waiting for another … How to do so is another story!

142 Art of Multiprocessor Programming 142 Composability A system is linearizable if and only if each individual object is linearizable.

143 Modular, Dude! You can contract out pieces of your system. If each contractor individually guarantees that module’s linearizability … Then your entire system is linearizable. Do not take this property for granted!

144 Art of Multiprocessor Programming 144 Linearizability Each method should –“ Take effect ” –Instantaneously –Between invocation and response events

145 Art of Multiprocessor Programming 145 Sequential Consistency Each method should –“ Take effect ” –Instantaneously –In program order.

146 Art of Multiprocessor Programming 146 Sequential Consistency No need to preserve real-time order –Cannot re-order same-thread operations –Can re-order different-thread operations all you want Often used to describe multiprocessor memory architectures

147 Art of Multiprocessor Programming 147 Composability Sequential consistency is not composable. Composing sequentially-consistent modules does not guarantee a sequentially consistent system. Sequential consistency is useful, but maybe not for software.

148 Art of Multiprocessor Programming 148 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

149 Art of Multiprocessor Programming 149 H|p Sequentially Consistent time p.enq(x)p.deq(y) p.enq(y) q.enq(x) q.enq(y)q.deq(x) time

150 Art of Multiprocessor Programming 150 H|q Sequentially Consistent time p.enq(x)p.deq(y)q.enq(x) q.enq(y)q.deq(x)p.enq(y) time

151 Art of Multiprocessor Programming 151 Ordering imposed by p time p.enq(x)p.deq(y)q.enq(x) q.enq(y)q.deq(x)p.enq(y) time

152 Art of Multiprocessor Programming 152 Ordering imposed by q time p.enq(x)p.deq(y)q.enq(x) q.enq(y)q.deq(x)p.enq(y) time

153 Art of Multiprocessor Programming 153 p.enq(x) Ordering imposed by both time q.enq(x) q.enq(y)q.deq(x) time p.deq(y) p.enq(y)

154 Art of Multiprocessor Programming 154 p.enq(x) Combining orders time q.enq(x) q.enq(y)q.deq(x) time p.deq(y) p.enq(y)

155 Art of Multiprocessor Programming 155 Fact Most hardware architectures don ’ t support sequential consistency Because they think it ’ s too strong Here ’ s another story …

156 Art of Multiprocessor Programming 156 The Flag Example time x.write(1) y.read(0) y.write(1) x.read(0) time

157 Art of Multiprocessor Programming 157 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

158 Art of Multiprocessor Programming 158 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

159 Art of Multiprocessor Programming 159 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 …

160 Art of Multiprocessor Programming 160 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!

161 Art of Multiprocessor Programming 161 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

162 Art of Multiprocessor Programming 162 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

163 Art of Multiprocessor Programming 163 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

164 Art of Multiprocessor Programming 164 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.

165 Art of Multiprocessor Programming 165 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?

166 Art of Multiprocessor Programming 166 Memory Hierarchy Writing to memory = mailing a letter Most of the time, –No need to wait at the post office If you want to synchronize –Announce it explicitly –Pay for it only when you need it

167 Art of Multiprocessor Programming 167 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

168 Art of Multiprocessor Programming 168 Volatile In Java, can ask compiler to keep a variable up-to-date with volatile keyword Also inhibits reordering, removing from loops, & other “ optimizations ” Stay tuned …

169 Art of Multiprocessor Programming 169 Sequential Consistency Not composable Harder to work with Good way to think about hardware models

170 Summary Linearizability is the gold standard for software object correctness –Non-blocking –Composable Not always what you want –But it’s the condition you’re not satisfying …

171 Art of Multiprocessor Programming 171 This work is licensed under a Creative Commons Attribution- ShareAlike 2.5 License.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.


Download ppt "Concurrent Objects MIT 6.05s by Maurice Herlihy & Nir Shavit."

Similar presentations


Ads by Google