lock-free linked lists using compare-and-swap

43
1 Lock-Free Linked Lists Lock-Free Linked Lists Using Compare-and-Swap Using Compare-and-Swap by John Valois Speaker’s Name: Talk Title: Larry Bush Larry Bush

Upload: luella

Post on 06-Jan-2016

52 views

Category:

Documents


0 download

DESCRIPTION

Talk Title:. Lock-Free Linked Lists Using Compare-and-Swap. by John Valois. Speaker’s Name:. Larry Bush. Concurrent Object. Shared Memory. P n. P 1. P 2. Lock-Free. No Mutual Exclusion Appear Atomic Lamport Massalin and Pu. Lock-Free Linked Lists?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lock-Free Linked Lists Using Compare-and-Swap

1

Lock-Free Linked Lists Using Lock-Free Linked Lists Using Compare-and-SwapCompare-and-Swap

by John Valois

Speaker’s Name:

Talk Title:

Larry BushLarry Bush

Page 2: Lock-Free Linked Lists Using Compare-and-Swap

2

ConcurrentConcurrentObjectObject

P1 P2 Pn

Shared Memory

Page 3: Lock-Free Linked Lists Using Compare-and-Swap

3

Lock-FreeLock-Free

No Mutual Exclusion

Appear Atomic

Lamport

Massalin and Pu

Page 4: Lock-Free Linked Lists Using Compare-and-Swap

4

Lock-Free Linked Lists?Lock-Free Linked Lists?

Page 5: Lock-Free Linked Lists Using Compare-and-Swap

5

bool Compare&Swap ( Type * x, Type old, Type new) {

// BEGIN ATOMIC if *x != old {

• *x = new;• return TRUE;

} else {• return FALSE

}// END ATOMIC

}

Compare&Swap Compare&Swap Synchronization Synchronization PrimitivePrimitive

Page 6: Lock-Free Linked Lists Using Compare-and-Swap

6

Why is this important ?Why is this important ?

Avoids Mutual Exclusion Problems

Convoying

Deadlock

Priority Inversion Busy Waiting

Blocking

Page 7: Lock-Free Linked Lists Using Compare-and-Swap

7

Universal methods are not efficient (Herlihy).

Massalin and Pu’s method required the uncommon double word Compare&Swap.

Limitations of current Limitations of current implementationsimplementations

Page 8: Lock-Free Linked Lists Using Compare-and-Swap

8

As quick as spin locks

Without blocking or busy waiting (wait free)

Benefits of new Benefits of new implementationimplementation

Page 9: Lock-Free Linked Lists Using Compare-and-Swap

9

Part 2Part 2Concurrent Linked ListConcurrent Linked List

Cursor

Cursor

Page 10: Lock-Free Linked Lists Using Compare-and-Swap

10

Traversal ( no problem )Traversal ( no problem )

Cursor

Cursor

Page 11: Lock-Free Linked Lists Using Compare-and-Swap

11

Insert ( small problem )Insert ( small problem )

sp

q

Page 12: Lock-Free Linked Lists Using Compare-and-Swap

12

Insert ( small problem )Insert ( small problem )

sp

q

Page 13: Lock-Free Linked Lists Using Compare-and-Swap

13

Insert ( small problem )Insert ( small problem )

sp

qswing pointer

Page 14: Lock-Free Linked Lists Using Compare-and-Swap

14

DeleteDelete

ba d

Page 15: Lock-Free Linked Lists Using Compare-and-Swap

15

DeleteDelete

ba d

Page 16: Lock-Free Linked Lists Using Compare-and-Swap

16

Delete Delete ( big problem )( big problem )

Cursor

a d

cb

Cursor

Page 17: Lock-Free Linked Lists Using Compare-and-Swap

17

Delete Delete ( big problem )( big problem )

Cursor

a d

cb

Cursor

Page 18: Lock-Free Linked Lists Using Compare-and-Swap

18

Delete Delete ( big problem )( big problem )

Cursor

a d

cb

Cursor

Page 19: Lock-Free Linked Lists Using Compare-and-Swap

19

Delete Delete ( big problem )( big problem )

a d

cb

Cursor

Cursor

Page 20: Lock-Free Linked Lists Using Compare-and-Swap

20

Iterator ( at node b )Iterator ( at node b )

a dcb

pre_aux

pre_cell target

Page 21: Lock-Free Linked Lists Using Compare-and-Swap

21

Auxiliary nodesAuxiliary nodes

a dcb

Page 22: Lock-Free Linked Lists Using Compare-and-Swap

22

Step OneStep One

a d

c

b

Page 23: Lock-Free Linked Lists Using Compare-and-Swap

23

Step TwoStep Two

a d

c

b

Page 24: Lock-Free Linked Lists Using Compare-and-Swap

24

Step ThreeStep Three

a d

c

b

