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

Similar documents
Coarse-grained and fine-grained locking Niklas Fors

Locking Granularity. CS 475, Spring 2019 Concurrent & Distributed Systems. With material from Herlihy & Shavit, Art of Multiprocessor Programming

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

Programming Paradigms for Concurrency Lecture 6 Synchronization of Concurrent Objects

Last Lecture: Spin-Locks

Introduction to Concurrency and Multicore Programming. Slides adapted from Art of Multicore Programming by Herlihy and Shavit

Concurrent Skip Lists. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrency. Part 2, Chapter 10. Roger Wattenhofer. ETH Zurich Distributed Computing

Concurrent Data Structures

The Art of Multiprocessor Programming Copyright 2007 Elsevier Inc. All rights reserved. October 3, 2007 DRAFT COPY

Linked Lists: The Role of Locking. Erez Petrank Technion

Coarse-Grained Synchronization

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Solution: a lock (a/k/a mutex) public: virtual void unlock() =0;

Locking. Part 2, Chapter 7. Roger Wattenhofer. ETH Zurich Distributed Computing

Programming Paradigms for Concurrency Lecture 3 Concurrent Objects

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrent Queues, Monitors, and the ABA problem. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Hashing and Natural Parallism. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Lecture 7: Mutual Exclusion 2/16/12. slides adapted from The Art of Multiprocessor Programming, Herlihy and Shavit

Spin Locks and Contention

Modern High-Performance Locking

Practice: Small Systems Part 2, Chapter 3

Shared-Memory Computability

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Non-Blocking Synchronization

Universality of Consensus. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Mutual Exclusion. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Parallel linked lists

Integrating Transactionally Boosted Data Structures with STM Frameworks: A Case Study on Set

Building Efficient Concurrent Graph Object through Composition of List-based Set

Concurrent Data Structures Concurrent Algorithms 2016

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

A simple correctness proof of the MCS contention-free lock. Theodore Johnson. Krishna Harathi. University of Florida. Abstract

PERFORMANCE ANALYSIS AND OPTIMIZATION OF SKIP LISTS FOR MODERN MULTI-CORE ARCHITECTURES

Concurrent Queues and Stacks. 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

Peterson s Algorithm

Concurrent Objects and Linearizability

Sequen&al Consistency and Linearizability

A Concurrent Skip List Implementation with RTM and HLE

Programming Paradigms for Concurrency Lecture 2 - Mutual Exclusion

Locks Chapter 4. Overview. Introduction Spin Locks. Queue locks. Roger Wattenhofer. Test-and-Set & Test-and-Test-and-Set Backoff lock

Formal Verification of a Lazy Concurrent List-Based Set Algorithm

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

A Simple Optimistic skip-list Algorithm

Programming Paradigms for Concurrency Introduction

Spin Locks and Contention. Companion slides for Chapter 7 The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit. Art of Multiprocessor Programming

Whatever can go wrong will go wrong. attributed to Edward A. Murphy. Murphy was an optimist. authors of lock-free programs LOCK FREE KERNEL

Overview of Lecture 4. Memory Models, Atomicity & Performance. Ben-Ari Concurrency Model. Dekker s Algorithm 4

Parallel Programming

What We'll Cover Today

15 418/618 Project Final Report Concurrent Lock free BST

Lecture 21: Transactional Memory. Topics: consistency model recap, introduction to transactional memory

Advance Operating Systems (CS202) Locks Discussion

Page 1. Challenges" Concurrency" CS162 Operating Systems and Systems Programming Lecture 4. Synchronization, Atomic operations, Locks"

Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006

CS510 Concurrent Systems. Jonathan Walpole

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrent Objects. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

COMP Parallel Computing. CC-NUMA (2) Memory Consistency

Whatever can go wrong will go wrong. attributed to Edward A. Murphy. Murphy was an optimist. authors of lock-free programs 3.

Spin Locks and Contention Management

Locking. Part 2, Chapter 11. Roger Wattenhofer. ETH Zurich Distributed Computing

Design of Parallel and High-Performance Computing Fall 2015 Lecture: Locks and Lock-Free continued

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Lecture: Consistency Models, TM. Topics: consistency models, TM intro (Section 5.6)

Combining Techniques Application for Tree Search Structures

Concurrent Counting using Combining Tree

Futures, Scheduling, and Work Distribution. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Parallel Programming

Lecture 9: Multiprocessor OSs & Synchronization. CSC 469H1F Fall 2006 Angela Demke Brown

Lecture 21: Transactional Memory. Topics: Hardware TM basics, different implementations

Scheduler Activations. CS 5204 Operating Systems 1

