Whatever can go wrong will go wrong. attributed to Edward A. Murphy. Murphy was an optimist. authors of lock-free programs 3.

Similar documents
Whatever can go wrong will go wrong. attributed to Edward A. Murphy. Murphy was an optimist. authors of lock-free programs LOCK FREE KERNEL

Hazard Pointers. Number of threads unbounded time to check hazard pointers also unbounded! difficult dynamic bookkeeping! thread B - hp1 - hp2

Non-blocking Array-based Algorithms for Stacks and Queues!

On the Design and Implementation of an Efficient Lock-Free Scheduler

On the Design and Implementation of an Efficient Lock-Free Scheduler

Non-blocking Array-based Algorithms for Stacks and Queues. Niloufar Shafiei

LOCKLESS ALGORITHMS. Lockless Algorithms. CAS based algorithms stack order linked list

Concurrent Queues and Stacks. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Overview of Lecture 4. Memory Models, Atomicity & Performance. Ben-Ari Concurrency Model. Dekker s Algorithm 4

Concurrent Queues, Monitors, and the ABA problem. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Fine-grained synchronization & lock-free programming

Design of Concurrent and Distributed Data Structures

Programming Paradigms for Concurrency Lecture 3 Concurrent Objects

Fine-grained synchronization & lock-free data structures

Linked Lists: The Role of Locking. Erez Petrank Technion

30. Parallel Programming IV

Concurrent Preliminaries

Order Is A Lie. Are you sure you know how your code runs?

A Non-Blocking Concurrent Queue Algorithm

Blocking Non-blocking Caveat:

Unit 6: Indeterminate Computation

Lock-Free Techniques for Concurrent Access to Shared Objects

Lock-Free Concurrent Data Structures, CAS and the ABA-Problem

Transactional Memory: Architectural Support for Lock-Free Data Structures Maurice Herlihy and J. Eliot B. Moss ISCA 93

Concurrent Objects and Linearizability

Lecture 10: Avoiding Locks

CS 241 Honors Concurrent Data Structures

The Universality of Consensus

Cache-Aware Lock-Free Queues for Multiple Producers/Consumers and Weak Memory Consistency

1 P age DS & OOPS / UNIT II

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

An Introduction to Parallel Systems

Lecture 9: Multiprocessor OSs & Synchronization. CSC 469H1F Fall 2006 Angela Demke Brown

CSE 613: Parallel Programming. Lecture 17 ( Concurrent Data Structures: Queues and Stacks )

A simple correctness proof of the MCS contention-free lock. Theodore Johnson. Krishna Harathi. University of Florida. Abstract

Modern High-Performance Locking

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Solution: a lock (a/k/a mutex) public: virtual void unlock() =0;

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Intel Thread Building Blocks, Part IV

Advance Operating Systems (CS202) Locks Discussion

8. Fundamental Data Structures

An Almost Non- Blocking Stack

Linked Lists: Locking, Lock-Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

Universality of Consensus. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Allocating memory in a lock-free manner

Lecture Notes on Memory Layout

The Five-Fold Path. Queues & Stacks. Bounded vs Unbounded. Blocking vs Non-Blocking. Concurrent Queues and Stacks. Another Fundamental Problem

Marwan Burelle. Parallel and Concurrent Programming

Lock-free Serializable Transactions

Algorithms for Scalable Synchronization on Shared Memory Multiprocessors by John M. Mellor Crummey Michael L. Scott

Spin Locks and Contention

2.3. ACTIVITY MANAGEMENT

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

The Art of Multiprocessor Programming Copyright 2007 Elsevier Inc. All rights reserved. October 3, 2007 DRAFT COPY

Fast and Lock-Free Concurrent Priority Queues for Multi-Thread Systems

Automatic Memory Reclamation for Lock-Free Data Structures

CMSC 132: Object-Oriented Programming II. Stack and Queue

1 Process Coordination

NON-BLOCKING DATA STRUCTURES AND TRANSACTIONAL MEMORY. Tim Harris, 31 October 2012

Non-Blocking Concurrent FIFO Queues With Single Word Synchronization Primitives

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Lecture 6: Lazy Transactional Memory. Topics: TM semantics and implementation details of lazy TM

