Hazard Pointers: Safe Memory Reclamation of Lock-Free Objects

Slides:



Advertisements
Similar presentations
Data Structures ADT List
Advertisements

CHP-5 LinkedList.
Wait-Free Queues with Multiple Enqueuers and Dequeuers
Ceng-112 Data Structures I Chapter 5 Queues.
Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
CS252: Systems Programming Ninghui Li Program Interview Questions.
6. Deadlocks 6.1 Deadlocks with Reusable and Consumable Resources
M180: Data Structures & Algorithms in Java
CS510 – Advanced Operating Systems 1 The Synergy Between Non-blocking Synchronization and Operating System Structure By Michael Greenwald and David Cheriton.
Memory management.
Maged M. Michael, “Hazard Pointers: Safe Memory Reclamation for Lock- Free Objects” Presentation Robert T. Bauer.
Wait-Free Reference Counting and Memory Management Håkan Sundell, Ph.D.
Data Structures: A Pseudocode Approach with C
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09.
CS510 Concurrent Systems Class 2 A Lock-Free Multiprocessor OS Kernel.
Simple, Fast and Practical Non- Blocking and Blocking Concurrent Queue Algorithms Maged M. Michael & Michael L. Scott Presented by Ahmed Badran.
Computer Laboratory Practical non-blocking linked lists Tim Harris Computer Laboratory.
Practical and Lock-Free Doubly Linked Lists Håkan Sundell Philippas Tsigas.
CS510 Concurrent Systems Jonathan Walpole. A Lock-Free Multiprocessor OS Kernel.
28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.
1 © R. Guerraoui Regular register algorithms R. Guerraoui Distributed Programming Laboratory lpdwww.epfl.ch.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects Maged M. Michael Presented by Abdulai Sei.
The read-copy-update mechanism for supporting real-time applications on shared-memory multiprocessor systems with Linux Guniguntala et al.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Files and file allocation.
Memory Management -Memory allocation -Garbage collection.
Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money.
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
An algorithm of Lock-free extensible hash table Yi Feng.
Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects MAGED M. MICHAEL PRESENTED BY NURIT MOSCOVICI ADVANCED TOPICS IN CONCURRENT PROGRAMMING,
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
Read-Copy-Update Synchronization in the Linux Kernel 1 David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
LINKED LISTS.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
File-System Management
Memory Management.
Processes and threads.
Day 03 Introduction to C.
Hazard Pointers C++ Memory Ordering Issues
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Håkan Sundell Philippas Tsigas
Stacks and Queues.
A Lock-Free Algorithm for Concurrent Bags
Dynamic Memory CSCE 121 J. Michael Moore.
CS510 Concurrent Systems Jonathan Walpole.
Waves!.
This pointer, Dynamic memory allocation, Constructors and Destructor
Anders Gidenstam Håkan Sundell Philippas Tsigas
Dynamic Memory Allocation
Concurrent Data Structures Concurrent Algorithms 2016
CS510 Concurrent Systems Jonathan Walpole.
CS510 - Portland State University
B- Trees D. Frey with apologies to Tom Anastasio
B- Trees D. Frey with apologies to Tom Anastasio
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CS510 Advanced Topics in Concurrency
CS510 Concurrent Systems Jonathan Walpole.
LINKED LIST Dr. T. Kokilavani Assistant Professor
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Hazard Pointers: Safe Memory Reclamation of Lock-Free Objects Maged M. Michael

The Problem Lock-free algorithms assume that threads can operate on any object at any time Freeing memory could break this assumption How can we free memory of deleted nodes in a safe and lock-free manner?

Prior Work No reuse Recycling of constant static storage Type safe memory Reference counts with DCAS or CAS2

Goals of Hazard Pointers Readers inform writers of references they hold Bound the number of unreclaimed objects Tolerance for thread failures (wait-free) No special hardware of kernel support needed Suitable for user or kernel use

Hazard Pointers Each thread has a set of pointers that only it writes, but other threads can read Each pointer is either null or points to an object the thread is currently using Before accessing any dynamic object a thread must first store a pointer to it in its list Thread 0 1 2 … n Objects

Retired List Each thread also has a list of nodes it has deleted and would like to free When the length of this list reaches R, the thread scans the hazard pointer lists of all other threads to see if its safe to free any of its nodes. RetNod HP – Thread 2 HP – Thread 3 Thread 1

The ABA Problem Process 1 has a copy of a pointer to Object x Process 2 deletes object x, frees its memory, and reuses the same memory to create a new object y Process 1 applies CAS on the address of object x which is the same as that of object y The CAS succeeds but the object has changed! Process 1 Process 2 Pointer P Pointer P Y X Object

ABA Problem Solved Process 1 has a hazard pointer to object x Process 2 deletes object x and moves object x to its retired nodes list Process 1 can still safely access object x Hazard Pointer disallows freeing the memory and hence reuse of the same memory The ABA problem cannot occur with Hazard Pointers Process 1 Process 2 Retired Nodes List Hazard Pointers X Object

Hazard Pointer Types

Retiring Nodes

Scanning Hazard Pointer Lists and Freeing Memory

Lock Free Enqueue Enqueue(data:DataType){ 1: node <- NewNode(); 2: node.Data <- data; 3: node.Next <- null; while true { 4: t <- Tail; 4a: *hp0 <- t; 4b: if (Tail != t) continue; 5: next <- t.next; 6: if(Tail != t) continue; 7: if(next != null) { CAS(&Tail , t , next);continue; } 8: if CAS(&t.next , null , node) break; 9: CAS(&Tail , t , node); Initialising the new node. Point a Hazard Pointer towards t What if t was removed b/w 4 & 4a? Access Hazard !! What if t was freed by some thread ? ABA Hazard !! What if Tail is pointing to some other new node. ABA hazard !! ABA and Access Hazard !!

Lock Free Dequeue Dequeue : DataType(){ while true { 4: h <- Head; 4a: *hp0 <- h; 4b: if (Head != h) continue; t <- Tail; 5: next <- h.next; *hp1 <- next; 6: if(Head != h) continue; 7: if(next == null) return EMPTY; if(h==t){ CAS(&Tail , t ,next);continue; } data <- next.Data ; 8: if CAS(&Head , h , next) break; 9: RetireNode(h); return data; Point a Hazard Pointer towards t What if h was removed b/w 4 & 4a? Access Hazard !! What if h was freed by some thread ? ABA Hazard !! What if Head is pointing to some other new node. ABA hazard on t !! Access Hazard using next !! ABA hazard using h !!

Lock Free Stack Push 1: node <-NewNode(); Intialising the new node Push(data:DataType { 1: node <-NewNode(); 2: node.Data <- data ; while true { 3: t <- Top ; 4: node.Next <- t ; 5: if CAS(&Top ,t ,node) return; } Intialising the new node Not an Accces Hazard !! Not ABA Hazard as change of Top does not corrupt the Stack. !!

Lock Free Stack Pop Pop() : DataType { While true { 6: t <- Top; 7: if (t == null) return EMPTY; 7a: *hp <- t ; 7b: If (Top != t) continue; 8: next <- t.Next; 9: if CAS (&Top , t ,next) break; } 10: data <- t.Data ; 10a: RetireNode(t) ; 11: return data; Access Hazard on t !! ABA Hazard on Top !!

Performance (Queues)

Conclusions Safe memory reclamation Solves ABA problem Wait-free if the original algorithm is wait-free Reasonable performance? readers must now write and these writes require memory barriers!