112
Linked Lists: The Role of Locking Erez Petrank Technion

Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Linked Lists: The Role of Locking

Erez Petrank Technion

Page 2: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Why Data Structures?• Concurrent Data Structures are building blocks – Used as libraries – Construction principles apply broadly

Page 3: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

This Lecture• Designing the first concurrent data structure: the

linked-list – How do we use locks? – How do we achieve progress guarantees

Page 4: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Proper CreditSeveral drawings are taken from the book, or from its accompanying slides.

Page 5: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Locking vs. Progress Guarantees

Page 6: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Counter Example

local := counter local++ counter := local

local := counter local++ counter := local

T1 T2

concurrent execution is not safe !

Page 7: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Use a LockLock (L) local := counter local++ counter := local Unlock(L)

Synchronization: Only one thread can acquire a lock L

Page 8: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Compare and Swap (CAS)• CAS (addr, expected, new)Atomically: If ( MEM[addr] == expected ) { MEM[addr] = new return (TRUE) } else return (FALSE)

Page 9: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Use a Lock or a CASLock (L) local := counter local++ counter := local Unlock(L)

Synchronization: Only one thread can acquire a lock L

Synchronization: Only one thread changes from old in a concurrent CAS.

START: old := counter new := old ++ if ( ! CAS(&counter,old,new) ) goto START

Page 10: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Use a Lock or a CASLock (L) local := counter local++ counter := local Unlock(L)

Issues: Efficiency, scalability, Fairness, progress guarantee, design complexity.

START: old := counter new := old ++ if ( ! CAS(&counter,old,new) ) goto START

Page 11: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

What Does ״Lock-Free״ Mean?• First try: “never using a lock”. • Well, is this using a lock? (word is initially zero.)

• It is not easy to say if something is a “lock”.

while (!CAS(&word,0,1)) {}local := counter local++ counter := local word := 0

Page 12: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

What Does ״Lock-Free״ Mean?• Better: “No matter which interleaving is scheduled, my program will

make progress.”. • A.k.a. non-blocking. • Our second example is lock-free.

START: old := counter new := old ++ if ( ! CAS(&counter,old,new) ) goto START

Page 13: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Lock-Freedom

• Realistic (though difficult): Various lock-free data structures exist in the literature (stack, queue, hashing, skiplist, trees, etc.).

• Advantages:Worst-case responsiveness, scalability, no deadlocks, no livelocks, added robustness to threads fail-stop.

Lock-Freedom If you schedule enough steps across all threads, one of them will make progress.

Page 14: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Linked List: Our First Example

Fine-grained locking and lock-freedom

Page 15: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

The Linked List

Support: insert, delete, contains. Implements a set: no duplicates, order maintained.

4 6 9-∞

+∞

Page 16: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Sequential Implementation • Delete 6:

• Insert 7:

4 6 9

4 6 9

7

Page 17: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

But don’t try this concurrently!

• Delete 6 || Insert 7:

• A similar problem with concurrent deletes.

4 6 9

7

Page 18: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Solutions• Coarse-grained locking. – Sequential with overhead…

• Fine-grained locking – hand-over-hand, optimistic, lazy synchronization.

• Lock-free (or wait-free) implementation.

4 6 9

7

Simplicity

scalability / performance

Coarse-grained

Lock-free

Fine- grained

Page 19: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

scalability / performance

Page 20: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Fine-Grained Locking #1:

Hand-Over-Hand

Page 21: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming21

Hand-over-Hand locking [Bayer-Schkolnik 1977]

a b c

Page 22: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming22

Operation 1: Remove a Node

a b c d

remove(b)

Page 23: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming23

Removing a Node

a b c d

remove(b)

Page 24: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming24

Removing a Node

a b c d

remove(b)

Page 25: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming25

Removing a Node

a b c d

remove(b)

Page 26: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming26

Removing a Node

a b c d

remove(b)

Page 27: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming27

Removing a Node

a c d

remove(b)

Why do we need to always hold 2 locks?

Page 28: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming28

Concurrent Removes

a b c d

remove(c)remove(b)

Page 29: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming29

Concurrent Removes

a b c d

remove(b)remove(c)

Page 30: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming30

Concurrent Removes

a b c d

remove(b)remove(c)

Page 31: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming31

Concurrent Removes

a b c d

remove(b)remove(c)

Page 32: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming32

Concurrent Removes

a b c d

remove(b)remove(c)

Page 33: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming33

Concurrent Removes

a b c d

remove(b)remove(c)

Page 34: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming34

Uh, Oh

a c d

remove(b)remove(c)

b

Page 35: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming35

Uh, Oh

a c d

Bad news, C not removed

remove(b)remove(c)

Page 36: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming36

With Two Locks

a b c d

remove(b)remove(c)

Page 37: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming37

Removing a Node

a b c d

remove(b)remove(c)

Page 38: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming38

Removing a Node

a b c d

remove(b)remove(c)

Page 39: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming39

Removing a Node

a b c d

remove(b)remove(c)

Page 40: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming40

Removing a Node

a b c d

remove(b)remove(c)

Page 41: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming41