Lecture 21: Transactional Memory. Topics: consistency model recap, introduction to transactional memory

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

NON-BLOCKING DATA STRUCTURES AND TRANSACTIONAL MEMORY. Tim Harris, 17 November 2017

Exam Distributed Systems

Boot Procedure. BP (boot processor) Start BIOS Firmware Load A2 Bootfile Initialize modules Module Machine Module Heaps

Programming Languages

DCAS-Based Concurrent Deques

CPSC/ECE 3220 Summer 2017 Exam 2

Spin Locks and Contention. Companion slides for Chapter 7 The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

6.852: Distributed Algorithms Fall, Class 15

Midterm Exam (REGULAR SECTION)

Part 1: Concepts and Hardware- Based Approaches

Application Programming

Lecture Notes on Advanced Garbage Collection

Proving liveness. Alexey Gotsman IMDEA Software Institute

Synchronization SPL/2010 SPL/20 1

Synchronization for Concurrent Tasks

Building Efficient Concurrent Graph Object through Composition of List-based Set

Concurrent Objects. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Agenda. Lecture. Next discussion papers. Bottom-up motivation Shared memory primitives Shared memory synchronization Barriers and locks

Chapter 6: Process Synchronization

Parallel GC. (Chapter 14) Eleanor Ainy December 16 th 2014

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

Final Exam. 12 December 2018, 120 minutes, 26 questions, 100 points

.:: UNIT 4 ::. STACK AND QUEUE

Solo-Valency and the Cost of Coordination

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Lecture 10: Multi-Object Synchronization

Programming Language Seminar Concurrency 2: Lock-free algorithms

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

6.852: Distributed Algorithms Fall, Class 21

Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory

Stanford University Computer Science Department CS 140 Midterm Exam Dawson Engler Winter 1999

Transcription:

Whatever can go wrong will go wrong. attributed to Edward A. Murphy Murphy was an optimist. authors of lock-free programs 3. LOCK FREE KERNEL 309

Literature Maurice Herlihy and Nir Shavit. The Art of Multiprocessor Programming. Morgan Kaufmann Publishers Inc., San Francisco, CA, USA, 2008. Florian Negele. Combining Lock-Free Programming with Cooperative Multitasking for a Portable Multiprocessor Runtime System. ETH-Zürich, 2014. http://dx.doi.org/10.3929/ethz-a-010335528 A substantial part of the following material is based on Florian Negele's Thesis. Florian Negele, Felix Friedrich, Suwon Oh and Bernhard Egger, On the Design and Implementation of an Efficient Lock-Free Scheduler, 19th Workshop on Job Scheduling Strategies for Parallel Processing (JSSPP) 2015. 310

Problems with Locks Deadlock Livelock Starvation Parallelism? Progress Guarantees? Reentrancy? Granularity? Fault Tolerance?

Politelock 312

Lock-Free 313

Definitions Lock-freedom: at least one algorithm makes progress even if other algorithms run concurrently, fail or get suspended. Implies system-wide progress but not freedom from starvation. implies Wait-freedom: each algorithm eventually makes progress. Implies freedom from starvation. 314

Progress Conditions Blocking Non-Blocking Someone make progress Everyone makes progress Deadlock-free Starvation-free Lock-free Wait-free 315

Goals Lock Freedom Portability Progress Guarantees Reentrant Algorithms Hardware Independence Simplicity, Maintenance

Guiding principles 1. Keep things simple 2. Exclusively employ non-blocking algorithms in the system Use implicit cooperative multitasking no virtual memory limits in optimization

Where are the Locks in the Kernel? Scheduling Queues / Heaps object header PP P P ready queues array NIL NIL NIL P P P Memory Management 318

atomic CAS (again) Compare old with data at memory location If and only if data at memory equals old overwrite data with new int CAS (memref a, int old, int new) previous = mem[a]; if (old == previous) Mem[a] = new; return previous; Return previous memory value CAS is implemented wait-free(!) by hardware. Parallel Programming SS 2015 319

