Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lock-Free concurrent algorithm for Linked lists: Implementation

Similar presentations


Presentation on theme: "Lock-Free concurrent algorithm for Linked lists: Implementation"— Presentation transcript:

1 Lock-Free concurrent algorithm for Linked lists: Implementation
DAML+OIL Lock-Free concurrent algorithm for Linked lists: Implementation Speaker: Alexandre Walzberg Date: April, 28th 2009 Paper: MikhailFomitchev and Eric Ruppert, Lock-Free Linked Lists and Skip Lists.PODC'04, 2004. CSE-COSC6490A : Concurrent Object-Oriented Languages York University - W09

2 DAML+OIL Plan Implementation Tests Performances

3 1. The Implementation Reminder: a node Node value key backlink next
DAML+OIL 1. The Implementation Reminder: a node Node value 31 2 1 key 31 2 1 backlink 31 2 1 next 31 2 1 2 LSB of a pointer : always 0 ! - one will represent the flag - the other one the mark Allow to update the 3 information atomically

4 1. The Implementation Reminder: a node Node int value long key
DAML+OIL 1. The Implementation Reminder: a node Node int value 31 2 1 long key 31 2 1 Node backlink 31 2 1 next 31 2 1 2 LSB of a pointer : always 0 ! - one will represent the flag - the other one the mark Allow to update the 3 information atomically

5 1. The Implementation AtomicStampedReference<T> Own:
DAML+OIL 1. The Implementation AtomicStampedReference<T> Own: A reference to an object of class T A Stamp (integer) Getters and Setters for the reference and the stamp Atomic CAS operation which change both the reference and the stamp atomically Reading and writing on both the reference and the stamp are Volatile public boolean compareAndSet( V expectedReference, V newReference, int expectedStamp, int newStamp)

6 1. The Implementation The class Node Node int value long key
DAML+OIL 1. The Implementation The class Node Node int value 31 2 1 long key 31 2 1 Node backlink 31 2 1 AtomicStampedReference<T> next Node int 31 2 1 31 2 1

7 1. The Implementation Flag and Mark
DAML+OIL 1. The Implementation Flag and Mark Use the stamp to represent the Flag and the Mark 0: flag=0 ; mark=0 1: flag=1 ; mark=0 2: flag=0 ; mark=1 compareAndSet vs compareAndSwap var.compareAndSet(expected, new){ if(var == expected){ var = new; return true; } return false; } var.compareAndSwap(expected, new){ old = var; if(var == expected) var = new; return old; } The paper’s algorithm was using compareAndSwap. This is different of the compareAndSet function java provides. => The way to use these atomic function differ a little bit ! (but the result is the same)

8 1. The Implementation Class concurrentLinkedList
DAML+OIL 1. The Implementation Class concurrentLinkedList private Node head, tail public Node Insert(), Delete(), Search() public void print() private SearchFrom(), SearchFrom2() private Try Flag(),TryMark(), HelpFlagged(), HelpMarked() concurrentLinkedList Node head Node Node Node tail

