Java Memory Model. Jian Cao. Department of Electrical and Computer Engineering Rice University. Sep 22, 2016

Similar documents
The Java Memory Model

The Java Memory Model

The Java Memory Model

Foundations of the C++ Concurrency Memory Model

The Java Memory Model

Threads and Locks. Chapter Introduction Locks

Memory Consistency Models: Convergence At Last!

The New Java Technology Memory Model

JSR-133: Java TM Memory Model and Thread Specification

Synchronization SPL/2010 SPL/20 1

Threads and Java Memory Model

New description of the Unified Memory Model Proposal for Java

Programming Language Memory Models: What do Shared Variables Mean?

Final Field Semantics

C++ Memory Model. Don t believe everything you read (from shared memory)

RELAXED CONSISTENCY 1

A unified machine-checked model for multithreaded Java

Threads Questions Important Questions

CMSC 433 Programming Language Technologies and Paradigms. Synchronization

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II)

CMSC 132: Object-Oriented Programming II

Relaxed Memory-Consistency Models

Potential violations of Serializability: Example 1

Sharing Objects Ch. 3

Memory model for multithreaded C++: August 2005 status update

11/19/2013. Imperative programs

CS510 Advanced Topics in Concurrency. Jonathan Walpole

Multicore Programming Java Memory Model

Safe Optimisations for Shared-Memory Concurrent Programs. Tomer Raz

CSE 160 Lecture 7. C++11 threads C++11 memory model

Typed Assembly Language for Implementing OS Kernels in SMP/Multi-Core Environments with Interrupts

Weak memory models. Mai Thuong Tran. PMA Group, University of Oslo, Norway. 31 Oct. 2014

Audience. Revising the Java Thread/Memory Model. Java Thread Specification. Revising the Thread Spec. Proposed Changes. When s the JSR?

Shared Memory Programming with OpenMP. Lecture 8: Memory model, flush and atomics

MultiJav: A Distributed Shared Memory System Based on Multiple Java Virtual Machines. MultiJav: Introduction

CSE 374 Programming Concepts & Tools

