![Page 1: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/1.jpg)
Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”
Presentation
Robert T. Bauer
![Page 2: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/2.jpg)
The Problem
• Lock-free approaches scale (with the number of processors) and avoid deadlock issues.
• Lock-free means concurrent access which is problematic for storage reclamation
• Previous papers described lock-free techniques that updated in place or assumed dedicated constant width data; i.e., storage was not collected and reused.
![Page 3: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/3.jpg)
The Constraints
• Detection Property:– Distinguish live objects from garbage
• Reclamation Property:– Reclaim garbage objects’ storage
• Safety Properties:– Cannot reclaim “live” objects– Cannot access reclaimed objects
• Liveness Property:– “Garbage” objects eventually reclaimed
Note: These are more than those identified in the paper
![Page 4: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/4.jpg)
The Lock-Free Idea
• Do all the “work” on the side, but accessible from a pointer
• Use CAS to update, in place, the pointer• Do this in a loop*p_new = new datado {
p_old = p……
} while (!cas(&p, p_old, p_new))
When can this be collected (reclaimed)?
![Page 5: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/5.jpg)
Example: Lock-Free Stack
push(node):do {
t = TOPnodenext =
TOP} until
CAS(&TOP,t,node)
node pop:do {
t = TOPif t == NULL
return NULLnext = tnext
} untilCAS(&TOP,t,next)
return t
![Page 6: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/6.jpg)
ABA Problem• Suppose a list has A B C• Thread X:
t = TOP; …; next = tnext
• Thread Y:N = POP: t = TOP; …; next = tnext; CAS(&TOP, t, next)POP: t = TOP; …; next = tnext; CAS(&TOP, t, next)
List is now just “C”, since A and B have been poppedPUSH(N): t = TOP; Anext = TOP; CAS (&TOP, t, A)
List is now “AC”, since we pushed A
• Thread X continues:cas(&TOP,t,next)
List is now “BC”
• Issue: What if Thread “Y” had reclaimed “B” because it “knew” that it would never use it?
![Page 7: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/7.jpg)
POP With Tags
node pop:do {
<t, tag> = TOPif t == null
return nullnext = tnext
} until CAS(&TOP, <t,tag>, <next,tag+1>)return t
Since tag is monotonic with respect to calls to pop, the A—B—A problem is eliminated as long the number of calls to pop is limited.
![Page 8: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/8.jpg)
Hazard PointerThread 0 1 2 … n Hazard Pointers
Objects
Hazard Pointers identify theobjects that the thread willaccess.
![Page 9: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/9.jpg)
“Releasing” an ObjectThread n
When the “object” is released by thread n,the pointer on the hazard list is removed.
We add the pointer to the object to the“to be released” list.
After the object reference is added to theList we check the length of the list andif it is greater than “R”, we “scan” to see what can be reclaimed.
to be released
![Page 10: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/10.jpg)
Reclaiming Storagehp_1 hp_2 hp_n
Thread n, scansthe hp_i lists foreach thread, if ptr_knot any hp list, thenit can be reclaimed.
ptr_k
![Page 11: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/11.jpg)
Problem 1 of 2
• Problem 1:– Thread X removes node N ptr (count < R)– Thread Y removes node N ptr (count >= R)
• Scan and reclaim N
– Thread X removes node M ptr (count >= R)• Scan and reclaim N
![Page 12: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/12.jpg)
Problem 2 of 2
free
Thread X scans list for “u”.At this “point”, thread Y runsand adds “u”.
Thread Y HP list
Thread Y HP list
u
Since Thread X did not see “M” it will reclaimthe storage. But, Y has “M” on its hazardlist.
![Page 13: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/13.jpg)
Transforming a FIFO Queue: Identifying the hazards
Access hazard because*t may have been removedand reclaimed
ABA hazards
Access and ABA hazard
ABA hazard
Note: Only one hazard pointer is needed, since “t” is the only hazardreference.
![Page 14: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/14.jpg)
Transforming a FIFO Queue: Adding hazard pointers
Protect *t data
This is supposed to makesure that t is “safe” – thatthe “t” protected by hp0 isThe same as Tail
So, hp0 “protects” t onlyduring the time this routineis active; but, it mightprotect it much longer!
![Page 15: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/15.jpg)
Transforming a FIFO Queue: Dequeue
Don’t’ want head to bereclaimed. We willuse it later!
h (head) will end up on the“to be released” list. Notethat “next” is still on thehp list – so I am not surehow another processorcan retire it?
![Page 16: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/16.jpg)
Double Linked List: Something Curious
QuickTime™ and aTIFF (LZW) decompressor
are needed to see this picture.What’s this about?
![Page 17: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/17.jpg)
Performance
QuickTime™ and aTIFF (LZW) decompressor
are needed to see this picture.
Performance of FIFO queue
![Page 18: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/18.jpg)
Performance
QuickTime™ and aTIFF (LZW) decompressor
are needed to see this picture.
Hash Table: Load Factor = 5
![Page 19: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/19.jpg)
Author Notes
• Superior Performance of hazard pointers– operate directly on shared objects without
need for managing locks– read-only operations do not result in writes
other than private hazard pointers– no spinning– progress guaranteed under preemption
![Page 20: Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects”](https://reader031.vdocument.in/reader031/viewer/2022032612/56813392550346895d9aa16f/html5/thumbnails/20.jpg)
Conclusion (mine)
• Paper’s attempt at formalism was not useful
• Idea of hazard pointers is simple, but implementations are broken in one way or another
• Author seems confused about ABA problem and garbage collection