Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists: Lazy and Non-Blocking Synchronization Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)

Similar presentations


Presentation on theme: "Linked Lists: Lazy and Non-Blocking Synchronization Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)"— Presentation transcript:

1 Linked Lists: Lazy and Non-Blocking Synchronization Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)

2 Art of Multiprocessor Programming© Herlihy-Shavit 20072 Lazy List Like optimistic, except –Scan once –contains(x) never locks … Key insight –Removing nodes causes trouble –Do it “lazily”

3 Art of Multiprocessor Programming© Herlihy-Shavit 20073 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)

4 Art of Multiprocessor Programming© Herlihy-Shavit 20074 Lazy Removal aa b c d

5 Art of Multiprocessor Programming© Herlihy-Shavit 20075 Lazy Removal aa b c d Present in list

6 Art of Multiprocessor Programming© Herlihy-Shavit 20076 Lazy Removal aa b c d Logically deleted

7 Art of Multiprocessor Programming© Herlihy-Shavit 20077 Lazy Removal aa b c d Physically deleted

8 Art of Multiprocessor Programming© Herlihy-Shavit 20078 Lazy Removal aa b d Physically deleted

9 Art of Multiprocessor Programming© Herlihy-Shavit 20079 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.

10 Art of Multiprocessor Programming© Herlihy-Shavit 200710 Validation No need to rescan list! Check that pred is not marked Check that curr is not marked Check that pred points to curr

11 Art of Multiprocessor Programming© Herlihy-Shavit 200711 Business as Usual abc

12 Art of Multiprocessor Programming© Herlihy-Shavit 200712 Business as Usual abc

13 Art of Multiprocessor Programming© Herlihy-Shavit 200713 Business as Usual abc

14 Art of Multiprocessor Programming© Herlihy-Shavit 200714 Business as Usual abc remove(b)

15 Art of Multiprocessor Programming© Herlihy-Shavit 200715 Business as Usual abc a not marked

16 Art of Multiprocessor Programming© Herlihy-Shavit 200716 Business as Usual abc a still points to b

17 Art of Multiprocessor Programming© Herlihy-Shavit 200717 Business as Usual a bc Logical delete

18 Art of Multiprocessor Programming© Herlihy-Shavit 200718 Business as Usual a bc physical delete

19 Art of Multiprocessor Programming© Herlihy-Shavit 200719 Business as Usual a bc

20 Art of Multiprocessor Programming© Herlihy-Shavit 200720 Invariant If not marked then item in the set and reachable from head and if not yet traversed it is reachable from pred Marked elements may be reachable or not

21 Art of Multiprocessor Programming© Herlihy-Shavit 200721 New Abstraction Map S(head) = –{ x | there exists node a such that a reachable from head and a.item = x and a is unmarked –}

22 Art of Multiprocessor Programming© Herlihy-Shavit 200722 Validation private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }

23 Art of Multiprocessor Programming© Herlihy-Shavit 200723 private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); } List Validate Method Predecessor not logically removed

24 Art of Multiprocessor Programming© Herlihy-Shavit 200724 private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); } List Validate Method Current not logically removed

25 Art of Multiprocessor Programming© Herlihy-Shavit 200725 private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); } List Validate Method Predecessor still points to current

26 Art of Multiprocessor Programming© Herlihy-Shavit 200726 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(); }

27 Art of Multiprocessor Programming© Herlihy-Shavit 200727 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

28 Art of Multiprocessor Programming© Herlihy-Shavit 200728 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

29 Art of Multiprocessor Programming© Herlihy-Shavit 200729 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

30 Art of Multiprocessor Programming© Herlihy-Shavit 200730 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

31 Art of Multiprocessor Programming© Herlihy-Shavit 200731 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; }

32 Art of Multiprocessor Programming© Herlihy-Shavit 200732 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

33 Art of Multiprocessor Programming© Herlihy-Shavit 200733 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

34 Art of Multiprocessor Programming© Herlihy-Shavit 200734 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)

35 Art of Multiprocessor Programming© Herlihy-Shavit 200735 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?

36 Art of Multiprocessor Programming© Herlihy-Shavit 200736 Summary: Wait-free Contains a 0 0 0 a b c 0 e 1 d Use Mark bit + Fact that List is ordered 1.Not marked  in the set 2.Marked or missing  not in the set

37 Art of Multiprocessor Programming© Herlihy-Shavit 200737 Lazy List: summary a 0 0 0 a b c 0 e 1 d Wait-free contains() Blocking add() and remove(): possible starvation (if validate fails repeatedly)

38 Art of Multiprocessor Programming© Herlihy-Shavit 200738 Linearizability Add and remove use locks: linearization points chosen as usual (successful remove linearized when mark bit set to 1) Contains does not use locks: pay attention to unsuccessful calls. Earlier of: Point where a removed matching node, or a node with larger key is found Point before a new matching node is added to the list

39 Art of Multiprocessor Programming© Herlihy-Shavit 200739 Evaluation Good: –contains() doesn’t lock –In fact, it is wait-free! –Good because typically high % contains() –Uncontended calls don’t re-traverse Bad –Contended calls do re-traverse –Traffic jam if one thread delays