Advanced MEIC. (Lesson #18)

Relaxed Memory-Consistency Models

7/6/2015. Motivation & examples Threads, shared memory, & synchronization. Imperative programs

NOW Handout Page 1. Memory Consistency Model. Background for Debate on Memory Consistency Models. Multiprogrammed Uniprocessor Mem.

Motivation & examples Threads, shared memory, & synchronization

Review of last lecture. Peer Quiz. DPHPC Overview. Goals of this lecture. Lock-based queue

High-level languages

Finding Concurrency Bugs in Java

Safety SPL/2010 SPL/20 1

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

Software Speculative Multithreading for Java

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

Threads Cannot Be Implemented As a Library

Hardware Memory Models: x86-tso

Memory model for multithreaded C++: Issues

Memory Consistency Models

Lecture 13: Memory Consistency. + a Course-So-Far Review. Parallel Computer Architecture and Programming CMU , Spring 2013

Quis Custodiet Ipsos Custodes The Java memory model

Java: Pitfalls and Strategies

Shared Memory Consistency Models: A Tutorial

Models of concurrency & synchronization algorithms

ANALYZING THREADS FOR SHARED MEMORY CONSISTENCY BY ZEHRA NOMAN SURA

Relaxed Memory-Consistency Models

Discussion CSE 224. Week 4

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

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

Advanced Operating Systems (CS 202)

Dealing with Issues for Interprocess Communication

Concurrent Programming Lecture 10

CMSC 330: Organization of Programming Languages

Language- Level Memory Models

Lecture 32: Volatile variables, Java memory model

C++ Memory Model Tutorial

CSE332: Data Abstractions Lecture 19: Mutual Exclusion and Locking

JRF-E: using model checking to give advice on eliminating memory model-related bugs

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

Declarative semantics for concurrency. 28 August 2017

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

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

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

The Java Memory Model: a Formal Explanation 1

CS 153 Design of Operating Systems Winter 2016

The Java Memory Model

A Uniform Transactional Execution Environment for Java

Review of last lecture. Goals of this lecture. DPHPC Overview. Lock-based queue. Lock-based queue

Advanced concurrent programming in Java Shared objects

Distributed Algorithms: A Case Study of the Java Memory Model

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

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

Shared Memory Consistency Models: A Tutorial

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

Recent Advances in Memory Consistency Models for Hardware Shared Memory Systems

Lecture 7: Transactional Memory Intro. Topics: introduction to transactional memory, lazy implementation

Lecture 8: September 30

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

Reasoning About The Implementations Of Concurrency Abstractions On x86-tso. By Scott Owens, University of Cambridge.

Atom-Aid: Detecting and Surviving Atomicity Violations

From IMP to Java. Andreas Lochbihler. parts based on work by Gerwin Klein and Tobias Nipkow ETH Zurich

A Parameterized Type System for Race-Free Java Programs

Overview: Memory Consistency

Lecture 13 The C++ Memory model Synchronization variables Implementing synchronization

New Programming Abstractions for Concurrency. Torvald Riegel Red Hat 12/04/05

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

ISSN: Monica Gahlyan et al, International Journal of Computer Science & Communication Networks,Vol 3(3),

Transcription:

Java Memory Model Jian Cao Department of Electrical and Computer Engineering Rice University Sep 22, 2016

Content Introduction Java synchronization mechanism Double-checked locking Out-of-Thin-Air violation and Happens-before Causality and the Java memory model 2

Today s References Manson, Jeremy, William Pugh, and Sarita V. Adve. The Java memory model. Vol. 40. No. 1. ACM, 2005. https://en.wikipedia.org/wiki/double-checked_locking http://www.cs.umd.edu/~pugh/java/memorymodel/doubl echeckedlocking.html http://winterbe.com/posts/2015/04/30/java8-concurrencytutorial-synchronized-locks-examples/ 3

Why a Memory Model for Java? For a high-level programming languages like Java, the memory model determines: The transformations the compiler may apply to a program when producing bytecode. The transformations that a virtual machine may apply to bytecode when producing native code. The optimizations that hardware may perform on the native code. 4

Java Memory Model Memory models for languages are hard. First Java memory model had several shortcomings: A class of executions containing data races were either allowed, or were only prohibited by enforcing traditional control and data dependences. Resulted in safety and security issues. Today s paper: second memory model for Java. 5

Today s Java Memory Model Guarantees sequential consistency to data-race-free programs. The behavior of incorrectly synchronized programs is bound by a well defined notion of causality. Provides balance between safety and security and compiler and hardware optimizations. 6

Synchronizing Access to Objects in Java Multiple threads share a counter Data race: multiple threads call increment() multiple threads read and write to count variable Conflicting access: the accesses to the same shared field, at least one is a write Result: unpredictable outcomes http://image.slidesharecdn.com/testingmultithreadedjavaapplicationsforsynchronizationproblems-140625015712-phpapp01/95/testing-multithreaded- 7 java-applications-for-synchronization-problems-ista-2011-3-638.jpg?cb=1403661489

Synchronization Mechanisms in Java Synchronized methods Synchronized blocks Explicit locking Volatile variables http://image.slidesharecdn.com/testingmultithreadedjavaapplicationsforsynchronizationproblems-140625015712-phpapp01/95/testing-multithreaded-javaapplications-for-synchronization-problems-ista-2011-3-638.jpg?cb=1403661489 8

Objects in Java Every object has an intrinsic lock (or monitor). A thread that needs exclusive and consistent access must: acquire the object's intrinsic lock before accessing them;; release the intrinsic lock when it's done with them. http://www.technology-ebay.de/data/mediapool/intrinsic.gif 9

Implicit Locking: Synchronized Keyword Synchronized method: Synchronized block: In Synchronized block, locks are bound to current thread, and other threads will have to wait when one thread is in. https://www.google.com/search?q=java+synchronization+mechanisms&espv=2&biw=1174&bih=568&source=lnms&tbm=isch&sa=x&ved=0ahuk EwiEyt_ovJ_PAhVLLyYKHeqAC3EQ_AUIBygC#imgrc=78-VgskbOayL-M%3A 10

Explicit Locking: Lock Interface ReentrantLock Acquire Released Only one thread can hold a lock at any given time. A thread waits inside the lock() operation until the lock is available (unlocked) 11

Explicit Locking: Lock Interface ReadWriteLock After the write lock has been released both read tasks are executed in parallel. The read-lock can be held simultaneously by multiple threads as long as no thread holds the write-lock. 12

Java in Practice: Double-checked Locking An efficient method for implementing lazy initialization in a multithreaded environment: When used in a multithreaded context, two or more Helper objects could be allocated. 13

Avoid Data Races with synchronized Mutual exclusion. When one thread is accessing the object, other threads must wait. 14

Avoid Data Races with synchronized The Helper object is initiated, yet the fields inside constructor might not be fully initiated. 15

Another Fix by Nested Synchronization Compiler reorder Actions before monitorexit must be performed before the monitor is released. Actions after monitorexit may be performed before the monitor is released. 16

Problem: Copies of Shared Data The JVM is free to optimize use of variables as it pleases, for example, putting copies in registers. 17

Using Java Volatile Keyword for Shared Data volatile Must update the shared variable for each write Must read the shared variable for each read Example Must happen before Volatile variable Must happen after 18

Double Checked Locking using Volatile A write of a volatile cannot be reordered with respect to any previous read or write;; A read of a volatile cannot be reordered with respect to any following read or write. 19

Avoiding Excess Loads and Fences *result* variable can be put in register When helper is already initialized, the volatile field is only read once, which can improve the method's overall performance by as much as 25 percent. 20

Java Memory Model in Detail 21

Correctly Synchronized Programs Synchronization order: Synchronizes-with: unlock and lock operations synchronize volatile variable: a write to a volatile variable v synchronizes-with all subsequent reads of v by any thread Happens-before: a transitive closure of program order and the synchronization order 22

Correctly Synchronized Programs Data races: Two accesses x and y form a data race in an execution of a program if they are from different threads, the conflict, and they are not ordered by happens-before. Definition: A program is said to be correctly synchronized or data-race-free if and only if all sequentially consistent executions of the program are free of data races. 23

An Example of Incorrect Program 24

An Example of Incorrect Program Incorrectly synchronized: Any sequentially consistent execution of the code contains conflicting accesses to x and y that are not ordered by happens-before. Fix: declare x and y as volatile variables. 25

Another Incorrectly Synchronized Example Thread 1 Thread 2 y = 0;; Lock(L) { y ++;; } Sync edge Unlock(L) Lock(L) { y ++;; } Unlock(L) if (y== ){ execute } Thread 1 Thread 2 y = 0;; Lock(L) { y ++;; } Unlock(L) Sync edge Lock(L) { y ++;; } Unlock(L) if (y == ){ execute } No data races Data races 26

Incorrect Program: Out-of-thin-air Guarantees 27

Incorrect Program: Out-of-thin-air Problem Incorrectly synchronized: Thread 1 could speculatively write the value 42 to y, which would allow Thread 2 to read 42 for y and write it out to x, which would allow Thread 1 to read 42 for x, and justify its original speculative write of 42 for y. 28

Incorrect Program: Out-of-thin-air Guarantees A self-justifying write speculation like that one can: Cause code to misbehave Create serious security violations and needs to be disallowed. For example, this would be a serious problem if the value that was produced out of thin air was a reference to an object that the thread was not supposed to have. 29

Simple Version: Happens-before Model A read r of a variable v is allowed to observe a write w to v if, in the happens-before partial order of the execution: r does not happen-before w a read cannot see a write that happens-after it there is no intervening write w0 to v, ordered by happens before the write w is not overwritten along a happensbefore path 30

Causality and the Java Memory Model Consistent with happens-before model Disallow in Java Memory Model Allow in Java Memory Model 31

Causality and the Java Memory Model Observation: Early execution of an action does not result in an undesirable causal cycle if its occurrence is not dependent on a read returning a value from a data race. Formalizing the *well-behaved* executions: A read that is not yet committed must return the value of a write that is ordered before it by happens-before. 32

Security and Safety Shows that the safety and security properties of Java require prohibiting a class of executions that contain data races and have not been previously characterized. Characterizes speculations that constitutes out-of-thin-air violations, that might lead to severe security issue. 33

Summary: Balances Three Crucial Needs Allows compiler to perform code transformations and optimizations Provides clear and simple programming model for those writing concurrent code For data-race-free programs, allows programmers to reason about their programs using simple semantics of sequential consistency Guarantees that even programs with data races don t violate security Provides a clear definition for program behaviors allowed when executing programs data races: behaviors must be causal;; can t read values that haven t been written or have been overwritten 34

Take Away Messages Major design goal for Java memory model: Provides a balance between security, compiler and hardware optimizations for performance, and usability. Volatile variable: Guarantees the happens-before order. For incorrect programs: Prevents Out-of-Thin-Air violation. 35