Linked Lists: Locking, Lock- Free, and Beyond … Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)

Slides:



Advertisements
Similar presentations
Enabling interoperable and rights-aware DRM using the Semantic Web
Advertisements

Roberta Gassman, Secretary Department of Workforce Development
PowerPoint Tutorial 1 Creating a Presentation
Word Tutorial 2 Editing and Formatting a Document
Excel Tutorial 3 Working with Formulas and Functions
Objectives Create an action query to create a table
Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
The Wiki and the Blog NIH Wiki Fair D. Calvin Andrus Chief Technology Officer Center for Mission Innovation Central Intelligence Agency 28 February 2007.
May 23, ALICE Offline Tutorial Markus Oldenburg – CERN May 23, 2007 – University of São Paulo.
Rule 132 Medicaid Community Mental Health Service Program
Equilibria and Complexity: What now? Christos H. Papadimitriou UC Berkeley christos.
LAW February A 5/3 approximation algorithm for a hard case of stable marriage Rob Irving Computing Science Department University of Glasgow (joint.
3 october Brown easyBorrow (beta) Brown University Library October 2007.
Word Tutorial 1 Creating a Document
V3.0 12/04/ For Coral Systems With PRI Services Presented by Education Technology Services.
Ordered linked list implementation of a set
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by 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-+
Objectives Explore a structured range of data Freeze rows and columns
March 8, Dynamic Fault Tree analysis using Input/Output Interactive Markov Chains Hichem Boudali, Pepijn Crouzen, and Mariëlle Stoelinga. Formal.
Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Uitspraakevaluatie & training met behulp van spraaktechnologie Pronunciation assessment & training by means of speech technology Helmer Strik – and many.
Input & OutputCS-502 Fall Input and Output CS502 Operating Systems Fall 2007 (Slides include materials from Operating System Concepts, 7 th ed.,
Word Tutorial 5 Working with Templates and Outlines
Fall The Crisis in Funding for Public Education: Your Child’s Future Is at Stake A presentation by Washington State PTA
ITINERANT PSYCHOLOGICAL MODELS A SPEECH BETWEEN SELF PSYCHOLOGY, GROUP- ANALYSIS AND ANALYTICAL PSYCHOLOGY Dott. Antonino Sammartano.
SchedulingCS-502 Fall Scheduling (continued) The art and science of allocating the CPU and other resources to processes (Slides include materials.
Sell offs, spin offs, carve outs and tracking stock
Scalable Web Architectures Common Patterns & Approaches Cal Henderson.
Feza Baskaya – Anne Kakkonen - University of Tampere Second International Seminar on Subject Access to Information, Helsinki 30th November 2007.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
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.
Linked Lists: Lazy and Non-Blocking Synchronization Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)
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.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Data structures and algorithms in the collection framework 1 Part 2.
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.
Linked Lists: Optimistic, Lock-Free, … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by Pavol Černý,
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.
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
Advanced Locking Techniques
A Simple Optimistic skip-list Algorithm Maurice Herlihy Brown University & Sun Microsystems Laboratories Yossi Lev Brown University & Sun Microsystems.
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)
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.
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.
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 …
Linked Lists: Locking, Lock-Free, and Beyond …
Linked Lists: Locking, Lock-Free, and Beyond …
Presentation transcript:

Linked Lists: Locking, Lock- Free, and Beyond … Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)

Art of Multiprocessor Programming© Herlihy-Shavit Coarse-Grained Synchronization Each method locks the object –Easy to reason about In simple cases –Standard Java model Synchronized blocks and methods So, are we done?

Art of Multiprocessor Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit This Lecture Introduce four patterns –Bag of tricks … –Methods that work more than once … For highly-concurrent objects Goal: –Concurrent access –More threads, more throughput

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

Art of Multiprocessor Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit Fourth: Lock-Free Synchronization Dont use locks at all –Use compareAndSet() & relatives … Advantages –Robust against asynchrony Disadvantages –Complex –Sometimes high overhead

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

Art of Multiprocessor Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit Set Interface public interface Set { public boolean add(T x); public boolean remove(T x); public boolean contains(T x); }

Art of Multiprocessor Programming© Herlihy-Shavit Set Interface 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 Programming© Herlihy-Shavit Set Interface 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 Programming© Herlihy-Shavit Set Interface 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 Programming© Herlihy-Shavit List Node public class Node { public T item; public int key; public Node next; }

Art of Multiprocessor Programming© Herlihy-Shavit List Node public class Node { public T item; public int key; public Node next; } item of interest

Art of Multiprocessor Programming© Herlihy-Shavit List Node public class Node { public T item; public int key; public Node next; } Usually hash code