40 Art of Multiprocessor Programming© Herlihy-Shavit 200740 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 … Software error, … –Everyone else using that lock is stuck!

41 Art of Multiprocessor Programming© Herlihy-Shavit 200741 Reminder: Lock-Free Data Structures No matter what … –Some thread will complete method call –Even if others halt at malicious times –Weaker than wait-free, yet Implies that –You can’t use locks

42 Art of Multiprocessor Programming© Herlihy-Shavit 200742 Lock-free Lists Next logical step Eliminate locking entirely contains() wait-free and add() and remove() lock-free Use only compareAndSet() What could go wrong?

43 Art of Multiprocessor Programming© Herlihy-Shavit 200743 Adding a Node acd

44 Art of Multiprocessor Programming© Herlihy-Shavit 200744 Adding a Node acdb

45 Art of Multiprocessor Programming© Herlihy-Shavit 200745 Adding a Node acdb CAS

46 Art of Multiprocessor Programming© Herlihy-Shavit 200746 Adding a Node acdb

47 Art of Multiprocessor Programming© Herlihy-Shavit 200747 Adding a Node acdb

48 Art of Multiprocessor Programming© Herlihy-Shavit 200748 Removing a Node abcd remov e b remov e c CAS

49 Art of Multiprocessor Programming© Herlihy-Shavit 200749 Bad news Look Familiar? abcd remov e b remov e c

50 Art of Multiprocessor Programming© Herlihy-Shavit 200750 Problem Method updates node’s next field after node has been removed

51 Art of Multiprocessor Programming© Herlihy-Shavit 200751 Solution Use AtomicMarkableReference Atomically –Swing reference and –Update flag Remove in two steps –Set mark bit in next field –Redirect predecessor’s pointer

52 Art of Multiprocessor Programming© Herlihy-Shavit 200752 Marking a Node AtomicMarkableReference class –Java.util.concurrent.atomic package address F mark bit Reference

53 Art of Multiprocessor Programming© Herlihy-Shavit 200753 Extracting Reference & Mark Public Object get(boolean[] marked);

54 Art of Multiprocessor Programming© Herlihy-Shavit 200754 Public Object get(boolean[] marked); Returns reference Returns mark at array index 0! Extracting Reference & Mark

55 Art of Multiprocessor Programming© Herlihy-Shavit 200755 Extracting Reference Only public boolean isMarked(); Value of mark

56 Art of Multiprocessor Programming© Herlihy-Shavit 200756 Changing State Public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark);

57 Art of Multiprocessor Programming© Herlihy-Shavit 200757 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 …

58 Art of Multiprocessor Programming© Herlihy-Shavit 200758 Changing State Public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark); …then change to this new reference … … and this new mark

59 Art of Multiprocessor Programming© Herlihy-Shavit 200759 Changing State public boolean attemptMark( Object expectedRef, boolean updateMark);

60 Art of Multiprocessor Programming© Herlihy-Shavit 200760 Changing State public boolean attemptMark( Object expectedRef, boolean updateMark); If this is the current reference …

61 Art of Multiprocessor Programming© Herlihy-Shavit 200761 Changing State public boolean attemptMark( Object expectedRef, boolean updateMark);.. then change to this new mark.

62 Art of Multiprocessor Programming© Herlihy-Shavit 200762 Removing a Node abcd remov e c CAS

63 Art of Multiprocessor Programming© Herlihy-Shavit 200763 Removing a Node abd remov e b remov e c c CAS failed

64 Art of Multiprocessor Programming© Herlihy-Shavit 200764 Removing a Node abd remov e b remov e c c

65 Art of Multiprocessor Programming© Herlihy-Shavit 200765 Removing a Node ad remov e b remov e c

66 Art of Multiprocessor Programming© Herlihy-Shavit 200766 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)

67 Art of Multiprocessor Programming© Herlihy-Shavit 200767 Lock-Free Traversal abcd CAS Uh-oh

68 Art of Multiprocessor Programming© Herlihy-Shavit 200768 The Window Class class Window { public Node pred; public Node curr; Window(Node pred, Node curr) { this.pred = pred; this.curr = curr; }

69 Art of Multiprocessor Programming© Herlihy-Shavit 200769 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

70 Art of Multiprocessor Programming© Herlihy-Shavit 200770 Using the Find Method Window window = find(head, key); Node pred = window.pred; curr = window.curr;

71 Art of Multiprocessor Programming© Herlihy-Shavit 200771 Using the Find Method Window window = find(head, key); Node pred = window.pred; curr = window.curr; Find returns window

72 Art of Multiprocessor Programming© Herlihy-Shavit 200772 Using the Find Method Window window = find(head, key); Node pred = window.pred; curr = window.curr; Extract pred and curr

73 Art of Multiprocessor Programming© Herlihy-Shavit 200773 The Find Method Window window = find(item); At some instant, predcurrsucc item or …

74 Art of Multiprocessor Programming© Herlihy-Shavit 200774 The Find Method Window window = find(item); At some instant, pred curr= null succ item not in list

75 Art of Multiprocessor Programming© Herlihy-Shavit 200775 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; } }}

