Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Slides:



Advertisements
Similar presentations
Ordered linked list implementation of a set
Advertisements

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 … 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-+
Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.
Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Control Structure: Loop (Part 1) Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes.
CS492B Analysis of Concurrent Programs Lock Basics Jaehyuk Huh Computer Science, KAIST.
Reduction, abstraction, and atomicity: How much can we prove about concurrent programs using them? Serdar Tasiran Koç University Istanbul, Turkey Tayfun.
Monitors & Blocking Synchronization 1. Producers & Consumers Problem Two threads that communicate through a shared FIFO queue. These two threads can’t.
Linked Lists: Locking, Lock-Free, and Beyond … The Art of Multiprocessor Programming Spring 2007.
Data Structures: A Pseudocode Approach with C
Linked Lists: Lazy and Non-Blocking Synchronization Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)
Scalable Synchronous Queues By William N. Scherer III, Doug Lea, and Michael L. Scott Presented by Ran Isenberg.
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.
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.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
שירן חליבה Concurrent Queues. Outline: Some definitions 3 queue implementations : A Bounded Partial Queue An Unbounded Total Queue An Unbounded Lock-Free.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Relativistic Red Black Trees. Relativistic Programming Concurrent reading and writing improves performance and scalability – concurrent readers may disagree.
CS510 Concurrent Systems Jonathan Walpole. 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.
Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
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.
Consider the program fragment below left. Assume that the program containing this fragment executes t1() and t2() on separate threads running on separate.
DOUBLE INSTANCE LOCKING A concurrency pattern with Lock-Free read operations Pedro Ramalhete Andreia Correia November 2013.
Advanced Locking Techniques
A Simple Optimistic skip-list Algorithm Maurice Herlihy Brown University & Sun Microsystems Laboratories Yossi Lev Brown University & Sun Microsystems.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
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)
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Concurrent Hashing and Natural Parallelism Chapter 13 in The Art of Multiprocessor Programming Instructor: Erez Petrank Presented by Tomer Hermelin.
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.
Shared Counters and Parallelism 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.
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.
Slides on threads borrowed by Chase Landon Cox. Thread states BlockedReady Running Thread is scheduled Thread is Pre-empted (or yields) Thread calls Lock.
Concurrency: Locks and synchronization Slides by Prof. Cox.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
LINKED 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 …
CO890 CONCURRENCY & PARALLELISM
Linked Lists: Locking, Lock-Free, and Beyond …
Concurrent Data Structures Concurrent Algorithms 2017
Linked Lists: Locking, Lock-Free, and Beyond …
Concurrent Hashing and Natural Parallelism
Multicore programming
Linked Lists: Locking, Lock-Free, and Beyond …
Multicore programming
Linked Lists: Locking, Lock-Free, and Beyond …
Presentation transcript:

Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Art of Multiprocessor Programming2 Last Lecture: Spin-Locks CS Resets lock upon exit spin lock critical section...

Art of Multiprocessor Programming3 Today: Concurrent Objects Adding threads should not lower throughput –Contention effects –Mostly fixed by Queue locks

Art of Multiprocessor Programming4 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

Art of Multiprocessor Programming5 Coarse-Grained Synchronization Each method locks the object –Avoid contention using queue locks

Art of Multiprocessor Programming6 Coarse-Grained Synchronization Each method locks the object –Avoid contention using queue locks –Easy to reason about In simple cases

Art of Multiprocessor Programming7 Coarse-Grained Synchronization Each method locks the object –Avoid contention using queue locks –Easy to reason about In simple cases So, are we done?

Art of Multiprocessor Programming8 Coarse-Grained Synchronization Sequential bottleneck –Threads stand in line

Art of Multiprocessor Programming9 Coarse-Grained Synchronization Sequential bottleneck –Threads stand in line Adding more threads –Does not improve throughput –Struggle to keep it from getting worse

Art of Multiprocessor Programming10 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 …

Art of Multiprocessor Programming11 This Lecture Introduce four patterns –Bag of tricks … –Methods that work more than once …

Art of Multiprocessor Programming12 This Lecture Introduce four patterns –Bag of tricks … –Methods that work more than once … For highly-concurrent objects –Concurrent access –More threads, more throughput

Art of Multiprocessor Programming13 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

Art of Multiprocessor Programming14 Second: Optimistic Synchronization Search without locking …

Art of Multiprocessor Programming15 Second: Optimistic Synchronization Search without locking … If you find it, lock and check … –OK: we are done –Oops: start over

