42
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

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

Embed Size (px)

Citation preview

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

2

ConcurrentConcurrentObjectObject

P1 P2 Pn

Shared Memory

3

Lock-FreeLock-Free

No Mutual Exclusion

Appear Atomic

Lamport

Massalin and Pu

4

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

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

6

Why is this important ?Why is this important ?

Avoids Mutual Exclusion Problems

Convoying

Deadlock

Priority Inversion Busy Waiting

Blocking

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

8

As quick as spin locks

Without blocking or busy waiting (wait free)

Benefits of new Benefits of new implementationsimplementations

9

Part 2Part 2Concurrent Linked ListConcurrent Linked List

Cursor

Cursor

10

Traversal ( no problem )Traversal ( no problem )

Cursor

Cursor

11

Insert ( small problem )Insert ( small problem )

sp

q

12

Insert ( small problem )Insert ( small problem )

sp

q

13

Insert ( small problem )Insert ( small problem )

sp

qswing pointer

14

DeleteDelete

ba d

15

DeleteDelete

ba d

16

Delete Delete ( big problem )( big problem )

Cursor

a d

cb

Cursor

17

Delete Delete ( big problem )( big problem )

Cursor

a d

cb

Cursor

18

Delete ( big problem )Delete ( big problem )

Cursor

a d

cb

Cursor

19

Delete ( big problem )Delete ( big problem )

a d

cb

Cursor

Cursor

20

Auxiliary nodesAuxiliary nodes

a dcb

21

Delete ( with auxiliary nodes )Delete ( with auxiliary nodes )

a d

c

b

22

Delete ( with auxiliary nodes )Delete ( with auxiliary nodes )

a d

c

b

23

Delete ( with auxiliary nodes )Delete ( with auxiliary nodes )

a d

c

b

24

Delete ( with auxiliary nodes )Delete ( with auxiliary nodes )

a d

c

b

25

Conclusion Conclusion Allows concurrent operations

Good for:parallel processingdistributed memory

Should be used everywhere

26

Questions/FactsQuestions/Facts

27

ABA problemABA problem

sp

qswing pointer

28

ABA SolutionsABA Solutions

Double Compare&Swap

No Cell Reuse

Memory Management

29

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.

30

Related WorkRelated Work

LamportHerlihyMassalin and Pu

31

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

ContributionsContributions

32

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)

33

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

34

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 )

35

node * target; // -> data

node * pre_aux; // -> preceding auxiliary node

node * pre_cell; // -> previous cell

struct Cursor {struct Cursor {

};};

36

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

// removes double aux_node.

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

};};

37

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

};};

38

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

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

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

41

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

regardless of interleaving.

Wait-FreeWait-Free

42

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