76 Art of Multiprocessor Programming© Herlihy-Shavit 200776 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. Lock-Free because we start over only if someone else makes progress

77 Art of Multiprocessor Programming© Herlihy-Shavit 200777 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

78 Art of Multiprocessor Programming© Herlihy-Shavit 200778 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

79 Art of Multiprocessor Programming© Herlihy-Shavit 200779 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 mark bit of current node

80 Art of Multiprocessor Programming© Herlihy-Shavit 200780 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 marked nodes in path… code details soon

81 Art of Multiprocessor Programming© Herlihy-Shavit 200781 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 is greater or equal, return pred and curr

82 Art of Multiprocessor Programming© Herlihy-Shavit 200782 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

83 Art of Multiprocessor Programming© Herlihy-Shavit 200783 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); } …

84 Art of Multiprocessor Programming© Herlihy-Shavit 200784 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

85 Art of Multiprocessor Programming© Herlihy-Shavit 200785 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 must retry whole traversal

86 Art of Multiprocessor Programming© Herlihy-Shavit 200786 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

87 Art of Multiprocessor Programming© Herlihy-Shavit 200787 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.attemptMark(succ, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}}

88 Art of Multiprocessor Programming© Herlihy-Shavit 200788 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.attemptMark(succ, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}} Keep trying

89 Art of Multiprocessor Programming© Herlihy-Shavit 200789 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.attemptMark(succ, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}} Find neighbors

90 Art of Multiprocessor Programming© Herlihy-Shavit 200790 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.attemptMark(succ, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}} Key is not there …

91 Art of Multiprocessor Programming© Herlihy-Shavit 200791 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.attemptMark(succ, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}} Try to mark node as deleted

92 Art of Multiprocessor Programming© Herlihy-Shavit 200792 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.attemptMark(succ, 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

93 Art of Multiprocessor Programming© Herlihy-Shavit 200793 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.attemptMark(succ, 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

94 Art of Multiprocessor Programming© Herlihy-Shavit 200794 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; }}}

95 Art of Multiprocessor Programming© Herlihy-Shavit 200795 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

96 Art of Multiprocessor Programming© Herlihy-Shavit 200796 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

97 Art of Multiprocessor Programming© Herlihy-Shavit 200797 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

98 Art of Multiprocessor Programming© Herlihy-Shavit 200798 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]) }

99 Art of Multiprocessor Programming© Herlihy-Shavit 200799 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 use method get and check array marked

100 Art of Multiprocessor Programming© Herlihy-Shavit 2007100 Summary: Lock-free Removal a 0 0 0 a b c 0 e 1 c Logical Removal = Set Mark Bit Physical Removal CAS pointer Use CAS to verify pointer is correct Not enough!

101 Art of Multiprocessor Programming© Herlihy-Shavit 2007101 Lock-free Removal a 0 0 0 a b c 0 e 1 c Logical Removal = Set Mark Bit Physical Removal CAS 0 d Problem: d not added to list… Must Prevent manipulation of removed node’s pointer Node added Before Physical Removal CAS

102 Art of Multiprocessor Programming© Herlihy-Shavit 2007102 Our 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 Fail CAS: Node not added after logical Removal

103 Art of Multiprocessor Programming© Herlihy-Shavit 2007103 A Lock-free Algorithm a 0 0 0 a b c 0 e 1 c 1. Lock-free add() and remove() physically remove marked nodes 2. Wait-free find() traverses both marked and removed nodes

104 Art of Multiprocessor Programming© Herlihy-Shavit 2007104 Performance On 16 node shared memory machine Benchmark throughput of Java List-based Set algs. Vary % of Contains() method Calls.

105 Art of Multiprocessor Programming© Herlihy-Shavit 2007105 High Contains Ratio Lock-free Lazy list Course Grained Fine Lock-coupling

106 Art of Multiprocessor Programming© Herlihy-Shavit 2007106 Low Contains Ratio

107 Art of Multiprocessor Programming© Herlihy-Shavit 2007107 As Contains Ratio Increases % Contains()

108 Art of Multiprocessor Programming© Herlihy-Shavit 2007108 Summary Coarse-grained locking Fine-grained locking Optimistic synchronization Lazy synchronization Lock-free synchronization

109 Art of Multiprocessor Programming© Herlihy-Shavit 2007109 “To Lock or Not to Lock” Locking vs. Non-blocking: Extremist views on both sides The answer: nobler to compromise, combine locking and non-blocking –Example: Lazy list combines blocking add() and remove() and a wait-free contains() –Blocking/non-blocking is a property of a method

110 Art of Multiprocessor Programming© Herlihy-Shavit 2007110 This work is licensed under a Creative Commons Attribution- ShareAlike 2.5 License.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.


Download ppt "Linked Lists: Lazy and Non-Blocking Synchronization Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)"

Similar presentations


Ads by Google