Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Similar presentations


Presentation on theme: "Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects"— Presentation transcript:

1 Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

2 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

3 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?

4 Hazard Pointers List of Pointers Readable by all threads
Writable by one thread Retirement List Wait-Free No Special HW/Kernel Support + Memory Reclamation - ABA Problem

5 Hazard Pointers: Data Thread 1 . . . Thread N Hazard Ptr List
... pN p0 p1 p2 p3 ... pN Retirement List Retirement List node0 ... nodeR node0 ... nodeR

6 Hazard Pointers: Safe Access
Thread A Thread B RetList B RetList A p0 p1 p2 p3 ... pN p0 p1 p2 p3 ... pN node0 node1 ... node0 node1 ... Object A

7 Hazard Pointers: Safe Access
Thread A Thread B RetList B RetList A p0 p1 p2 p3 ... pN p0 p1 p2 p3 ... pN node0 node1 ... node0 node1 ... Thread A allocates obj B Object A Object B

8 Hazard Pointers: Safe Access
Thread A Thread B RetList B RetList A p0 p1 p2 p3 ... pN p0 p1 p2 p3 ... pN node0 node1 ... node0 node1 ... Thread B references obj B with a Hazard Pointer Object A Object B

9 Hazard Pointers: Safe Access
Thread A Thread B RetList B RetList A p0 p1 p2 p3 ... pN p0 p1 p2 p3 ... pN node0 node1 ... node0 node1 ... Thread A “Retires” obj B to its Retirement List Object A Object B

10 Hazard Pointers: Safe Access
Thread A Thread B RetList B RetList A p0 p1 p2 p3 ... pN p0 p1 p2 p3 ... pN node0 node1 ... node0 node1 ... 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

11 Hazard Pointers: Safe Access
Thread A Thread B RetList B RetList A p0 p1 p2 p3 ... pN p0 p1 p2 p3 ... pN node0 node1 ... node0 node1 ... When will it be freed? Object A Object B

12 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.

13 Retirement t1 t2 … tN p0 p1 p2 p3 ... pN p0 p1 p2 p3 ... pN p0 p1 p2
RetList node0 ... nodeR len(RetList) == R

14 Retirement pList t1 t2 … tN p0 p1 p2 p3 ... pN p0 p1 p2 p3 ... pN p0
RetList node0 ... nodeR len(RetList) == R

15 Retirement Matches? P0 (0x11) P1 (0x0b) Node0 (0x0a) Node1 (0x33) P2
(0xFF) P3 (0x33) P4 (0x20) Matches? Node0 (0x0a) Node1 (0x33) Node2 (0xFF) Node3 (0xF1)

16 Retirement Matches? Yes. -Node1 (P3) -Node2 (P2) P0 (0x11) P1 (0x0b)
(0xFF) P3 (0x33) P4 (0x20) Matches? Yes. -Node1 (P3) -Node2 (P2) Node0 (0x0a) Node1 (0x33) Node2 (0xFF) Node3 (0xF1)

17 Retirement Matches? Yes. -Node1 (P3) -Node2 (P2)
(0x11) P1 (0x0b) P2 (0xFF) P3 (0x33) P4 (0x20) Matches? Yes. -Node1 (P3) -Node2 (P2) Node0 (0x0a) Node1 (0x33) Node2 (0xFF) Node3 (0xF1) These objects in the RetList are referenced by other threads. Can not free these nodes!

18 Retirement The Remaining Nodes Can Be Freed or Reused! P0 (0x11) P1
(0x0b) P2 (0xFF) P3 (0x33) P4 (0x20) The Remaining Nodes Can Be Freed or Reused! Node0 (0x0a) Node1 (0x33) Node2 (0xFF) Node3 (0xF1)

19 Implementing Hazard Pointers

20 Implementing Hazard Pointers

21 Performance Hash Table

22 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


Download ppt "Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects"

Similar presentations


Ads by Google