Memory Model for Lockfree Active Oberon Only two rules 1. Data shared between two or more activities at the same time has to be protected using exclusive blocks unless the data is read or modified using the compare-and-swap operation 2. Changes to shared data visible to other activities after leaving an exclusive block or executing a compare-and-swap operation. Implementations are free to reorder all other memory accesses as long as their effect equals a sequential execution within a single activity. 320

Inbuilt CAS CAS instruction as statement of the language PROCEDURE CAS(VAR variable, old, new: BaseType): BaseType Operation executed atomically, result visible instantaneously to other processes CAS(variable, x, x) constitutes an atomic read Compiler required to implement CAS as a synchronisation barrier Portability, even for non-blocking algorithms Consistent view on shared data, even for systems that represent words using bytes 321

Simple Example: Non-blocking counter PROCEDURE Increment(VAR counter: LONGINT): LONGINT; VAR previous, value: LONGINT; BEGIN REPEAT previous := CAS(counter,0,0); value := CAS(counter, previous, previous + 1); UNTIL value = previous; return previous; END Increment; 322

Lock-Free Programming Performance of CAS on the H/W level, CAS triggers a memory barrier performance suffers with increasing number of contenders to the same variable Successful CAS Operations [10 6 ] 6 5 4 3 2 1 4 8 12 16 20 24 28 32 #Processors

CAS with backoff Successful CAS Operations [10 6 ] 6 5 4 constant backoff with 10 6 iterations 10 5 iterations 10 4 iterations 10 3 iterations 3 2 1 4 8 12 16 20 24 28 32 #Processors LOOP value := CAS (counter, 0, 0); IF CAS (counter, v, v+1) = v THEN EXIT END; CPU.Backoff; END; 324

Stack Node = POINTER TO RECORD item: Object; next: Node; END; Stack = OBJECT VAR top: Node; PROCEDURE Pop(VAR head: Node): BOOLEAN; PROCEDURE Push(head: Node); END; top item next item next item next NIL 325

Stack -- Blocking PROCEDURE Push(node: Node): BOOLEAN; BEGIN{EXCLUSIVE} node.next := top; top := node; END Push; PROCEDURE Pop(VAR head: Node): BOOLEAN; VAR next: Node; BEGIN{EXCLUSIVE} head := top; IF head = NIL THEN RETURN FALSE ELSE top := head.next; RETURN TRUE; END; END Pop; top item next item next item next NIL 326

Stack -- Lockfree PROCEDURE Pop(VAR head: Node): BOOLEAN; VAR next: Node; BEGIN LOOP head := CAS(top, NIL, NIL); IF head = NIL THEN RETURN FALSE END; next := CAS(head.next, NIL, NIL); IF CAS(top, head, next) = head THEN RETURN TRUE END; CPU.Backoff END; END Pop; head top next A B C NIL 327

Stack -- Lockfree PROCEDURE Push(new: Node); BEGIN LOOP head := CAS(top, NIL, NIL); CAS(new.next, new.next, head); IF CAS(top, head, new) = head THEN EXIT END; CPU.Backoff; END; END Push; new top head A B C NIL 328

Node Reuse Assume we do not want to allocate a new node for each Push and maintain a Node-pool instead. Does this work? NO! WHY NOT? 329

ABA Problem Thread X in the middle of pop: after read but before CAS Thread Y pops A Thread Z pushes B Pool Thread Z' pushes A Thread X completes pop head Pool A head A top A top A top B top B top B next next NIL NIL NIL NIL NIL time

The ABA-Problem "The ABA problem... occurs when one activity fails to recognise that a single memory location was modified temporarily by another activity and therefore erroneously assumes that the overal state has not been changed." X observes Variable V as A meanwhile V changes to B..... and back to A X observes A again and assumes the state is unchanged A B A A time 331

How to solve the ABA problem? DCAS (double compare and swap) not available on most platforms Hardware transactional memory not available on most platforms memory restrictions Garbage Collection Pointer Tagging Hazard Pointers relies on the existence of a GC impossible to use in the inner of a runtime kernel can you implement a lock-free garbage collector relying on garbage collection? does not cure the problem, rather delay it can be practical 332

