1 / 18

Hazard Pointers: Safe memory Reclamation of Lock Free Objects Maged M. Michael

Hazard Pointers: Safe memory Reclamation of Lock Free Objects Maged M. Michael. Presenter: Tanu Jain. Agenda. Problem Prior Work Solution and Methodology used. Examples Performance Conclusions. Problem – What and Why.

wconti
Télécharger la présentation

Hazard Pointers: Safe memory Reclamation of Lock Free Objects Maged M. Michael

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hazard Pointers: Safe memory Reclamation of Lock Free ObjectsMaged M. Michael Presenter: Tanu Jain CS510 - Portland State University

  2. Agenda • Problem • Prior Work • Solution and Methodology used. • Examples • Performance • Conclusions CS510 - Portland State University

  3. Problem – What and Why • Lock-free implies an unrestricted opportunity for a thread to operate on any object, at any time • An algorithm is needed that allows freeing of deleted nodes in a lock free manner while guaranteeing that no thread accesses free memory. • No efficient method for reclamation of lock free dynamic objects. Previous methods assumed constant static storage. No reuse. • Unsafe memory removal may lead to access violation errors or wrong results or object corruption. Cannot let that happen. CS510 - Portland State University

  4. Prior Work • Wait and delete. Wait for a "sufficiently long time" and then delete it. The problem is deciding how long to wait.. • Reference Counting with Objects. Needed to update the pointer and the reference count (sitting at a different location) at the same time (atomically). Used DCAS. No hardware support. • Keep a reference count next to the pointer. Reasonable-to-implement CAS2 primitive. Most 32-bit machines have it, but not a lot of 64-bit machines. • Writers need to wait for a quantum of time when there are absolutely zero readers. As long as at least one reader starts before all other readers finish using the object, the writer threads wait powerlessly—and that ain't lock-free anymore. CS510 - Portland State University

  5. Goal • Develop memory management methodology that allows memory reclamation for arbitrary use. • A mechanism for Reading Threads to tell Writing Threads to not reclaim replaced Objects from under them, but without allowing the readers to force the writers to hang on to an unbounded number of replaced Objects. • Efficient performance in face of thread failures • Independent of special hardware or kernel support • Suitable for user as well as OS applications. There is a solution that is not only lock-free, but is actually wait-free !!! Hazard Pointers !! CS510 - Portland State University

  6. Solution – Hazard Pointers Thread 0 1 2 … n • Associate a number of single writer and multi reader shared pointers with each thread • Hazard pointer is null or points to a node that may be accessed later. • No thread can access a dynamic node unless it has a hazard pointer pointing to it from the time when it was reachable. Objects CS510 - Portland State University

  7. RetNod Solution – Hazard Pointers (Contd.) • All retired nodes are kept in a private list by each thread. When the list.length == R (some set number) the thread scans hazard pointers of all other threads. • No retired node can be reclaimed if its being pointed to by any hazard pointer from a point prior to its removal • If no hazard pointer is pointing to a retired node in your list at the time of scan. The node can be reclaimed. Otherwise we leave it in private list. HP – Thread 2 HP – Thread 3 Thread 1 CS510 - Portland State University

  8. Process 2 Pointer P X Y ABA Problem Process 1 • Process 1 has a copy of pointer to Object x. • Process 2 starts and deletes Object x frees the memory and reuses the same memory to create a new Object y. • Process 1 wakes up and applies CAS on address of Object X which is same as Object Y. • CAS succeeds but the Object has CHANGED. Pointer P Object CS510 - Portland State University

  9. Process 1 Process 2 Retired Nodes List X ABA Problem Solved • As a Side Effect of Hazard Pointers • Process 1 has a hazard pointer to Object X. • Process 2 deletes Object X and moves Object X to its Retired Nodes List. • Process 1 can still access Object X. • Hazard Pointer disallows freeing the Memory and hence reuse of the same memory. ABA problem cannot develop. Hazard Pointers Object CS510 - Portland State University

  10. Algorithm – Types and Structures CS510 - Portland State University

  11. Algorithm – Retire Node Routine CS510 - Portland State University

  12. Algorithm – Scan Routine CS510 - Portland State University

  13. Example 1 – Lock Free Queue (Enqueue Operation ) Enqueue(data:DataType){ 1: node <- NewNode(); 2: node.Data <- data; 3: node.Next <- null; while true { 4: t <- Tail; 4a: *hp0 <- t; 4b: if (Tail != t) continue; 5: next <- t.next; 6: if(Tail != t) continue; 7: if(next != null) { CAS(&Tail , t , next);continue; } 8: if CAS(&t.next , null , node) break; } 9: CAS(&Tail , t , node); } Initialising the new node. Point a Hazard Pointer towards t What if t was removed b/w 4 & 4a? Access Hazard !! What if t was freed by some thread ? ABA Hazard !! What if Tail is pointing to some other new node. ABA hazard !! ABA and Access Hazard !! ABA hazard !! CS510 - Portland State University

  14. Example 1 – Lock Free Queue (Dequeue Operation ) Dequeue : DataType(){ while true { 4: h <- Head; 4a: *hp0 <- h; 4b: if (Head != h) continue; t <- Tail; 5: next <- h.next; *hp1 <- next; 6: if(Head != h) continue; 7: if(next == null) return EMPTY; if(h==t){ CAS(&Tail , t ,next);continue; } data <- next.Data ; 8: if CAS(&Head , h , next) break; } 9: RetireNode(h); return data; } Point a Hazard Pointer towards t What if h was removed b/w 4 & 4a? Access Hazard !! What if h was freed by some thread ? ABA Hazard !! What if Head is pointing to some other new node. ABA hazard on t !! Access Hazard using next !! ABA hazard using h !! CS510 - Portland State University

  15. Example 2 – Lock Free Stack Implementation (Push Routine) Push(data:DataType { 1: node <-NewNode(); 2: node.Data <- data ; while true { 3: t <- Top ; 4: node.Next <- t ; 5: if CAS(&Top ,t ,node) return; } } Intialising the new node. Not an Accces Hazard !! Not ABA Hazard as change of Top does not corrupt the Stack. !! No hazard pointers are required !! CS510 - Portland State University

  16. Example 2 – Lock Free Stack Implementation (Pop Routine) Pop() : DataType { While true { 6: t <- Top; 7: if (t == null) return EMPTY; 7a: *hp <- t ; 7b: If (Top != t) continue; 8: next <- t.Next; 9: if CAS (&Top , t ,next) break; } 10: data <- t.Data ; 10a: RetireNode(t) ; 11: return data; } Access Hazard on t !! ABA Hazard on Top !! CS510 - Portland State University

  17. Performance (FIFO Queues) CS510 - Portland State University

  18. Conclusions • Combination with an efficient lock free algorithm could result in truly dynamic objects. • Comparable performance with locks even under no contention. • No need for DCAS. • Wait Free – no loop in scan routine which makes execution time of any thread dependent upon other. • Perfect algorithm for write-rarely-read-many objects. CS510 - Portland State University

More Related