Removing a Node

a b c d

remove(b)remove(c)

Page 42: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming42

Removing a Node

a b c d

remove(b)remove(c)

Page 43: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming43

Removing a Node

a b c d

remove(b)remove(c)

Page 44: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming44

Removing a Node

a b c d

Must acquire Lock of b

remove(c)

Page 45: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming45

Removing a Node

a b c d

Cannot acquire lock of b

remove(c)

Page 46: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming46

Removing a Node

a b c d

Wait!remove(c)

Page 47: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming47

Removing a Node

a b d

Proceed to remove(b)

Page 48: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming48

Removing a Node

a b d

remove(b)

Page 49: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming49

Removing a Node

a b d

remove(b)

Page 50: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming50

Removing a Node

a d

remove(b)

Page 51: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming51

Removing a Node

a d

Page 52: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming52

Adding Nodes• To add node e – Go hand-over-hand – Lock predecessor – Lock successor

• Neither can be deleted • Actually it is enough to lock predecessor (for an insert). – But must go hand-over-hand.

Page 53: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

No Deadlock• In general, no deadlock if locks are always acquired in the same order.

Page 54: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Why Is It Correct• The idea: snapshot.

• Each thread sees all operations executed “earlier”, and no operation that started “afterwards”.

• Start time: take head’s lock.

• Implications:

• sequentialization of operations

• Good only for “hierarchical” data structures.

Page 55: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Properties • Scalability better than coarse-grained locking. • But long chains of threads waiting for the first thread to advance. – Limited parallelism.

• Excessive locking harms performance.

• Can we obtain more parallelism and better performance?

Page 56: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Second List: Optimistic

(First was hand-over-hand.)

Page 57: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming57

Optimistic Synchronization [Herlihy-Shavit 2008]

• Find nodes without locking • Lock nodes • Check that everything is OK

Page 58: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming58

Optimistic: Traverse without Locking

b d ea

add(c) Aha!

Page 59: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming59

Optimistic: Lock and Load

b d ea

add(c)

Page 60: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming60

What could go wrong?

b d ea

add(c)

remove(b)Aha!

Page 61: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

First node must be in the list!• While holding the lock, check that first node is reachable from the head

• While we hold the lock this node cannot be removed.

Page 62: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming62

Validate – Part 1 (while holding locks)

b d ea

add(c) Yes, b still reachable from

head (after locks acquired!)

Page 63: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming63

What Else Can Go Wrong?

b d ea

add(c)

Page 64: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming64

What Else Can Go Wrong?

b d ea

add(c)

add(b’)

b’

Page 65: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming65

What Else Can Go Wrong?

b d ea

add(c)

b’

Aha!

Page 66: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

First node must still point to second!

• Validation 1: first node still reachable. • While we hold the lock the node cannot be removed.

• Validation 2: first node pointing to second. • While we hold the lock the pointer from the first node

cannot be modified (no adding and no removing).

Page 67: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming67

Validate Part 2 (while holding locks)

b d ea

add(c)Yes, b still points to d (after locks were acquired!)

Page 68: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Validation Failure? • Upon failure to validate start from scratch. • Assumed to happen infrequently.

Page 69: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming69

Insert (After Validation)

b d ea

add(c)

c

Page 70: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

70

Optimistic Synchronization• More parallelism, better scalability. – Only lock nodes where actually modifying. – Scalability depends on the actual workload.

• Excessive work on validation (double traversal). – Less efficient.

• There is another fine-grained locking methodology. – But let’s jump to lock-freedom

Page 71: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Third List: Lock-FreeWe did hand-over-hand and optimistic

Page 72: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Lock-Freedom• Don’t use locks. • And more important: guarantee progress! – Complete robustness against worst-case scheduling – No swapping problems – Even when a thread dies, the other threads will continue to make progress.

• Design by [Harris 2001], improvement by [Michael 2002].

Page 73: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Lock-Free Linked Lists [Harris-Michael 2001-2]

• First attempt: insert/delete using CAS instead of a regular read/write operation.

Page 74: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

First Attempt: Use CASes Instead of Locks

• Delete 6:

• Insert 7:

4 6 9

4 6 9

7

CAS

CAS

Page 75: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

The original problem still exists

• Outcome for deleting 6 and inserting 7 in parallel:

4 6 9

7

Page 76: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

We Keep the Simple Insert

• A single CAS to insert 7, after locally allocating and initializing it.

4 6 9

7

• But delete will be more complicated.

Page 77: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Recall the Problem

• Outcome for deleting 6 and inserting 7 in parallel:

4 6 9

7

Page 78: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

The Crux of the Problem• When deleting 6, we want to block changes both on the pointer that points at 6,

as well as the pointer that points out of 6.

4 6 9

Page 79: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

The Crux of the Problem• When deleting 6, we want to block changes both on the pointer that points at 6,

as well as the pointer that points out of 6.

• Harris’s idea: 1.“mark” the pointer out of 6, and then 2.“modify” the pointer out of 4.

4 6 9

Page 80: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Solution: Mark & Delete

• Logically delete 6 by marking the outgoing pointer of 6.

