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 p0 p1 p2 p3... pN p0 p1 p2 p3... pN node0... nodeR node0... nodeR Retirement List Hazard Ptr List Thread 1... Thread N

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

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

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

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

10 Hazard Pointers: Safe Access p0 p1 p2 p3... pN p0 p1 p2 p3... pN Thread AThread B node0 node1... node0 node1... RetList A RetList B Object AObject B Thread B can still safely access obj B, as it will not be freed or reused until no Hazard Pointers point to it.

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

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 p0 p1 p2 p3... pN p0 p1 p2 p3... pN node0... nodeR RetList p0 p1 p2 p3... pN p0 p1 p2 p3... pN len(RetList) == R t1 t2 … tN

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

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

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

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

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

19 Implementing Hazard Pointers

20

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