Reagent Based Lock Free Concurrent Link List Spring 2012 Ancsa Hannak and Mitesh Jain April 28, 2012

Distributed Computing Group

Experience with Model Checking Linearizability

The Universality of Consensus

Agenda. Lecture. Next discussion papers. Bottom-up motivation Shared memory primitives Shared memory synchronization Barriers and locks

Optimistic Transactional Boosting

Thread Synchronization: Foundations. Properties. Safety properties. Edsger s perspective. Nothing bad happens

Goldibear and the 3 Locks. Programming With Locks Is Tricky. More Lock Madness. And To Make It Worse. Transactional Memory: The Big Idea

A Comparison of Relativistic and Reader-Writer Locking Approaches to Shared Data Access

A Skiplist-based Concurrent Priority Queue with Minimal Memory Contention

Lecture: Consistency Models, TM

CS 310: Hash Table Collision Resolution

6. Intermediate Representation!

Unit 6: Indeterminate Computation

CS 310: Hash Table Collision Resolution Strategies

Lecture 7: Transactional Memory Intro. Topics: introduction to transactional memory, lazy implementation

Design and Evaluation of Scalable Software

Parallel access to linked data structures

IN5050: Programming heterogeneous multi-core processors Thinking Parallel

Today: Synchronization. Recap: Synchronization

Story so far. Parallel Data Structures. Parallel data structure. Working smoothly with Galois iterators

Snapshots and Software Transactional Memory

CMSC Computer Architecture Lecture 15: Memory Consistency and Synchronization. Prof. Yanjing Li University of Chicago

Design of Parallel and High-Performance Computing Fall 2017 Lecture: Locks (contd.) and Lock-Free

Transcription:

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

Coarse-Grained Synchronization Each method locks the object Avoid contention using queue locks Easy to reason about In simple cases Standard Java model Synchronized blocks and methods So, are we done? Art of Multiprocessor Programming 2

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 Programming 3

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 4

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 5

List-Based Sets public interface Set<T> { public boolean add(t x); public boolean remove(t x); public boolean contains(t x); } Art of Multiprocessor Programming 6

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

List-Based Sets public interface Set<T> { public boolean add(t x); public boolean remove(t x); public boolean contains(tt x); } Remove item from set Art of Multiprocessor Programming 8

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

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

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

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

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

The List-Based Set - a b c + Sorted with Sentinel nodes (min & max possible keys) Art of Multiprocessor Programming 14

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 Programming 15

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

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

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 Programming 18

Abstract Data Types Concrete representation a b Abstract Type {a, b} Art of Multiprocessor Programming 19

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

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

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

Blame Game Suppose add() leaves behind 2 copies of x remove() removes only 1 Which one is incorrect? If rep invariant says no duplicates add() is incorrect Otherwise remove() is incorrect Art of Multiprocessor Programming 23

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

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

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

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

Course Grained Locking a b d Art of Multiprocessor Programming 28

Course Grained Locking a b d c Art of Multiprocessor Programming 29

Course Grained Locking a b d honk! honk! c Simple but hotspot + bottleneck Art of Multiprocessor Programming 30

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 31

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 Programming 32

Hand-over-Hand locking a b c Art of Multiprocessor Programming 33

Hand-over-Hand locking a b c Art of Multiprocessor Programming 34

Hand-over-Hand locking a b c Art of Multiprocessor Programming 35

Hand-over-Hand locking a b c Art of Multiprocessor Programming 36

Hand-over-Hand locking a b c Art of Multiprocessor Programming 37

Removing a Node a b c d remove(b) Art of Multiprocessor Programming 38

Removing a Node a b c d remove(b) Art of Multiprocessor Programming 39

Removing a Node a b c d remove(b) Art of Multiprocessor Programming 40

Removing a Node a b c d remove(b) Art of Multiprocessor Programming 41

Removing a Node a b c d remove(b) Art of Multiprocessor Programming 42

Removing a Node a c d remove(b) Why do we need to always hold 2 locks? Art of Multiprocessor Programming 43

Concurrent Removes a b c d remove(b) remove(c) Art of Multiprocessor Programming 44

Concurrent Removes a b c d remove(b) remove(c) Art of Multiprocessor Programming 45

Concurrent Removes a b c d remove(b) remove(c) Art of Multiprocessor Programming 46

Concurrent Removes a b c d remove(b) remove(c) Art of Multiprocessor Programming 47

Concurrent Removes a b c d remove(b) remove(c) Art of Multiprocessor Programming 48

Concurrent Removes a b c d remove(b) remove(c) Art of Multiprocessor Programming 49

