170
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 Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Linked Lists: Locking, Lock-Free,

and Beyond …

Companion slides for

The Art of Multiprocessor Programming

by Maurice Herlihy & Nir Shavit

Page 2: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 2

Concurrent Objects

• Adding threads should not lower

throughput

– Contention effects

– Mostly fixed by scalable locks

Page 3: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 3

Concurrent Objects

• Adding threads should not lower

throughput

– Contention effects

– Mostly fixed by scalable locks

• Should increase throughput

– Not possible if inherently sequential

– Surprising things are parallelizable

Page 4: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 4

Coarse-Grained Synchronization

• Each method locks the object

– Avoid contention using scalable locks

Page 5: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 5

Coarse-Grained Synchronization

• Each method locks the object

– Avoid contention using scalable locks

– Easy to reason about

• In simple cases

Page 6: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 6

Coarse-Grained Synchronization

• Each method locks the object

– Avoid contention using scalable locks

– Easy to reason about

• In simple cases

• So, are we done?

Page 7: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 7

Coarse-Grained Synchronization

• Sequential bottleneck

– Threads “stand in line”

Page 8: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 8

Coarse-Grained Synchronization

• Sequential bottleneck

– Threads “stand in line”

• Adding more threads

– Does not improve throughput

– Struggle to keep it from getting worse

Page 9: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 9

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 …

Page 10: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 10

This Lecture

• Introduce four “patterns”

– Bag of tricks …

– Methods that work more than once …

Page 11: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 11

This Lecture

• Introduce four “patterns”

– Bag of tricks …

– Methods that work more than once …

• For highly-concurrent objects

– Concurrent access

– More threads, more throughput

Page 12: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 12

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

Page 13: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 13

Second:

Optimistic Synchronization

• Search without locking …

Page 14: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 14

Second:

Optimistic Synchronization

• Search without locking …

• If you find it, lock and check …

– OK: we are done

– Oops: start over

Page 15: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 15

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

Page 16: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 16

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

Page 17: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 17

Fourth:

Lock-Free Synchronization

• Don’t use locks at all

– Use compareAndSet() & relatives …

Page 18: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 18

Fourth:

Lock-Free Synchronization

• Don’t use locks at all

– Use compareAndSet() & relatives …

• Advantages

– No Scheduler Assumptions/Support

Page 19: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 19

Fourth:

Lock-Free Synchronization

• Don’t use locks at all

– Use compareAndSet() & relatives …

• Advantages

– No Scheduler Assumptions/Support

• Disadvantages

– Complex

– Sometimes high overhead

Page 20: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 20

Linked List

• Illustrate these patterns …

• Using a list-based Set

– Common application

– Building block for other apps

Page 21: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 21

Set Interface

• Unordered collection of items

Page 22: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 22

Set Interface

• Unordered collection of items

• No duplicates

Page 23: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 23

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

Page 24: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 24

List-Based Sets

public interface Set<T> { public boolean add(T x); public boolean remove(T x); public boolean contains(T x); }

Page 25: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 25

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

Page 26: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 26

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

Page 27: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 27

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?

Page 28: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 28

List Node

public class Node { public T item; public int key; public Node next; }

Page 29: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 29

List Node

public class Node { public T item; public int key; public Node next; }

item of interest

Page 30: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 30

List Node

public class Node { public T item; public int key; public Node next; }

Usually hash code

Page 31: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 31

List Node

public class Node { public T item; public int key; public Node next; }

Reference to next node

Page 32: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 32

The List-Based Set

a b c

Sorted with Sentinel nodes

(min & max possible keys)

-∞

+∞

Page 33: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 33

Reasoning about Concurrent

Objects

• Invariant

– Property that always holds

Page 34: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 34

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

Page 35: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 41

Sequential List Based Set

a c d

a b c

add()

remove()

Page 36: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 42

Sequential List Based Set

a c d

b

a b c

add()

remove()

Page 37: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 43

Coarse-Grained Locking

a b d

Page 38: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 44

Coarse-Grained Locking

a b d

c

Page 39: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 45

honk!

Coarse-Grained Locking

a b d

c

Simple but hotspot + bottleneck

honk!

Page 40: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 46

Coarse-Grained Locking

• Easy, same as synchronized methods

