Post on 01-Jan-2016
description
LOCK-FREE BINARY SEARCH TREE
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
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
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
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
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
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
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 !
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 !
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.
KEY STEPS OF INSERT
CAS’s for Insert:
Flag B
Change it’s child pointer
unFlag B
B
D
α
E
E
B
KEY STEPS OF DELETE
CAS’s for Delete
Flag B
Mark D
Change B’s child pointer
unFlag B
B
D
α C
β
B
D
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
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