Concurrent Removes a b c d remove(b) remove(c) Art of Multiprocessor Programming 50

Concurrent Removes a b c d remove(b) remove(c) Art of Multiprocessor Programming 51

Uh, Oh a c d remove(b) remove(c) Art of Multiprocessor Programming 52

Uh, Oh Bad news, C not removed a c d remove(b) remove(c) Art of Multiprocessor Programming 53

Problem To delete node c Swing node b s next field to d a b c Problem is, Someone deleting b concurrently could direct a pointer a b c to c Art of Multiprocessor Programming 54

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

Hand-Over-Hand Again a b c d remove(b) Art of Multiprocessor Programming 56

Hand-Over-Hand Again a b c d remove(b) Art of Multiprocessor Programming 57

Hand-Over-Hand Again a b c d remove(b) Art of Multiprocessor Programming 58

Hand-Over-Hand Again a b c d remove(b) Found it! Art of Multiprocessor Programming 59

Hand-Over-Hand Again a b c d remove(b) Found it! Art of Multiprocessor Programming 60

Hand-Over-Hand Again a c d remove(b) Art of Multiprocessor Programming 61

Removing a Node a b c d remove(b) remove(c) Art of Multiprocessor Programming 62

Removing a Node a b c d remove(b) remove(c) Art of Multiprocessor Programming 63

Removing a Node a b c d remove(b) remove(c) Art of Multiprocessor Programming 64

Removing a Node a b c d remove(b) remove(c) Art of Multiprocessor Programming 65

Removing a Node a b c d remove(b) remove(c) Art of Multiprocessor Programming 66

Removing a Node a b c d remove(b) remove(c) Art of Multiprocessor Programming 67

Removing a Node a b c d remove(b) remove(c) Art of Multiprocessor Programming 68

Removing a Node a b c d remove(b) remove(c) Art of Multiprocessor Programming 69

Removing a Node a b c d Must acquire Lock of b remove(c) Art of Multiprocessor Programming 70

Removing a Node a b c d Cannot acquire lock of b remove(c) Art of Multiprocessor Programming 71

Removing a Node a b c d Wait! remove(c) Art of Multiprocessor Programming 72

Removing a Node a b d Proceed to remove(b) Art of Multiprocessor Programming 73

Removing a Node a b d remove(b) Art of Multiprocessor Programming 74

Removing a Node a b d remove(b) Art of Multiprocessor Programming 75

Removing a Node a d remove(b) Art of Multiprocessor Programming 76

Removing a Node a d Art of Multiprocessor Programming 77

Remove method public boolean remove(item item) { int key = item.hashcode(); Node pred, curr; try { } finally { curr.unlock(); pred.unlock(); }} Art of Multiprocessor Programming 78

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 79

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 80

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 81

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 82

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

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

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

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

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 Programming 87

Remove: searching while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } Search key range pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false; Art of Multiprocessor Programming 88

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 Programming 89

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 Programming 90

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 Programming 91

Remove: searching Unlock predecessor 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 92

Remove: searching Only one node locked! 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 93

Remove: searching while (curr.key <= key) { demote current 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 94

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 Programming 95

Remove: searching while (curr.key <= key) { Lock if (item invariant == curr.item) restored { pred.next = curr.next; return true; } pred.unlock(); pred = currnode; curr = curr.next; curr.lock(); } return false; Art of Multiprocessor Programming 96

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 Programming 97

Why does this work? To remove node e Must lock e Must lock e s predecessor Therefore, if you lock a node It can t be removed And neither can its successor Art of Multiprocessor Programming 98

Why remove() is linearizable 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; pred reachable from head curr is pred.next So curr.item is in the set Art of Multiprocessor Programming 99

Why remove() is linearizable 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; Linearization point if item is present Art of Multiprocessor Programming 100

Why remove() is linearizable 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; Node locked, so no other thread can remove it. Art of Multiprocessor Programming 101

Why remove() is linearizable 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; Item not present Art of Multiprocessor Programming 102

Why remove() is linearizable while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; pred reachable from head curr.lock(); curr is pred.next } pred.key < key return false; key < curr.key Art of Multiprocessor Programming 103

Why remove() is linearizable 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; Linearization point Art of Multiprocessor Programming 104

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

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

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

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

This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. You are free: to Share to copy, distribute and transmit the work to Remix to adapt the work Under the following conditions: Attribution. You must attribute the work to The Art of Multiprocessor Programming (but not in any way that suggests that the authors endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to http://creativecommons.org/licenses/by-sa/3.0/. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights. Art of Multiprocessor Programming 109