6.852: distributed algorithms fall, 2009 - mit opencourseware · 6.852: distributed algorithms...
TRANSCRIPT
![Page 1: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/1.jpg)
6.852: Distributed AlgorithmsFall, 2009
Class 19
![Page 2: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/2.jpg)
Today’s planz Techniques for implementing concurrent objects:
� Coarse-grained mutual exclusion � Fine-grained locking (mutex and read/write) � Optimistic locking � Lock-free/nonblocking algorithms � “Lazy” synchronization
� We illustrate on list-based sets, but the techniques apply toother data structures
z Reading:� Herlihy, Shavit, Chapter 9
z Next: � Transactional memory � HS, Chapter 18 � Guerraoui, Kapalka
![Page 3: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/3.jpg)
Shared-memory model • Shared variables • At most one memory access per atomic step. • Read/write access • Synchronization primitives:
– Compare-and-swap (CAS) – Load-linked/store-conditional (LL/SC) – Assume lock and unlock methods for every object.
• Most (not all) of our algorithms use locking. • Memory management:
– Allocate objects dynamically, assume unlimited supply. – In practice, would garbage collect and reuse, but we won’t worry
about this. • Assume no failures (mostly).
![Page 4: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/4.jpg)
Correctness guarantees• Linearizability (atomicity) of object operations. • Liveness properties:
– Different guarantees for different algorithms. – Progress:
• Some operations keep completing. – Lockout-freedom (AKA starvation-freedom):
• Every operation completes. – “Nonblocking” conditions:
• Wait-freedom: Even if other processes stop, a particular operation bya process that keeps taking steps eventually finishes.
• Lock-freedom: Even if some processes stop, if some keep takingsteps, then some operation finishes.
• Can think of the stopped processes as failing, or as going slowly. • Captures the idea that slow processes don’t block others. • Rules out locking strategies.
• Performance – Worst-case (time bounds) vs. average case (throughput). – No good formal models
![Page 5: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/5.jpg)
List-based sets• Data type: Set S of integers (no duplicates)
– S.add(x): Boolean: S := S {x}; return true iff x not already in S – S.remove(x): Boolean: S := S \ {x}; return true iff x in S initially – S.contains(x): Boolean: return true iff x in S (no change to S)
• Simple ordered linked-list-based implementation – Illustrates techniques useful for pointer-based data structures. – Unless set is small, this is a poor data structure for this specific
data type--better to use arrays, hash tables, etc.
head
�� 1 4 9 �
![Page 6: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/6.jpg)
Sequential list-based set head
�� 1 4 9 �
add(3) 3
head
1 4 9 ���
remove(4)
![Page 7: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/7.jpg)
Sequential list-based setS.add(x) pred := S.head curr := pred.next while (curr.key < x) pred := currcurr := pred.next
if curr.key = x thenreturn false
else node := new Node(x) node.next := curr pred.next := node return true
S.remove(x) pred := S.head curr := pred.next while (curr.key < x) pred := curr curr := pred.next
if curr.key = x then pred.next := curr.next return true
elsereturn false
S.contains(x) curr := S.head while (curr.key < x)
curr := curr.next if curr.key = x then return true
else return false
![Page 8: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/8.jpg)
Sequential list-based set head
S.remove(x) pred := S.head
4 9 �1��
pred curr
remove(4)
curr := pred.next while (curr.key < x) pred := curr curr := pred.next
if curr.key = x then pred.next := curr.next return true
else return false
![Page 9: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/9.jpg)
Correctness• Assume algorithm queues up operations, runs them
sequentially. • Atomicity (linearizability):
– Show the algorithm implements a canonical atomic set object. – Use forward simulation relation: Set consists of those elements
that are reachable from the head of the list via list pointers. – When do “perform” steps occur?
• add(x): If successful, then when pred.next := node, else any timeduring the operation.
• remove(x): If successful, then when pred.next := curr.next, else any time during the operation.
• contains(x): Any time during the operation. – Proof uses invariants saying that the list is ordered and contains no
duplicates. • Liveness: Lockout-free, but blocking (not wait-free or lock-
free)
![Page 10: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/10.jpg)
Invariants
• Keys strictly increase down the list. – List is ordered. – No duplicates.
• Keys of first and last nodes (i.e., the “sentinels”) are � and respectively.
• pred.key < x • pred.key < curr.key • pred.next ำ null • …
![Page 11: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/11.jpg)
Allowing concurrent access z Can this algorithm tolerate concurrent execution of the operations by different processes?
z What can go wrong? z How can we fix it?
![Page 12: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/12.jpg)
Concurrent operations (bad) head
9 �1 4��
pred curr
remove(4)
S.remove(x) pred := S.head curr := pred.next while (curr.key < x) pred := curr curr := pred.next
if curr.key = x then pred.next := curr.next return true
elsereturn false
pred curr
remove(9)
![Page 13: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/13.jpg)
Techniques for managing concurrent operations
• Coarse-grained mutual exclusion • Fine-grained locking • Optimistic locking • Lock-free/nonblocking algorithms • “Lazy” synchronization
![Page 14: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/14.jpg)
Coarse-grained mutual exclusion
• Each process acquires a global lock, for the entire time it is executing significant steps of an operation implementation.
![Page 15: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/15.jpg)
Coarse-grained lockingS.add(x) Why can we unlock early here?S.lock()
pred := S.head curr := pred.next while (curr.key < x)
pred := curr curr := pred.next
if curr.key = x then S.unlock() return false
else node := new Node(x) node.next := curr pred.next := node S.unlock() return true
S.lock() pred := S.head curr := pred.next while (curr.key < x)
pred := curr curr := pred.next
if curr.key = x then pred.next := curr.next S.unlock() return true
else S.unlock() return false
S.contains(x) S.lock() curr := S.head while (curr.key < x)
curr := curr.next S.unlock() if curr.key = x then return true
else return false
![Page 16: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/16.jpg)
Correctness• Similar to sequential implementation. • Atomicity:
– Show the algorithm implements a canonical atomic set object. – Use forward simulation: S = elements that are reachable in the list – When do “perform” steps occur?
• add(x): If successful, then when pred.next := node, else any time the lock is held.
• remove(x): If successful, then when pred.next := curr.next, else any time the lock is held.
• contains(x): Any time the lock is held. – Invariant: If an operation holds the lock, then any node it visits is
reachable in the list. • Liveness:
– Guarantees progress, assuming that the lock does. – May or may not be lockout-free, depending on whether the lock is. – Blocking (not wait-free or lock-free):
• Everything comes to a halt if someone stops while holding the lock.
![Page 17: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/17.jpg)
Coarse-grained locking
4 9 �1 head
��
pred curr
remove(4)
![Page 18: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/18.jpg)
Coarse-grained locking
• Easy For many applications, this is the best solution!
– to write, (Don't underrate simplicity.)
– to prove correct. • Guarantees progress • If we use queue locks, it’s lockout-free. • But:
– Blocking (not wait-free, not lock-free) – Poor performance when contention is high
• Essentially no concurrent access. • But often good enough for low contention.
![Page 19: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/19.jpg)
Coarse-grained locking with high contention
head 4�� 1 9 �
pred curr
remove(4)remove(9)
add(6)contains(4)
add(3)
![Page 20: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/20.jpg)
Improving coarse-grained locking
z Reader/writer locks � Multiple readers can hold the lock simultaneously, but writer cannot share with anyone else (reader or writer).
z Using reader/writer lock for coarse-grained locking, in the list-based set implementation:� Contains takes only a read lock
z Can be a big win if contains is the most common operation.
� What about add or remove that returns false?z Let add/remove start with a read lock, then “upgrade” to a write lock
if needed. z If it can’t upgrade, abandon/restart the operation.
![Page 21: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/21.jpg)
Fine-grained locking
• Associate locks with smaller pieces of data, not entire data structure.
• Process acquires/releases locks as it executes steps of an operation.
• Operations that work on disjoint pieces of data proceed concurrently.
![Page 22: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/22.jpg)
Two-phase locking• Finish acquiring all locks before releasing any.
– Typically, release all locks at end of the op: “strict 2-phase locking”. • Easy to prove atomicity:
– Serialize each operation at any point when it holds all its locks. – For strict 2-phase locking, usually the end of the operation. – Algorithm behaves like sequential algorithm, with operations
performed in order of serialization points. • But acquiring all the locks at once can be costly (delays). • Must avoid deadlock, e.g., by acquiring locks in
predetermined order.
• Naïve 2-phase locking for list-based set implementation: – Lock each node as visited, using a mutex lock. – Avoids deadlock by acquiring all locks in list order. – Doesn’t help performance. – Using reader/writer locks might help performance, but introduces
new deadlock possibilities.
![Page 23: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/23.jpg)
Hand-over-hand locking z Fine-grained locking, but not “two-phase”� Atomicity doesn't follow from general rule; trickier to prove.
z Each process holds at most two locks at a time. � Acquires lock for successor before releasing lock for predecessor.
� Keeps operations “pipelined”.
head �� 1 4 9 �
remove(4)
pred curr
![Page 24: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/24.jpg)
Hand-over-hand locking z Must we lock a node we are trying to remove?
z Can’t we just lock its predecessor, while resetting the predecessor’s next pointer?
z No. Counterexample (from Herlihy and Shavit’s slides):
![Page 25: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/25.jpg)
Removing a Node
a b c d
remove(b)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 26: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/26.jpg)
Removing a Node
a b c d
remove(b)
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 27: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/27.jpg)
Removing a Node
a b c d
remove(b)
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 28: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/28.jpg)
Removing Two Nodes
a b c d
remove(c)remove(b)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 29: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/29.jpg)
Removing Two Nodes
a b c d
remove(b) remove(c)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 30: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/30.jpg)
Removing Two Nodes
a b c d
remove(b) remove(c)
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 31: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/31.jpg)
Removing Two Nodes
a b c d
remove(b) remove(c)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 32: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/32.jpg)
Removing Two Nodes
a b c d
remove(b) remove(c)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 33: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/33.jpg)
Removing Two Nodes
a b c d
remove(b) remove(c)
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 34: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/34.jpg)
Removing Two Nodes
a b c d
remove(b) remove(c)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 35: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/35.jpg)
Removing Two Nodes
a b c d
remove(b) remove(c)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 36: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/36.jpg)
Removing Two Nodes
a c d
remove(b)
b
remove(c)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 37: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/37.jpg)
Removing Two Nodes
a c d
remove(b)
b
remove(c)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 38: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/38.jpg)
Hand-over-hand locking
• add(x) – Lock hand-over-hand. – When adding new node, keep both predecessor
and successor locked (HS Fig. 9.6). – We could actually release the lock on the
successor before adding the new node.• contains(x)
– Lock hand-over-hand, can unlock everything before reading curr.key.
![Page 39: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/39.jpg)
Hand-over-hand lockingS.add(x) pred := S.head pred.lock() curr := pred.next curr.lock() while (curr.key < x) pred.unlock() pred := curr curr := pred.next curr.lock()
if curr.key = x then pred.unlock()curr.unlock()return false
else node := new Node(x) node.next := curr pred.next := node pred.unlock()curr.unlock()return true
S.remove(x) pred := S.head pred.lock() curr := pred.next curr.lock() while (curr.key < x) pred.unlock() pred := curr curr := pred.next curr.lock()
if curr.key = x then pred.next := curr.next pred.unlock()curr.unlock()return true
elsepred.unlock()curr.unlock()return false
S.contains(x) curr := S.head curr.lock() while (curr.key < x) temp := curr curr := curr.next curr.lock()temp.unlock()
curr.unlock()if curr.key = x then return true
else return false
![Page 40: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/40.jpg)
Correctness• Atomicity:
– Similar to coarse-grained locking. – Forward simulation to canonical atomic set object: S = elements
that are reachable in the list. – “perform” steps:
• add(x): – If successful, then when pred.next := node. – Else any time the lock on the node already containing x is held.
• remove(x): – If successful, then when pred.next := curr.next – Else any time the lock on the node seen to have a higher key is held.
• contains(x): LTTR – If true, then any time the lock on the node containing x is held. – Else any time the lock on the node seen to have a higher key is held.
– Invariant: Any locked node is reachable in the list.
![Page 41: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/41.jpg)
Correctness• Atomicity:
– Forward simulation to canonical atomic set object:• S = elements that are reachable in the list.
• Liveness: – Guarantees progress, assuming that the locks do. – Guarantees lockout-freedom, assuming the locks do.
• All processes compete for locks in the same order.
– Blocking (not wait-free or lock-free).
![Page 42: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/42.jpg)
Evaluation• Problems:
– Each operation must acquire O(|S|) locks. – Pipelining means that fast threads can get stuck
behind slow threads. – Using reader/writer locks might help performance, but
introduces new deadlock possibilities. • Idea:
– Can we examine the nodes first without locking, and then lock only the nodes we need?
– Must ensure that the node we modify is still in list.– Optimistic locking.
![Page 43: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/43.jpg)
Optimistic locking
• Examine the nodes first without locking. • Lock the nodes we need. • Verify that the locked nodes are still in the
list, before making modifications or determining results
![Page 44: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/44.jpg)
Optimistic locking• add(x):
–Traverse the list from the head, without locking, looking for the nodes we need (pred and curr).
–Lock nodes pred and curr. –Validate that pred and curr are still in the list, and are still consecutive
(pred.next = curr), by traversing the list once again. – If this works, then add the node and return true (or return false if it’s
already there). – If it doesn’t work, start over.
• remove(x), contains(x): Similar.
z Better than hand-over-hand if � Traversing twice without locking is cheaper than once with locking. � Validation usually succeeds
![Page 45: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/45.jpg)
Optimistic locking
b d ea
add(c) Aha!
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 46: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/46.jpg)
Optimistic locking
b d ea
add(c)
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 47: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/47.jpg)
What can go wrong? (Part 1)
b d ea
add(c)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 48: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/48.jpg)
What can go wrong? (Part 1)
b d ea
add(c) remove(b)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 49: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/49.jpg)
What can go wrong? (Part 1)
b d ea
add(c)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 50: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/50.jpg)
Validate (Part 1)
b d ea
add(c) Yes, b still reachable from head
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 51: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/51.jpg)
What can go wrong? (Part 2)
b d ea
add(c)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 52: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/52.jpg)
What can go wrong? (Part 2)
b d ea
add(c) add(b’)
b’
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 53: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/53.jpg)
What can go wrong? (Part 2)
b d ea
add(c)
b’
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 54: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/54.jpg)
Validate (Part 2)
b d ea
add(c) Yes, b still points to d
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 55: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/55.jpg)
Optimistic locking
b d ea
add(c) c
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 56: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/56.jpg)
Correctness• Atomicity: Similar to hand-over-hand locking.
– Forward simulation to canonical atomic set object: • S = elements that are reachable in the list.
– “perform” steps: As for hand-over-hand locking, but consider only the last attempt (for which validation succeeds).
• Liveness: – Guarantees progress, assuming the locks do. – Does not guarantee lockout-freedom (even if locks
do). – Blocking (not wait-free or lock-free).
![Page 57: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/57.jpg)
Evaluation• Works well if lock-free traversal is fast, and
contention is infrequent. • Problems:
– Repeated traversals. – Need to acquire locks.
• Even contains() needs locks. z Locks can cause problems: � Some operations take 1000x (or more) longer than
others, due to page faults, descheduling, etc. � If this happens to anyone holding a lock, everyone else
who wants to access that lock must wait.
• Q: Can we avoid locks?
![Page 58: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/58.jpg)
Lock-free algorithm
• Avoids locks/blocking entirely. • Instead, separates logical vs. physical node
removal, marking nodes before deleting them.
• Operations help other operations by deleting marked nodes.
![Page 59: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/59.jpg)
Lock-freedom
• If any process executing an operation does not stop then some operation completes.
• Weaker than wait-free: lockout is possible.• Rules out a delayed process from blocking
other processes indefinitely, and so, no locks.
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit..
![Page 60: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/60.jpg)
Lock-free list-based set
• Idea: Use CAS to change pred.next pointer.• Make sure pred.next pointer hasn't changed
since you read it.
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 61: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/61.jpg)
Adding a Node
a c d
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 62: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/62.jpg)
Adding a Node
a c d
b
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 63: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/63.jpg)
Adding a Node
a c d
b
CAS
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 64: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/64.jpg)
Adding a Node
a c d
b
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 65: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/65.jpg)
Removing a Node
a b c d
remove b
remove c
CAS
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 66: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/66.jpg)
Removing a Node
a c d
remove b
remove c
CAS
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 67: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/67.jpg)
Look Familiar?Bad news
a b c d
remove b
remove c
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 68: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/68.jpg)
Lock-free list-based set• Idea: Add “mark” bit to a node to indicate whether its key has been
removed from the abstract set S. – If mark = true, then node’s key is not in the set. – When a node is first added to the list, its mark = false. – Set mark := true before physically removing node from list by detaching its
incoming pointer. – Setting the mark logically removes the node’s key from the set: It is the
serialization point of a successful remove operation.
• Simulation relation: – S is the set of values in reachable nodes with mark = false.
• Don't change next pointer of a marked node. – Mark and next pointer must be in the same word, change atomically. – “Steal” a bit from pointers. – Jave class AtomicMarkableReference (in Java concurrency library)
supports techniques like those in this algorithm.
![Page 69: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/69.jpg)
Lock-free list-based set• To perform any operation, traverse the list, through
marked and unmarked nodes, to find needed nodes. • If needed nodes are marked, retry the operation. • If needed nodes are unmarked then operate as follows:
– For contains(x) or unsuccessful add/remove(x), return appropriate value as usual based on whether curr.key = x
– For successful add(x), CAS pred’s (curr, false) to (node, false).
– For successful remove(x), • Logical removal: CAS curr’s (next, false) to (next, true) • Physical removal: CAS pred’s (curr, false) to (curr.next, false)
– If any CAS except for the physical remove fails, retry the operation.
![Page 70: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/70.jpg)
Helping• Whenever an operation encounters marked nodes
during traversal, it helps: • If curr is marked:
– CAS pred’s (curr, false) to (curr.next, false). – If this CAS fails (because next is no longer curr or
mark is now true), then retry the operation.
• Such helping is characteristic of lock-free andwait-free algorithms (not all have it, but most do).
• See HS Section 9.8.
![Page 71: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/71.jpg)
Lock-free list: Find subroutineReturns (pred, curr) such that at some point during execution, the following held simultaneously: pred.next = (curr, false), curr.next.mark = false, and pred.key < x d curr.key.
S.find(x) retry:
pred := S.head; curr := pred.next.ref while (curr.key < x or curr.next.mark) do
if curr.next.mark then if CAS(pred.next, (curr, false), (curr.next.ref, false)) then curr := pred.next.ref else if pred.next.mark then goto retryelse curr := pred.next.ref
else // It must be that curr.key < x.pred : = curr; curr := pred.next.ref
return (pred, curr)
![Page 72: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/72.jpg)
Lock-free list: Add S.add(x) retry:
(pred, curr) := S.find(x) if curr.key = x then return false else
node := new Node(x) node.next.ref := curr if CAS(pred.next, (curr, false), (node, false)) then return true else goto retry
![Page 73: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/73.jpg)
Lock-free list: Remove and Contains
S.remove(x) retry:
(pred, curr) := S.find(x) if curr.key = x then
next := curr.next.ref if CAS(curr.next, (next, false), (next, true)) then
CAS(pred.next, (curr, false), (curr.next.ref, false)) return true
else goto retryelse return false
S.contains(x) (pred, curr) := S.find(x) if curr.key = x then return true else return false
![Page 74: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/74.jpg)
Removing a Node
a c d
remove c
CAS
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 75: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/75.jpg)
Removing a Node
a d
remove b
remove c
cCASCAS
failed
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 76: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/76.jpg)
Removing a Node
a b d
remove b
remove c
c
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 77: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/77.jpg)
Removing a Node
a d
remove b
remove c
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 78: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/78.jpg)
Correctness• Atomicity:
– Forward simulation to canonical atomic set: • S = values in unmarked nodes that are reachable from the head via list
pointers (through marked and unmarked nodes). – “perform” steps:
• contains(x) or unsuccessful add(x) or remove(x): When curr is read from pred.next.
• Successful add(x): When successful CAS sets pred.next := node. • Successful remove(x): When successful CAS marks node x (sets
curr.mark := true). – Invariant: Any unmarked node encountered while traversing the list
is reachable in the list. • Liveness:
– Nonblocking: lock-free • Operations may retry, but some must succeed.
– Allows starvation (not lockout-free).
![Page 79: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/79.jpg)
Evaluation
• No locks! • Nonblocking, lock-free algorithm. • But: Overhead for CAS and for helping.
![Page 80: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/80.jpg)
Lazy algorithm
• Uses the marking trick as in the lock-free algorithm, removing nodes in two stages.
• Avoids CAS and helping. • Instead, uses short-duration locks.
![Page 81: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/81.jpg)
Lazy list algorithm• Idea: Use mark as in lock-free list. • “Lazy” removal: First mark node, then splice around it. • Now mark can be separate from next pointer. • No helping---assume each remove operation completes its
own physical removal.
• Locks curr and pred nodes, with short-duration locks. • Validation: Check locally that nodes are adjacent and
unmarked; if not, retry the operation.
• See HS, Section 9.7.
![Page 82: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/82.jpg)
Lazy Removal
a b c d
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 83: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/83.jpg)
Lazy Removal
a b c d
Present in list
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 84: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/84.jpg)
Lazy Removal
a b c d
Logically deleted
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 85: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/85.jpg)
Lazy Removal
a b c d
Physically deleted
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 86: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/86.jpg)
Lazy list algorithm
• Observation: contains(x) doesn't need to lock/validate.
• Just find first node with key ุ x, return true iff key = x and unmarked.
![Page 87: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/87.jpg)
Lazy list algorithm
a b c
contains(b)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 88: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/88.jpg)
Lazy list algorithm
a b c
contains(b)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 89: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/89.jpg)
Lazy list algorithm
a b c
contains(b)
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 90: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/90.jpg)
Lazy list algorithm
a b c
remove(b)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 91: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/91.jpg)
Lazy list algorithm
a b c
a not marked
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 92: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/92.jpg)
Lazy list algorithm
© 2005 Herlihy & Shavit From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
a b c
a still points
to b
![Page 93: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/93.jpg)
Lazy list algorithm
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
a b c
logical delete
![Page 94: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/94.jpg)
Lazy list algorithm
© 2005 Herlihy & Shavit From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
a b c
physical delete
![Page 95: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/95.jpg)
Lazy list algorithm
a b c
contains(b)
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 96: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/96.jpg)
Lazy list algorithm
a b c
add(b)
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 97: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/97.jpg)
Lazy list algorithm
a b c
add(b)
b
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 98: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/98.jpg)
Lazy list algorithm
a b
contains(b)
b
c
Is this okay?
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 99: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/99.jpg)
Lazy list algorithm
a c
add(b)
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 100: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/100.jpg)
Lazy list algorithm
a c
add(b)
b
© 2005 Herlihy & ShavitFrom The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 101: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/101.jpg)
Lazy list algorithm
a c
contains(b)
b
© 2005 Herlihy & Shavit
From The Art of Multiprocessor Programming, Maurice Herlihy and Nir Shavit.
![Page 102: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/102.jpg)
Lazy List: Add Nodes have fields: key, next, mark.
S.add(x) retry:
pred := S.head; curr := pred.next while (curr.key < x) do pred := curr; curr := curr.next if (curr.key = x and curr.mark = false) then return false else
pred.lock()if (pred.mark = false and pred.next = curr) then node := new Node(x)node.next := curr pred.next := nodepred.unlock() return true
elsepred.unlock()goto retry
![Page 103: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/103.jpg)
Lazy List: Remove S.remove(x) retry:
pred := S.head; curr := pred.next while (curr.key < x) do pred := curr; curr := curr.next if (curr.key > x or curr.mark = true) then return false else
pred.lock(); curr.lock()if (pred.mark = curr.mark = false and pred.next = curr) then
curr.mark := true pred.next := curr.nextpred.unlock(); curr.unlock() return true
elsepred.unlock(); curr.unlock()goto retry
![Page 104: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/104.jpg)
Lazy List: Contains S.contains(x) curr := S.head.next while (curr.key < x) do curr := curr.next if (curr.key = x and curr.mark = false) then return true else return false
![Page 105: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/105.jpg)
Lazy list algorithm z Serializing contains(x) that returns false � if node found has key > x
z when node.key is read? z when pred.next is read? z when pred is marked (if it is marked)?
� if node with key = x is marked z when mark is read? z when pred.next is read? z when mark is set?
![Page 106: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/106.jpg)
Lazy list algorithm z Serializing contains(x) that returns false � if node found has key > x
z when node.key is read? z when pred.next is read? z when pred is marked (if it is marked)?
� if node with key = x is marked z when mark is read? z when pred.next is read? z when mark is set?
Can we do this for the optimistic list?
![Page 107: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/107.jpg)
Correctness• Atomicity:
– Forward simulation to canonical atomic set: • S = values in reachable unmarked nodes.
– “perform” steps: • contains(x) or unsuccessful add(x) or remove(x): LTTR, based
on some technical cases. • Successful add(x): When pred.next := node. • Successful remove(x): When curr.mark := true.
• Liveness: – contains is wait-free. – add, remove are blocking. – add, remove satisfy progress, but not lockout-
freedom.
![Page 108: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/108.jpg)
Lock-free list with wait-free contains()
• Add and remove just like lock-free list. • Contains() does not help, does not retry, just
like in lazy list.
![Page 109: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/109.jpg)
Evaluation/Comparison
• Lock-free list with wait-free contains(): – contains() is wait-free – add() and remove() are nonblocking (lock-free) – Incurs overhead of CAS and of cleanup.
• Lazy list: – contains() is wait-free – add() and remove() are blocking, but use short
lock durations. – Low overhead.
![Page 110: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/110.jpg)
Application of list techniques z Trees z Skip lists � multiple layers of links � list at each layer is sublist of layer below � logarithmic expected search time if each list has halfelements of next lower level z probabilistic guarantees
2 5
8
7 90
![Page 111: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/111.jpg)
Next timez Transactional memory z Reading: �HS, Chapter 9 �Guerraoui, Kapalka
![Page 112: 6.852: Distributed Algorithms Fall, 2009 - MIT OpenCourseWare · 6.852: Distributed Algorithms Fall, 2009 Class 19. Today’s plan z Techniques for implementing concurrent objects:](https://reader030.vdocuments.us/reader030/viewer/2022021717/5b51abf37f8b9af4408c71bb/html5/thumbnails/112.jpg)
MIT OpenCourseWarehttp://ocw.mit.edu
6.852J / 18.437J Distributed Algorithms Fall 2009
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.