– “One lock to rule them all …”

Page 41: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 47

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

Page 42: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 48

Fine-grained Locking

• Requires careful thought

– “Do not meddle in the affairs of wizards, for

they are subtle and quick to anger”

Page 43: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 49

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

Page 44: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 50

Hand-over-Hand locking

a b c

Page 45: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 51

Hand-over-Hand locking

a b c

Page 46: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 52

Hand-over-Hand locking

a b c

Page 47: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 53

Hand-over-Hand locking

a b c

Page 48: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 54

Hand-over-Hand locking

a b c

Page 49: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 55

Removing a Node

a b c d

remove(b)

Page 50: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 56

Removing a Node

a b c d

remove(b)

Page 51: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 57

Removing a Node

a b c d

remove(b)

Page 52: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 58

Removing a Node

a b c d

remove(b)

Page 53: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 59

Removing a Node

a b c d

remove(b)

Page 54: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 60

Removing a Node

a c d

remove(b) Why hold 2 locks?

Page 55: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 61

Concurrent Removes

a b c d

remove(c) remove(b)

Page 56: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 62

Concurrent Removes

a b c d

remove(b) remove(c)

Page 57: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 63

Concurrent Removes

a b c d

remove(b) remove(c)

Page 58: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 64

Concurrent Removes

a b c d

remove(b) remove(c)

Page 59: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 65

Concurrent Removes

a b c d

remove(b) remove(c)

Page 60: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 66

Concurrent Removes

a b c d

remove(b) remove(c)

Page 61: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 67

Concurrent Removes

a b c d

remove(b) remove(c)

Page 62: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 68

Concurrent Removes

a b c d

remove(b) remove(c)

Page 63: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 69

Concurrent Removes

a b c d

remove(b) remove(c)

Page 64: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 70

Concurrent Removes

a b c d

remove(b) remove(c)

Page 65: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 71

Uh, Oh

a c d

remove(b) remove(c)

Page 66: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 72

Uh, Oh

a c d

Bad news, c not removed

remove(b) remove(c)

Page 67: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 73

Problem

• To delete node c

– Swing node b’s next field to d

• Problem is,

– Someone deleting b concurrently could

direct a pointer

to c

b a c

b a c

Page 68: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 74

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

Page 69: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 75

Hand-Over-Hand Again

a b c d

remove(b)

Page 70: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 76

Hand-Over-Hand Again

a b c d

remove(b)

Page 71: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 77

Hand-Over-Hand Again

a b c d

remove(b)

Page 72: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 78

Hand-Over-Hand Again

a b c d

remove(b) Found

it!

Page 73: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 79

Hand-Over-Hand Again

a b c d

remove(b) Found

it!

Page 74: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 80

Hand-Over-Hand Again

a c d

remove(b)

Page 75: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 81

Removing a Node

a b c d

remove(b) remove(c)

Page 76: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 82

Removing a Node

a b c d

remove(b) remove(c)

Page 77: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 83

Removing a Node

a b c d

remove(b) remove(c)

Page 78: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 84

Removing a Node

a b c d

remove(b) remove(c)

Page 79: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 85

Removing a Node

a b c d

remove(b) remove(c)

Page 80: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 86

Removing a Node

a b c d

remove(b) remove(c)

Page 81: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 87

Removing a Node

a b c d

remove(b) remove(c)

Page 82: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 88

Removing a Node

a b c d

remove(b) remove(c)

Page 83: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 89

Removing a Node

a b c d

Must

acquire

Lock for

b

remove(c)

Page 84: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 90

Removing a Node

a b c d

Cannot

acquire

lock for b

remove(c)

Page 85: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 91

Removing a Node

a b c d

Wait! remove(c)

Page 86: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 92

Removing a Node

a b d

Proceed

to

remove(b)

Page 87: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 93

Removing a Node

a b d

remove(b)

Page 88: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 94

Removing a Node

a b d

remove(b)

Page 89: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 95

Removing a Node

a d

remove(b)

Page 90: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 96

Removing a Node

a d

Page 91: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

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?)

Page 92: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 105

Drawbacks

• Better than coarse-grained lock

– Threads can traverse in parallel

• Still not ideal

– Long chain of acquire/release