Art of Multiprocessor Programming16 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

Art of Multiprocessor Programming17 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

Art of Multiprocessor Programming18 Fourth: Lock-Free Synchronization Dont use locks at all –Use compareAndSet() & relatives …

Art of Multiprocessor Programming19 Fourth: Lock-Free Synchronization Dont use locks at all –Use compareAndSet() & relatives … Advantages –No Scheduler Assumptions/Support

Art of Multiprocessor Programming20 Fourth: Lock-Free Synchronization Dont use locks at all –Use compareAndSet() & relatives … Advantages –No Scheduler Assumptions/Support Disadvantages –Complex –Sometimes high overhead

Art of Multiprocessor Programming21 Linked List Illustrate these patterns … Using a list-based Set –Common application –Building block for other apps

Art of Multiprocessor Programming22 Set Interface Unordered collection of items

Art of Multiprocessor Programming23 Set Interface Unordered collection of items No duplicates

Art of Multiprocessor Programming24 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

Art of Multiprocessor Programming25 List-Based Sets public interface Set { public boolean add(T x); public boolean remove(T x); public boolean contains(T x); }

Art of Multiprocessor Programming26 List-Based Sets public interface Set { public boolean add(T x); public boolean remove(T x); public boolean contains(T x); } Add item to set

Art of Multiprocessor Programming27 List-Based Sets public interface Set { public boolean add(T x); public boolean remove(T x); public boolean contains(Tt x); } Remove item from set

Art of Multiprocessor Programming28 List-Based Sets public interface Set { public boolean add(T x); public boolean remove(T x); public boolean contains(T x); } Is item in set?

Art of Multiprocessor Programming29 List Node public class Node { public T item; public int key; public Node next; }

Art of Multiprocessor Programming30 List Node public class Node { public T item; public int key; public Node next; } item of interest

Art of Multiprocessor Programming31 List Node public class Node { public T item; public int key; public Node next; } Usually hash code

Art of Multiprocessor Programming32 List Node public class Node { public T item; public int key; public Node next; } Reference to next node

Art of Multiprocessor Programming33 The List-Based Set abc Sorted with Sentinel nodes (min & max possible keys) - +

Art of Multiprocessor Programming34 Reasoning about Concurrent Objects Invariant –Property that always holds

Art of Multiprocessor Programming35 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

Art of Multiprocessor Programming36 Specifically … Invariants preserved by –add() –remove() –contains()

Art of Multiprocessor Programming37 Specifically … Invariants preserved by –add() –remove() –contains() Most steps are trivial –Usually one step tricky –Often linearization point

Art of Multiprocessor Programming38 Interference Invariants make sense only if –methods considered –are the only modifiers

Art of Multiprocessor Programming39 Interference Invariants make sense only if –methods considered –are the only modifiers Language encapsulation helps –List nodes not visible outside class

Art of Multiprocessor Programming40 Interference Invariants make sense only if –methods considered –are the only modifiers Language encapsulation helps –List nodes not visible outside class

Art of Multiprocessor Programming41 Interference Freedom from interference needed even for removed nodes –Some algorithms traverse removed nodes –Careful with malloc() & free() ! Garbage collection helps here

Art of Multiprocessor Programming42 Abstract Data Types Concrete representation: Abstract Type: –{a, b} ab

Art of Multiprocessor Programming43 Abstract Data Types Meaning of rep given by abstraction map –S( ) = {a,b} a b

Art of Multiprocessor Programming44 Rep Invariant Which concrete values meaningful? –Sorted? –Duplicates? Rep invariant –Characterizes legal concrete reps –Preserved by methods –Relied on by methods

Art of Multiprocessor Programming45 Blame Game Rep invariant is a contract Suppose –add() leaves behind 2 copies of x –remove() removes only 1 Which is incorrect?

Art of Multiprocessor Programming46 Blame Game Suppose –add() leaves behind 2 copies of x –remove() removes only 1

Art of Multiprocessor Programming47 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

Art of Multiprocessor Programming48 Rep Invariant (partly) Sentinel nodes –tail reachable from head Sorted No duplicates

Art of Multiprocessor Programming49 Abstraction Map S(head) = –{ x | there exists a such that a reachable from head and a.item = x –}

Art of Multiprocessor Programming50 Sequential List Based Set a c d a b c Add() Remove()

Art of Multiprocessor Programming51 Sequential List Based Set a c d b a b c add() remove()

Art of Multiprocessor Programming52 Coarse-Grained Locking a b d

