Data Races and Locks

Similar documents
Common Correctness and Performance Issues

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

CMSC 132: Object-Oriented Programming II

The University of Texas at Arlington

The University of Texas at Arlington

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

Programmazione di sistemi multicore

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 4 Shared-Memory Concurrency & Mutual Exclusion

Dealing with Issues for Interprocess Communication

PROCESS SYNCHRONIZATION

Sharing is the Key. Lecture 25: Parallelism. Canonical Example. Bad Interleavings. Common to have: CS 62 Fall 2016 Kim Bruce & Peter Mawhorter

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

Advanced Threads & Monitor-Style Programming 1/24

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

CS 153 Design of Operating Systems Winter 2016

CSE332: Data Abstractions Lecture 19: Mutual Exclusion and Locking

Introduction to Parallel Programming Part 4 Confronting Race Conditions

Tom Ball Sebastian Burckhardt Madan Musuvathi Microsoft Research

Midterm Exam Amy Murphy 19 March 2003

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019

Page 1. Challenges" Concurrency" CS162 Operating Systems and Systems Programming Lecture 4. Synchronization, Atomic operations, Locks"

+ Today. Lecture 26: Concurrency 3/31/14. n Reading. n Objectives. n Announcements. n P&C Section 7. n Race conditions.

Runtime Atomicity Analysis of Multi-threaded Programs

Program Graph. Lecture 25: Parallelism & Concurrency. Performance. What does it mean?

Java Threads and intrinsic locks

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Synchronization Spinlocks - Semaphores

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

CROWDMARK. Examination Midterm. Spring 2017 CS 350. Closed Book. Page 1 of 30. University of Waterloo CS350 Midterm Examination.

CSE 332: Locks and Deadlocks. Richard Anderson, Steve Seitz Winter 2014

Concurrent & Distributed Systems Supervision Exercises

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

Synchronization SPL/2010 SPL/20 1

Database Management System Prof. D. Janakiram Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

CMSC 330: Organization of Programming Languages

3/25/14. Lecture 25: Concurrency. + Today. n Reading. n P&C Section 6. n Objectives. n Concurrency

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

Operating Systems. Synchronization

CSCI-1200 Data Structures Fall 2009 Lecture 25 Concurrency & Asynchronous Computing

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Intro to Transactions

18-642: Race Conditions

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

CS-537: Midterm Exam (Spring 2001)

Concurrency, Thread. Dongkun Shin, SKKU

Cooperative Concurrency for a Multicore World

CS510 Advanced Topics in Concurrency. Jonathan Walpole

Patterns for Programming Shared Memory

Lecture 8: September 30

CSE 374 Programming Concepts & Tools

More thread safety and Concurrent collections. Nasser Giacaman CompSci 230: Software Design and Construction

More Shared Memory Programming

Midterm Exam Solutions and Grading Guidelines March 3, 1999 CS162 Operating Systems

CHAPTER 6: PROCESS SYNCHRONIZATION

Programmazione di sistemi multicore

G52CON: Concepts of Concurrency

First Midterm Exam September 28, 2017 CS162 Operating Systems

Lecture 5: Synchronization w/locks

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

Process Management And Synchronization

Virtual Machine Design

Synchronization 1. Synchronization

Synchronization Lecture 24 Fall 2018

RACE CONDITIONS AND SYNCHRONIZATION

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

CMSC 433 Spring 2013 Exam 1

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections

Cache Coherence and Atomic Operations in Hardware

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

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

Synchronization in Java

Static Analysis for Safe Concurrency

Synchronization I. Jo, Heeseung

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Process Synchronization - I

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Effective Data-Race Detection for the Kernel

1 of 6 Lecture 7: March 4. CISC 879 Software Support for Multicore Architectures Spring Lecture 7: March 4, 2008

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

Concurrency in Java Prof. Stephen A. Edwards

Threads and Synchronization. Kevin Webb Swarthmore College February 15, 2018

Sharing Objects Ch. 3

Midterm Exam March 3, 1999 CS162 Operating Systems

