Download presentation
Presentation is loading. Please wait.
1
CS510 - Portland State University
Hazard Pointers: Safe memory Reclamation of Lock Free Objects Maged M. Michael Presenter: Tanu Jain 2 December 2018 CS510 - Portland State University
2
CS510 - Portland State University
Agenda Problem Prior Work Solution and Methodology used. Examples Performance Conclusions 2 December 2018 CS510 - Portland State University
3
CS510 - Portland State University
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. Unsafe memory removal may lead to access violation errors or wrong results or object corruption. Cannot let that happen. 2 December 2018 CS510 - Portland State University
4
CS510 - Portland State University
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. 2 December 2018 CS510 - Portland State University
5
CS510 - Portland State University
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 !! ABA problem in case of “ a program that moves dynamic nodes back and forth between two lists”. GC fails in this case Efficient performance because there is an upper bound on number of nodes not available for reuse. No.of threads * Retired Nodes per thread. 2 December 2018 CS510 - Portland State University
6
Solution – Hazard Pointers
Thread … 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 2 December 2018 CS510 - Portland State University
7
Solution – Hazard Pointers (Contd.)
RetNod 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 2 December 2018 CS510 - Portland State University
8
CS510 - Portland State University
ABA Problem Process 1 Process 2 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 Pointer P Y X Object 2 December 2018 CS510 - Portland State University
9
CS510 - Portland State University
ABA Problem Solved Process 1 Process 2 Retired Nodes List 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 X Object 2 December 2018 CS510 - Portland State University
10
Algorithm – Types and Structures
2 December 2018 CS510 - Portland State University
11
Algorithm – Retire Node Routine
2 December 2018 CS510 - Portland State University
12
Algorithm – Scan Routine
2 December 2018 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 !! 2 December 2018 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 !! 2 December 2018 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 !! 2 December 2018 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 !! 2 December 2018 CS510 - Portland State University
17
Performance (FIFO Queues)
2 December 2018 CS510 - Portland State University
18
CS510 - Portland State University
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. 2 December 2018 CS510 - Portland State University
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.