Art of Multiprocessor Programming53 Coarse-Grained Locking a b d c

Art of Multiprocessor Programming54 honk! Coarse-Grained Locking a b d c Simple but hotspot + bottleneck honk!

Art of Multiprocessor Programming55 Coarse-Grained Locking Easy, same as synchronized methods –One lock to rule them all …

Art of Multiprocessor Programming56 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 Programming57 Fine-grained Locking Requires careful thought –Do not meddle in the affairs of wizards, for they are subtle and quick to anger

Art of Multiprocessor Programming58 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

Art of Multiprocessor Programming59 Hand-over-Hand locking abc

Art of Multiprocessor Programming60 Hand-over-Hand locking abc

Art of Multiprocessor Programming61 Hand-over-Hand locking abc

Art of Multiprocessor Programming62 Hand-over-Hand locking abc

Art of Multiprocessor Programming63 Hand-over-Hand locking abc

Art of Multiprocessor Programming64 Removing a Node abcd remove(b)

Art of Multiprocessor Programming65 Removing a Node abcd remove(b)

Art of Multiprocessor Programming66 Removing a Node abcd remove(b)

Art of Multiprocessor Programming67 Removing a Node abcd remove(b)

Art of Multiprocessor Programming68 Removing a Node abcd remove(b)

Art of Multiprocessor Programming69 Removing a Node acd remove(b) Why hold 2 locks?

Art of Multiprocessor Programming70 Concurrent Removes abcd remove(c) remove(b)

Art of Multiprocessor Programming71 Concurrent Removes abcd remove(b) remove(c)

Art of Multiprocessor Programming72 Concurrent Removes abcd remove(b) remove(c)

Art of Multiprocessor Programming73 Concurrent Removes abcd remove(b) remove(c)

Art of Multiprocessor Programming74 Concurrent Removes abcd remove(b) remove(c)

Art of Multiprocessor Programming75 Concurrent Removes abcd remove(b) remove(c)

Art of Multiprocessor Programming76 Concurrent Removes abcd remove(b) remove(c)

Art of Multiprocessor Programming77 Concurrent Removes abcd remove(b) remove(c)

Art of Multiprocessor Programming78 Concurrent Removes abcd remove(b) remove(c)

Art of Multiprocessor Programming79 Concurrent Removes abcd remove(b) remove(c)

Art of Multiprocessor Programming80 Uh, Oh acd remove(b) remove(c)

Art of Multiprocessor Programming81 Uh, Oh acd Bad news, c not removed remove(b) remove(c)

Art of Multiprocessor Programming82 Problem To delete node c –Swing node bs next field to d Problem is, –Someone deleting b concurrently could direct a pointer to c ba cbac

Art of Multiprocessor Programming83 Insight If a node is locked –No one can delete nodes successor If a thread locks –Node to be deleted –And its predecessor –Then it works

Art of Multiprocessor Programming84 Hand-Over-Hand Again abcd remove(b)

Art of Multiprocessor Programming85 Hand-Over-Hand Again abcd remove(b)

Art of Multiprocessor Programming86 Hand-Over-Hand Again abcd remove(b)

Art of Multiprocessor Programming87 Hand-Over-Hand Again abcd remove(b) Found it!

Art of Multiprocessor Programming88 Hand-Over-Hand Again abcd remove(b) Found it!

Art of Multiprocessor Programming89 Hand-Over-Hand Again acd remove(b)

Art of Multiprocessor Programming90 Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming91 Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming92 Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming93 Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming94 Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming95 Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming96 Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming97 Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming98 Removing a Node abcd Must acquire Lock for b remove(c)

Art of Multiprocessor Programming99 Removing a Node abcd Cannot acquire lock for b remove(c)

Art of Multiprocessor Programming100 Removing a Node abcd Wait! remove(c)

Art of Multiprocessor Programming101 Removing a Node abd Proceed to remove(b)

Art of Multiprocessor Programming102 Removing a Node abd remove(b)

Art of Multiprocessor Programming103 Removing a Node abd remove(b)

Art of Multiprocessor Programming104 Removing a Node ad remove(b)

Art of Multiprocessor Programming105 Removing a Node ad

Art of Multiprocessor Programming106 Remove method public boolean remove(Item item) { int key = item.hashCode(); Node pred, curr; try { … } finally { curr.unlock(); pred.unlock(); }}

Art of Multiprocessor Programming107 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