Art of Multiprocessor Programming© Herlihy-Shavit List Node public class Node { public T item; public int key; public Node next; } Reference to next node

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

Art of Multiprocessor Programming© Herlihy-Shavit Reasoning about Concurrent Objects Invariant –Property that always holds Established by –True when object is created –Truth preserved by each method Each step of each method

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

Art of Multiprocessor Programming© Herlihy-Shavit Representation Invariants Sentinel nodes never added/removed –tail reachable from head Elements sorted by key (hash value) No duplicates

Art of Multiprocessor Programming© Herlihy-Shavit Interference Invariants make sense only if methods considered are the only modifiers Language encapsulation helps –List nodes not visible outside class –This guarantees freedom from interference

Art of Multiprocessor Programming© Herlihy-Shavit Abstract Data Types Concrete representation Abstract Type –{a, b} ab

Art of Multiprocessor Programming© Herlihy-Shavit Abstract Data Types Meaning of representation given by abstraction map –S( ) = {a,b} a b

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

Art of Multiprocessor Programming© Herlihy-Shavit Sequential List Based Set a c d a b c Add() Remove()

Art of Multiprocessor Programming© Herlihy-Shavit Sequential List Based Set a c d b a b c Add() Remove()

Art of Multiprocessor Programming© Herlihy-Shavit Sequential List Based Set a c d b a b c Add() Remove()

Art of Multiprocessor Programming© Herlihy-Shavit Course Grained Locking a b d

Art of Multiprocessor Programming© Herlihy-Shavit Course Grained Locking a b d c

Art of Multiprocessor Programming© Herlihy-Shavit honk! Course Grained Locking a b d c Simple but hotspot + bottleneck honk!

Art of Multiprocessor Programming© Herlihy-Shavit Coarse-Grained Locking Easy, same as synchronized methods –One lock to rule them all … Simple, clearly correct –Deserves respect! Works poorly with contention –Queue locks help –But bottleneck still an issue

Art of Multiprocessor Programming© Herlihy-Shavit Fine-grained Locking Requires careful thought Split object into pieces –Each piece has own lock –Methods that work on disjoint pieces need not exclude each other Allows pipelined list traversal

Art of Multiprocessor Programming© Herlihy-Shavit Hand-over-Hand locking abc

Art of Multiprocessor Programming© Herlihy-Shavit Hand-over-Hand locking abc

Art of Multiprocessor Programming© Herlihy-Shavit Hand-over-Hand locking abc

Art of Multiprocessor Programming© Herlihy-Shavit Hand-over-Hand locking abc

Art of Multiprocessor Programming© Herlihy-Shavit Hand-over-Hand locking abc

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node acd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(c) remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Uh, Oh acd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Uh, Oh acd Bad news remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Problem To delete node b –Swing node as next field to c Problem is, –Someone could delete c concurrently ba cbac

Art of Multiprocessor Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit Hand-Over-Hand Again abcd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Hand-Over-Hand Again abcd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Hand-Over-Hand Again abcd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Hand-Over-Hand Again abcd remove(b) Found it!

Art of Multiprocessor Programming© Herlihy-Shavit Hand-Over-Hand Again abcd remove(b) Found it!

Art of Multiprocessor Programming© Herlihy-Shavit Hand-Over-Hand Again acd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abcd remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abd remove(b)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node ad remove(b) remove(c)

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node ad

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

Art of Multiprocessor Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit Remove method try { pred = this.head; pred.lock(); curr = pred.next; curr.lock(); … } finally { … }

Art of Multiprocessor Programming© Herlihy-Shavit Remove method try { pred = this.head; pred.lock(); curr = pred.next; curr.lock(); … } finally { … } Lock pred == head

Art of Multiprocessor Programming© Herlihy-Shavit Remove method try { pred = this.head; pred.lock(); curr = pred.next; curr.lock(); … } finally { … } Lock current

Art of Multiprocessor Programming© Herlihy-Shavit Remove method try { pred = this.head; pred.lock(); curr = pred.next; curr.lock(); … } finally { … } Traversing list

Art of Multiprocessor Programming© Herlihy-Shavit Remove: traversing list 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 Programming© Herlihy-Shavit Remove: traversing list 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 Programming© Herlihy-Shavit Remove: traversing list 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 Programming© Herlihy-Shavit Remove: traversing list 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 Programming© Herlihy-Shavit Remove: traversing list 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 Programming© Herlihy-Shavit Remove: traversing list 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 Programming© Herlihy-Shavit Remove: traversing list 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 Programming© Herlihy-Shavit Remove: traversing list 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 Programming© Herlihy-Shavit Remove: traversing list 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 Programming© Herlihy-Shavit Remove: traversing list 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 So item is not in the set