CSE 332: Data Structures & Parallelism Lecture 18: Race Conditions & Deadlock. Ruth Anderson Autumn 2017

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2002

CPSC 261 Midterm 2 Thursday March 17 th, 2016

CMSC 330: Organization of Programming Languages. Concurrency & Multiprocessing

W4118: concurrency error. Instructor: Junfeng Yang

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

Transcription:

Data Races and Locks Unit 2.a 1

Acknowledgments Authored by Sebastian Burckhardt, MSR Redmond 9/20/2010 2

Concepts Correctness Concept Atomicity Violations Data Races Data-Race-Free Discipline Immutability Isolation Deadlocks Code Concept Lock(myobj) 6/16/2010 3

Part 1 ATOMICITY VIOLATIONS 4

CS15 Atomic, Informally A statement sequence S is atomicif S s effects appear to other threads as if S executed without interruption Atomicity Violation: An error caused by unexpected lack of atomicity. 5

Slide 5 CS15 Also picture? Caitlin Sadowski, 7/20/2010

CS17 Atomicity Violation Example 1: Naïvely Parallelized AntiSocialRobots Robots are moved in parallel Check if destination cell free, then move This sequence is not atomic! Cell may fill between check and move Schedule of events: r1.check, r2.check, r1.move, r2.movcrash r1 r2 r1 r2 6

Slide 6 CS17 Can we additionally add an even more compelling slide about data race consequences? (either here or at slide 10) For example, these are virtual robots, but what about a more dangerous example where they were objects in the real world? Could mention Therac-25 disaster, 2003 blackout, etc. Caitlin Sadowski, 7/14/2010