– Inefficient

Page 93: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 106

Optimistic Synchronization

• Find nodes without locking

• Lock nodes

• Check that everything is OK

Page 94: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 107

Optimistic: Traverse without

Locking

b d e a

add(c) Aha!

Page 95: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 108

Optimistic: Lock and Load

b d e a

add(c)

Page 96: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 109

Optimistic: Lock and Load

b d e a

add(c)

c

Page 97: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 110

What could go wrong?

b d e a

add(c) Aha!

Page 98: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 111

What could go wrong?

b d e a

add(c)

Page 99: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 112

What could go wrong?

b d e a

remove(b)

Page 100: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 113

What could go wrong?

b d e a

remove(b)

Page 101: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 114

What could go wrong?

b d e a

add(c)

Page 102: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 115

What could go wrong?

b d e a

add(c)

c

Page 103: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 116

What could go wrong?

d e a

add(c) Uh-oh

Page 104: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 117

Validate – Part 1

b d e a

add(c) Yes, b still

reachable

from head

Page 105: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 118

What Else Could Go Wrong?

b d e a

add(c) Aha!

Page 106: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 119

What Else Coould Go Wrong?

b d e a

add(c)

add(b’)

Page 107: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 120

What Else Coould Go Wrong?

b d e a

add(c)

add(b’) b’

Page 108: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 121

What Else Could Go Wrong?

b d e a

add(c) b’

Page 109: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 122

What Else Could Go Wrong?

b d e a

add(c)

c

Page 110: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 123

Validate Part 2

(while holding locks)

b d e a

add(c) Yes, b still

points to d

Page 111: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 124

Invariants

• Careful: we may traverse deleted nodes

• But we establish properties by

– Validation

– After we lock target nodes

Page 112: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 125

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

Page 113: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 126

Unsuccessful Remove

a b d e

remove(c) Aha!

Page 114: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 127

Validate (1)

a b d e

Yes, b still

reachable

from head

remove(c)

Page 115: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 128

Validate (2)

a b d e

remove(c) Yes, b still

points to d

Page 116: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 129

OK Computer

a b d e

remove(c) return false

Page 117: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 130

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

Page 118: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 131

Optimistic List

• Limited hot-spots

– Targets of add(), remove(), contains()

– No contention on traversals

• Moreover

– Traversals are wait-free

– Food for thought …

Page 119: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 132

So Far, So Good

• Much less lock acquisition/release

– Performance

– Concurrency

• Problems

– Need to traverse list twice

– contains() method acquires locks

Page 120: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 133

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

Page 121: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 134

Lazy List

• Like optimistic, except

– Scan once

– contains(x) never locks …

• Key insight

– Removing nodes causes trouble

– Do it “lazily”

Page 122: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 135

Lazy List

• remove()

– Scans list (as before)

– Locks predecessor & current (as before)

• Logical delete

– Marks current node as removed (new!)

• Physical delete

– Redirects predecessor’s next (as before)

Page 123: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 136

Lazy Removal

a a b c d

Page 124: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

c

Art of Multiprocessor Programming 137

Lazy Removal

a a b d

Present in list

Page 125: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

c

Art of Multiprocessor Programming 138

Lazy Removal

a a b d

Logically deleted

Page 126: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 139

Lazy Removal

a a b c d

Physically deleted

Page 127: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 140

Lazy Removal

a a b d

Physically deleted

Page 128: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 141

Lazy List

• All Methods

– Scan through locked and marked nodes

– Removing a node doesn’t slow down other

method calls …

• Must still lock pred and curr nodes.

Page 129: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 142

Validation

• No need to rescan list!

• Check that pred is not marked

• Check that curr is not marked

• Check that pred points to curr

Page 130: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 143

Business as Usual

a b c

Page 131: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 144

Business as Usual

a b c

Page 132: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 145

Business as Usual

a b c

Page 133: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 146

Business as Usual

a b c

remove(b)

Page 134: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 147

Business as Usual

a b c

a not

marked

Page 135: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 148

Business as Usual

a b c

a still

points

to b

Page 136: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 149

Business as Usual

a b c

Logical

delete

Page 137: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 150

Business as Usual

a b c

physical

delete

Page 138: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 151

Business as Usual

a b c

