14
LOCK-FREE BINARY SEARCH TREE

Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

Embed Size (px)

Citation preview

Page 1: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

LOCK-FREE BINARY SEARCH TREE

Page 2: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

BINARY SEARCH TREE (BST)

Implements dictionary abstract data structure

Each node can have 0-2 children

All nodes in left subtree of X has keys that smaller than key of X

All nodes in right subtree of X has keys that greater than (or equal to) key of X

B

A D

C G

E H

Page 3: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

LEAF ORIENTED BST

Real keys are stored in leaf

Internal nodes stores dummy keys and exactly has 2 children

Leaves stores only a key

B

A D

C G

E H

Property #1

Page 4: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

LEAF ORIENTED BST

Duplicate keys are not allowed

Keys stored in internal nodes are not real keys, so they may have duplicates

H

A H

C H

E H

Property #2

X

<X ≥X

Page 5: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

INSERT OPERATION

Insert(E)

search for location

create a node for E

create dummy node with key=max(E,D)

point its left and right child to D and E

change B’s pointer to the new dummy node

B

D

α

E

E

We also need to know this pointer, which is

D’s parent

We also need to know this pointer, which is

D’s parent

Page 6: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

DELETE OPERATION

Delete(C)

Search C’s location

Change B’s child pointer to C’s sibling

B

D

α C

β

We also need to know this pointer, which is

C’s grandparent

We also need to know this pointer, which is

C’s grandparent

And this pointer, which can be found if we

know C’s parent

And this pointer, which can be found if we

know C’s parent

Page 7: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

UPDATE OPERATIONS

Update operations only change 1 pointer in the shared data structure

However, simply using Compare-and-swap for updating this pointer can have some problem

B

D

α

E

E

B

α

β

Insert Delete

Page 8: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

PROBLEMExample #1

B

A D

C G

E H

op1:Delete(C)

op2:Delete(E)

Problem arises when op1 splice node D out of tree, while op2 tries

to update its child pointer

Problem arises when op1 splice node D out of tree, while op2 tries

to update its child pointer

E is not deleted !E is not deleted !

Page 9: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

PROBLEM

B

A D

C G

E H

op1:Insert(F)

op2:Delete(E)

F

F

Example #2

Problem arises when op2 splice node G out of tree, while op1 tries

to update its child pointer

Problem arises when op2 splice node G out of tree, while op1 tries

to update its child pointer

F is not inserted !F is not inserted !

Page 10: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

MARK & FLAG BITS

To overcome the previous problem, we add MARK and FLAG bits to each node

MARK bit indicate that a node will be spliced out soon

FLAG bit indicate that a node will have one of its child pointer changed soon

We want to avoid a node being spliced out and changed its child pointer at same time.

This can be achieved by using these 2 bits, that stored in a single word.

We want to avoid a node being spliced out and changed its child pointer at same time.

This can be achieved by using these 2 bits, that stored in a single word.

Page 11: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

KEY STEPS OF INSERT

CAS’s for Insert:

Flag B

Change it’s child pointer

unFlag B

B

D

α

E

E

B

Page 12: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

KEY STEPS OF DELETE

CAS’s for Delete

Flag B

Mark D

Change B’s child pointer

unFlag B

B

D

α C

β

B

D

Page 13: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

LOCK-FREE PROPERTY

MARK and FLAG bits behaves like a lock

When an operation tries to update something, it has to own the lock first, and unlock it after the update is done

If a process who holds the lock dies, it will prevent other processes to access that part of the tree forever !

If a process who holds the lock dies, it will prevent other processes to access that part of the tree forever !

Use HELPING procedure to prevent lockingUse HELPING procedure to prevent locking

Page 14: Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, Panagiota Fatourou, and Faith Ellen L OCK -F REE B INARY S

HELPER NODE

Before a process tries to hold a lock, it stores enough information on a Helper Node

•Leaf pointer•Parent pointer

•Grandparent pointer

•Last known MARK and FLAG bits statuses of parent node

•Pointer to the node to be inserted (for Insert only)

A pointer to this helper node is stored in same field as MARK and FLAG bit