intbalance = 0; Atomicity Violation Example 2: Bank Account public void Deposit(int amount) { int b = balance; // read current balance b = b + amount; // add amount balance = b; // write balance back } public void TestParallelDeposit() { Parallel.Invoke( () => Deposit(2), () => Deposit(5) ); Assert.AreEqual<int>(7, balance); } Problematic schedule: task 1 reads balance 0 task 2 reads balance 0 task 1 writes balance 2 task 2 writes balance 5 Final balance: 5, not 7! 7

Sometimes, it just looks atomic (1) intbalance = 0; public void DepositOne() { balance++; } public void TestParallelDepositOne() { Parallel.Invoke( () => DepositOne(), () => DepositOne() ); Assert.AreEqual<int>(2, balance); } This is not an atomic operation, because it is internally still executed in three steps: b = balance; b = b + 1; balance = b; 8

Sometimes, it just looks atomic (2) struct RoomPoint { public int X; public int Y;... } RoomPoint p = new RoomPoint(2,3); r.location = p; This is not an atomic assignment, because the structis internally copied in several steps: r.location.x = p.x; r.location.y = p.y; 9

Finding Atomicity Problems Atomicity Problems are often a result of unexpected concurrency Programmer may not have been aware of issue Data races are excellent indicators of potential atomicity problems Allof the atomicity problems shown on previous slides are also data races. First line of defenseagainst atomicity problems: Prevent data races. 10

Part 2 DATA RACES 11

CS5 What is a Data Race? Two concurrentaccesses to a memory location at least one of which is a write. Example: Data race between a read and a write int x = 1; Parallel.Invoke( writes x () => { x = 2; }, () => { System.Console.WriteLine(x); } ); Outcome nondeterministic or worse may print 1 or 2, or arbitrarily bad things on a relaxed memory model reads x 12

Slide 12 CS5 Why is it called a data "race"? Can you also explain with a metaphor? Jaeheon has one that he likes involving multiple people trying to get or examine cookies from the same tray. Caitlin Sadowski, 7/20/2010

CS4 Data Races and Happens-Before Example of a data race with two writes: int x = 1; Parallel.Invoke( () => { x = 2; }, () => { x = 3; } ); System.Console.WriteLine(x); We visualize the ordering of memory accesses with a happens-before graph: There is no path between (write 2 to x) and (write 3 to x), thus they are concurrent, thus they create a data race write 2 to x write 1 to x write 3 to x (note: the read is not in a data race) read x 13

Slide 13 CS4 Another good example to have here would be two threads that both perform (for example) x++, and then ask the class what could happen. Caitlin Sadowski, 7/19/2010

Quiz: Where are the data races? Parallel.For(1,2, i => { x = a[i]; }); Parallel.For(1,2, i => { a[i] = x; }); Parallel.For(1,2, i => { a[i] = a[i+1]; }); 14

Quiz: Where are the data races? Parallel.For(1,2, i => { x = a[i]; }); Parallel.For(1,2, i => { a[i] = x; }); Parallel.For(1,2, i => { a[i] = a[i+1]; }); reads a[0] reads a[1] reads x reads x reads a[2] reads a[3] writes x race writes x writes a[0] writes a[1] writes a[1] writes a[2] Race between two writes. No Race between two reads. Race between a read and a write. 15

Spotting Reads & Writes Sometimes a single statement performs multiple memory accesses When you execute x += y there are actually two reads and one write: reads x reads y writes x When you execute a[i] = x there are actually three reads and one write: reads x reads a reads i writes a[i] 16

Data Races can be hard to spot. Parallel.For(0, 10000, i => {a[i] = new Foo();}) Code looks fine... at first. 17

Data Races can be hard to spot. Parallel.For(0, 10000, i => {a[i] = new Foo();}) Problem: we have to follow calls... even if they look harmless at first (like a constructor). class Foo { private static int counter; private int unique_id; public Foo() { unique_id = counter++; } } 18

In this Course: We Strictly Follow DRF Discipline Data-Race-Free (DRF) Discipline means we avoid ALL data races (no such thing as a benign data race). Already best practice for many, but not all programmers.

DRF Discipline Advantages Avoid issues with memory model Make code more declarative Make Race Detection Tools More Useful Race Detectors are excellent at finding forgotten dependencies unexpected conflicts Atomicity problems 20

Part 3 DATA RACE PREVENTION 21

Avoiding Data Races The three most frequent ways to avoid data races on a variable Make it isolated variable is only ever accessed by one task Make it immutable variable is only ever read Make it synchronized Use a lock to arbitrate concurrent accesses Can use combination over time 22

Labeling memory accesses This loop is race-free because the accessed locations are either isolated or immutable! Parallel.For(1,1000, i => { a[i] = x; }); i: isolated a: immutable a[i]: isolated x: immutable 23

Cool Trick: Avoid Data Races By Encapsulation public class Coordinate { private double x, y; } public Coordinate(double a, double b) { x = a; y = b; } public void GetX() { return x; } public void GetY() { return y; } No data race on xor y isolatedduring construction (no other tasks can access this object yet) immutable once constructor is finished (fields x,yare private, and methods only read from them) 24

Part 4 BASIC LOCKING 25

Using locks We can often restrict accesses to a variable and make it isolated variable is only ever accessed by one task make it immutable variable is only ever read But if we want to decide on-the-fly who gets to access the variable, we make it synchronized Use a lock to arbitrate between concurrent accesses 26

Use lock to arbitrate accesses Example: No data race on variable x, because lock guarantees mutual exclusion! int x = 1; Object mylock = new Object(); Parallel.Invoke( () => { lock(mylock) { x = 2; writes x } }, reads x () => { lock(mylock) { System.Console.WriteLine(x); } }); 27

CS6 Basic Locking Any object can serve as a lock At most one task can have the lock at a time Task acquires/releases the lock C# syntax: lock(myobj) {...code to execute with lock goes here... } called critical section Lock acquired when task enters critical section May have to wait until lock becomes available Lock released when task exits critical section When exiting either normally or due to an uncaught exception 28

Slide 28 CS6 You have lock in quotes here; can you also have motivation for this term? (perhaps on a different slide) Specifically, what is the lock metaphor? Caitlin Sadowski, 7/20/2010

CS7 Lock Semantics Can not enter critical section unless lock is available Task blocks indefinitely if lock is currently held by a different task Several tasks may be blocked & wait for the same lock to become available Tasks may race to acquire a lock.. This is not a data race, but a controlled race. Winner is chosen nondeterministically. Reentrance is o.k. lock (x) { lock (x) { } } is equivalent to lock(x) { } nesting depth is tracked automatically 29

Slide 29 CS7 Can you have a picture here instead of (or in addition to) the text? Caitlin Sadowski, 7/20/2010

Using Locks to Prevent Races This idiom is commonly used to prevent data races on a field x: Choose some lock to protect x Ensure lock held whenever x is accessed Object mylock = new Object(); // use this lock to protect x Parallel.Invoke( ) () => { lock(mylock) { x = 2; } }, () => { w = 2; }, () => { z = 2; }, () => { lock(mylock) { y = x; } }, 30

Often: protect local fields Create a private lock object to protect the fields Guarantees: no data race on field isolated during construction synchronized afterwards public class SafeCounter { private int value; private Object mylock = new Object(); } public SafeCounter() { value = 0; } public void Increment() { lock(mylock) { value = value + 1; } } 31

Simple Locking Policy For each field that may be accessed concurrently: Designate a lock object (in your mind, and with comments) Any object will do (can use thisas well) The same lock object can be used for many fields Every time you access a synchronized object: Make sure the lock is held during the access Guarantees: no data races 32

Part 5 COMMON PROBLEMS WITH LOCKS 33

Locks = Easy Fix for all problems? Suppose you have locked everything, and your code is data-race-free. Is that the end of all trouble? No. Can still have correctness problems: Deadlocks Atomicity problems Can still have performance problems: Lock contention Locking overhead 34

CS10 Pitfall 1: Deadlock Object A = new Object(); Object B = new Object(); Parallel.Invoke( ) () => { lock(a) { lock (B) { ; } } }, () => { lock(b) { lock (A) { ; } } }, Deadlocking schedule: Task 1 acquires A Task 2 acquires B Task 1 tries to acquire B, waits for Task 2 to release B Task 2tries to acquire A, waits for Task 1 to release A Deadlock! Nobody can make progress 35

Slide 35 CS10 Maybe put a more exciting/motivating example here first? I have used one with a lock per bank account, and a trasfer method that involves locking first the "from" account and then the "to" account. Caitlin Sadowski, 7/20/2010

Deadlock = Cycle in Wait-For Graph Visualize deadlocked configuration Each vertex represents a waiting task Each edge x->y represents x is waiting for y Parallel.Invoke( () => { lock(a) { lock (B) { ; } } }, () => { lock(b) { lock (A) { ; } } }, ) task1 Wait for B task2 If there is a cycle: potential deadlock If there is no cycle: safe Wait for A 36

Solution: Consistent Order If locks are acquired in consistent order, no cycle and thus no deadlock! Object A = new Object(); Object B = new Object(); Object C = new Object(); Parallel.Invoke( () => { lock(a) { lock (B) { ; } } }, () => { lock(b) { lock (C) { ; } } }, () => { lock(a) { lock (C) { ; } } }, ) 37

Lock Leveling Simple policy to avoid deadlocks: Assign a level (some arbitrarily chosen number) to each lock (in your mind and in comments). Follow policy: whenever acquiring a lock, its level must be higher than all the levels of the locks already held Effect: no cycles possible 38

Pitfall 2: Atomicity Violation Consider AntiSocial Robots Code: Parallel.Invoke( ) () => { if (IsFree(r1.Destination)) MoveRobot(r1); }, () => { if (IsFree(r2.Destination)) MoveRobot(r2); } Bool IsFree(Cell c) { } lock(c) { return c.robot == null }; Void MoveRobot(Robot r) { } lock(r.destination) { r.destination.robot = r; } Problem: robots may move into same cell! Because lock is released & re-acquired in between checking & moving! 39

Next Lecture Typical Performance Problems Detailed Case Study: Antisocial Robots 40

http://code.msdn.microsoft.com/parextsamples ParallelExtensionsExtras. FastBitmap 6/16/2010 41

Parallel Programming with Microsoft.NET Appendix B (Debugging and Profiling Parallel Applications) 6/16/2010 42