Art of Multiprocessor Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit Progress To avoid deadlock, locks must be taken by all methods in the same order Starvation freedom (if locks are starvation free)

Art of Multiprocessor Programming© Herlihy-Shavit Adding Nodes To add a node: –Traverse list using hand-by-hand locking –Lock predecessor –Lock successor Neither can be deleted –… and thus item can be inserted inbetween –(Is successor lock actually required?)

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

Art of Multiprocessor Programming© Herlihy-Shavit Representation Invariant Easy to check that –tail always reachable from head –Nodes sorted –No duplicates

Art of Multiprocessor Programming© Herlihy-Shavit Drawbacks Better than coarse-grained lock –Threads can traverse in parallel Still not ideal –Long chain of acquire/release locks –Inefficient –Threads working at the beginning of the list may block other threads

Art of Multiprocessor Programming© Herlihy-Shavit Optimistic Synchronization Find nodes without locking Lock nodes Check that everything is OK

Art of Multiprocessor Programming© Herlihy-Shavit Optimistic: Traverse without Locking b d e a add(c) Aha!

Art of Multiprocessor Programming© Herlihy-Shavit Optimistic: Lock and Load b d e a add(c)

Art of Multiprocessor Programming© Herlihy-Shavit What Can Possibly Go Wrong? b d e a add(c) Aha!

Art of Multiprocessor Programming© Herlihy-Shavit What Can Possibly Go Wrong? b d e a add(c)

Art of Multiprocessor Programming© Herlihy-Shavit What Can Possibly Go Wrong? b d e a add(c) remove(b )

Art of Multiprocessor Programming© Herlihy-Shavit What Can Possibly Go Wrong? b d e a add(c)

Art of Multiprocessor Programming© Herlihy-Shavit What Can Possibly Go Wrong? b d e a add(c) c would be added between b and d, but b is no more reachable (deleted)!

Art of Multiprocessor Programming© Herlihy-Shavit Validate (1) b d e a add(c) Yes, b still reachable from head

Art of Multiprocessor Programming© Herlihy-Shavit What Else Can Go Wrong? b d e a add(c) Aha!

Art of Multiprocessor Programming© Herlihy-Shavit What Else Can Go Wrong? b d e a add(c)

Art of Multiprocessor Programming© Herlihy-Shavit What Else Can Go Wrong? b d e a add(c) add(b) b

Art of Multiprocessor Programming© Herlihy-Shavit What Else Can Go Wrong? b d e a add(c) b add(b) invalidated by add( c )!

Art of Multiprocessor Programming© Herlihy-Shavit Optimistic: Validate(2) b d e a add(c) Yes, b still points to d

Art of Multiprocessor Programming© Herlihy-Shavit Optimistic: Linearization Point b d e a add(c) c

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

Art of Multiprocessor Programming© Herlihy-Shavit Invariants Careful: we may traverse deleted nodes But we establish properties by –Validation –After we lock target nodes

Art of Multiprocessor Programming© Herlihy-Shavit 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 add and return true

Art of Multiprocessor Programming© Herlihy-Shavit Removing a Node abde remove(c ) Aha!

Art of Multiprocessor Programming© Herlihy-Shavit Validate (1) abde Yes, b still reachable from head remove(c )

Art of Multiprocessor Programming© Herlihy-Shavit Validate (2) abde remove(c ) Yes, b still points to d

Art of Multiprocessor Programming© Herlihy-Shavit OK Computer abde remove(c ) return false

Art of Multiprocessor Programming© Herlihy-Shavit Correctness If –Nodes b and d both locked –Node b still accessible –Node d still successor to b Then –No thread can remove b, d, e –No thread can add a node between b and d or between d and e –OK to remove node or return false

Art of Multiprocessor Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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; } Validation Is current node next?

Art of Multiprocessor Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit Remove: details on … 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 Programming© Herlihy-Shavit 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: details on … Always unlock

Art of Multiprocessor Programming© Herlihy-Shavit 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: details on … Lock both nodes

Art of Multiprocessor Programming© Herlihy-Shavit 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: details on … Check for synchronization conflicts

Art of Multiprocessor Programming© Herlihy-Shavit 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: details on … target found, remove node

Art of Multiprocessor Programming© Herlihy-Shavit 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: details on … target not found

Art of Multiprocessor Programming© Herlihy-Shavit 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 Programming© Herlihy-Shavit So Far, So Good Much less lock acquisition/release –Performance –Concurrency Problems –Need to traverse list twice –contains() method acquires locks Most common method call

Art of Multiprocessor Programming© Herlihy-Shavit Evaluation Optimistic is effective if –cost of scanning twice without locks less than –cost of scanning once with locks Drawback –contains() acquires locks –90% of calls in many apps