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

Slides:



Advertisements
Similar presentations
Ordered linked list implementation of a set
Advertisements

Linked Lists: Locking, Lock- Free, and Beyond … Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)
Multicore Programming Skip list Tutorial 10 CS Spring 2010.
Wait-Free Linked-Lists Shahar Timnat, Anastasia Braginsky, Alex Kogan, Erez Petrank Technion, Israel Presented by Shahar Timnat 469-+
Process Synchronization Continued 7.2 The Critical-Section Problem.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Scalable and Lock-Free Concurrent Dictionaries
Linked Lists: Locking, Lock-Free, and Beyond … The Art of Multiprocessor Programming Spring 2007.
Introduction Companion slides for
Spin Locks and Contention Based on slides by by Maurice Herlihy & Nir Shavit Tomer Gurevich.
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
CSC1016 Coursework Clarification Derek Mortimer March 2010.
Shared Counters and Parallelism Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
CS510 Advanced OS Seminar Class 10 A Methodology for Implementing Highly Concurrent Data Objects by Maurice Herlihy.
CS510 Concurrent Systems Class 2 A Lock-Free Multiprocessor OS Kernel.
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Two-Process Systems TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A A AA Companion slides for Distributed Computing.
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
Multicore Programming
CS510 Concurrent Systems Jonathan Walpole. A Lock-Free Multiprocessor OS Kernel.
November 15, 2007 A Java Implementation of a Lock- Free Concurrent Priority Queue Bart Verzijlenberg.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
State Teleportation How Hardware Transactional Memory can Improve Legacy Data Structures Maurice Herlihy and Eli Wald Brown University.
Linked Lists: Optimistic, Lock-Free, … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by Pavol Černý,
1 Lock-Free concurrent algorithm for Linked lists: Verification CSE-COSC6490A : Concurrent Object-Oriented Languages York University - W09 Speaker: Alexandre.
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by.
Concurrent Linked Lists and Linearizability Proofs Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified.
Wait-Free Multi-Word Compare- And-Swap using Greedy Helping and Grabbing Håkan Sundell PDPTA 2009.
Advanced Locking Techniques
Practical concurrent algorithms Mihai Letia Concurrent Algorithms 2012 Distributed Programming Laboratory Slides by Aleksandar Dragojevic.
A Simple Optimistic skip-list Algorithm Maurice Herlihy Brown University & Sun Microsystems Laboratories Yossi Lev Brown University & Sun Microsystems.
Software Transactional Memory Should Not Be Obstruction-Free Robert Ennals Presented by Abdulai Sei.
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Gal Milman Based on Chapter 10 (Concurrent Queues and the ABA Problem) in The Art of Multiprocessor Programming by Herlihy and Shavit Seminar 2 (236802)
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Multiprocessor Architecture Basics Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Concurrent Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
SkipLists and Balanced Search The Art Of MultiProcessor Programming Maurice Herlihy & Nir Shavit Chapter 14 Avi Kozokin.
An algorithm of Lock-free extensible hash table Yi Feng.
CS510 Concurrent Systems Tyler Fetters. A Methodology for Implementing Highly Concurrent Data Objects.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Spin Locks and Contention Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Queue Locks and Local Spinning Some Slides based on: The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
The Relative Power of Synchronization Operations Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Lecture 9 : Concurrent Data Structures Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
6.852 Lecture 21 ● Techniques for highly concurrent objects – coarse-grained mutual exclusion – read/write locking – fine-grained locking (mutex and read/write)
Linked Lists: Locking, Lock-Free, and Beyond …
Lecture 6-1 : Concurrent Data Structures (Concurrent Linked Lists)
Linked Lists: Locking, Lock-Free, and Beyond …
Concurrent Objects Companion slides for
CO890 CONCURRENCY & PARALLELISM
Linked Lists: Locking, Lock-Free, and Beyond …
Concurrent Data Structures Concurrent Algorithms 2017
Linked Lists: Locking, Lock-Free, and Beyond …
Concurrency and Multicore Programming
Simulations and Reductions
Linked Lists: Locking, Lock-Free, and Beyond …
Multicore programming
Linked Lists: Locking, Lock-Free, and Beyond …
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Art of Multiprocessor Programming© Herlihy-Shavit Summary: Wait-free Contains a 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Art of Multiprocessor Programming© Herlihy-Shavit Summary: Lock-free Removal a 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!

Art of Multiprocessor Programming© Herlihy-Shavit Lock-free Removal a 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

Art of Multiprocessor Programming© Herlihy-Shavit Our Solution: Combine Bit and Pointer a 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

Art of Multiprocessor Programming© Herlihy-Shavit A Lock-free Algorithm a 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

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

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

Art of Multiprocessor Programming© Herlihy-Shavit Low Contains Ratio

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

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

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

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