honk!

advertisement
The List-Based Set
-∞
a
b
c
+∞
Sorted with Sentinel nodes
(min & max possible keys)
Art of Multiprocessor Programming
2
Invariants
• Sentinel nodes
– tail reachable from head • Sorted
• No duplicates
Art of Multiprocessor Programming
3
Sequential List Based Set
Add()
a
c
d
a
b
c
Remove()
Art of Multiprocessor Programming
4
Sequential List Based Set
Add()
a
c
d
b
c
b
Remove()
a
Art of Multiprocessor Programming
5
Coarse Grained Locking
a
b
Art of Multiprocessor Programming
d
6
Coarse Grained Locking
a
d
b
c
Art of Multiprocessor Programming
7
Coarse Grained Locking
a
d
b
honk!
honk!
c
Simple but hotspot + bottleneck
Art of Multiprocessor Programming
8
Coarse-Grained Locking
•
Easy, same as synchronized methods
– “One lock to rule them all …”
Art of Multiprocessor Programming
9
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
Art of Multiprocessor Programming
10
Fine-grained Locking
• Requires careful thought
• Split object into pieces
– Each piece has own lock
– Methods that work on disjoint pieces need not
exclude each other
Art of Multiprocessor Programming
11
Hand-over-Hand locking
a
b
Art of Multiprocessor Programming
c
12
Hand-over-Hand locking
a
b
Art of Multiprocessor Programming
c
13
Hand-over-Hand locking
a
b
Art of Multiprocessor Programming
c
14
Hand-over-Hand locking
a
b
Art of Multiprocessor Programming
c
15
Hand-over-Hand locking
a
b
Art of Multiprocessor Programming
c
16
Removing a Node
a
b
c
d
remove(b)
Art of Multiprocessor Programming
17
Removing a Node
a
b
c
d
remove(b)
Art of Multiprocessor Programming
18
Removing a Node
a
b
c
d
remove(b)
Art of Multiprocessor Programming
19
Removing a Node
a
b
c
d
remove(b)
Art of Multiprocessor Programming
20
Removing a Node
a
b
c
d
remove(b)
Art of Multiprocessor Programming
21
Removing a Node
a
remove(b)
c
d
Why hold 2 locks?
Art of Multiprocessor Programming
22
Concurrent Removes
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
23
Concurrent Removes
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
24
Concurrent Removes
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
25
Concurrent Removes
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
26
Concurrent Removes
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
27
Concurrent Removes
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
28
Concurrent Removes
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
29
Concurrent Removes
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
30
Concurrent Removes
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
31
Concurrent Removes
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
32
Uh, Oh
a
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
33
Uh, Oh
Bad news, c not removed
a
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
34
Problem
• To delete node c
– Swing node b’s next field to d
• Problem is,
a
b
c
– Someone deleting b concurrently could direct a pointer to c
a
b
c
Art of Multiprocessor Programming
35
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
Art of Multiprocessor Programming
36
Hand-Over-Hand Again
a
b
c
d
remove(b)
Art of Multiprocessor Programming
37
Hand-Over-Hand Again
a
b
c
d
remove(b)
Art of Multiprocessor Programming
38
Hand-Over-Hand Again
a
b
c
d
remove(b)
Art of Multiprocessor Programming
39
Hand-Over-Hand Again
a
b
c
d
Found it!
remove(b)
Art of Multiprocessor Programming
40
Hand-Over-Hand Again
a
b
c
d
Found it!
remove(b)
Art of Multiprocessor Programming
41
Hand-Over-Hand Again
a
c
d
remove(b)
Art of Multiprocessor Programming
42
Removing a Node
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
43
Removing a Node
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
44
Removing a Node
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
45
Removing a Node
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
46
Removing a Node
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
47
Removing a Node
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
48
Removing a Node
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
49
Removing a Node
a
b
c
d
remove(c)
remove(b)
Art of Multiprocessor Programming
50
Removing a Node
a
b
c
d
remove(c)
Must acquire Lock of b
Art of Multiprocessor Programming
51
Removing a Node
a
b
c
d
remove(c)
Cannot
acquire lock
of b
Art of Multiprocessor Programming
52
Removing a Node
a
b
c
d
remove(c)
Wait!
Art of Multiprocessor Programming
53
Removing a Node
a
b
d
Proceed to
remove(b)
Art of Multiprocessor Programming
54
Removing a Node
a
b
d
remove(b)
Art of Multiprocessor Programming
55
Removing a Node
a
b
d
remove(b)
Art of Multiprocessor Programming
56
Removing a Node
a
d
remove(b)
Art of Multiprocessor Programming
57
Removing a Node
a
d
Art of Multiprocessor Programming
58
Download