Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects MAGED M. MICHAEL PRESENTED BY NURIT MOSCOVICI ADVANCED TOPICS IN CONCURRENT PROGRAMMING,

Similar presentations


Presentation on theme: "Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects MAGED M. MICHAEL PRESENTED BY NURIT MOSCOVICI ADVANCED TOPICS IN CONCURRENT PROGRAMMING,"— Presentation transcript:

1 Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects MAGED M. MICHAEL PRESENTED BY NURIT MOSCOVICI ADVANCED TOPICS IN CONCURRENT PROGRAMMING, TECHNION, WINTER 2015-16 1

2 Motivation o Many lock-free algorithms dynamically allocate memory o Reclamation is difficult with no Garbage Collection o In lock-free algorithms any thread can operate on any part of the object at any time. 2

3 Memory Reclamation o Reclamation for reuse by algorithm: o May corrupt object (ABA) o Reclamation for return to OS: o May cause violation error 3

4 Memory Reclamation o Reclamation for reuse by algorithm: o May corrupt object (ABA) o Reclamation for return to OS: o May cause violation error 4

5 ABA problem o Common in lock-free algorithms when memory is released and then reused. o Some ABA problems fixed by safe memory reclamation o Common misconception: All ABA can be fixed this way A P1 B P2 P2: Read val=A Write val = B Write val = A 5 Val

6 Example – Lock-Free Queue 6 Access Hazard! ABA Hazard!ABA Hazard! ABA Hazard! Access+ ABA Hazard! ABA Hazard! Structure: Head, Tail Node: {Data, Next}

7 What do we want? o Lock-freedom: tolerate thread delays and failures o Bounded un-reclaimable memory o Enable reuse of memory/return to OS o Easily applicable to existing algorithms o Portability: No need for special HW/kernel support o Efficiency 7

8 Agenda o Previous Solutions o Hazard Pointers: Basic Solution o Correctness o Using Hazard Pointers o Hazard Pointers: Extended Solution o Results 8

9 Previous Solutions 9

10 Non-Blocking Solutions ABA-Prevention Tags o Requires double-width atomic operations o Often does not allow return to OS Lock-Free Reference Counting o Two options: o Use DCAS (mostly unsupported) o Independently CAS pointers and counter non-atomically (hinders reclamation) 10 A12 Cas(&Val,, ) B13 A34

11 Scheduler-Dependent and Blocking Methods 11

12 Hazard Pointers 12

13 Assumptions o For now, assume number of threads is known to be N o Assume each thread has exactly K unsafe references o Unsafe reference: may cause problems if used without validation o Observation: most lock-free algorithms use only a small number of unsafe references per thread. 13

14 General Idea null C B C ABC … D 14

15 null General Idea Using Memory: o Must store references in hazard pointers before accessing memory. Reclaiming Memory: o Check hazard pointers of all other threads o Reclamation is forbidden if node referenced by some hazard pointer. A B null C B C ABC … D AB 15

16 Data Structures o Shared Data: o HPList: Linked list of HPRecords containing hazard pointers o Private (per-thread) Data: o RList: List of nodes the thread has retired from the structure, but has not yet released. AB HPList Head … (N records) C E RList CB 16

17 Algorithm Algorithm consists of two parts: 1. Algorithm for processing retired nodes o Can be used as-is in every lock-free data structure 2. Condition: an invariant that must be upheld when adding hazard pointers to existing code. o Requires understanding of target lock-free data structure o Must be specially adjusted for every such data structure 17

18 Part 1: Retiring a Node 18 AFB HPList Head … (N records) C E RList CB F

19 Scan: Phase 1 o Create temporary local hash table o Iterate over all HPRecs in HPList o Add every non-NULL Hazard Pointer to hash table 19 AFB HPList Head … (N records) C E RList CB F A FB C H G

20 Scan: Phase 2 o Copy Rlist to temporary new local list o Iterate over tempList and search for each node in hash table o Found: return to Rlist until next scan o Not Found: Call function to release/reuse node o Free the temporary hash table and list 20 RList C E F A FB C H G C E F C F

21 Complexity of Retiring a Node 21

22 Complexity of Retiring a Node 22

23 Some Definitions 23 Head

24 Safety 24

25 Some More Definitions 25

26 Condition for correctness 26 What does this give us?

27 Correctness 27

28 A Quick Sketch of Correctness 28

29 Which gives us: 29

30 Using Hazard Pointers 30

31 So, How Do We Apply Hazard Pointers? 31

32 Back to the Lock-Free Queue 32 Access Hazard! ABA Hazard!ABA Hazard! ABA Hazard! Access+ ABA Hazard! ABA Hazard! Structure: Head, Tail Node: {Data, Next}

33 What About Dequeue? 33 ABA Hazard!ABA Hazard! Access Hazard! ABA Hazard!

34 What About Dequeue? 34 ABA Hazard!ABA Hazard! Access Hazard! ABA Hazard!

35 What About Dequeue? 35 ABA Hazard!ABA Hazard! Access Hazard! ABA Hazard! Access Hazard!

36 What Now? 36

37 Applying Hazard Pointers to Enqueue 37 ABA Hazard!ABA Hazard!

38 Applying Hazard Pointers to Dequeue 38 ABA Hazard!ABA Hazard!

39 Hazard Pointer Application o Hazard Pointer algorithm can be applied by algorithm designers o Cannot easily be done automatically (by a compiler) o Requires a good understanding of the semantics of the algorithm 39

40 List-Based Set 40 ABA Hazard!ABA Hazard!

41 List-Based Set: Adding Hazard Pointers 41 ABA Hazard!ABA Hazard! Assume prev holds head, and so is safe here.

42 List-Based Set 42 ABA Hazard!ABA Hazard! Search( head, key ) return Find( head, key )

43 Extended Version 43

44 Unknown Number of Threads o Allow threads to push new records to global list o Can also be used to make K dynamic o Wait free, as number of threads is finite* 44 HPList Head null

45 Unknown Number of Threads o Allow threads to push new records to global list o Can also be used to make K dynamic o Wait free, as number of threads is finite* 45 HPList Head null

46 Unknown Number of Threads o Allow threads to push new records to global list o Can also be used to make K dynamic o Wait free, as number of threads is finite* 46 HPList Head null

47 Dynamic Join and Leave of Threads Allow reuse of HPs : o Add active indicator flag to each HP record o Acquire inactive record using TAS on flag o Only allocate new record if all existing records are active o Save retired list in record, so retiring thread can pass on it’s un-reclaimed nodes 47 HPList Head null

48 Guarantee Freeing of All Nodes 48 HPList Head null

49 Results 49

50 Results o Same as lock-based implementation under no contention o Better than or equal to ABA-tags and reference counting 50

51 Questions? 51


Download ppt "Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects MAGED M. MICHAEL PRESENTED BY NURIT MOSCOVICI ADVANCED TOPICS IN CONCURRENT PROGRAMMING,"

Similar presentations


Ads by Google