ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.....
Transcript of ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.....
![Page 1: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/1.jpg)
Concurrent Linked Lists
Acknowledgement: Slides adopted from the companion slides for the book "The Art of Mul<processor Programming" by Maurice Herlihy and Nir Shavit
![Page 2: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/2.jpg)
What We'll Cover Today Chapter 9 of:
Digital copy can be obtained via WUSTL library: hHp://catalog.wustl.edu/search/
![Page 3: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/3.jpg)
Today: Concurrent Objects
• Adding threads should not lower throughput – ContenAon effects – Mostly fixed by Queue locks
• Should increase throughput – Not possible if inherently sequenAal – Surprising things are parallelizable
![Page 4: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/4.jpg)
Coarse-‐Grained SynchronizaAon: the Good
• Each method locks the object – Avoid contenAon using queue locks – Easy to reason about
• In simple cases
![Page 5: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/5.jpg)
Art of Multiprocessor Programming 5
• SequenAal boQleneck – Threads “stand in line”
• Adding more threads – Does not improve throughput – Struggle to keep it from geUng worse
Coarse-‐Grained SynchronizaAon: the Bad
![Page 6: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/6.jpg)
Art of Multiprocessor Programming 6
This Lecture
• Introduce four “paQerns” – Bag of tricks … – Methods that work more than once …
• For highly-‐concurrent objects – Concurrent access – More threads, more throughput
![Page 7: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/7.jpg)
This Lecture
• Coarse-‐grained locking • Fine-‐grained locking • OpAmisAc synchronizaAon • Lazy synchronizaAon • Lock-‐free synchronizaAon
![Page 8: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/8.jpg)
First: Fine-‐Grained SynchronizaAon
• Instead of using a single lock … • Split object into
– Independently-‐synchronized components
• Methods conflict when they access – The same component … – At the same Ame
![Page 9: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/9.jpg)
Second: OpAmisAc SynchronizaAon
• Search without locking … • If you find it, lock and check …
– OK: we are done – Oops: start over
• EvaluaAon – Usually cheaper than locking, but – Mistakes are expensive
![Page 10: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/10.jpg)
Third: Lazy SynchronizaAon
• Postpone hard work • Removing components is tricky
– Logical removal • Mark component to be deleted
– Physical removal • Do what needs to be done
![Page 11: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/11.jpg)
Fourth: Lock-‐Free SynchronizaAon
• Don’t use locks at all – Use compareAndSet() & relaAves …
• Advantages – No Scheduler AssumpAons/Support
• Disadvantages – Complex – SomeAmes high overhead
![Page 12: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/12.jpg)
Linked List
• Illustrate these paQerns … • Using a list-‐based Set
– Common applicaAon – Building block for other apps
![Page 13: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/13.jpg)
Set Interface
• Unordered collecAon 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 14: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/14.jpg)
List-‐Based Sets
public interface Set<T> { public boolean add(T x); public boolean remove(T x); public boolean contains(T x); }
![Page 15: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/15.jpg)
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 16: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/16.jpg)
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 17: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/17.jpg)
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 18: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/18.jpg)
List Node
public class Node { public T item; public int key; public volatile Node next; }
![Page 19: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/19.jpg)
List Node
public class Node { public T item; public int key; public volatile Node next; }
item of interest
![Page 20: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/20.jpg)
List Node
public class Node { public T item; public int key; public volatile Node next; }
Usually hash code
![Page 21: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/21.jpg)
List Node
public class Node { public T item; public int key; public Node next; }
Reference to next node
![Page 22: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/22.jpg)
The List-‐Based Set
a b c
Sorted with SenAnel nodes (min & max possible keys)
-∞
+∞
Once you find a key larger than the key you are searching for, you are done.
![Page 23: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/23.jpg)
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 24: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/24.jpg)
Specifically …
• Invariants preserved by – add() – remove() – contains()
• Most steps are trivial – Usually one step tricky – Oeen linearizaAon point
![Page 25: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/25.jpg)
Interference • Invariants make sense only if we assume freedom from interference: methods considered are the only modifiers to the data structure.
• Language encapsulaAon helps – List nodes not visible outside class
• Freedom from interference needed even for removed nodes – Some algorithms traverse removed nodes – Careful with malloc()& free()!
• We rely on garbage collecAon
![Page 26: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/26.jpg)
Abstract Data Types
• Concrete representaAon:
• Abstract Type: {a, b}
a b
![Page 27: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/27.jpg)
• Meaning of representaAon given by abstrac'on map, carrying lists that saAsfy representaAon invariant to set.
S( ) = {a,b}
Abstract Data Types
a b
![Page 28: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/28.jpg)
RepresentaAon Invariant
• Which concrete values meaningful? – Sorted? – Duplicates?
• Rep invariant – Characterizes legal concrete reps – Preserved by methods – Relied on by methods
![Page 29: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/29.jpg)
RepresentaAon Invariant
• SenAnel nodes – tail reachable from head
• Sorted • No duplicates
![Page 30: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/30.jpg)
AbstracAon Map
• S(head) = { x | there exists a such that
• a reachable from head and • a.item = x
}
![Page 31: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/31.jpg)
SequenAal List Based Set
a c d
a b c
add()
remove()
![Page 32: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/32.jpg)
SequenAal List Based Set
a c d
b
a b c
add()
remove()
![Page 33: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/33.jpg)
Coarse-‐Grained Locking
• Easy, same as synchronized methods – "One lock to rule them all … "
• Simple, clearly correct – Deserves respect!
• Works poorly with contenAon – Queue locks help – But boQleneck sAll an issue
![Page 34: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/34.jpg)
Fine-‐grained Locking
• Requires careful thought – “Do not meddle in the affairs of wizards, for they are subtle and quick to anger”
![Page 35: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/35.jpg)
Art of Multiprocessor Programming 35
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 36: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/36.jpg)
Hand-‐over-‐Hand locking
a b c
![Page 37: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/37.jpg)
Hand-‐over-‐Hand locking
a b c
![Page 38: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/38.jpg)
Hand-‐over-‐Hand locking
a b c
![Page 39: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/39.jpg)
Hand-‐over-‐Hand locking
a b c
![Page 40: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/40.jpg)
Hand-‐over-‐Hand locking
a b c
![Page 41: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/41.jpg)
Removing a Node
a b c d
remove(b)
![Page 42: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/42.jpg)
Removing a Node
a b c d
remove(b)
![Page 43: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/43.jpg)
Removing a Node
a b c d
remove(b)
![Page 44: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/44.jpg)
Art of Multiprocessor Programming 44
Removing a Node
a b c d
remove(b)
![Page 45: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/45.jpg)
Removing a Node
a b c d
remove(b)
![Page 46: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/46.jpg)
Removing a Node
a c d
remove(b) Why lock victim node?
![Page 47: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/47.jpg)
Concurrent Removes
a b c d
remove(c) remove(b)
![Page 48: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/48.jpg)
Concurrent Removes
a b c d
remove(b) remove(c)
![Page 49: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/49.jpg)
Concurrent Removes
a b c d
remove(b) remove(c)
![Page 50: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/50.jpg)
Concurrent Removes
a b c d
remove(b) remove(c)
![Page 51: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/51.jpg)
Concurrent Removes
a b c d
remove(b) remove(c)
![Page 52: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/52.jpg)
Concurrent Removes
a b c d
remove(b) remove(c)
![Page 53: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/53.jpg)
Concurrent Removes
a b c d
remove(b) remove(c)
![Page 54: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/54.jpg)
Concurrent Removes
a b c d
remove(b) remove(c)
![Page 55: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/55.jpg)
Art of Multiprocessor Programming 55
Concurrent Removes
a b c d
remove(b) remove(c)
![Page 56: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/56.jpg)
Art of Multiprocessor Programming 56
Concurrent Removes
a b c d
remove(b) remove(c)
![Page 57: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/57.jpg)
Uh, Oh
a c d
remove(b) remove(c)
![Page 58: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/58.jpg)
Uh, Oh
a c d
Bad news, c not removed
remove(b) remove(c)
![Page 59: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/59.jpg)
Problem
• To delete node c – Swing node b’s next field to d (c's next field)
• Problem is, – Someone deleAng b concurrently could direct a pointer to c (reading b's next field)
b a c
b a c
![Page 60: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/60.jpg)
Insight
• If a node is locked – No one can change node’s successor
• If a thread locks – Node to be deleted (so its successor don't change)
– And its predecessor (so you are the only one changing its successor)
– Then it works
![Page 61: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/61.jpg)
Removing a Node
a b c d
remove(b) remove(c)
![Page 62: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/62.jpg)
Removing a Node
a b c d
remove(b) remove(c)
![Page 63: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/63.jpg)
Removing a Node
a b c d
remove(b) remove(c)
![Page 64: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/64.jpg)
Removing a Node
a b c d
remove(b) remove(c)
![Page 65: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/65.jpg)
Removing a Node
a b c d
remove(b) remove(c)
![Page 66: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/66.jpg)
Removing a Node
a b c d
remove(b) remove(c)
![Page 67: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/67.jpg)
Removing a Node
a b c d
remove(b) remove(c)
![Page 68: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/68.jpg)
Removing a Node
a b c d
remove(b) remove(c)
![Page 69: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/69.jpg)
Removing a Node
a b c d
Must acquire Lock for
b
remove(c)
![Page 70: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/70.jpg)
Removing a Node
a b c d
Waiting to acquire
lock for b
remove(c)
![Page 71: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/71.jpg)
Removing a Node
a b c d
Wait! remove(c)
![Page 72: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/72.jpg)
Removing a Node
a b d
Proceed to
remove(b)
![Page 73: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/73.jpg)
Removing a Node
a b d
remove(b)
![Page 74: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/74.jpg)
Removing a Node
a b d
remove(b)
![Page 75: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/75.jpg)
Removing a Node
a d
remove(b)
![Page 76: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/76.jpg)
Remove method public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}
![Page 77: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/77.jpg)
Remove method public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}
Key used to order node
![Page 78: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/78.jpg)
Remove method public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}
Predecessor and current nodes
![Page 79: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/79.jpg)
Remove method public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}
lock pred == head before accessing its
next field
![Page 80: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/80.jpg)
Remove method public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}
lock the node after head
![Page 81: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/81.jpg)
Remove method public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}
When enter try, we hold locks on
pred and curr
![Page 82: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/82.jpg)
Remove method public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}
Traverse the rest of the list
![Page 83: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/83.jpg)
Remove method public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}
Make sure locks released
![Page 84: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/84.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;
Remove: searching (Inside the Try Block)
![Page 85: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/85.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;
Remove: searching (Inside the Try Block)
Search key range
![Page 86: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/86.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;
Remove: searching (Inside the Try Block)
At start of each loop: curr and pred locked
![Page 87: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/87.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;
Remove: searching (Inside the Try Block)
If item found, remove node
![Page 88: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/88.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;
Remove: searching (Inside the Try Block)
Hand-over-hand locking again
otherwise
![Page 89: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/89.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;
Remove: searching (Inside the Try Block)
Lock invariant restored
![Page 90: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/90.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;
Remove: searching (Inside the Try Block)
Otherwise, not present
![Page 91: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/91.jpg)
Aside: Next Field Must be VolaAle!
public class Node { public T item; public int key; public volatile Node next; }
Since we are no longer holding a lock when we read the "next" field, it needs to be volaAle to avoid race condiAons
(more on that in future lecture).
![Page 92: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/92.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;
Why remove() is linearizable
• pred reachable from head • curr is pred.next • So curr.item is in the set
![Page 93: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/93.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;
Why remove() is linearizable
Linearization point if item is present
![Page 94: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/94.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;
Why remove() is linearizable
Node locked, so no other thread can remove it ….
![Page 95: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/95.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;
Why remove() is linearizable
Item not present
![Page 96: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/96.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;
Why remove() is linearizable
• pred reachable from head • curr is pred.next • pred.key < key • key < curr.key
![Page 97: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/97.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;
Why remove() is linearizable
Linearization point: the most recent
read before return
![Page 98: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/98.jpg)
Adding Nodes
• To add node e – Must lock predecessor – Must lock successor
• Neither can be deleted – (Is successor lock actually required?)
![Page 99: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/99.jpg)
AbstracAon Map
• S(head) = { x | there exists a such that
• a reachable from head and • a.item = x
}
![Page 100: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/100.jpg)
RepresentaAon Invariant
• Easy to check that – tail always reachable from head – Nodes sorted, no duplicates
![Page 101: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/101.jpg)
Drawbacks
• BeQer than coarse-‐grained lock – Threads can traverse in parallel
• SAll not ideal – Long chain of acquire/release – Inefficient
![Page 102: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/102.jpg)
OpAmisAc SynchronizaAon
• Find nodes without locking • Lock nodes • Check that everything is OK
![Page 103: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/103.jpg)
OpAmisAc: Traverse without Locking
b d e a
add(c) Aha!
![Page 104: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/104.jpg)
OpAmisAc: Lock and Load
b d e a
add(c)
![Page 105: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/105.jpg)
Art of Multiprocessor Programming 105
OpAmisAc: Lock and Load
b d e a
add(c)
c
![Page 106: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/106.jpg)
What could go wrong?
b d e a
add(c) Aha!
![Page 107: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/107.jpg)
Art of Multiprocessor Programming 107
What could go wrong?
b d e a
add(c)
![Page 108: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/108.jpg)
Art of Multiprocessor Programming 108
What could go wrong?
b d e a
remove(b)
![Page 109: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/109.jpg)
Art of Multiprocessor Programming 109
What could go wrong?
b d e a
remove(b)
![Page 110: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/110.jpg)
Art of Multiprocessor Programming 110
What could go wrong?
b d e a
add(c)
![Page 111: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/111.jpg)
Art of Multiprocessor Programming 111
What could go wrong?
b d e a
add(c)
c
![Page 112: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/112.jpg)
Art of Multiprocessor Programming 112
What could go wrong?
d e a
add(c) Uh-oh
![Page 113: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/113.jpg)
Validate – Part 1
b d e a
add(c) Yes, b still reachable from head
![Page 114: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/114.jpg)
Art of Multiprocessor Programming 114
What Else Could Go Wrong?
b d e a
add(c) Aha!
![Page 115: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/115.jpg)
What Else Coould Go Wrong?
b d e a
add(c) add(b’)
![Page 116: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/116.jpg)
Art of Multiprocessor Programming 116
What Else Coould Go Wrong?
b d e a
add(c) add(b’) b’
![Page 117: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/117.jpg)
Art of Multiprocessor Programming 117
What Else Could Go Wrong?
b d e a
add(c)
b’
![Page 118: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/118.jpg)
Art of Multiprocessor Programming 118
What Else Could Go Wrong?
b d e a
add(c)
c
![Page 119: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/119.jpg)
Validate Part 2 (while holding locks)
b d e a
add(c) Yes, b still points to d
![Page 120: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/120.jpg)
OpAmisAc: LinearizaAon Point
b d e a
add(c) c
![Page 121: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/121.jpg)
Same AbstracAon Map
• S(head) = { x | there exists a such that
• a reachable from head and • a.item = x
}
![Page 122: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/122.jpg)
Invariants
• Careful: we may traverse deleted nodes • But we establish properAes by
– ValidaAon – Aeer we lock target nodes
![Page 123: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/123.jpg)
Correctness
• If – Nodes b and c both locked – Node b sAll accessible – Node c sAll successor to b
• Then – Neither will be deleted – OK to delete and return true
![Page 124: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/124.jpg)
Unsuccessful Remove
a b d e
remove(c) Aha!
![Page 125: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/125.jpg)
Validate (1)
a b d e
Yes, b still reachable from head
remove(c)
![Page 126: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/126.jpg)
Validate (2)
a b d e
remove(c) Yes, b still points to d
![Page 127: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/127.jpg)
OK Computer
a b d e
remove(c) return false
![Page 128: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/128.jpg)
Correctness
• If – Nodes b and d both locked – Node b sAll accessible – Node d sAll successor to b
• Then – Neither will be deleted – No thread can add c aeer b – OK to return false
![Page 129: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/129.jpg)
ValidaAon
private 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 130: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/130.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; }
ValidaAon
Predecessor & current nodes
![Page 131: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/131.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; }
ValidaAon
Begin at the beginning
![Page 132: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/132.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; }
ValidaAon
Search range of keys
![Page 133: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/133.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; }
ValidaAon
Predecessor reachable
![Page 134: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/134.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; }
ValidaAon
Is current node next?
![Page 135: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/135.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; }
ValidaAon
Otherwise move on
![Page 136: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/136.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; }
ValidaAon
Predecessor not reachable
![Page 137: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/137.jpg)
Remove: the Traversal
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; } …
![Page 138: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/138.jpg)
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: the Traversal
Search key
![Page 139: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/139.jpg)
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: the Traversal
Retry on synchronization conflict (If validation fails, we come back here.)
![Page 140: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/140.jpg)
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: the Traversal
Examine predecessor and current nodes
![Page 141: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/141.jpg)
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: the Traversal
Search by key
![Page 142: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/142.jpg)
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: the Traversal
Stop if we find item
![Page 143: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/143.jpg)
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: the Traversal
Move along
![Page 144: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/144.jpg)
On Exit from Inner 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 synchronizaAon problems
![Page 145: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/145.jpg)
Remove ConAnue: the DeleAon (Aeer ExisAng Inner Loop)
pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock(); curr.unlock();
}}}
![Page 146: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/146.jpg)
pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock(); curr.unlock();
}}}
Always unlock
Remove ConAnue: the DeleAon (Aeer ExisAng Inner Loop)
![Page 147: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/147.jpg)
pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock(); curr.unlock();
}}}
Remove ConAnue: the DeleAon (Aeer ExisAng Inner Loop)
Check for synchronization
conflicts
![Page 148: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/148.jpg)
pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock(); curr.unlock();
}}}
Remove ConAnue: the DeleAon (Aeer ExisAng Inner Loop)
target found, remove node
![Page 149: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/149.jpg)
pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {
pred.unlock(); curr.unlock();
}}}
Remove ConAnue: the DeleAon (Aeer ExisAng Inner Loop)
target not found
![Page 150: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/150.jpg)
OpAmisAc List
• Limited hot-‐spots – Holding locks only on the targets of add(), remove(), contains()
– No contenAon on traversals – Traversals are "wait-‐free" (What's wait free?)
![Page 151: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/151.jpg)
Progress CondiAons
• Deadlock-‐free: some thread trying to acquire the lock eventually succeeds.
• Starva'on-‐free: every thread trying to acquire the lock eventually succeeds.
• Lock-‐free: some thread calling a method eventually returns.
• Wait-‐free: every thread calling a method eventually returns.
![Page 152: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/152.jpg)
Progress CondiAons
Wait-free
Lock-free
Starvation-free
Deadlock-free
Everyone makes
progress
Non-Blocking Blocking
Someone makes
progress
![Page 153: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/153.jpg)
So Far, So Good
• Much less lock acquisiAon/release – Performance – Concurrency
• Problems – Need to traverse list twice – contains() method acquires locks
![Page 154: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/154.jpg)
EvaluaAon
• OpAmisAc is effecAve 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 155: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/155.jpg)
Lazy List
• Like opAmisAc, except – Scan once – contains(x) never locks …
• Key insight – Removing nodes causes trouble – Do it “lazily”
![Page 156: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/156.jpg)
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 157: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/157.jpg)
Lazy Removal
a a b c d
![Page 158: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/158.jpg)
c
Lazy Removal
a a b d
Present in list
![Page 159: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/159.jpg)
c
Art of Multiprocessor Programming 159
Lazy Removal
a a b d
Logically deleted
![Page 160: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/160.jpg)
Lazy Removal
a a b c d
Physically deleted
![Page 161: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/161.jpg)
Lazy Removal
a a b d
Physically deleted
![Page 162: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/162.jpg)
Lazy List
• All Methods – Scan through locked and marked nodes – add and remove sAll locks pred and curr, but not contain
– Adding / removing a node doesn’t slow down contain() …
![Page 163: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/163.jpg)
Lazy List ValidaAon
• No need to rescan list! • Check that pred is not marked • Check that curr is not marked • Check that pred points to curr
![Page 164: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/164.jpg)
New AbstracAon Map
• S(head) = { x | there exists node a such that
• a reachable from head and • a.item = x and • a is unmarked
}
![Page 165: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/165.jpg)
Invariant
• If an item is not marked, it is reachable from head and sAll in the set.
• Any unmarked reachable node remains reachable even if its predecessor is logically or physically removed
![Page 166: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/166.jpg)
ValidaAon
private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }
![Page 167: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/167.jpg)
private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }
List Validate Method
Predecessor not Logically removed
![Page 168: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/168.jpg)
private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }
List Validate Method
Current not Logically removed
![Page 169: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/169.jpg)
private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }
List Validate Method
Predecessor still Points to current
![Page 170: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/170.jpg)
ValidaAon
private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }
Both the next and marked fields need to be vola<le!
![Page 171: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/171.jpg)
Remove: the DeleAon ... // the traversal pred.lock(); curr.lock(); try { 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 172: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/172.jpg)
Remove: the DeleAon ... // the traversal pred.lock(); curr.lock(); try { 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 173: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/173.jpg)
Remove: the DeleAon ... // the traversal pred.lock(); curr.lock(); try { 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 174: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/174.jpg)
Remove: the DeleAon ... // the traversal pred.lock(); curr.lock(); try { 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 175: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/175.jpg)
Remove: the DeleAon ... // the traversal pred.lock(); curr.lock(); try { 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 176: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/176.jpg)
Contains
public 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 177: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/177.jpg)
public boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked; }
Contains
Start at the head
![Page 178: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/178.jpg)
public boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked; }
Contains
Search key range
![Page 179: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/179.jpg)
public boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked; }
Contains
Traverse without locking (nodes may have been removed)
![Page 180: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/180.jpg)
public boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked; }
Contains
Present and undeleted?
![Page 181: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/181.jpg)
Summary: Wait-‐free Contains
a 0 0 a b c e d
Use Mark bit + list ordering 1. Not marked à in the set 2. Marked or missing à not in the set 3. Traverse the list only once!
![Page 182: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/182.jpg)
EvaluaAon
• Good: – contains() doesn’t lock – In fact, its wait-‐free! – Good because typically high % contains() – Uncontended calls to add and remove don’t re-‐traverse
• Bad – Contended add() and remove() calls must re-‐traverse
– Traffic jam if one thread delays
![Page 183: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/183.jpg)
Traffic Jam
• Any concurrent data structure based on mutual exclusion has a weakness
• If one thread – Enters criAcal secAon – And “eats the big muffin”
• Cache miss, page fault, descheduled …
– Everyone else using that lock is stuck! – Need to trust the scheduler….
![Page 184: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/184.jpg)
Lock-‐Free Data Structures
• No maQer what … – Guarantees minimal progress in any execuAon – i.e. Some thread will always complete a method call
– Even if others halt at malicious Ames – Implies that implementaAon can’t use locks
![Page 185: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/185.jpg)
Lock-‐free Lists
• Next logical step – Wait-‐free contains() – lock-‐free add() and remove()
• Use only compareAndSet() – What could go wrong?
![Page 186: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/186.jpg)
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 187: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/187.jpg)
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 188: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/188.jpg)
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 189: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/189.jpg)
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 190: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/190.jpg)
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 191: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/191.jpg)
a 0 0 0 a b c 0 e 1 c
Logical Removal
Physical Removal Use CAS to verify pointer is correct
Not enough!
Lock-‐free Lists
![Page 192: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/192.jpg)
Problem…
a 0 0 0 a b c 0 e 1 c
T1: Logical Removal
T1: Physical Removal 0 d
T2: Node added
Lost Update!
![Page 193: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/193.jpg)
The SoluAon: Combine Bit and Pointer
a 0 0 0 a b c 0 e 1 c
Logical Removal = Set Mark Bit
Physical Removal CAS
0 d
Mark-Bit and Pointer are CASed together (AtomicMarkableReference)
Fail CAS: Node not added after logical Removal
![Page 194: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/194.jpg)
Marking a Node
• AtomicMarkableReference class – Java.util.concurrent.atomic package
address F
mark bit
Reference
![Page 195: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/195.jpg)
ExtracAng Reference & Mark
public Object get(boolean[] marked);
![Page 196: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/196.jpg)
ExtracAng Reference & Mark
public Object get(boolean[] marked);
Returns reference
Returns mark at array index 0!
![Page 197: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/197.jpg)
ExtracAng Mark Only
public boolean isMarked();
Value of mark
![Page 198: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/198.jpg)
Changing State
public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark);
![Page 199: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/199.jpg)
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 200: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/200.jpg)
Changing State
public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark);
…then change to this new reference …
… and this new mark
![Page 201: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/201.jpg)
Changing State
public boolean attemptMark( Object expectedRef, boolean updateMark);
![Page 202: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/202.jpg)
Changing State
public boolean attemptMark( Object expectedRef, boolean updateMark);
If this is the current reference …
![Page 203: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/203.jpg)
Changing State
public boolean attemptMark( Object expectedRef, boolean updateMark);
.. then change to this new mark.
![Page 204: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/204.jpg)
b CAS
Removing a Node
a c d
remove(c)
![Page 205: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/205.jpg)
Removing a Node
a b d
remove(b)
c
failed
CAS CAS
remove(c)
![Page 206: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/206.jpg)
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 207: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/207.jpg)
Lock-‐Free Traversal (only Add and Remove)
a b c d CAS
Uh-oh
pred curr pred curr
remove(c)
![Page 208: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/208.jpg)
The Window Class
class Window { public Node pred; public Node curr; Window(Node pred, Node curr) { pred = pred; curr = curr; } }
![Page 209: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/209.jpg)
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 210: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/210.jpg)
Using the Find Method
Window window = find(head, key); Node pred = window.pred; curr = window.curr;
![Page 211: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/211.jpg)
Using the Find Method
Window window = find(head, key); Node pred = window.pred; curr = window.curr;
Find returns window
![Page 212: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/212.jpg)
Using the Find Method
Window window = find(head, key); Node pred = window.pred; curr = window.curr;
Extract pred and curr
![Page 213: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/213.jpg)
The Find Method
Window window = find(item);
At some instant,
pred curr succ
item or …
![Page 214: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/214.jpg)
The Find Method
Window window = find(item);
At some instant,
pred curr= null
succ
item not in list
![Page 215: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/215.jpg)
Remove
public 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 216: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/216.jpg)
Remove
public 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 217: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/217.jpg)
Remove
public 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 218: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/218.jpg)
Remove
public 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 219: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/219.jpg)
Remove
public 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 220: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/220.jpg)
Remove
public 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 221: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/221.jpg)
Remove
public 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 222: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/222.jpg)
Remove
public 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; }}}
Linearization point if removal is successful
![Page 223: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/223.jpg)
Remove
public 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; }}}
Linearization point is when we found this node (in Find()) if removal returns false.
![Page 224: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/224.jpg)
Add
public 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 225: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/225.jpg)
Add
public 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 226: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/226.jpg)
Add
public 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 227: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/227.jpg)
Add
public 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 228: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/228.jpg)
Add
public 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;} }}}
Linearization point if add is successful
![Page 229: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/229.jpg)
Add
public 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;} }}}
Linearization point is when we found this node (in Find()) if removal returns false.
![Page 230: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/230.jpg)
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 231: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/231.jpg)
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 232: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/232.jpg)
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 233: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/233.jpg)
Lock-‐free Find 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; } }}
![Page 234: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/234.jpg)
Lock-‐free Find 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; } }}
If list changes while traversed,
start over
![Page 235: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/235.jpg)
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 Start looking from head
![Page 236: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/236.jpg)
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 237: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/237.jpg)
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 238: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/238.jpg)
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 239: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/239.jpg)
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 240: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/240.jpg)
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 241: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/241.jpg)
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 242: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/242.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); } …
Lock-‐free Find Try to snip out node
![Page 243: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/243.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); } …
Lock-‐free Find if predecessor’s next field changed,
retry whole traversal
![Page 244: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/244.jpg)
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 245: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/245.jpg)
Performance
• Different list-‐based set implementaions • SunFire 6800 (bus based cache coherence) • 16-‐node machine, each 1.2 GHz • Vary percentage of contains() calls
![Page 246: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/246.jpg)
High Contains RaAo
Lock-free Lazy list
Coarse Grained Fine Lock-coupling
Ops/sec (90% contain, 9% add, 1% remove)
![Page 247: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/247.jpg)
Low Contains RaAo
Lock-free
Lazy list
Coarse Grained Fine Lock-coupling
Ops/sec (50% contain, 45% add, 5% remove)
![Page 248: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/248.jpg)
As Contains RaAo Increases
Lock-free Lazy list
Coarse Grained Fine Lock-coupling
% Contains()
Ops/sec using 32 threads
![Page 249: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/249.jpg)
“To Lock or Not to Lock”
• Locking vs. Non-‐blocking: – Depending on the applicaAon usage
• 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 250: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/250.jpg)
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.
![Page 251: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/251.jpg)
Business as Usual
a b c
![Page 252: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/252.jpg)
Business as Usual
a b c
![Page 253: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/253.jpg)
Business as Usual
a b c
![Page 254: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/254.jpg)
Business as Usual
a b c
remove(b)
![Page 255: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/255.jpg)
Business as Usual
a b c
a not marked
![Page 256: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/256.jpg)
Business as Usual
a b c
a still points
to b
![Page 257: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/257.jpg)
Business as Usual
a b c
Logical delete
![Page 258: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/258.jpg)
Business as Usual
a b c
physical delete
![Page 259: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul](https://reader033.fdocuments.in/reader033/viewer/2022060218/5f0671547e708231d4180496/html5/thumbnails/259.jpg)
Business as Usual
a b c