Transactional Locking Nir Shavit Tel Aviv University Joint work with Dave Dice and Ori Shalev.
Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf ·...
Transcript of Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf ·...
![Page 1: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/1.jpg)
Linked Lists: Locking, Lock-Free, and Beyond …
Companion slides forThe Art of Multiprocessor Programming
by Maurice Herlihy & Nir Shavit
![Page 2: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/2.jpg)
Art of Multiprocessor Programming 2
Last Lecture: Spin-Locks
CS
Resets lockupon exit
spinlock
criticalsection
...
![Page 3: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/3.jpg)
Art of Multiprocessor Programming 3
Today: Concurrent Objects
• Adding threads should not lower throughput– Contention effects– Mostly fixed by Queue locks
![Page 4: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/4.jpg)
Art of Multiprocessor Programming 4
Today: Concurrent Objects
• Adding threads should not lower throughput– Contention effects– Mostly fixed by Queue locks
• Should increase throughput– Not possible if inherently sequential– Surprising things are parallelizable
![Page 5: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/5.jpg)
Art of Multiprocessor Programming 5
Coarse-Grained Synchronization
• Each method locks the object– Avoid contention using queue locks
![Page 6: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/6.jpg)
Art of Multiprocessor Programming 6
Coarse-Grained Synchronization
• Each method locks the object– Avoid contention using queue locks – Easy to reason about
• In simple cases
![Page 7: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/7.jpg)
Art of Multiprocessor Programming 7
Coarse-Grained Synchronization
• Each method locks the object– Avoid contention using queue locks – Easy to reason about
• In simple cases
• So, are we done?
![Page 8: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/8.jpg)
Art of Multiprocessor Programming 8
Coarse-Grained Synchronization
• Sequential bottleneck– Threads “stand in line”
![Page 9: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/9.jpg)
Art of Multiprocessor Programming 9
Coarse-Grained Synchronization
• Sequential bottleneck– Threads “stand in line”
• Adding more threads– Does not improve throughput– Struggle to keep it from getting worse
![Page 10: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/10.jpg)
Art of Multiprocessor Programming 10
Coarse-Grained Synchronization
• Sequential bottleneck– Threads “stand in line”
• Adding more threads– Does not improve throughput– Struggle to keep it from getting worse
• So why even use a multiprocessor?– Well, some apps inherently parallel …
![Page 11: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/11.jpg)
Art of Multiprocessor Programming 11
This Lecture
• Introduce four “patterns”– Bag of tricks …– Methods that work more than once …
![Page 12: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/12.jpg)
Art of Multiprocessor Programming 12
This Lecture
• Introduce four “patterns”– Bag of tricks …– Methods that work more than once …
• For highly-concurrent objects– Concurrent access– More threads, more throughput
![Page 13: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/13.jpg)
Art of Multiprocessor Programming 13
First:Fine-Grained Synchronization
• Instead of using a single lock …
![Page 14: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/14.jpg)
Art of Multiprocessor Programming 14
First:Fine-Grained Synchronization
• Instead of using a single lock …• Split object into
– Independently-synchronized components
![Page 15: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/15.jpg)
Art of Multiprocessor Programming 15
First:Fine-Grained Synchronization
• Instead of using a single lock …• Split object into
– Independently-synchronized components• Methods conflict when they access
– The same component …– At the same time
![Page 16: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/16.jpg)
Art of Multiprocessor Programming 16
Second:Optimistic Synchronization
• Search without locking …
![Page 17: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/17.jpg)
Art of Multiprocessor Programming 17
Second:Optimistic Synchronization
• Search without locking …• If you find it, lock and check …
– OK: we are done– Oops: start over
![Page 18: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/18.jpg)
Art of Multiprocessor Programming 18
Second:Optimistic Synchronization
• Search without locking …• If you find it, lock and check …
– OK: we are done– Oops: start over
• Evaluation– Usually cheaper than locking, but– Mistakes are expensive
![Page 19: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/19.jpg)
Art of Multiprocessor Programming 19
Third:Lazy Synchronization
• Postpone hard work
![Page 20: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/20.jpg)
Art of Multiprocessor Programming 20
Third:Lazy Synchronization
• Postpone hard work• Removing components is tricky
![Page 21: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/21.jpg)
Art of Multiprocessor Programming 21
Third:Lazy Synchronization
• Postpone hard work• Removing components is tricky
– Logical removal• Mark component to be deleted
![Page 22: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/22.jpg)
Art of Multiprocessor Programming 22
Third:Lazy Synchronization
• Postpone hard work• Removing components is tricky
– Logical removal• Mark component to be deleted
– Physical removal• Do what needs to be done
![Page 23: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/23.jpg)
Art of Multiprocessor Programming 23
Fourth:Lock-Free Synchronization
• Don’t use locks at all– Use compareAndSet() & relatives …
![Page 24: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/24.jpg)
Art of Multiprocessor Programming 24
Fourth:Lock-Free Synchronization
• Don’t use locks at all– Use compareAndSet() & relatives …
• Advantages– No Scheduler Assumptions/Support
![Page 25: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/25.jpg)
Art of Multiprocessor Programming 25
Fourth:Lock-Free Synchronization
• Don’t use locks at all– Use compareAndSet() & relatives …
• Advantages– No Scheduler Assumptions/Support
• Disadvantages– Complex– Sometimes high overhead
![Page 26: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/26.jpg)
Art of Multiprocessor Programming 26
Linked List
• Illustrate these patterns …• Using a list-based Set
– Common application– Building block for other apps
![Page 27: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/27.jpg)
Art of Multiprocessor Programming 27
Set Interface
• Unordered collection of items
![Page 28: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/28.jpg)
Art of Multiprocessor Programming 28
Set Interface
• Unordered collection of items• No duplicates
![Page 29: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/29.jpg)
Art of Multiprocessor Programming 29
Set Interface
• Unordered collection of items• No duplicates• Methods– add(x) put x in set– remove(x) take x out of set– contains(x) tests if x in set
![Page 30: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/30.jpg)
Art of Multiprocessor Programming 30
List-Based Sets
public interface Set<T> { public boolean add(T x); public boolean remove(T x); public boolean contains(T x);}
![Page 31: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/31.jpg)
Art of Multiprocessor Programming 31
List-Based Sets
public interface Set<T> { public boolean add(T x); public boolean remove(T x); public boolean contains(T x);}
Add item to set
![Page 32: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/32.jpg)
Art of Multiprocessor Programming 32
List-Based Sets
public interface Set<T> { public boolean add(T x); public boolean remove(T x); public boolean contains(Tt x);}
Remove item from set
![Page 33: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/33.jpg)
Art of Multiprocessor Programming 33
List-Based Sets
public interface Set<T> { public boolean add(T x); public boolean remove(T x); public boolean contains(T x);}
Is item in set?
![Page 34: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/34.jpg)
Art of Multiprocessor Programming 34
List Node
public class Node { public T item; public int key; public volatile Node next;}
![Page 35: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/35.jpg)
Art of Multiprocessor Programming 35
List Node
public class Node { public T item; public int key; public volatile Node next;}
item of interest
![Page 36: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/36.jpg)
Art of Multiprocessor Programming 36
List Node
public class Node { public T item; public int key; public volatile Node next;}
Usually hash code
![Page 37: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/37.jpg)
Art of Multiprocessor Programming 37
List Node
public class Node { public T item; public int key; public volatile Node next;}
Reference to next node
![Page 38: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/38.jpg)
Art of Multiprocessor Programming 38
The List-Based Set
a b c
Sorted with Sentinel nodes(min & max possible keys)
-∞
+∞
![Page 39: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/39.jpg)
Art of Multiprocessor Programming 39
Reasoning about Concurrent Objects
• Invariant– Property that always holds
![Page 40: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/40.jpg)
Art of Multiprocessor Programming 40
Reasoning about Concurrent Objects
• Invariant– Property that always holds
• Established because– True when object is created– Truth preserved by each method
• Each step of each method
![Page 41: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/41.jpg)
Art of Multiprocessor Programming 41
Specifically …
• Invariants preserved by– add()– remove()– contains()
![Page 42: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/42.jpg)
Art of Multiprocessor Programming 42
Specifically …
• Invariants preserved by– add()– remove()– contains()
• Most steps are trivial– Usually one step tricky– Often linearization point
![Page 43: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/43.jpg)
Art of Multiprocessor Programming 43
Interference
• Invariants make sense only if – methods considered– are the only modifiers
![Page 44: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/44.jpg)
Art of Multiprocessor Programming 44
Interference
• Invariants make sense only if – methods considered– are the only modifiers
• Language encapsulation helps– List nodes not visible outside class
![Page 45: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/45.jpg)
Art of Multiprocessor Programming 45
Interference
• Invariants make sense only if – methods considered– are the only modifiers
• Language encapsulation helps– List nodes not visible outside class
![Page 46: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/46.jpg)
Art of Multiprocessor Programming 46
Interference
• Freedom from interference needed even for removed nodes– Some algorithms traverse removed nodes– Careful with malloc()& free()!
• We rely on garbage collection
![Page 47: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/47.jpg)
Art of Multiprocessor Programming 47
Abstract Data Types
• Concrete representation:
• Abstract Type: {a, b}
a b
![Page 48: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/48.jpg)
Art of Multiprocessor Programming 48
Abstract Data Types
• Meaning of rep given by abstraction map
S( ) = {a,b}a b
![Page 49: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/49.jpg)
Art of Multiprocessor Programming 49
Rep Invariant
• Which concrete values meaningful?– Sorted?– Duplicates?
• Rep invariant– Characterizes legal concrete reps– Preserved by methods– Relied on by methods
![Page 50: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/50.jpg)
Art of Multiprocessor Programming 50
Blame Game
• Rep invariant is a contract• Suppose– add()leaves behind 2 copies of x– remove() removes only 1
• Which is incorrect?
![Page 51: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/51.jpg)
Art of Multiprocessor Programming 51
Blame Game
• Suppose– add() leaves behind 2 copies of x– remove() removes only 1
![Page 52: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/52.jpg)
Art of Multiprocessor Programming 52
Blame Game
• Suppose– add() leaves behind 2 copies of x– remove() removes only 1
• Which is incorrect?– If rep invariant says no duplicates
• add() is incorrect– Otherwise
• remove() is incorrect
![Page 53: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/53.jpg)
Art of Multiprocessor Programming 53
Rep Invariant (partly)
• Sentinel nodes– tail reachable from head
• Sorted• No duplicates
![Page 54: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/54.jpg)
Art of Multiprocessor Programming 54
Abstraction Map
• S(head) ={ x | there exists a such that
• a reachable from head and• a.item = x
}
![Page 55: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/55.jpg)
Art of Multiprocessor Programming 55
Sequential List Based Set
a c d
a b c
add()
remove()
![Page 56: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/56.jpg)
Art of Multiprocessor Programming 56
Sequential List Based Set
a c d
b
a b c
add()
remove()
![Page 57: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/57.jpg)
Art of Multiprocessor Programming 57
Coarse-Grained Locking
a b d
![Page 58: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/58.jpg)
Art of Multiprocessor Programming 58
Coarse-Grained Locking
a b d
c
![Page 59: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/59.jpg)
Art of Multiprocessor Programming 59
honk!
Coarse-Grained Locking
a b d
c
Simple but hotspot + bottleneck
honk!
![Page 60: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/60.jpg)
Art of Multiprocessor Programming 60
Coarse-Grained Locking
• Easy, same as synchronized methods– “One lock to rule them all …”
![Page 61: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/61.jpg)
Art of Multiprocessor Programming 61
Coarse-Grained Locking
• Easy, same as synchronized methods– “One lock to rule them all …”
• Simple, clearly correct– Deserves respect!
• Works poorly with contention– Queue locks help– But bottleneck still an issue
![Page 62: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/62.jpg)
Art of Multiprocessor Programming 62
Fine-grained Locking
• Requires careful thought– “Do not meddle in the affairs of wizards, for
they are subtle and quick to anger”
![Page 63: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/63.jpg)
Art of Multiprocessor Programming 63
Fine-grained Locking
• Requires careful thought– “Do not meddle in the affairs of wizards, for
they are subtle and quick to anger”• Split object into pieces
– Each piece has own lock– Methods that work on disjoint pieces need
not exclude each other
![Page 64: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/64.jpg)
Art of Multiprocessor Programming 64
Hand-over-Hand locking
a b c
![Page 65: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/65.jpg)
Art of Multiprocessor Programming 65
Hand-over-Hand locking
a b c
![Page 66: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/66.jpg)
Art of Multiprocessor Programming 66
Hand-over-Hand locking
a b c
![Page 67: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/67.jpg)
Art of Multiprocessor Programming 67
Hand-over-Hand locking
a b c
![Page 68: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/68.jpg)
Art of Multiprocessor Programming 68
Hand-over-Hand locking
a b c
![Page 69: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/69.jpg)
Art of Multiprocessor Programming 69
Removing a Node
a b c d
remove(b)
![Page 70: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/70.jpg)
Art of Multiprocessor Programming 70
Removing a Node
a b c d
remove(b)
![Page 71: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/71.jpg)
Art of Multiprocessor Programming 71
Removing a Node
a b c d
remove(b)
![Page 72: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/72.jpg)
Art of Multiprocessor Programming 72
Removing a Node
a b c d
remove(b)
![Page 73: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/73.jpg)
Art of Multiprocessor Programming 73
Removing a Node
a b c d
remove(b)
![Page 74: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/74.jpg)
Art of Multiprocessor Programming 74
Removing a Node
a c d
remove(b)Why lock victim node?
![Page 75: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/75.jpg)
Art of Multiprocessor Programming 75
Concurrent Removes
a b c d
remove(c)remove(b)
![Page 76: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/76.jpg)
Art of Multiprocessor Programming 76
Concurrent Removes
a b c d
remove(b)remove(c)
![Page 77: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/77.jpg)
Art of Multiprocessor Programming 77
Concurrent Removes
a b c d
remove(b)remove(c)
![Page 78: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/78.jpg)
Art of Multiprocessor Programming 78
Concurrent Removes
a b c d
remove(b)remove(c)
![Page 79: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/79.jpg)
Art of Multiprocessor Programming 79
Concurrent Removes
a b c d
remove(b)remove(c)
![Page 80: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/80.jpg)
Art of Multiprocessor Programming 80
Concurrent Removes
a b c d
remove(b)remove(c)
![Page 81: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/81.jpg)
Art of Multiprocessor Programming 81
Concurrent Removes
a b c d
remove(b)remove(c)
![Page 82: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/82.jpg)
Art of Multiprocessor Programming 82
Concurrent Removes
a b c d
remove(b)remove(c)
![Page 83: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/83.jpg)
Art of Multiprocessor Programming 83
Concurrent Removes
a b c d
remove(b)remove(c)
![Page 84: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/84.jpg)
Art of Multiprocessor Programming 84
Concurrent Removes
a b c d
remove(b)remove(c)
![Page 85: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/85.jpg)
Art of Multiprocessor Programming 85
Uh, Oh
a c d
remove(b)remove(c)
![Page 86: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/86.jpg)
Art of Multiprocessor Programming 86
Uh, Oh
a c d
Bad news, c not removed
remove(b)remove(c)
![Page 87: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/87.jpg)
Art of Multiprocessor Programming 87
Problem
• To delete node c– Swing node b’s next field to d
• Problem is,– Someone deleting b concurrently could direct a pointer to c
ba c
ba c
![Page 88: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/88.jpg)
Art of Multiprocessor Programming 88
Insight
• If a node is locked– No one can delete node’s successor
• If a thread locks– Node to be deleted– And its predecessor– Then it works
![Page 89: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/89.jpg)
Art of Multiprocessor Programming 89
Hand-Over-Hand Again
a b c d
remove(b)
![Page 90: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/90.jpg)
Art of Multiprocessor Programming 90
Hand-Over-Hand Again
a b c d
remove(b)
![Page 91: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/91.jpg)
Art of Multiprocessor Programming 91
Hand-Over-Hand Again
a b c d
remove(b)
![Page 92: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/92.jpg)
Art of Multiprocessor Programming 92
Hand-Over-Hand Again
a b c d
remove(b)Found
it!
![Page 93: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/93.jpg)
Art of Multiprocessor Programming 93
Hand-Over-Hand Again
a b c d
remove(b)Found
it!
![Page 94: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/94.jpg)
Art of Multiprocessor Programming 94
Hand-Over-Hand Again
a c d
remove(b)
![Page 95: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/95.jpg)
Art of Multiprocessor Programming 95
Removing a Node
a b c d
remove(b)remove(c)
![Page 96: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/96.jpg)
Art of Multiprocessor Programming 96
Removing a Node
a b c d
remove(b)remove(c)
![Page 97: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/97.jpg)
Art of Multiprocessor Programming 97
Removing a Node
a b c d
remove(b)remove(c)
![Page 98: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/98.jpg)
Art of Multiprocessor Programming 98
Removing a Node
a b c d
remove(b)remove(c)
![Page 99: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/99.jpg)
Art of Multiprocessor Programming 99
Removing a Node
a b c d
remove(b)remove(c)
![Page 100: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/100.jpg)
Art of Multiprocessor Programming 100
Removing a Node
a b c d
remove(b)remove(c)
![Page 101: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/101.jpg)
Art of Multiprocessor Programming 101
Removing a Node
a b c d
remove(b)remove(c)
![Page 102: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/102.jpg)
Art of Multiprocessor Programming 102
Removing a Node
a b c d
remove(b)remove(c)
![Page 103: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/103.jpg)
Art of Multiprocessor Programming 103
Removing a Node
a b c d
Must acquire Lock for
b
remove(c)
![Page 104: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/104.jpg)
Art of Multiprocessor Programming 104
Removing a Node
a b c d
Waiting to acquire
lock for b
remove(c)
![Page 105: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/105.jpg)
Art of Multiprocessor Programming 105
Removing a Node
a b c d
Wait!remove(c)
![Page 106: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/106.jpg)
Art of Multiprocessor Programming 106
Removing a Node
a b d
Proceed to
remove(b)
![Page 107: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/107.jpg)
Art of Multiprocessor Programming 107
Removing a Node
a b d
remove(b)
![Page 108: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/108.jpg)
Art of Multiprocessor Programming 108
Removing a Node
a b d
remove(b)
![Page 109: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/109.jpg)
Art of Multiprocessor Programming 109
Removing a Node
a d
remove(b)
![Page 110: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/110.jpg)
Art of Multiprocessor Programming 110
Removing a Node
a d
![Page 111: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/111.jpg)
Art of Multiprocessor Programming 111
Remove method
public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; try { … } finally { curr.unlock(); pred.unlock(); }}
![Page 112: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/112.jpg)
Art of Multiprocessor Programming 112
Remove method
public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; try { … } finally { curr.unlock(); pred.unlock(); }}
Key used to order node
![Page 113: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/113.jpg)
Art of Multiprocessor Programming 113
Remove method
public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; try { … } finally { currNode.unlock(); predNode.unlock(); }}
Predecessor and current nodes
![Page 114: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/114.jpg)
Art of Multiprocessor Programming 114
Remove method
public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; try { … } finally { curr.unlock(); pred.unlock(); }}
Make sure locks released
![Page 115: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/115.jpg)
Art of Multiprocessor Programming 115
Remove method
public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; try { … } finally { curr.unlock(); pred.unlock(); }}
Everything else
![Page 116: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/116.jpg)
Art of Multiprocessor Programming 116
Remove method
try { pred = head; pred.lock(); curr = pred.next; curr.lock(); …} finally { … }
![Page 117: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/117.jpg)
Art of Multiprocessor Programming 117
Remove method
try { pred = head; pred.lock(); curr = pred.next; curr.lock(); …} finally { … }
lock pred == head
![Page 118: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/118.jpg)
try { pred = head; pred.lock(); curr = pred.next; curr.lock(); …} finally { … }
Art of Multiprocessor Programming 118
Remove method
Lock current
![Page 119: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/119.jpg)
try { pred = head; pred.lock(); curr = pred.next; curr.lock(); …} finally { … }
Art of Multiprocessor Programming 119
Remove method
Traversing list
![Page 120: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/120.jpg)
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Art of Multiprocessor Programming 120
Remove: searching
![Page 121: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/121.jpg)
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Art of Multiprocessor Programming 121
Remove: searching
Search key range
![Page 122: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/122.jpg)
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
At start of each loop: curr and pred locked
Art of Multiprocessor Programming 122
Remove: searching
![Page 123: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/123.jpg)
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Art of Multiprocessor Programming 123
Remove: searching
If item found, remove node
![Page 124: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/124.jpg)
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Art of Multiprocessor Programming 124
Remove: searching
If node found, remove it
![Page 125: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/125.jpg)
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Art of Multiprocessor Programming 125
Remove: searchingUnlock predecessor
![Page 126: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/126.jpg)
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Art of Multiprocessor Programming 126
Remove: searchingOnly one node locked!
![Page 127: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/127.jpg)
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Art of Multiprocessor Programming 127
Remove: searching
demote current
![Page 128: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/128.jpg)
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = currNode; curr = curr.next; curr.lock(); } return false;
Art of Multiprocessor Programming 128
Remove: searching
Find and lock new current
![Page 129: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/129.jpg)
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = currNode; curr = curr.next; curr.lock(); } return false;
Art of Multiprocessor Programming 129
Remove: searching
Lock invariant restored
![Page 130: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/130.jpg)
Art of Multiprocessor Programming 130
Remove: searching
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Otherwise, not present
![Page 131: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/131.jpg)
Art of Multiprocessor Programming 131
Why does this work?
• To remove node e– Must lock e– Must lock e’s predecessor
• Therefore, if you lock a node– It can’t be removed– And neither can its successor
![Page 132: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/132.jpg)
Art of Multiprocessor Programming 132
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Why remove() is linearizable
•pred reachable from head•curr is pred.next•So curr.item is in the set
![Page 133: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/133.jpg)
Art of Multiprocessor Programming 133
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Why remove() is linearizable
Linearization point ifitem is present
![Page 134: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/134.jpg)
Art of Multiprocessor Programming 134
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Why remove() is linearizable
Node locked, so no other thread can remove it ….
![Page 135: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/135.jpg)
Art of Multiprocessor Programming 135
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Why remove() is linearizable
Item not present
![Page 136: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/136.jpg)
Art of Multiprocessor Programming 136
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Why remove() is linearizable
•pred reachable from head•curr is pred.next•pred.key < key •key < curr.key
![Page 137: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/137.jpg)
Art of Multiprocessor Programming 137
while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;
Why remove() is linearizable
Linearization point
![Page 138: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/138.jpg)
Art of Multiprocessor Programming 138
Adding Nodes
• To add node e– Must lock predecessor– Must lock successor
• Neither can be deleted– (Is successor lock actually required?)
![Page 139: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/139.jpg)
Art of Multiprocessor Programming 139
Same Abstraction Map
• S(head) ={ x | there exists a such that
• a reachable from head and• a.item = x
}
![Page 140: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/140.jpg)
Art of Multiprocessor Programming 140
Rep Invariant
• Easy to check that– tail always reachable from head– Nodes sorted, no duplicates
![Page 141: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/141.jpg)
Art of Multiprocessor Programming 141
Drawbacks
• Better than coarse-grained lock– Threads can traverse in parallel
• Still not ideal– Long chain of acquire/release– Inefficient
![Page 142: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/142.jpg)
Art of Multiprocessor Programming 142
Optimistic Synchronization
• Find nodes without locking• Lock nodes• Check that everything is OK
![Page 143: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/143.jpg)
Art of Multiprocessor Programming 143
Optimistic: Traverse without Locking
b d ea
add(c) Aha!
![Page 144: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/144.jpg)
Art of Multiprocessor Programming 144
Optimistic: Lock and Load
b d ea
add(c)
![Page 145: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/145.jpg)
Art of Multiprocessor Programming 145
Optimistic: Lock and Load
b d ea
add(c)
c
![Page 146: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/146.jpg)
Art of Multiprocessor Programming 146
What could go wrong?
b d ea
add(c) Aha!
![Page 147: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/147.jpg)
Art of Multiprocessor Programming 147
What could go wrong?
b d ea
add(c)
![Page 148: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/148.jpg)
Art of Multiprocessor Programming 148
What could go wrong?
b d ea
remove(b)
![Page 149: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/149.jpg)
Art of Multiprocessor Programming 149
What could go wrong?
b d ea
remove(b)
![Page 150: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/150.jpg)
Art of Multiprocessor Programming 150
What could go wrong?
b d ea
add(c)
![Page 151: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/151.jpg)
Art of Multiprocessor Programming 151
What could go wrong?
b d ea
add(c)
c
![Page 152: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/152.jpg)
Art of Multiprocessor Programming 152
What could go wrong?
d ea
add(c) Uh-oh
![Page 153: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/153.jpg)
Art of Multiprocessor Programming 153
Validate – Part 1
b d ea
add(c) Yes, b still reachable from head
![Page 154: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/154.jpg)
Art of Multiprocessor Programming 154
What Else Could Go Wrong?
b d ea
add(c) Aha!
![Page 155: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/155.jpg)
Art of Multiprocessor Programming 155
What Else Coould Go Wrong?
b d ea
add(c)add(b’)
![Page 156: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/156.jpg)
Art of Multiprocessor Programming 156
What Else Coould Go Wrong?
b d ea
add(c)add(b’)b’
![Page 157: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/157.jpg)
Art of Multiprocessor Programming 157
What Else Could Go Wrong?
b d ea
add(c)
b’
![Page 158: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/158.jpg)
Art of Multiprocessor Programming 158
What Else Could Go Wrong?
b d ea
add(c)
c
![Page 159: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/159.jpg)
Art of Multiprocessor Programming 159
Validate Part 2(while holding locks)
b d ea
add(c) Yes, b still points to d
![Page 160: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/160.jpg)
Art of Multiprocessor Programming 160
Optimistic: Linearization Point
b d ea
add(c)c
![Page 161: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/161.jpg)
Art of Multiprocessor Programming 161
Same Abstraction Map
• S(head) ={ x | there exists a such that
• a reachable from head and• a.item = x
}
![Page 162: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/162.jpg)
Art of Multiprocessor Programming 162
Invariants
• Careful: we may traverse deleted nodes• But we establish properties by
– Validation– After we lock target nodes
![Page 163: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/163.jpg)
Art of Multiprocessor Programming 163
Correctness
• If– Nodes b and c both locked– Node b still accessible– Node c still successor to b
• Then– Neither will be deleted– OK to delete and return true
![Page 164: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/164.jpg)
Art of Multiprocessor Programming 164
Unsuccessful Remove
a b d e
remove(c)Aha!
![Page 165: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/165.jpg)
Art of Multiprocessor Programming 165
Validate (1)
a b d e
Yes, b still reachable from head
remove(c)
![Page 166: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/166.jpg)
Art of Multiprocessor Programming 166
Validate (2)
a b d e
remove(c) Yes, b still points to d
![Page 167: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/167.jpg)
Art of Multiprocessor Programming 167
OK Computer
a b d e
remove(c) return false
![Page 168: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/168.jpg)
Art of Multiprocessor Programming 168
Correctness
• If– Nodes b and d both locked– Node b still accessible– Node d still successor to b
• Then– Neither will be deleted– No thread can add c after b– OK to return false
![Page 169: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/169.jpg)
Art of Multiprocessor Programming 169
Validationprivate boolean validate(Node pred, Node curry) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false;}
![Page 170: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/170.jpg)
private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false;}
Art of Multiprocessor Programming 170
Validation
Predecessor & current nodes
![Page 171: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/171.jpg)
private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false;}
Art of Multiprocessor Programming 171
Validation
Begin at the beginning
![Page 172: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/172.jpg)
private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false;}
Art of Multiprocessor Programming 172
Validation
Search range of keys
![Page 173: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/173.jpg)
private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false;}
Art of Multiprocessor Programming 173
Validation
Predecessor reachable
![Page 174: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/174.jpg)
Art of Multiprocessor Programming 174
private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false;}
Validation
Is current node next?
![Page 175: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/175.jpg)
Art of Multiprocessor Programming 175
private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false;}
ValidationOtherwise move on
![Page 176: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/176.jpg)
Art of Multiprocessor Programming 176
private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false;}
ValidationPredecessor not reachable
![Page 177: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/177.jpg)
Art of Multiprocessor Programming 177
Remove: searchingpublic boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …
![Page 178: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/178.jpg)
Art of Multiprocessor Programming 178
public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …
Remove: searching
Search key
![Page 179: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/179.jpg)
Art of Multiprocessor Programming 179
public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …
Remove: searching
Retry on synchronization conflict
![Page 180: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/180.jpg)
Art of Multiprocessor Programming 180
public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …
Remove: searching
Examine predecessor and current nodes
![Page 181: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/181.jpg)
Art of Multiprocessor Programming 181
public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …
Remove: searching
Search by key
![Page 182: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/182.jpg)
Art of Multiprocessor Programming 182
public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …
Remove: searching
Stop if we find item
![Page 183: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/183.jpg)
Art of Multiprocessor Programming 183
public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …
Remove: searching
Move along
![Page 184: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/184.jpg)
Art of Multiprocessor Programming 184
On Exit from Loop
• If item is present– curr holds item– pred just before curr
• If item is absent– curr has first higher key– pred just before curr
• Assuming no synchronization problems
![Page 185: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/185.jpg)
Art of Multiprocessor Programming 185
Remove Methodtry { pred.lock(); curr.lock(); if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock();curr.unlock();
}}}
![Page 186: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/186.jpg)
Art of Multiprocessor Programming 186
try { pred.lock(); curr.lock(); if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock();curr.unlock();
}}}
Remove Method
Always unlock
![Page 187: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/187.jpg)
Art of Multiprocessor Programming 187
try { pred.lock(); curr.lock(); if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock();curr.unlock();
}}}
Remove Method
Lock both nodes
![Page 188: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/188.jpg)
Art of Multiprocessor Programming 188
try { pred.lock(); curr.lock(); if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock();curr.unlock();
}}}
Remove Method
Check for synchronization conflicts
![Page 189: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/189.jpg)
Art of Multiprocessor Programming 189
try { pred.lock(); curr.lock(); if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock();curr.unlock();
}}}
Remove Method
target found, remove node
![Page 190: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/190.jpg)
Art of Multiprocessor Programming 190
try { pred.lock(); curr.lock(); if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock();curr.unlock();
}}}
Remove Method
target not found
![Page 191: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/191.jpg)
Art of Multiprocessor Programming 191
Optimistic List
• Limited hot-spots– Targets of add(), remove(), contains()– No contention on traversals
• Moreover– Traversals are wait-free– Food for thought …
![Page 192: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/192.jpg)
Art of Multiprocessor Programming 192
So Far, So Good
• Much less lock acquisition/release– Performance– Concurrency
• Problems– Need to traverse list twice– contains() method acquires locks
![Page 193: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/193.jpg)
Art of Multiprocessor Programming 193
Evaluation
• Optimistic is effective if– cost of scanning twice without locks
is less than– cost of scanning once with locks
• Drawback– contains() acquires locks– 90% of calls in many apps
![Page 194: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/194.jpg)
Art of Multiprocessor Programming 194
Lazy List
• Like optimistic, except– Scan once– contains(x) never locks …
• Key insight– Removing nodes causes trouble– Do it “lazily”
![Page 195: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/195.jpg)
Art of Multiprocessor Programming 195
Lazy List
• remove()– Scans list (as before)– Locks predecessor & current (as before)
• Logical delete– Marks current node as removed (new!)
• Physical delete– Redirects predecessor’s next (as before)
![Page 196: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/196.jpg)
Art of Multiprocessor Programming 196
Lazy Removal
aa b c d
![Page 197: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/197.jpg)
c
Art of Multiprocessor Programming 197
Lazy Removal
aa b d
Present in list
![Page 198: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/198.jpg)
c
Art of Multiprocessor Programming 198
Lazy Removal
aa b d
Logically deleted
![Page 199: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/199.jpg)
Art of Multiprocessor Programming 199
Lazy Removal
aa b c d
Physically deleted
![Page 200: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/200.jpg)
Art of Multiprocessor Programming 200
Lazy Removal
aa b d
Physically deleted
![Page 201: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/201.jpg)
Art of Multiprocessor Programming 201
Lazy List
• All Methods– Scan through locked and marked nodes– Removing a node doesn’t slow down other
method calls …• Must still lock pred and curr nodes.
![Page 202: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/202.jpg)
Art of Multiprocessor Programming 202
Validation
• No need to rescan list!• Check that pred is not marked• Check that curr is not marked• Check that pred points to curr
![Page 203: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/203.jpg)
Art of Multiprocessor Programming 203
Business as Usual
a b c
![Page 204: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/204.jpg)
Art of Multiprocessor Programming 204
Business as Usual
a b c
![Page 205: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/205.jpg)
Art of Multiprocessor Programming 205
Business as Usual
a b c
![Page 206: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/206.jpg)
Art of Multiprocessor Programming 206
Business as Usual
a b c
remove(b)
![Page 207: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/207.jpg)
Art of Multiprocessor Programming 207
Business as Usual
a b c
a not marked
![Page 208: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/208.jpg)
Art of Multiprocessor Programming 208
Business as Usual
a b c
a still points
to b
![Page 209: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/209.jpg)
Art of Multiprocessor Programming 209
Business as Usual
a b c
Logical delete
![Page 210: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/210.jpg)
Art of Multiprocessor Programming 210
Business as Usual
a b c
physical delete
![Page 211: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/211.jpg)
Art of Multiprocessor Programming 211
Business as Usual
a b c
![Page 212: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/212.jpg)
Art of Multiprocessor Programming 212
New Abstraction Map
• S(head) ={ x | there exists node a such that
• a reachable from head and• a.item = x and• a is unmarked
}
![Page 213: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/213.jpg)
Art of Multiprocessor Programming 213
Invariant
• If not marked then item in the set• and reachable from head • and if not yet traversed it is reachable
from pred
![Page 214: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/214.jpg)
Art of Multiprocessor Programming 214
Validationprivate boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }
![Page 215: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/215.jpg)
Art of Multiprocessor Programming 215
private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }
List Validate Method
Predecessor not Logically removed
![Page 216: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/216.jpg)
Art of Multiprocessor Programming 216
private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }
List Validate Method
Current not Logically removed
![Page 217: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/217.jpg)
Art of Multiprocessor Programming 217
private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }
List Validate Method
Predecessor stillPoints to current
![Page 218: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/218.jpg)
Art of Multiprocessor Programming 218
Removetry { pred.lock(); curr.lock(); if (validate(pred,curr) { if (curr.key == key) { curr.marked = true; pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock();curr.unlock();
}}}
![Page 219: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/219.jpg)
Art of Multiprocessor Programming 219
Removetry { pred.lock(); curr.lock(); if (validate(pred,curr) { if (curr.key == key) { curr.marked = true; pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock();curr.unlock();
}}}
Validate as before
![Page 220: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/220.jpg)
Art of Multiprocessor Programming 220
Removetry { pred.lock(); curr.lock(); if (validate(pred,curr) { if (curr.key == key) { curr.marked = true; pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock();curr.unlock();
}}}
Key found
![Page 221: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/221.jpg)
Art of Multiprocessor Programming 221
Removetry { pred.lock(); curr.lock(); if (validate(pred,curr) { if (curr.key == key) { curr.marked = true; pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock();curr.unlock();
}}}
Logical remove
![Page 222: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/222.jpg)
Art of Multiprocessor Programming 222
Removetry { pred.lock(); curr.lock(); if (validate(pred,curr) { if (curr.key == key) { curr.marked = true; pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock();curr.unlock();
}}}
physical remove
![Page 223: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/223.jpg)
Art of Multiprocessor Programming 223
Containspublic boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked;}
![Page 224: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/224.jpg)
Art of Multiprocessor Programming 224
Containspublic boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked;}
Start at the head
![Page 225: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/225.jpg)
Art of Multiprocessor Programming 225
Containspublic boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked;}
Search key range
![Page 226: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/226.jpg)
Art of Multiprocessor Programming 226
Containspublic boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked;}
Traverse without locking(nodes may have been removed)
![Page 227: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/227.jpg)
Art of Multiprocessor Programming 227
Containspublic boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked;}
Present and undeleted?
![Page 228: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/228.jpg)
Art of Multiprocessor Programming 228
Summary: Wait-free Contains
a 0 0a b c ed
Use Mark bit + list ordering 1. Not marked → in the set2. Marked or missing → not in the set
![Page 229: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/229.jpg)
Art of Multiprocessor Programming 229
Lazy List
a 0 0 0a b c 0e1d
Lazy add()and remove() + Wait-free contains()
![Page 230: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/230.jpg)
Art of Multiprocessor Programming 230
Evaluation
• Good:– contains() doesn’t lock– In fact, its wait-free! – Good because typically high % contains()– Uncontended calls don’t re-traverse
• Bad– Contended add() and remove() calls
must re-traverse– Traffic jam if one thread delays
![Page 231: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/231.jpg)
Art of Multiprocessor Programming 231
Traffic Jam
• Any concurrent data structure based on mutual exclusion has a weakness
• If one thread– Enters critical section– And “eats the big muffin”
• Cache miss, page fault, descheduled …– Everyone else using that lock is stuck!– Need to trust the scheduler….
![Page 232: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/232.jpg)
Art of Multiprocessor Programming 232
Reminder: Lock-Free Data Structures
• No matter what …– Guarantees minimal progress in any
execution– i.e. Some thread will always complete a
method call– Even if others halt at malicious times– Implies that implementation can’t use locks
![Page 233: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/233.jpg)
Art of Multiprocessor Programming 233
Lock-free Lists
• Next logical step– Wait-free contains()– lock-free add() and remove()
• Use only compareAndSet()– What could go wrong?
![Page 234: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/234.jpg)
234
public abstract class CASObject { private int value; public boolean synchronized compareAndSet(int expected, int update) { if (value==expected) { value = update; return true; } return false; } … }
compareAndSet
Art of Multiprocessor Programming
![Page 235: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/235.jpg)
Art of Multiprocessor Programming
235
public abstract class CASObject { private int value; public boolean synchronized compareAndSet(int expected, int update) { if (value==expected) { value = update; return true; } return false; } … }
compareAndSet
If value is as expected, …
![Page 236: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/236.jpg)
Art of Multiprocessor Programming
236
public abstract class CASOBJECT{ private int value; public boolean synchronized compareAndSet(int expected, int update) { if (value==expected) { value = update; return true; } return false; } … }
compareAndSet
… replace it
![Page 237: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/237.jpg)
Art of Multiprocessor Programming
237
public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { if (value==expected) { value = update; return true; } return false; } … }
compareAndSet
Report success
![Page 238: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/238.jpg)
Art of Multiprocessor Programming
238
public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { if (value==expected) { value = update; return true; } return false; } … }
compareAndSet
Otherwise report failure
![Page 239: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/239.jpg)
Art of Multiprocessor Programming 239
a 0 0 0a b c 0e1c
Logical Removal
Physical RemovalUse CAS to verify pointer is correct
Not enough!
Lock-free Lists
![Page 240: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/240.jpg)
Art of Multiprocessor Programming 240
Problem…
a 0 0 0a b c 0e1c
Logical Removal
Physical Removal0d
Node added
![Page 241: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/241.jpg)
Art of Multiprocessor Programming 241
The Solution: Combine Bit and Pointer
a 0 0 0a b c 0e1c
Logical Removal =Set Mark Bit
PhysicalRemovalCAS
0d
Mark-Bit and Pointerare CASed together(AtomicMarkableReference)
Fail CAS: Node not added after logical Removal
![Page 242: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/242.jpg)
Art of Multiprocessor Programming 242
Solution
• Use AtomicMarkableReference• Atomically
– Swing reference and– Update flag
• Remove in two steps– Set mark bit in next field– Redirect predecessor’s pointer
![Page 243: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/243.jpg)
Art of Multiprocessor Programming 243
Marking a Node
• AtomicMarkableReference class– Java.util.concurrent.atomic package
address F
mark bit
Reference
![Page 244: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/244.jpg)
Art of Multiprocessor Programming 244
Extracting Reference & Mark
public Object get(boolean[] marked);
![Page 245: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/245.jpg)
Art of Multiprocessor Programming 245
Extracting Reference & Mark
public Object get(boolean[] marked);
Returns reference
Returns mark at array index 0!
![Page 246: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/246.jpg)
Art of Multiprocessor Programming 246
Extracting Mark Only
public boolean isMarked();
Value of mark
![Page 247: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/247.jpg)
Art of Multiprocessor Programming 247
Changing State
public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark);
![Page 248: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/248.jpg)
Art of Multiprocessor Programming 248
Changing State
public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark);
If this is the current reference …
And this is the current mark …
![Page 249: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/249.jpg)
Art of Multiprocessor Programming 249
Changing State
public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark);
…then change to this new reference …
… and this new mark
![Page 250: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/250.jpg)
Art of Multiprocessor Programming 250
Changing State
public boolean attemptMark( Object expectedRef, boolean updateMark);
![Page 251: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/251.jpg)
Art of Multiprocessor Programming 251
Changing State
public boolean attemptMark( Object expectedRef, boolean updateMark);
If this is the current reference …
![Page 252: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/252.jpg)
Art of Multiprocessor Programming 252
Changing State
public boolean attemptMark( Object expectedRef, boolean updateMark);
.. then change to this new mark.
![Page 253: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/253.jpg)
bCAS
Art of Multiprocessor Programming 253
Removing a Node
a c d
remove(c)
![Page 254: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/254.jpg)
Art of Multiprocessor Programming 254
Removing a Node
a b d
remove(b)
c
failed
CAS
CAS
remove(c)
![Page 255: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/255.jpg)
Art of Multiprocessor Programming 255
Removing a Node
a b dc
remove(b)
remove(c)
![Page 256: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/256.jpg)
Art of Multiprocessor Programming 256
Removing a Node
a d
remove(b)
remove(c)
![Page 257: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/257.jpg)
Art of Multiprocessor Programming 257
Traversing the List
• Q: what do you do when you find a “logically” deleted node in your path?
• A: finish the job.– CAS the predecessor’s next field– Proceed (repeat as needed)
![Page 258: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/258.jpg)
Art of Multiprocessor Programming 258
Lock-Free Traversal(only Add and Remove)
a b c dCAS
Uh-oh
pred currpred curr
![Page 259: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/259.jpg)
Art of Multiprocessor Programming 259
The Window Class
class Window { public Node pred; public Node curr; Window(Node pred, Node curr) { pred = pred; curr = curr; }}
![Page 260: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/260.jpg)
Art of Multiprocessor Programming 260
The Window Class
class Window { public Node pred; public Node curr; Window(Node pred, Node curr) { pred = pred; curr = curr; }}
A container for pred and current values
![Page 261: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/261.jpg)
Art of Multiprocessor Programming 261
Using the Find Method
Window window = find(head, key); Node pred = window.pred; curr = window.curr;
![Page 262: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/262.jpg)
Art of Multiprocessor Programming 262
Using the Find Method
Window window = find(head, key); Node pred = window.pred; curr = window.curr;
Find returns window
![Page 263: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/263.jpg)
Art of Multiprocessor Programming 263
Using the Find Method
Window window = find(head, key); Node pred = window.pred; curr = window.curr;
Extract pred and curr
![Page 264: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/264.jpg)
Art of Multiprocessor Programming© Herlihy-Shavit 2007
264
The Find Method
Window window = find(item);
At some instant,
pred curr succ
item or …
![Page 265: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/265.jpg)
Art of Multiprocessor Programming© Herlihy-Shavit 2007
265
The Find Method
Window window = find(item);
At some instant,
predcurr= null
succ
item not in list
![Page 266: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/266.jpg)
Art of Multiprocessor Programming 266
Removepublic boolean remove(T item) {Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true;}}}
![Page 267: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/267.jpg)
Art of Multiprocessor Programming 267
Removepublic boolean remove(T item) {Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet (succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true;}}}
Keep trying
![Page 268: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/268.jpg)
Art of Multiprocessor Programming 268
Removepublic boolean remove(T item) {Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet (succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true;}}} Find neighbors
![Page 269: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/269.jpg)
Art of Multiprocessor Programming 269
Removepublic boolean remove(T item) {Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true;}}} Not there …
![Page 270: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/270.jpg)
Art of Multiprocessor Programming 270
Removepublic boolean remove(T item) {Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true;}}}
Try to mark node as deleted
![Page 271: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/271.jpg)
Art of Multiprocessor Programming 271
Removepublic boolean remove(T item) {Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true;}}}
If it doesn’t work, just retry, if it
does, job essentially done
![Page 272: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/272.jpg)
Art of Multiprocessor Programming 272
Removepublic boolean remove(T item) {Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true;}}}
Try to advance reference(if we don’t succeed,someone else did or will).
a
![Page 273: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/273.jpg)
Art of Multiprocessor Programming 273
Addpublic boolean add(T item) { boolean splice; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key == key) { return false; } else { Node node = new Node(item); node.next = new AtomicMarkableRef(curr, false); if (pred.next.compareAndSet(curr, node, false, false)) {return true;}}}}
![Page 274: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/274.jpg)
Art of Multiprocessor Programming 274
Addpublic boolean add(T item) { boolean splice; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key == key) { return false; } else { Node node = new Node(item); node.next = new AtomicMarkableRef(curr, false); if (pred.next.compareAndSet(curr, node, false, false)) {return true;}}}} Item already there
![Page 275: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/275.jpg)
Art of Multiprocessor Programming 275
Addpublic boolean add(T item) { boolean splice; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key == key) { return false; } else { Node node = new Node(item); node.next = new AtomicMarkableRef(curr, false); if (pred.next.compareAndSet(curr, node, false, false)) {return true;}}}}
create new node
![Page 276: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/276.jpg)
Art of Multiprocessor Programming 276
Addpublic boolean add(T item) { boolean splice; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key == key) { return false; } else { Node node = new Node(item); node.next = new AtomicMarkableRef(curr, false); if (pred.next.compareAndSet(curr, node, false, false)) {return true;}}}}
Install new node, else retry loop
![Page 277: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/277.jpg)
Art of Multiprocessor Programming 277
Wait-free Contains
public boolean contains(T item) { boolean marked; int key = item.hashCode(); Node curr = head; while (curr.key < key) curr = curr.next; Node succ = curr.next.get(marked); return (curr.key == key && !marked[0]) }
![Page 278: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/278.jpg)
Art of Multiprocessor Programming 278
Wait-free Contains
public boolean contains(T item) { boolean marked; int key = item.hashCode(); Node curr = head; while (curr.key < key) curr = curr.next; Node succ = curr.next.get(marked); return (curr.key == key && !marked[0]) }
Only difference is that we get and check marked
![Page 279: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/279.jpg)
Art of Multiprocessor Programming 279
Lock-free Findpublic Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; }}}
![Page 280: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/280.jpg)
Art of Multiprocessor Programming 280
Lock-free Findpublic Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; }}}
If list changes while traversed,
start over
![Page 281: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/281.jpg)
Art of Multiprocessor Programming 281
public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; }}}
Lock-free FindStart looking from head
![Page 282: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/282.jpg)
Art of Multiprocessor Programming 282
public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; }}}
Lock-free Find
Move down the list
![Page 283: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/283.jpg)
Art of Multiprocessor Programming 283
public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; }}}
Lock-free Find
Get ref to successor and current deleted bit
![Page 284: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/284.jpg)
Art of Multiprocessor Programming 284
public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; }}}
Lock-free Find
Try to remove deleted nodes in path…code details soon
![Page 285: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/285.jpg)
Art of Multiprocessor Programming 285
public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; }}}
Lock-free Find
If curr key that is greater or equal, return pred and curr
![Page 286: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/286.jpg)
Art of Multiprocessor Programming 286
public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; }}}
Lock-free Find
Otherwise advance window and loop again
![Page 287: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/287.jpg)
Art of Multiprocessor Programming 287
Lock-free Find
retry: while (true) { … while (marked[0]) { snip = pred.next.compareAndSet(curr, succ, false, false); if (!snip) continue retry; curr = succ; succ = curr.next.get(marked); }…
![Page 288: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/288.jpg)
retry: while (true) { … while (marked[0]) { snip = pred.next.compareAndSet(curr, succ, false, false); if (!snip) continue retry; curr = succ; succ = curr.next.get(marked); }…
Art of Multiprocessor Programming 288
Lock-free FindTry to snip out node
![Page 289: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/289.jpg)
retry: while (true) { … while (marked[0]) { snip = pred.next.compareAndSet(curr, succ, false, false); if (!snip) continue retry; curr = succ; succ = curr.next.get(marked); }…
Art of Multiprocessor Programming 289
Lock-free Findif predecessor’s next field changed,
retry whole traversal
![Page 290: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/290.jpg)
Art of Multiprocessor Programming 290
Lock-free Find
retry: while (true) { … while (marked[0]) { snip = pred.next.compareAndSet(curr, succ, false, false); if (!snip) continue retry; curr = succ; succ = curr.next.get(marked); }…
Otherwise move on to check if next node deleted
![Page 291: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/291.jpg)
Performance
• Different list-based set implementaions• 16-node machine• Vary percentage of contains() calls
Art of Multiprocessor Programming 291
![Page 292: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/292.jpg)
Art of Multiprocessor Programming 292
High Contains Ratio
Lock-free Lazy list
Coarse GrainedFine Lock-coupling
![Page 293: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/293.jpg)
Art of Multiprocessor Programming 293
Low Contains Ratio
Lock-free
Lazy list
Coarse GrainedFine Lock-coupling
![Page 294: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/294.jpg)
Art of Multiprocessor Programming 294
As Contains Ratio Increases
Lock-free Lazy list
Coarse GrainedFine Lock-coupling
% Contains()
![Page 295: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/295.jpg)
Art of Multiprocessor Programming 295
Summary
• Coarse-grained locking• Fine-grained locking• Optimistic synchronization• Lazy synchronization• Lock-free synchronization
![Page 296: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/296.jpg)
Art of Multiprocessor Programming 296
“To Lock or Not to Lock”
• Locking vs. Non-blocking:– Extremist views on both sides
• The answer: nobler to compromise– Example: Lazy list combines blocking add()
and remove()and a wait-free contains()– Remember: Blocking/non-blocking is a property
of a method
![Page 297: Linked Lists: Locking, Lock-Free, and Beyondcs.brown.edu/courses/cs176/lectures/chapter_09.pdf · 2018-10-27 · The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.](https://reader033.fdocuments.in/reader033/viewer/2022050516/5f9ff925fd1af800ff3a78ab/html5/thumbnails/297.jpg)
Art of Multiprocessor Programming 297
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.