Page 25: Lock-Free Linked Lists Using Compare-and-Swap

25

Step FourStep Four

a d

c

b

Page 26: Lock-Free Linked Lists Using Compare-and-Swap

26

Conclusion Conclusion Allows concurrent operations

Good for:parallel processingdistributed memory

Should be used everywhere

Page 27: Lock-Free Linked Lists Using Compare-and-Swap

27

Questions/FactsQuestions/Facts

Page 28: Lock-Free Linked Lists Using Compare-and-Swap

28

Why do you say this is lock-free if Why do you say this is lock-free if it uses a mutual exclusion it uses a mutual exclusion

primitive?primitive?Limited to atomic actions provided by the

hardware.Only when swinging pointers. Allows simultaneous traversal, insertion

and deletion.

Lamport (made the distinction)Massalin and Pu (coined the term)

Page 29: Lock-Free Linked Lists Using Compare-and-Swap

29

Universal Algorithm (a.k.a. Universal Method) An algorithm that provides lock-free functionality for ANY generic

ADT is called universal. Universal meaning “for any.” This requires a powerful synchronization primitive. This is an application that defines a primitives strength.

Universal Primitive A universal primitive can be used to provide lock-free functionality for

any generic Data Type. A universal primitive can also solve the consensus problem.

Analogous Problems Generic Lock-Free ADT is analogous to the consensus problem. If a primitive is powerful enough to solve one it can also solve the

other.

UniversalUniversal

Page 30: Lock-Free Linked Lists Using Compare-and-Swap

30

An implementation is wait-free if every process finishes in a bounded number of steps,

regardless of interleaving.

Wait-FreeWait-Free

Page 31: Lock-Free Linked Lists Using Compare-and-Swap

31

Aux NodesAux Nodes

Insertion of new cells takes place between an auxiliary node and an existing regular cell.

Chains of auxilary nodes are allowed. An auxilary node is not required to have a real cell node before and after it.

Page 32: Lock-Free Linked Lists Using Compare-and-Swap

32

ptr = find ptr of interest

repeat old = Read( ptr ); new = compute new pointer value r = Compare&Swap(ptr, old, new)

until ( r = TRUE )}

Swinging the PointerSwinging the Pointer

Page 33: Lock-Free Linked Lists Using Compare-and-Swap

33

ABA problemABA problem

sp

qswing pointer

Page 34: Lock-Free Linked Lists Using Compare-and-Swap

34

ABA SolutionsABA Solutions

Double Compare&Swap

No Cell Reuse

Memory Management

Page 35: Lock-Free Linked Lists Using Compare-and-Swap

35

q = new cell

Repeat

r = SafeRead ( p -> next ) Write ( q -> next, r )

until Compare&Swap( p -> next, r, q )

Insert ( p, x )Insert ( p, x )

Page 36: Lock-Free Linked Lists Using Compare-and-Swap

36

node * target; // -> data

node * pre_aux; // -> preceding auxiliary node

node * pre_cell; // -> previous cell

struct Cursor {struct Cursor {

};};

Page 37: Lock-Free Linked Lists Using Compare-and-Swap

37

// Updates pointers in the cursor so that it becomes valid.

// removes double aux_node.

Update(cursor c) {Update(cursor c) {

};};

Page 38: Lock-Free Linked Lists Using Compare-and-Swap

38

c.pre_cell = next // deletes cellback_link = c->pre_celldelete pre_auxConcurrent deletions may stall process and create

chains of aux nodes.The last deletion follows the back_links of the

deleted cells.After all deletions the list will have no extra

aux_nodes

Try_delete(cursor c) {Try_delete(cursor c) {

};};

Page 39: Lock-Free Linked Lists Using Compare-and-Swap

39

Can be implemented using Compare&Swap

Test&Set Sets new value to TRUE.

Fetch&AddAdds an arbitrary value to shared variable.

Test&Set Fetch&AddTest&Set Fetch&Add

Page 40: Lock-Free Linked Lists Using Compare-and-Swap

40

Created algorithms and data structures that directly implement a non-blocking singly-linked list.

Allows multiple processes to traverse, insert and delete.

Using only commonly available Compare&Swap. Single word version Commonly available on most systems

ValoisValois

Page 41: Lock-Free Linked Lists Using Compare-and-Swap

41

Lock-Free Structures & Memory Management Techniques for linked list, dictionary and tree.

ContributionsContributions

Page 42: Lock-Free Linked Lists Using Compare-and-Swap

42

Related WorkRelated Work

LamportHerlihyMassalin and Pu

Page 43: Lock-Free Linked Lists Using Compare-and-Swap

43

Discovered that mutual exclusion problems can be avoided using lock-free methods.

Gave the first lock-free algorithm for the single writer/ multiple reader shared variable.

Led to more research on the topic.

27 years

LamportLamport