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