• Physically delete 6 by unlinking it from the list.

4 6 9

4 6 9

Page 81: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Implementing a “Red Pointer”• Use least bit. • Essentially unused with pointers as words are composed of 4

or 8 bytes.

00010…1010010101110000100Unmarked:

00010…1010010101110000101Marked:

Page 82: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming82

Logical Deletion

a 0 0 0a b c 0e1c

Logical Removal = Set Mark Bit

Physical Removal CAS

0d

Mark-Bit and Pointer are CASed together

An attempted insert will fail the CAS after logical removal

Page 83: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming83

Concurrent Removal

a b d

remove b

remove c

cCASCAS

failed

Page 84: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming84

Removing a Node

a b d

remove b

remove c

c

Page 85: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming85

Removing a Node

a d

remove b

remove c

Page 86: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming86

Traversing the List• When you find a “logically” deleted node in your path: – Finish the job: • CAS the predecessor’s next field,

– Proceed (repeat as needed).

Page 87: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming87

Lock-Free Traversal (only Add and Remove)

a b c dCAS

Uh-oh

pred currpred curr

Page 88: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

CAS Failures• Node removal: – Logical remove fails: start from scratch. – Physical remove fails: ignore. • Why?

• Node insert: – CAS fails: start from scratch.

Page 89: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Why is it Lock-Free?• Node removal: – Logical remove fails:

either someone else has succeeded to remove this node, or someone else has inserted a node.

– Logical remove succeeds: I succeeded to delete a node (and will finish the operation after trying the physical remove once).

• Node insert: – CAS fails: someone else succeeded to insert or delete a node. – CAS succeeds: I succeeded in inserting a node.

Page 90: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

The Main Intuition• Logical marking locks the next pointer from being modified. • But this “lock” can be unlocked by anyone (by trimming the node

from the list), so no one is stuck. • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free algorithms:

– Make a change in the data structure, leaving it “unstable”. – Anyone can stabilize the data structure and continue to work on it.

4 6 9

Page 91: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Progress Guarantees are good for• Real-time, OS, interactive systems, service level agreements, etc. • But it’s always good to have. – Avoid deadlock, live-lock, convoying, priority inversion, etc.

• Scalability.

Page 92: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Progress Guarantees

Lock-FreedomIf you schedule enough steps across all threads, one of them will make progress.

Great guarantee! Until recently considered difficult

to achieve and inefficient. Wait-FreedomIf you schedule enough steps of any thread, it will make progress.

Page 93: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Contains is Wait-Free• Contains(key); – curr = head; – while (curr.key < key) • curr = removeMark ( curr.next ) • succ = removeMark ( curr.next )

– return (curr.key == key && !marked(curr.next) )

Contains is important!

Page 94: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Fourth List: Third (and Best) Fine-Grained Locking:

Lazy ListWe discussed hand-over-hand, optimistic, and lock-free.

Page 95: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Lazy Synchronization [Heller et al. 2005]

• Lock only relevant nodes • Do not validate reachability • Instead, leave a mark on deleted nodes, like in lock-free

algorithm.

• To remove a node: – Logically remove it by marking it “removed’’. – Physically remove it by unlinking it.

Page 96: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Remove or Add• Scan through the list • Lock predecessor and current nodes • validate that – both are not deleted, and that – predecessor points to current.

• Perform the add or the remove.

• Search can simply traverse the list.

Page 97: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming97

Lazy List

a b c

Page 98: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming98

Lazy List

a b c

Page 99: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming99

Lazy List

a b c

Page 100: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming100

Lazy List

a b c

remove(b)

Page 101: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming101

Lazy List

a b c

a not marked

Page 102: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming102

Lazy List

a b c

b not marked

Page 103: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming103

Lazy List

a b c

a still points to b

Page 104: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming104

Lazy List

a b c

Logical delete

Page 105: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming105

Lazy List

a b c

physical delete

Page 106: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming106

Lazy List

a b c

Page 107: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Art of Multiprocessor Programming107

Invariant• If a node is not marked then its key is in the set – and reachable from head

Page 108: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

The contains Method• Simply traverses the list and reports finding. • Very efficient, progress guaranteed. – Wait-free

• Most “popular” method.

Page 109: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Properties of Lazy List• Good performance – no rescanning, – a small number of locks, – hopefully not too many validation failures

• Good scalability – Lock only relevant nodes.

• Still standard locking problems – No progress guarantee – A thread holding a lock may face a cache-miss, page-fault, swap-

out, etc. – Worst-case scalability issues, scheduler critical here…

Page 110: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Summary• Starting data structures • Locking and Lock-freedom • Linked list (ordered for sets) • Parallization problems • Fine grained locking:

• Hand-over-hand • Optimistic • Lazy

• Lock-Free version

Page 111: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

Which List Should You Use?• If little contention: coarse-grained locking. • To handle contention pretty well: lazy list. • To handle high contention and provide a progress

guarantee: lock-free list.

Page 112: Linked Lists: The Role of Lockingerez/courses/seminar/talks/05.pdf · • Different from “normal” locking that only the owner can unlock. • This is the methodology in all lock-free

The End