Art of Multiprocessor Programming108 Remove method public boolean remove(Item item) { int key = item.hashCode(); Node pred, curr; try { … } finally { currNode.unlock(); predNode.unlock(); }} Predecessor and current nodes

Art of Multiprocessor Programming109 Remove method public boolean remove(Item item) { int key = item.hashCode(); Node pred, curr; try { … } finally { curr.unlock(); pred.unlock(); }} Make sure locks released

Art of Multiprocessor Programming110 Remove method public boolean remove(Item item) { int key = item.hashCode(); Node pred, curr; try { … } finally { curr.unlock(); pred.unlock(); }} Everything else

Art of Multiprocessor Programming111 Remove method try { pred = this.head; pred.lock(); curr = pred.next; curr.lock(); … } finally { … }

Art of Multiprocessor Programming112 Remove method try { pred = this.head; pred.lock(); curr = pred.next; curr.lock(); … } finally { … } lock pred == head

Art of Multiprocessor Programming113 Remove method try { pred = this.head; pred.lock(); curr = pred.next; curr.lock(); … } finally { … } Lock current

Art of Multiprocessor Programming114 Remove method try { pred = this.head; pred.lock(); curr = pred.next; curr.lock(); … } finally { … } Traversing list

Art of Multiprocessor Programming115 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;

Art of Multiprocessor Programming116 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

Art of Multiprocessor Programming117 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

Art of Multiprocessor Programming118 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

Art of Multiprocessor Programming119 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 node found, remove it

Art of Multiprocessor Programming120 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

Art of Multiprocessor Programming121 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!

Art of Multiprocessor Programming122 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

Art of Multiprocessor Programming123 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

Art of Multiprocessor Programming124 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

Art of Multiprocessor Programming125 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

Art of Multiprocessor Programming126 Why does this work? To remove node e –Must lock e –Must lock es predecessor Therefore, if you lock a node –It cant be removed –And neither can its successor

Art of Multiprocessor Programming127 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

Art of Multiprocessor Programming128 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

Art of Multiprocessor Programming129 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 ….

Art of Multiprocessor Programming130 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

Art of Multiprocessor Programming131 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

Art of Multiprocessor Programming132 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

Art of Multiprocessor Programming133 Adding Nodes To add node e –Must lock predecessor –Must lock successor Neither can be deleted –(Is successor lock actually required?)

Art of Multiprocessor Programming134 Same Abstraction Map S(head) = –{ x | there exists a such that a reachable from head and a.item = x –}

Art of Multiprocessor Programming135 Rep Invariant Easy to check that –tail always reachable from head –Nodes sorted, no duplicates

Art of Multiprocessor Programming136 Drawbacks Better than coarse-grained lock –Threads can traverse in parallel Still not ideal –Long chain of acquire/release –Inefficient

Art of Multiprocessor Programming137 Optimistic Synchronization Find nodes without locking Lock nodes Check that everything is OK

Art of Multiprocessor Programming138 Optimistic: Traverse without Locking b d e a add(c) Aha!

Art of Multiprocessor Programming139 Optimistic: Lock and Load b d e a add(c)

Art of Multiprocessor Programming140 Optimistic: Lock and Load b d e a add(c) c

Art of Multiprocessor Programming141 What could go wrong? b d e a add(c) Aha!

Art of Multiprocessor Programming142 What could go wrong? b d e a add(c)

Art of Multiprocessor Programming143 What could go wrong? b d e a remove(b)

Art of Multiprocessor Programming144 What could go wrong? b d e a remove(b)

Art of Multiprocessor Programming145 What could go wrong? b d e a add(c)

Art of Multiprocessor Programming146 What could go wrong? b d e a add(c) c

Art of Multiprocessor Programming147 What could go wrong? d e a add(c) Uh-oh

Art of Multiprocessor Programming148 Validate – Part 1 b d e a add(c) Yes, b still reachable from head

Art of Multiprocessor Programming149 What Else Could Go Wrong? b d e a add(c) Aha!

Art of Multiprocessor Programming150 What Else Coould Go Wrong? b d e a add(c) add(b)

Art of Multiprocessor Programming151 What Else Coould Go Wrong? b d e a add(c) add(b) b

Art of Multiprocessor Programming152 What Else Could Go Wrong? b d e a add(c) b

Art of Multiprocessor Programming153 What Else Could Go Wrong? b d e a add(c) c

Art of Multiprocessor Programming154 Validate Part 2 (while holding locks) b d e a add(c) Yes, b still points to d

Art of Multiprocessor Programming155 Optimistic: Linearization Point b d e a add(c) c