9 1. The Implementation 5 Example: Insert(7) 10 13
DAML+OIL 1. The Implementation Example: Insert(7) public Node Insert(long k, int value){ … // search where to insert ; allocate new node while(true){ if(prevNode.getFlags() == FLAG) HelpFlagged(prevNode, prevNode.getNext()); else{ newNode.setNextAndFlag(nextNode, NONE); int result = prevNode.CASnext(nextNode, newNode, NONE, NONE); if(result == NONE && prevNode.getNext()== newNode) return newNode; if(result == FLAG) HelpFlagged(prevNode, prevNode.getNext()); while(prevNode.getFlags() == MARK) prevNode = prevNode.getBacklink(); } (prevNode, nextNode) = SearchFrom(k, prevNode); if(prevNode.getKey() == k) return null; 5 10 13

10 1. The Implementation 5 7 Example: Insert(7) prevNode nextNode 10 13
DAML+OIL 1. The Implementation Example: Insert(7) public Node Insert(long k, int value){ … // search where to insert ; allocate new node while(true){ if(prevNode.getFlags() == FLAG) HelpFlagged(prevNode, prevNode.getNext()); else{ newNode.setNextAndFlag(nextNode, NONE); int result = prevNode.CASnext(nextNode, newNode, NONE, NONE); if(result == NONE && prevNode.getNext()== newNode) return newNode; if(result == FLAG) HelpFlagged(prevNode, prevNode.getNext()); while(prevNode.getFlags() == MARK) prevNode = prevNode.getBacklink(); } (prevNode, nextNode) = SearchFrom(k, prevNode); if(prevNode.getKey() == k) return null; prevNode nextNode 5 10 13 newNode 7

11 1. The Implementation 5 7 Example: Insert(7) Will be deleted
DAML+OIL 1. The Implementation Example: Insert(7) public Node Insert(long k, int value){ … // search where to insert ; allocate new node while(true){ if(prevNode.getFlags() == FLAG) HelpFlagged(prevNode, prevNode.getNext()); else{ newNode.setNextAndFlag(nextNode, NONE); int result = prevNode.CASnext(nextNode, newNode, NONE, NONE); if(result == NONE && prevNode.getNext()== newNode) return newNode; if(result == FLAG) HelpFlagged(prevNode, prevNode.getNext()); while(prevNode.getFlags() == MARK) prevNode = prevNode.getBacklink(); } (prevNode, nextNode) = SearchFrom(k, prevNode); if(prevNode.getKey() == k) return null; Will be deleted prevNode nextNode 5 10 13 newNode 7

12 1. The Implementation 5 7 Example: Insert(7) prevNode nextNode 13
DAML+OIL 1. The Implementation Example: Insert(7) public Node Insert(long k, int value){ … // search where to insert ; allocate new node while(true){ if(prevNode.getFlags() == FLAG) HelpFlagged(prevNode, prevNode.getNext()); else{ newNode.setNextAndFlag(nextNode, NONE); int result = prevNode.CASnext(nextNode, newNode, NONE, NONE); if(result == NONE && prevNode.getNext()== newNode) return newNode; if(result == FLAG) HelpFlagged(prevNode, prevNode.getNext()); while(prevNode.getFlags() == MARK) prevNode = prevNode.getBacklink(); } (prevNode, nextNode) = SearchFrom(k, prevNode); if(prevNode.getKey() == k) return null; prevNode nextNode 5 13 newNode 7

13 1. The Implementation 5 7 Example: Insert(7) prevNode nextNode 13
DAML+OIL 1. The Implementation Example: Insert(7) public Node Insert(long k, int value){ … // search where to insert ; allocate new node while(true){ if(prevNode.getFlags() == FLAG) HelpFlagged(prevNode, prevNode.getNext()); else{ newNode.setNextAndFlag(nextNode, NONE); int result = prevNode.CASnext(nextNode, newNode, NONE, NONE); if(result == NONE && prevNode.getNext()== newNode) return newNode; if(result == FLAG) HelpFlagged(prevNode, prevNode.getNext()); while(prevNode.getFlags() == MARK) prevNode = prevNode.getBacklink(); } (prevNode, nextNode) = SearchFrom(k, prevNode); if(prevNode.getKey() == k) return null; prevNode nextNode 5 13 newNode 7

14 1. The Implementation 5 7 Example: Insert(7) prevNode nextNode 13
DAML+OIL 1. The Implementation Example: Insert(7) public Node Insert(long k, int value){ … // search where to insert ; allocate new node while(true){ if(prevNode.getFlags() == FLAG) HelpFlagged(prevNode, prevNode.getNext()); else{ newNode.setNextAndFlag(nextNode, NONE); int result = prevNode.CASnext(nextNode, newNode, NONE, NONE); if(result == NONE && prevNode.getNext()== newNode) return newNode; if(result == FLAG) HelpFlagged(prevNode, prevNode.getNext()); else while(prevNode.getFlags() == MARK) prevNode = prevNode.getBacklink(); } (prevNode, nextNode) = SearchFrom(k, prevNode); if(prevNode.getKey() == k) return null; CASnext 1) 2) 3) prevNode nextNode 5 13 newNode 7

15 1. The Implementation And so on… Example: Insert(7)
DAML+OIL 1. The Implementation Example: Insert(7) public Node Insert(long k, int value){ … // search where to insert ; allocate new node while(true){ if(prevNode.getFlags() == FLAG) HelpFlagged(prevNode, prevNode.getNext()); else{ newNode.setNextAndFlag(nextNode, NONE); int result = prevNode.CASnext(nextNode, newNode, NONE, NONE); if(result == NONE && prevNode.getNext()== newNode) return newNode; if(result == FLAG) HelpFlagged(prevNode, prevNode.getNext()); while(prevNode.getFlags() == MARK) prevNode = prevNode.getBacklink(); } (prevNode, nextNode) = SearchFrom(k, prevNode); if(prevNode.getKey() == k) return null; And so on…

16 DAML+OIL 2. Testing Classes concurrentLinkedList Node Thread listUser

17 Insertion of all even key Insertion of odd key | Deletion of even key
DAML+OIL 2. Testing Interleaving Concurrent insertion/deletion of nodes 1) with same key 2) with different key 3) with same and different key Insertion of all even key Insertion of odd key | Deletion of even key Pre-emption effects Insertion of sleep(100) into delete-group functions to force pre-emption by the scheduler in the middle of a deletion (between each step) => Test of the lock-free property and distribution of the algorithm

18 2. Testing Random Insertion and deletion after random delay
DAML+OIL 2. Testing Random Insertion and deletion after random delay Random operation (search / insert / delete) => TODO Random key => TODO Plan for Verification Finish testing To make the algorithm works : I add an operation compare to the described algorithm => seems to work this way but need to be CHECK See what tools can offer for lock-free algorithm verification

19 3. Performances Locks implementation
DAML+OIL 3. Performances Locks implementation Source: One mutex / node Lock node during research / insertion before reading the next field Deletion: lock the previous node and then the deleted node Implemented with the finest possible granularity

20 DAML+OIL 3. Performances Test done on navy (8 processors)

21 DAML+OIL Conclusion Implementation easy to do (just translate the paper algorithm) => be carefully with some little details (compareAndSet vs CompareAndSwap) => some unexpected bugs very hard to trace and debug Seems to work well according to the test done => Are the tests cover all cases? The lock-free algorithm is much more efficient than the lock based one. => Is that possible to improve the lock-based algorithm? And… All pointer changes will be made using CAS (so we can be sure that not any process wrote between we read and we write the pointer) Flag and mark are represented by the 2 last bit of a pointer (so we can retrieve and modify these free information (pointer, flag and mark) in the same time) As the other process can help in an operation, we always have to check if it is not already inserted, marked, flagged, deleted right before the operation and each time.

22 Thank you for your attention!
Any questions ?? Thank you for your attention!


Download ppt "Lock-Free concurrent algorithm for Linked lists: Implementation"

Similar presentations


Ads by Google