Docsity

Lock-Free Programming: Insertion and Deletion in a Linked List without Locks, Slides of Operating Systems

Partial preview of the text

Download Lock-Free Programming: Insertion and Deletion in a Linked List without Locks and more Slides Operating Systems in PDF only on Docsity!

Lock-free Programming

Nathaniel Wesley Filardo

October 23, 2013

Outline

Introduction

Lock-Free Linked List Insertion

Lock-Free Linked List Deletion

Read-Copy-Update Mutual Exclusion

Lessons

Introduction Locks Might Take A While

ˆ Consider XCHG style locks which use while( xchg( &locked, LOCKED ) == LOCKED ) as their core operation.

ˆ (^) We could spend an unbounded amount of time here waiting...

ˆ (^) This implies we’ll have very high latency on contention...

ˆ (^) Locks by definition reduce parallelism.

Introduction Locks Might Take A While

ˆ That is, if N people are contending for a lock, N − 1 of them are just wasting time.

ˆ (^) It would be nice if they could all work at once...

ˆ... being careful not to step on each other when there was actually a problem.

Introduction Locks Can Be... Not So Bad?

ˆ Instead of a lock around a tree, we could have a tree with locks: ROOT

 

LOCK

C

y y % %

LOCK

B LOCK D LOCK

ˆ (^) The protocol is lock the root, then (lock child & unlock parent) as you go down. ˆ (^) This kind of lock handoff is a very common design. ˆ Here every time a thread decides to go down one branch,

Introduction Locks Can Be... Not So Bad?

ˆ Trying to find node A.

ˆ Step 1: lock root pointer and top node

ROOT

 

LOCK

C

y y % %

LOCK

B

 

LOCK D

 

LOCK

A LOCK E LOCK

Introduction Locks Can Be... Not So Bad?

ˆ Trying to find node A.

ˆ Step 3: lock left child and unlock parent

ROOT

 

LOCK

C

y y % %

LOCK

B

 

LOCK D

 

LOCK

A LOCK E LOCK

Introduction

But let’s see what we can do without any locks at all.

Lock-Free Linked List Node

ˆ Node definition is simple: label t label void* next

ˆ (^) When drawing, we’ll use a shorthand: label t label = A void* next = &B

⇔ A &B

Insertion into a Linked List Without Locks Insertion Code

insertAfter(after, newlabel) { //lockList(); new = newNode(newlabel); prev = findLabel(after); new->next = prev->next; prev->next = new; //unlockList(); }

Insertion into a Linked List Without Locks Race trace in 410 notation

insertAfter(A,B) insertAfter(A,C) prev = &A B.next = A.next prev = &A C.next = A.next A.next = B A.next = C

ˆ Either of these assignments makes sense in isolation, but one of them will override the other!

Insertion into a Linked List Without Locks Precondition

A &D //D NULL

ˆ One list, two items on it: A and D.

Insertion into a Linked List Without Locks Second step

C &D

' ' A &D //D NULL

B &D

88

ˆ (^) Two threads point their respective nodes C and B into list at D

B.next=&D C.next=&D

Insertion into a Linked List Without Locks One thread goes

C &D

' ' A &C

D NULL

B &D

88

ˆ (^) Suppose the thread owning C completes its assignment first. A.next=&C