Art of Multiprocessor Programming156 Same Abstraction Map S(head) = –{ x | there exists a such that a reachable from head and a.item = x –}

Art of Multiprocessor Programming157 Invariants Careful: we may traverse deleted nodes But we establish properties by –Validation –After we lock target nodes

Art of Multiprocessor Programming158 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

Art of Multiprocessor Programming159 Unsuccessful Remove abde remove(c ) Aha!

Art of Multiprocessor Programming160 Validate (1) abde Yes, b still reachable from head remove(c)

Art of Multiprocessor Programming161 Validate (2) abde remove(c) Yes, b still points to d

Art of Multiprocessor Programming162 OK Computer abde remove(c) return false

Art of Multiprocessor Programming163 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

Art of Multiprocessor Programming164 Validation private boolean validate(Node pred, Node curry) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false; }

Art of Multiprocessor Programming165 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

Art of Multiprocessor Programming166 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

Art of Multiprocessor Programming167 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

Art of Multiprocessor Programming168 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

Art of Multiprocessor Programming169 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?

Art of Multiprocessor Programming170 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

Art of Multiprocessor Programming171 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

Art of Multiprocessor Programming172 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; } …

Art of Multiprocessor Programming173 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

Art of Multiprocessor Programming174 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

Art of Multiprocessor Programming175 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

Art of Multiprocessor Programming176 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

Art of Multiprocessor Programming177 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

Art of Multiprocessor Programming178 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

Art of Multiprocessor Programming179 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

Art of Multiprocessor Programming180 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(); }}}

Art of Multiprocessor Programming181 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

Art of Multiprocessor Programming182 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

Art of Multiprocessor Programming183 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

Art of Multiprocessor Programming184 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

Art of Multiprocessor Programming185 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

Art of Multiprocessor Programming186 Optimistic List Limited hot-spots –Targets of add(), remove(), contains() –No contention on traversals Moreover –Traversals are wait-free –Food for thought …

Art of Multiprocessor Programming187 So Far, So Good Much less lock acquisition/release –Performance –Concurrency Problems –Need to traverse list twice –contains() method acquires locks

Art of Multiprocessor Programming188 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

Art of Multiprocessor Programming189 Lazy List Like optimistic, except –Scan once –contains(x) never locks … Key insight –Removing nodes causes trouble –Do it lazily

Art of Multiprocessor Programming190 Lazy List remove() –Scans list (as before) –Locks predecessor & current (as before) Logical delete –Marks current node as removed (new!) Physical delete –Redirects predecessors next (as before)

Art of Multiprocessor Programming191 Lazy Removal aa b c d

c Art of Multiprocessor Programming192 Lazy Removal aa b d Present in list

c Art of Multiprocessor Programming193 Lazy Removal aa b d Logically deleted

Art of Multiprocessor Programming194 Lazy Removal aa b c d Physically deleted

Art of Multiprocessor Programming195 Lazy Removal aa b d Physically deleted

Art of Multiprocessor Programming196 Lazy List All Methods –Scan through locked and marked nodes –Removing a node doesnt slow down other method calls … Must still lock pred and curr nodes.

Art of Multiprocessor Programming197 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 Programming198 Business as Usual abc

Art of Multiprocessor Programming199 Business as Usual abc

Art of Multiprocessor Programming200 Business as Usual abc

Art of Multiprocessor Programming201 Business as Usual abc remove(b)

Art of Multiprocessor Programming202 Business as Usual abc a not marked

Art of Multiprocessor Programming203 Business as Usual abc a still points to b

Art of Multiprocessor Programming204 Business as Usual a bc Logical delete

Art of Multiprocessor Programming205 Business as Usual a bc physical delete

Art of Multiprocessor Programming206 Business as Usual a bc

Art of Multiprocessor Programming207 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 Programming208 Invariant If not marked then item in the set and reachable from head and if not yet traversed it is reachable from pred

Art of Multiprocessor Programming209 Validation private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }

Art of Multiprocessor Programming210 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 Programming211 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 Programming212 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 Programming213 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 Programming214 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 Programming215 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 Programming216 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 Programming217 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 Programming218 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 Programming219 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 Programming220 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 Programming221 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 Programming222 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 Programming223 Summary: Wait-free Contains a 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

Art of Multiprocessor Programming224 Lazy List a a b c 0 e 1 d Lazy add() and remove() + Wait-free contains()

