![Page 1: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/1.jpg)
Concurrent Objects
Companion slides forThe Art of Multiprocessor
Programmingby Maurice Herlihy & Nir Shavit
![Page 2: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/2.jpg)
Art of Multiprocessor Programming
2
Concurrent Computaton
memory
object object
![Page 3: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/3.jpg)
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: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/4.jpg)
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: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/5.jpg)
Art of Multiprocessor Programming
5
FIFO Queue: Enqueue Method
q.enq( )
![Page 6: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/6.jpg)
Art of Multiprocessor Programming
6
FIFO Queue: Dequeue Method
q.deq()/
![Page 7: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/7.jpg)
Art of Multiprocessor Programming
7
A Lock-Based Queue
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]; }
![Page 8: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/8.jpg)
Art of Multiprocessor Programming
8
A Lock-Based Queue
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]; }
0 1capacity-1
2
head tail
y z
Queue fields protected by single shared lock
![Page 9: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/9.jpg)
Art of Multiprocessor Programming
9
A Lock-Based Queue
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]; }
0 1capacity-1
2
head tail
y z
Initially head = tail
![Page 10: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/10.jpg)
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(); } }
0 1capacity-1
2
head tail
y z
![Page 11: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/11.jpg)
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
0 1capacity-1
2
head tail
y z
![Page 12: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/12.jpg)
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 emptythrow exception
0 1capacity-1
2
head tail
y z
![Page 13: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/13.jpg)
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
0 1capacity-1
2
head tail
y z
![Page 14: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/14.jpg)
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
0 1capacity-1
2
head tail
y z
![Page 15: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/15.jpg)
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!
0 1capacity-1
2
head tail
y z
![Page 16: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/16.jpg)
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…
![Page 17: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/17.jpg)
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
![Page 18: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/18.jpg)
Art of Multiprocessor Programming
18
Wait-free 2-Thread Queuepublic 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;}}
![Page 19: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/19.jpg)
Art of Multiprocessor Programming
19
Wait-free 2-Thread Queuepublic 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 1capacity-1
2
head tail
y z
![Page 20: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/20.jpg)
Art of Multiprocessor Programming
20
Lock-free 2-Thread Queuepublic 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 1capacity-1
2
head tail
y z
Queue is updated without a lock!How do we define
“correct” when
modifications are not
mutually exclusive?
![Page 21: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/21.jpg)
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 …
![Page 22: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/22.jpg)
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
![Page 23: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/23.jpg)
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
![Page 24: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/24.jpg)
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,
![Page 25: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/25.jpg)
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
![Page 26: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/26.jpg)
Art of Multiprocessor Programming
26
Pre- and Postconditions for Dequeue
• Precondition:– Queue is empty
• Postcondition:– Throws Empty exception
• Postcondition:– Queue state unchanged
![Page 27: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/27.jpg)
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
![Page 28: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/28.jpg)
Art of Multiprocessor Programming
28
What About Concurrent Specifications? x
• Methods? • Documentation?• Adding new methods?
![Page 29: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/29.jpg)
Art of Multiprocessor Programming
29
Methods Take Time
timetime
![Page 30: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/30.jpg)
Art of Multiprocessor Programming
30
Methods Take Time
time
invocation 12:00
q.enq(...
)
time
![Page 31: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/31.jpg)
Art of Multiprocessor Programming
31
Methods Take Time
time
Method call
invocation 12:00
q.enq(...
)
time
![Page 32: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/32.jpg)
Art of Multiprocessor Programming
32
Methods Take Time
time
Method call
invocation 12:00
q.enq(...
)
time
![Page 33: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/33.jpg)
Art of Multiprocessor Programming
33
Methods Take Time
time
Method call
invocation 12:00
q.enq(...
)
time
void
response 12:01
![Page 34: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/34.jpg)
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.
![Page 35: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/35.jpg)
Art of Multiprocessor Programming
35
time
Concurrent Methods Take Overlapping Time
time
![Page 36: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/36.jpg)
Art of Multiprocessor Programming
36
time
Concurrent Methods Take Overlapping Time
time
Method call
![Page 37: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/37.jpg)
Art of Multiprocessor Programming
37
time
Concurrent Methods Take Overlapping Time
time
Method call
Method call
![Page 38: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/38.jpg)
Art of Multiprocessor Programming
38
time
Concurrent Methods Take Overlapping Time
time
Method call Method call
Method call
![Page 39: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/39.jpg)
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
![Page 40: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/40.jpg)
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 enqs overlap?• Two deqs? enq and deq? …
![Page 41: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/41.jpg)
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
![Page 42: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/42.jpg)
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!
![Page 43: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/43.jpg)
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
![Page 44: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/44.jpg)
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(); } }
![Page 45: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/45.jpg)
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
![Page 46: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/46.jpg)
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
![Page 47: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/47.jpg)
Art of Multiprocessor Programming
47
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 48: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/48.jpg)
Art of Multiprocessor Programming
48
Is it really about the object? x
• 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 49: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/49.jpg)
Art of Multiprocessor Programming
49
Example
timetime
(6)
![Page 50: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/50.jpg)
Art of Multiprocessor Programming
50
Example
time
q.enq(x)
time
(6)
![Page 51: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/51.jpg)
Art of Multiprocessor Programming
51
Example
time
q.enq(x)
q.enq(y)
time
(6)
![Page 52: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/52.jpg)
Art of Multiprocessor Programming
52
Example
time
q.enq(x)
q.enq(y) q.deq(x)
time
(6)
![Page 53: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/53.jpg)
Art of Multiprocessor Programming
53
Example
time
q.enq(x)
q.enq(y) q.deq(x)
q.deq(y)
time
(6)
![Page 54: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/54.jpg)
Art of Multiprocessor Programming
54
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
(6)
![Page 55: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/55.jpg)
Art of Multiprocessor Programming
55
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
(6)
![Page 56: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/56.jpg)
Art of Multiprocessor Programming
56
Example
time
(5)
![Page 57: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/57.jpg)
Art of Multiprocessor Programming
57
Example
time
q.enq(x)
(5)
![Page 58: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/58.jpg)
Art of Multiprocessor Programming
58
Example
time
q.enq(x) q.deq(y)
(5)
![Page 59: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/59.jpg)
Art of Multiprocessor Programming
59
Example
time
q.enq(x)
q.enq(y)
q.deq(y)
(5)
![Page 60: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/60.jpg)
Art of Multiprocessor Programming
60
Example
time
q.enq(x)
q.enq(y)
q.deq(y)q.enq(x)
q.enq(y)
(5)
![Page 61: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/61.jpg)
Art of Multiprocessor Programming
61
Example
time
q.enq(x)
q.enq(y)
q.deq(y)q.enq(x)
q.enq(y)
(5)
not
linearizable
![Page 62: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/62.jpg)
Art of Multiprocessor Programming
62
Example
timetime
(4)
![Page 63: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/63.jpg)
Art of Multiprocessor Programming
63
Example
time
q.enq(x)
time
(4)
![Page 64: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/64.jpg)
Art of Multiprocessor Programming
64
Example
time
q.enq(x)
q.deq(x)
time
(4)
![Page 65: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/65.jpg)
Art of Multiprocessor Programming
65
Example
time
q.enq(x)
q.deq(x)
q.enq(x)
q.deq(x)
time
(4)
![Page 66: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/66.jpg)
Art of Multiprocessor Programming
66
Example
time
q.enq(x)
q.deq(x)
q.enq(x)
q.deq(x)
linearizable
time
(4)
![Page 67: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/67.jpg)
Art of Multiprocessor Programming
67
Example
time
q.enq(x)
time
(8)
![Page 68: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/68.jpg)
Art of Multiprocessor Programming
68
Example
time
q.enq(x)
q.enq(y)
time
(8)
![Page 69: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/69.jpg)
Art of Multiprocessor Programming
69
Example
time
q.enq(x)
q.enq(y)
q.deq(y)
time
(8)
![Page 70: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/70.jpg)
Art of Multiprocessor Programming
70
Example
time
q.enq(x)
q.enq(y)
q.deq(y)
q.deq(x)
time
(8)
![Page 71: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/71.jpg)
Art of Multiprocessor Programming
71
q.enq(x)
q.enq(y)
q.deq(y)
q.deq(x)
Comme ci Example
time
Comme ça multiple orders
OKlinearizable
![Page 72: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/72.jpg)
Art of Multiprocessor Programming
72
Read/Write Register Example
time
read(1)write(0)
write(1)
write(2)
time
read(0)
(4)
![Page 73: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/73.jpg)
Art of Multiprocessor Programming
73
Read/Write Register Example
time
read(1)write(0)
write(1)
write(2)
time
read(0)write(1) already
happened(4)
![Page 74: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/74.jpg)
Art of Multiprocessor Programming
74
Read/Write Register Example
time
read(1)write(0)
write(1)
write(2)
time
read(0)write(1)write(1) already
happened(4)
![Page 75: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/75.jpg)
Art of Multiprocessor Programming
75
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
![Page 76: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/76.jpg)
Art of Multiprocessor Programming
76
Read/Write Register Example
time
read(1)write(0)
write(1)
write(2)
time
read(1)write(1) already
happened(4)
![Page 77: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/77.jpg)
Art of Multiprocessor Programming
77
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
![Page 78: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/78.jpg)
Art of Multiprocessor Programming
78
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
![Page 79: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/79.jpg)
Art of Multiprocessor Programming
79
Read/Write Register Example
time
write(0)
write(1)
write(2)
time
read(1)
(4)
![Page 80: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/80.jpg)
Art of Multiprocessor Programming
80
Read/Write Register Example
time
write(0)
write(1)
write(2)
time
read(1)write(1)
write(2)
(4)
![Page 81: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/81.jpg)
Art of Multiprocessor Programming
81
Read/Write Register Example
time
write(0)
write(1)
write(2)
time
read(1)write(1)
write(2)
linearizable
(4)
![Page 82: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/82.jpg)
Art of Multiprocessor Programming
82
Read/Write Register Example
time
read(1)write(0)
write(1)
write(2)
time
read(1)
(2)
![Page 83: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/83.jpg)
Art of Multiprocessor Programming
83
Read/Write Register Example
time
read(1)write(0)
write(1)
write(2)
time
read(1)write(1)
(2)
![Page 84: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/84.jpg)
Art of Multiprocessor Programming
84
Read/Write Register Example
time
read(1)write(0)
write(1)
write(2)
time
read(1)write(1)
write(2)
(2)
![Page 85: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/85.jpg)
Art of Multiprocessor Programming
85
Read/Write Register Example
time
read(1)write(0)
write(1)
write(2)
time
read(2)write(1)
write(2)
Not
linearizable
(2)
![Page 86: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/86.jpg)
Art of Multiprocessor Programming
86
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 87: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/87.jpg)
Art of Multiprocessor Programming
87
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 88: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/88.jpg)
Art of Multiprocessor Programming
88
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 89: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/89.jpg)
Art of Multiprocessor Programming
89
Invocation Notation
A q.enq(x)
(4)
![Page 90: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/90.jpg)
Art of Multiprocessor Programming
90
Invocation Notation
A q.enq(x)
thread
(4)
![Page 91: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/91.jpg)
Art of Multiprocessor Programming
91
Invocation Notation
A q.enq(x)
thread method
(4)
![Page 92: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/92.jpg)
Art of Multiprocessor Programming
92
Invocation Notation
A q.enq(x)
thread
object(4)
method
![Page 93: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/93.jpg)
Art of Multiprocessor Programming
93
Invocation Notation
A q.enq(x)
thread
object
method
arguments(4)
![Page 94: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/94.jpg)
Art of Multiprocessor Programming
94
Response Notation
A q: void
(2)
![Page 95: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/95.jpg)
Art of Multiprocessor Programming
95
Response Notation
A q: void
thread
(2)
![Page 96: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/96.jpg)
Art of Multiprocessor Programming
96
Response Notation
A q: void
thread result
(2)
![Page 97: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/97.jpg)
Art of Multiprocessor Programming
97
Response Notation
A q: void
thread
object
result
(2)
![Page 98: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/98.jpg)
Art of Multiprocessor Programming
98
Response Notation
A q: void
thread
object
result
(2)
Met
hod
is
impl
icit
![Page 99: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/99.jpg)
Art of Multiprocessor Programming
99
Response Notation
A q: empty()
thread
object(2)
Met
hod
is
impl
icit
exception
![Page 100: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/100.jpg)
Art of Multiprocessor Programming
100
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 101: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/101.jpg)
Art of Multiprocessor Programming
101
Definition
• Invocation & response match if
A q.enq(3)
A q:void
Thread names agree
Object names agree
Method call
(1)
![Page 102: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/102.jpg)
Art of Multiprocessor Programming
102
Object Projections
A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3
H =
![Page 103: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/103.jpg)
Art of Multiprocessor Programming
103
Object Projections
A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3
H|q =
![Page 104: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/104.jpg)
Art of Multiprocessor Programming
104
Thread Projections
A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3
H =
![Page 105: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/105.jpg)
Art of Multiprocessor Programming
105
Thread Projections
A q.enq(3)A q:voidB p.enq(4)B p:voidB q.deq()B q:3
H|B =
![Page 106: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/106.jpg)
Art of Multiprocessor Programming
106
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 107: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/107.jpg)
Art of Multiprocessor Programming
107
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 108: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/108.jpg)
Art of Multiprocessor Programming
108
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 109: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/109.jpg)
Art of Multiprocessor Programming
109
Complete Subhistory
A q.enq(3)A q:void B p.enq(4)B p:voidB q.deq()B q:3
Complete(H) =
![Page 110: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/110.jpg)
Art of Multiprocessor Programming
110
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 111: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/111.jpg)
Art of Multiprocessor Programming
111
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 112: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/112.jpg)
Art of Multiprocessor Programming
112
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 113: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/113.jpg)
Art of Multiprocessor Programming
113
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 114: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/114.jpg)
Art of Multiprocessor Programming
114
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 115: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/115.jpg)
Art of Multiprocessor Programming
115
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 116: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/116.jpg)
Art of Multiprocessor Programming
116
Well-Formed Histories
H=
A q.enq(3)B p.enq(4)B p:voidB q.deq()A q:voidB q:3
![Page 117: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/117.jpg)
Art of Multiprocessor Programming
117
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 118: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/118.jpg)
Art of Multiprocessor Programming
118
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 119: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/119.jpg)
Art of Multiprocessor Programming
119
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 120: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/120.jpg)
Art of Multiprocessor Programming
120
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 121: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/121.jpg)
Art of Multiprocessor Programming
121
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 122: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/122.jpg)
Art of Multiprocessor Programming
122
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 123: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/123.jpg)
Art of Multiprocessor Programming
123
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
anotherMethod call
Method call
(1)
![Page 124: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/124.jpg)
Art of Multiprocessor Programming
124
Notation
• Given – History H
– method executions m0 and m1 in H
• We say m0 H m1, if
– m0 precedes m1
• Relation m0 H m1 is a
– Partial order
– Total order if H is sequentialm0 m1
![Page 125: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/125.jpg)
Art of Multiprocessor Programming
125
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 126: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/126.jpg)
Art of Multiprocessor Programming
126
What is 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 127: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/127.jpg)
Art of Multiprocessor Programming
127
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 128: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/128.jpg)
Art of Multiprocessor Programming
128
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 129: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/129.jpg)
Art of Multiprocessor Programming
129
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 130: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/130.jpg)
Art of Multiprocessor Programming
130
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:voidB q.deq()B q:4B q:enq(6)A q:void
![Page 131: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/131.jpg)
Art of Multiprocessor Programming
131
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:voidB q.deq()B q:4B q:enq(6)A q:void
discard this one
![Page 132: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/132.jpg)
Art of Multiprocessor Programming
132
Example
time
B.q.enq(4) B.q.deq(4)
B.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 133: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/133.jpg)
Art of Multiprocessor Programming
133
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)
B.q.enq(3)
![Page 134: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/134.jpg)
Art of Multiprocessor Programming
134
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)
B.q.enq(3)
![Page 135: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/135.jpg)
Art of Multiprocessor Programming
135
B.q.enq(4) B.q.deq(4)
B.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 136: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/136.jpg)
Art of Multiprocessor Programming
136
Concurrency
• How much concurrency does linearizability allow?
• When must a method invocation block?
![Page 137: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/137.jpg)
Art of Multiprocessor Programming
137
Concurrency
• Focus on total methods– Defined in every state
• Example:– deq() that throws Empty exception– Versus deq() that waits …
• Why?– Otherwise, blocking unrelated to
synchronization
![Page 138: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/138.jpg)
Art of Multiprocessor Programming
138
Concurrency
• Question: When does linearizability require a method invocation to block?
• Answer: never.• Linearizability is non-blocking
![Page 139: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/139.jpg)
Art of Multiprocessor Programming
139
Non-Blocking TheoremIf method invocation
A q.inv(…)
is pending in history H, then there exists a responseA q:res(…)
such thatH + A q:res(…)
is linearizable
![Page 140: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/140.jpg)
Art of Multiprocessor Programming
140
Proof
• Pick linearization S of H
• If S already contains – Invocation A q.inv(…) and response,
– Then we are done.
• Otherwise, pick a response such that– S + A q.inv(…) + A q:res(…)
– Possible because object is total.
![Page 141: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/141.jpg)
Art of Multiprocessor Programming
141
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 142: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/142.jpg)
Art of Multiprocessor Programming
142
Why Does Composability Matter?
• Modularity • Can prove linearizability of objects
in isolation• Can compose independently-
implemented objects
![Page 143: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/143.jpg)
Art of Multiprocessor Programming
143
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 144: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/144.jpg)
Art of Multiprocessor Programming
144
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 145: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/145.jpg)
Art of Multiprocessor Programming
145
More Reasoning: 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 1capacity-1
2
head tail
y z
![Page 146: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/146.jpg)
Art of Multiprocessor Programming
146
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
More Reasoning
Remem
ber that t
here
is only
one e
nqueuer
and only
one d
equeuer
![Page 147: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/147.jpg)
Art of Multiprocessor Programming
147
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 148: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/148.jpg)
Art of Multiprocessor Programming
148
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 149: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/149.jpg)
Art of Multiprocessor Programming
149
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 150: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/150.jpg)
Art of Multiprocessor Programming
150
Alternative: 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 151: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/151.jpg)
Art of Multiprocessor Programming
151
Example
time
(5)
![Page 152: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/152.jpg)
Art of Multiprocessor Programming
152
Example
time
q.enq(x)
(5)
![Page 153: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/153.jpg)
Art of Multiprocessor Programming
153
Example
time
q.enq(x) q.deq(y)
(5)
![Page 154: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/154.jpg)
Art of Multiprocessor Programming
154
Example
time
q.enq(x)
q.enq(y)
q.deq(y)
(5)
![Page 155: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/155.jpg)
Art of Multiprocessor Programming
155
Example
time
q.enq(x)
q.enq(y)
q.deq(y)q.enq(x)
q.enq(y)
(5)
![Page 156: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/156.jpg)
Art of Multiprocessor Programming
156
Example
time
q.enq(x)
q.enq(y)
q.deq(y)q.enq(x)
q.enq(y)
(5)
not
linearizable
![Page 157: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/157.jpg)
Art of Multiprocessor Programming
157
Example
time
q.enq(x)
q.enq(y)
q.deq(y)q.enq(x)
q.enq(y)
(5)
Yet
Sequentially
Consistent
![Page 158: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/158.jpg)
Art of Multiprocessor Programming
158
Theorem
Sequential Consistency is not a local property
(and thus we lose composability…)
![Page 159: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/159.jpg)
Art of Multiprocessor Programming
159
FIFO Queue Example
time
p.enq(x) p.deq(y)q.enq(x)
time
![Page 160: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/160.jpg)
Art of Multiprocessor Programming
160
FIFO Queue Example
time
p.enq(x) p.deq(y)q.enq(x)
q.enq(y) q.deq(x)p.enq(y)
time
![Page 161: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/161.jpg)
Art of Multiprocessor Programming
161
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 162: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/162.jpg)
Art of Multiprocessor Programming
162
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 163: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/163.jpg)
Art of Multiprocessor Programming
163
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 164: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/164.jpg)
Art of Multiprocessor Programming
164
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 165: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/165.jpg)
Art of Multiprocessor Programming
165
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 166: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/166.jpg)
Art of Multiprocessor Programming
166
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 167: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/167.jpg)
Art of Multiprocessor Programming
167
p.enq(x)
Combining orders
time
q.enq(x)
q.enq(y) q.deq(x)
time
p.deq(y)
p.enq(y)
![Page 168: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/168.jpg)
Art of Multiprocessor Programming
168
Fact
• Most hardware architectures don’t support sequential consistency
• Because they think it’s too strong• Here’s another story …
![Page 169: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/169.jpg)
Art of Multiprocessor Programming
169
The Flag Example
time
x.write(1) y.read(0)
y.write(1) x.read(0)
time
![Page 170: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/170.jpg)
Art of Multiprocessor Programming
170
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 171: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/171.jpg)
Art of Multiprocessor Programming
171
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 172: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/172.jpg)
Art of Multiprocessor Programming
172
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 173: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/173.jpg)
Art of Multiprocessor Programming
173
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 174: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/174.jpg)
Art of Multiprocessor Programming
174
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 175: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/175.jpg)
Art of Multiprocessor Programming
175
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 176: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/176.jpg)
Art of Multiprocessor Programming
176
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 177: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/177.jpg)
Art of Multiprocessor Programming
177
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 178: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/178.jpg)
Art of Multiprocessor Programming
178
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 179: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/179.jpg)
Art of Multiprocessor Programming
179
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 180: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/180.jpg)
Art of Multiprocessor Programming
180
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 181: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/181.jpg)
Art of Multiprocessor Programming
181
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 182: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/182.jpg)
Art of Multiprocessor Programming
182
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 183: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/183.jpg)
Art of Multiprocessor Programming
183
Critical Sections
• Easy way to implement linearizability– Take sequential object– Make each method a critical section
• Problems– Blocking– No concurrency
![Page 184: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/184.jpg)
Art of Multiprocessor Programming
184
Linearizability
• Linearizability– Operation takes effect
instantaneously between invocation and response
– Uses sequential specification, locality implies composablity
– Good for high level objects
![Page 185: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/185.jpg)
Art of Multiprocessor Programming
185
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 186: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/186.jpg)
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
186
![Page 187: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/187.jpg)
Maximal vs. Minimal
• Minimal progress: in some suffix of H, some pending active invocation has a matching response (some method call eventually completes ).
• Maximal progress: in every suffix of H, every pending active invocation has a matching response (every method call always completes).
Art of Multiprocessor Programming
187
![Page 188: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/188.jpg)
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
188
![Page 189: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/189.jpg)
Progress Conditions
Art of Multiprocessor Programming
189
Wait-free
Lock-free
Starvation-free
Deadlock-free
Everyone makes progress
Non-Blocking Blocking
Someone makes progress
![Page 190: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/190.jpg)
Art of Multiprocessor Programming
190
Summary
• We will look at linearizable blocking and non-blocking implementations of objects.
![Page 191: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/191.jpg)
Fair Histories
• A history is fair if each thread always continues takes steps.
• On multiprocessors this is controlled by the operating system…
• So fair histories are ones in which the operating system guarantees each thread continues to take steps
Art of Multiprocessor Programming
191
![Page 192: Art of Multiprocessor Programming - uni-freiburg.deproglang.informatik.uni-freiburg.de/.../2010ws/slides/03-chapter-03.pdf · The Art of Multiprocessor Programming by Maurice Herlihy](https://reader036.vdocuments.net/reader036/viewer/2022063018/5fdc3190cc5edf623c505292/html5/thumbnails/192.jpg)
Art of Multiprocessor Programming
192
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
• You are free:– to Share — to copy, distribute and transmit the work – to Remix — to adapt the work
• Under the following conditions:– Attribution. You must attribute the work to “The Art of
Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work).
– Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.
• For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to– http://creativecommons.org/licenses/by-sa/3.0/.
• Any of the above conditions can be waived if you get permission from the copyright holder.
• Nothing in this license impairs or restricts the author's moral rights.