150 likes | 260 Views
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. Binary Search Tree (BST). Implements dictionary abstract data structure Each node can have 0-2 children
E N D
Lock-Free Binary Search Tree Presented by Joanna Helga From draft paper “Lock-Free Binary Search Trees” by Eric Ruppert, PanagiotaFatourou, and Faith Ellen
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 Property #1 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
Leaf Oriented BST Property #2 Duplicate keys are not allowed Keys stored in internal nodes are not real keys, so they may have duplicates H A H X C H <X ≥X E H
Insert Operation We also need to know this pointer, which is D’s parent 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 E D E α
Delete Operation We also need to know this pointer, which is C’s grandparent Delete(C) Search C’s location Change B’s child pointer to C’s sibling B D α And this pointer, which can be found if we know C’s parent C β
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 B E α Insert Delete D E α β
Problem Example #1 B Problem arises when op1 splice node D out of tree, while op2 tries to update its child pointer op1:Delete(C) A D op2: Delete(E) C G E is not deleted ! E H
Problem Example #2 B op2: Delete(E) Problem arises when op2 splice node G out of tree, while op1 tries to update its child pointer A D C G op1: Insert(F) F F is not inserted ! E F H
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.
Key Steps of Insert CAS’s for Insert: • Flag B • Change it’s child pointer • unFlag B B B E D E α
Key Steps of Delete CAS’s for Delete • Flag B • Mark D • Change B’s child pointer • unFlag B B B D D α C β
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 ! Use 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