230 likes | 275 Views
Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects. Lock-Free Algorithms. Guarantee that when a thread executes some arbitrary, finite number of steps, some thread (potentially a different thread) makes progress Synthesis CAS-based algorithms LL/SC-based algorithms.
E N D
Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects
Lock-Free Algorithms • Guarantee that when a thread executes some arbitrary, finite number of steps, some thread (potentially a different thread) makes progress • Synthesis • CAS-based algorithms • LL/SC-based algorithms
Problems with Lock-Free Algorithms • In order to provide Lock-Free progress, each thread must have unrestricted access to shared objects at any time • If one thread removes an object while another is reading it, it could result in corrupted data or invalid reference to a null pointer. • Memory Reclamation: how do you allow the memory of removed nodes to be freed while guaranteeing no thread accesses the freed memory? • How can this be done while maintaining the Lock-Free property?
Wait-Free No Special HW/Kernel Support + Memory Reclamation - ABA Problem Hazard Pointers • List of Pointers • Readable by all threads • Writable by one thread • Retirement List
Hazard Pointers: Data Thread 1 . . . Thread N Hazard Ptr List Hazard Ptr List Retirement List Retirement List
Hazard Pointers: Safe Access Thread A Thread B RetList B RetList A Object A
Hazard Pointers: Safe Access Thread A Thread B RetList B RetList A Thread A allocates obj B Object A Object B
Hazard Pointers: Safe Access Thread A Thread B RetList B RetList A Thread B references obj B with a Hazard Pointer Object A Object B
Hazard Pointers: Safe Access Thread A Thread B RetList B RetList A Thread A “Retires” obj B to its Retirement List Object A Object B
Hazard Pointers: Safe Access Thread A Thread B RetList B RetList A Thread B can still safely access obj B, as it will not be freed or reused until no Hazard Pointers point to it. Object A Object B
Hazard Pointers: Safe Access Thread A Thread B RetList B RetList A When will it be freed? Object A Object B
Retirement • Once a threshold R is met (max size of RetList), scan Hazard Pointer Lists for non Null values • Make local list 'plist' consisting of those values • Compare with RetList • For all matches • That means there exists a hazard pointer still pointing to that object • No match? • Then no pointer pointing to object • Free, Reuse, etc.
Retirement t2 … tN t1 RetList len(RetList) == R
Retirement pList t2 … tN t1 RetList len(RetList) == R
Retirement Matches?
Retirement Matches? Yes. -Node1 (P3) -Node2 (P2)
Retirement Matches? Yes. -Node1 (P3) -Node2 (P2) These objects in the RetList are referenced by other threads. Can not free these nodes!
Retirement The Remaining Nodes Can Be Freed or Reused!
Performance Hash Table
Conclusions • Adds Memory Reclamation to lock-free and wait-free algorithms • Retains lock-free/wait-free property if already exists in algorithm • Solves ABA problem • Performs Well • Portable