Page 139: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 152

Invariant

• An item is in the set if

– not marked

– reachable from head

– and if not yet traversed it is reachable from

pred

Page 140: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 153

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

Page 141: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 154

private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }

List Validate Method

Predecessor not

Logically removed

Page 142: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 155

private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }

List Validate Method

Current not

Logically removed

Page 143: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 156

private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }

List Validate Method

Predecessor still

Points to current

Page 144: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 157

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

Page 145: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 158

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

Page 146: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 159

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)

Page 147: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 160

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?

Page 148: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 161

Summary: Wait-free Contains

a 0 0 0 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

Page 149: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 162

Lazy List

a 0 0 0 a b c 0 e 1 d

Lazy add() and remove() + Wait-free contains()

Page 150: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 163

Evaluation

• Good:

– contains() doesn’t lock

– In fact, its wait-free!

– Good because typically high % contains()

– Uncontended calls don’t re-traverse

• Bad

– Contended add() and remove() calls do re-traverse

– Traffic jam if one thread delays

Page 151: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 164

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….

Page 152: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 165

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 can’t use locks

Page 153: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 166

Lock-free Lists

• Next logical step

– Wait-free contains()

– lock-free add() and remove()

• Use only compareAndSet()

– What could go wrong?

Page 154: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 167

a 0 0 0 a b c 0 e 1 c

Logical Removal

Physical Removal Use CAS to verify pointer

is correct

Not enough!

Lock-free Lists

Page 155: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 168

Problem…

a 0 0 0 a b c 0 e 1 c

Logical Removal

Physical Removal

0 d

Node added

Page 156: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 169

The Solution: Combine Bit and

Pointer

a 0 0 0 a b c 0 e 1 c

Logical Removal =

Set Mark Bit

Physical

Removal

CAS

0 d

Mark-Bit and Pointer

are CASed as one (AtomicMarkableReference)

Failed CAS: Node not

added after logical

Removal

Page 157: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 170

Solution

• Use AtomicMarkableReference

• Atomically

– Swing reference and

– Update flag

• Remove in two steps

– Set mark bit in next field

– Redirect predecessor’s pointer

Page 158: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

b CAS

Art of Multiprocessor Programming 172

Removing a Node

a c d

remove

c

Page 159: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 173

Removing a Node

a b d

remove

b

remove

c

c

failed

CAS CAS

Page 160: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 174

Removing a Node

a b d

remove

b

remove

c

c

Page 161: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 175

Removing a Node

a d

remove

b

remove

c

Page 162: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 176

Traversing the List

• Q: what do you do when you find a

“logically” deleted node in your path?

• A: finish the job.

– CAS the predecessor’s next field

– Proceed (repeat as needed)

Page 163: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 177

Lock-Free Traversal

(only Add and Remove)

a b c d CAS

Uh-oh

pred curr pred curr

Page 164: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 178

Performance

On 16 node shared memory machine

Benchmark throughput of Java List-based Set

algs. Vary % of Contains() method Calls.

Page 165: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 179

High Contains Ratio

Lock-free

Lazy list

Coarse Grained Fine Lock-coupling

Page 166: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 180

Low Contains Ratio

Lock-free

Lazy list

Coarse Grained

Fine Lock-coupling

Page 167: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 181

As Contains Ratio Increases

Lock-free

Lazy list

Coarse Grained

Fine Lock-coupling

% Contains()

Page 168: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 182

Summary

• Coarse-grained locking

• Fine-grained locking

• Optimistic synchronization

• Lock-free synchronization

Page 169: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 183

“To Lock or Not to Lock”

• Locking vs. Non-blocking: Extremist views

on both sides

• The answer: nobler to compromise,

combine locking and non-blocking

– Example: Lazy list combines blocking add() and

remove() and a wait-free contains()

– Remember: Blocking/non-blocking is a property

of a method

Page 170: Linked Lists: Locking, Lock-Free, and Beyondhomes.cs.washington.edu/~djg/msr_russia2012/herlihy2.pdf · Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art

Art of Multiprocessor Programming 184

This work is licensed under a Creative Commons Attribution-

ShareAlike 2.5 License.

– — to copy, distribute and transmit the work

– — to adapt the work

– . 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).

– . 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.