Art of Multiprocessor Programming225 Evaluation Good: –contains() doesnt lock –In fact, its wait-free! –Good because typically high % contains() –Uncontended calls dont re-traverse Bad –Contended add() and remove() calls do re- traverse –Traffic jam if one thread delays

Art of Multiprocessor Programming226 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….

Art of Multiprocessor Programming227 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 cant use locks

Art of Multiprocessor Programming228 Lock-free Lists Next logical step –Wait-free contains() –lock-free add() and remove() Use only compareAndSet() –What could go wrong?

Art of Multiprocessor Programming229 a a b c 0 e 1 c Logical Removal Physical Removal Use CAS to verify pointer is correct Not enough! Lock-free Lists

Art of Multiprocessor Programming230 Problem… a a b c 0 e 1 c Logical Removal Physical Removal 0 d Node added

Art of Multiprocessor Programming231 The 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 (AtomicMarkableReference) Fail CAS: Node not added after logical Removal

Art of Multiprocessor Programming232 Solution Use AtomicMarkableReference Atomically –Swing reference and –Update flag Remove in two steps –Set mark bit in next field –Redirect predecessors pointer

Art of Multiprocessor Programming233 Marking a Node AtomicMarkableReference class –Java.util.concurrent.atomic package address F mark bit Reference

Art of Multiprocessor Programming234 Extracting Reference & Mark Public Object get(boolean[] marked);

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

Art of Multiprocessor Programming236 Extracting Mark Only public boolean isMarked(); Value of mark

Art of Multiprocessor Programming237 Changing State Public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark);

Art of Multiprocessor Programming238 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 Programming239 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 Programming240 Changing State public boolean attemptMark( Object expectedRef, boolean updateMark);

Art of Multiprocessor Programming241 Changing State public boolean attemptMark( Object expectedRef, boolean updateMark); If this is the current reference …

Art of Multiprocessor Programming242 Changing State public boolean attemptMark( Object expectedRef, boolean updateMark);.. then change to this new mark.

b CAS Art of Multiprocessor Programming243 Removing a Node acd remove c

Art of Multiprocessor Programming244 Removing a Node abd remove b remove c c failed CAS

Art of Multiprocessor Programming245 Removing a Node abd remove b remove c c

Art of Multiprocessor Programming246 Removing a Node ad remove b remove c

Art of Multiprocessor Programming247 Traversing the List Q: what do you do when you find a logically deleted node in your path? A: finish the job. –CAS the predecessors next field –Proceed (repeat as needed)

Art of Multiprocessor Programming248 Lock-Free Traversal (only Add and Remove) abcd CAS Uh-oh pred curr pred curr

Art of Multiprocessor Programming249 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 Programming250 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 Programming251 Using the Find Method Window window = find(head, key); Node pred = window.pred; curr = window.curr;

Art of Multiprocessor Programming252 Using the Find Method Window window = find(head, key); Node pred = window.pred; curr = window.curr; Find returns window

Art of Multiprocessor Programming253 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 Programming256 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; }}}

Art of Multiprocessor Programming257 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

Art of Multiprocessor Programming258 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

Art of Multiprocessor Programming259 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; }}} Shes not there …

Art of Multiprocessor Programming260 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

Art of Multiprocessor Programming261 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 doesnt work, just retry, if it does, job essentially done

Art of Multiprocessor Programming262 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 dont succeed, someone else did or will). a

Art of Multiprocessor Programming263 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 Programming264 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 Programming265 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 Programming266 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 Programming267 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 Programming268 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

Art of Multiprocessor Programming269 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 Programming270 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

Art of Multiprocessor Programming271 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 Programming272 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 Programming273 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

Art of Multiprocessor Programming274 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

Art of Multiprocessor Programming275 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

Art of Multiprocessor Programming276 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 Programming277 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 Programming278 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 Programming279 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 predecessors next field changed, retry whole traversal

Art of Multiprocessor Programming280 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

Performance Different list-based set implementaions 16-node machine Vary percentage of contains() calls Art of Multiprocessor Programming281

Art of Multiprocessor Programming282 High Contains Ratio Lock-free Lazy list Coarse Grained Fine Lock-coupling

Art of Multiprocessor Programming283 Low Contains Ratio

Art of Multiprocessor Programming284 As Contains Ratio Increases % Contains()

Art of Multiprocessor Programming285 Summary Coarse-grained locking Fine-grained locking Optimistic synchronization Lazy synchronization Lock-free synchronization

Art of Multiprocessor Programming286 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

Art of Multiprocessor Programming287 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.