Pointer Tagging ABA problem usually occurs with CAS on pointers Aligned addresses (values of pointers) make some bits available for pointer tagging. Example: pointer aligned modulo 32 5 bits available for tagging MSB... X X X X X X X X 0 0 0 0 0 Each time a pointer is stored in a data structure, the tag is increased by one. Access to a data structure via address x x mod 32 This makes the ABA problem very much less probable because now 32 versions of each pointer exist. 333

Hazard Pointers The ABA problem stems from reuse of a pointer P that has been read by some thread X but not yet written with CAS by the same thread. Modification takes place meanwhile by some other thread Y. Idea to solve: Before X reads P, it marks it hazarduous by entering it in a threaddedicated slot of the n (n= number threads) slots of an array associated with the data structure (e.g. the stack) When finished (after the CAS), process X removes P from the array Before a process Y tries to reuse P, it checks all entries of the hazard array 334

Unbounded Queue (FIFO) item item item item item item first last 335

Enqueue case last!= NIL item item item item item item 1 new first 2 last case last = NIL new 1 first 2 last 336

Dequeue last!= first item item item item item item first 1 last last == first item 1 2 first last 337

Naive Approach Enqueue (q, new) REPEAT last := CAS(q.last, NIL, NIL); e1 UNTIL CAS(q.last, last, new) = last; IF last!= NIL THEN e2 CAS(last.next, NIL, new); ELSE e3 CAS(q.first, NIL, new); END Dequeue (q) REPEAT first= CAS(q.first, null, null); d1 IF first = NIL THEN RETURN NIL END; next = CAS(first.next, NIL,NIL) d2 UNTIL CAS(q.first, first, next) = first; IF next == NIL THEN d3 CAS(q.last, first, NIL); END A B A A B A first e1 last first last first last first + e3 e1 + e2 d2 + d3 d2 last 338

Scenario Process P enqueues A Process Q dequeues A A A first last first last first last first last initial P: Q: e1 d1 e3 P: 339

Scenario Process P enqueues A Process Q dequeues B B A B A B A first last first last first last first last initial P: e1 Q: d2 P: e2 340

Analysis The problem is that enqueue and dequeue do under some circumstances have to update several pointers at once [first, last, next] The transient inconsistency can lead to permanent data structure corruption Solutions to this particular problem are not easy to find if no double compare and swap (or similar) is available Need another approach: Decouple enqueue and dequeue with a sentinel. A consequence is that the queue cannot be in-place. 341

Queues with Sentinel sentinel 1 2 3 item S next A B C Queue empty: Queue nonempty: Invariants: first = last first # last first # NIL last # NIL first last 342

Node Reuse simple idea: link from node to item and from item to node 2 B 343

Enqueue and Dequeue with Sentinel 1 2 3 1 2 S A B C next S A B first last first last Item enqueued together with associated node. A becomes the new sentinel. S associated with free item. 344

Enqueue 2 3 PROCEDURE Enqueue- (item: Item; VAR queue: Queue); VAR node, last, next: Node; BEGIN node := Allocate(); node.item := Item: LOOP last := CAS (queue.last, NIL, NIL); next := CAS (last.next, NIL, node); IF next = NIL THEN EXIT END; Set last node's next pointer IF CAS (queue.last, last, next) # last THEN CPU.Backoff END; END; ASSERT (CAS (queue.last, last, node) # NIL); END Enqueue; If setting last pointer failed, then help other processes to update last node Progress guarantee B last Set last node, can fail but then others have already helped C 345

Dequeue 1 2 PROCEDURE Dequeue- (VAR item: Item; VAR queue: Queue): BOOLEAN; VAR first, next, last: Node; BEGIN LOOP first := CAS (queue.first, NIL, NIL); next := CAS (first.next, NIL, NIL); IF next = NIL THEN RETURN FALSE END; last := CAS (queue.last, first, next); item := next.item; IF CAS (queue.first, first, next) = first THEN EXIT END; CPU.Backoff; END; item.node := first; RETURN TRUE; END Dequeue; Remove potential inconsistency, help other processes to set last pointer set first pointer first associate node with first S A B last 346

ABA Problems of unbounded lock-free queues unboundedness dynamic memory allocation is inevitable if the memory system is not lock-free, we are back to square 1 reusing nodes to avoid memory issues causes the ABA problem (where?!) Employ Hazard Pointers now. 347