Shared Mutable State SWEN-220

Similar documents
Introduction to Concurrency Principles of Concurrent System Design

Verifying a Compiler for Java Threads

Joeq Analysis Framework. CS 243, Winter

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls

Introduction to Locks. Intrinsic Locks

Concurrency EECS /36

Shared Objects & Mutual Exclusion

Shared Objects & Mutual Exclusion

An Introduction to Multicodes. Ben Stephenson Department of Computer Science University of Western Ontario

Encapsulation in C++

Synchronization SPL/2010 SPL/20 1

Synchronization COMPSCI 386

Java byte code verification

Safety SPL/2010 SPL/20 1

The Java Virtual Machine. CSc 553. Principles of Compilation. 3 : The Java VM. Department of Computer Science University of Arizona

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion

Over-view. CSc Java programs. Java programs. Logging on, and logging o. Slides by Michael Weeks Copyright Unix basics. javac.

JoeQ Framework CS243, Winter 20156

Run-time Program Management. Hwansoo Han

Space Exploration EECS /25

Java Class Loading and Bytecode Verification

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

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

Advances in Programming Languages: Generics, interoperability and implementation

Secure Programming Lecture 16: Race Conditions

02 B The Java Virtual Machine

CMSC 132: Object-Oriented Programming II

The Java Virtual Machine

A Quantitative Analysis of Java Bytecode Sequences

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

Agenda. Highlight issues with multi threaded programming Introduce thread synchronization primitives Introduce thread safe collections

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

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Static Program Analysis

Improving Java Code Performance. Make your Java/Dalvik VM happier

Shared Objects & Mutual Exclusion

Improving Java Performance

Advanced MEIC. (Lesson #18)

Tutorial 3: Code Generation

Let s make some Marc R. Hoffmann Eclipse Summit Europe

What is software testing? Software testing is designing, executing and evaluating test cases in order to detect faults.

Dealing with Issues for Interprocess Communication

The Java Virtual Machine

Hardware-Supported Pointer Detection for common Garbage Collections

Synchronising Threads

Lecture 16: Thread Safety in Java

Lecture 9: Introduction to Monitors

What is a thread anyway?

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Class-Component Testability Analysis

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

Code Generation Introduction

Synchronization in Java

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

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

Compositional Symbolic Execution through Program Specialization

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

The Hardware/Software Interface CSE351 Spring 2015

Project. Threads. Plan for today. Before we begin. Thread. Thread. Minimum submission. Synchronization TSP. Thread synchronization. Any questions?

Dept. of CSE, York Univ. 1

Java Code Coverage Mechanics. by Evgeny Mandrikov at EclipseCon Europe 2017

Compilation 2012 Code Generation

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

Concurrency. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Compiler construction 2009

Code Profiling. CSE260, Computer Science B: Honors Stony Brook University

Java Code Coverage Mechanics Evgeny Mandrikov Marc Hoffmann #JokerConf 2017, Saint-Petersburg

Message Passing. Advanced Operating Systems Tutorial 7

CSC 4181 Handout : JVM

CS263: Runtime Systems Lecture: High-level language virtual machines. Part 1 of 2. Chandra Krintz UCSB Computer Science Department

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

Plan for Today. Safe Programming Languages. What is a secure programming language?

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

Synchronization Possibilities and Features in Java

Concurrency. Fundamentals of Computer Science

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Lecture 8: September 30

Multi-threaded programming in Java

Principles of Software Construction: Objects, Design, and Concurrency. The Perils of Concurrency Can't live with it. Cant live without it.

Threads Synchronization

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

Java Instrumentation for Dynamic Analysis

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

Java Code Coverage Mechanics

JavaSplit. A Portable Distributed Runtime for Java. Michael Factor Assaf Schuster Konstantin Shagin

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

Faculty of Computers & Information Computer Science Department

Oak Intermediate Bytecodes

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

CSCE 314 Programming Languages

Compiling Faster, Compiling Better with Falcon. Iván

Part IV Other Systems: I Java Threads

COMP3131/9102: Programming Languages and Compilers

Synchronization Classic Problems

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

Parallel Programming Languages COMP360

Java Semaphore (Part 1)

Mnemonics Type-Safe Bytecode Generation in Scala

COMP 520 Fall 2009 Virtual machines (1) Virtual machines

Transcription:

Shared Mutable State SWEN-220

The Ultimate Culprit - Shared, Mutable State Most of your development has been in imperative languages. The fundamental operation is assignment to change state. Assignable variables are mutable. May be exposed as public (bad karma). May be exposed via interface methods (medium warm karma). Things get tricky very fast when > 1 thread can invoke a mutating function.

The Ultimate Culprit - Shared, Mutable State Most of your development has been in imperative languages. The fundamental operation is assignment to change state. Assignable variables are mutable. May be exposed as public (bad karma). May be exposed via interface methods (medium warm karma). Things get tricky very fast when > 1 thread can invoke a mutating function. If we call increment() 10,000 times and then call getcount(), what value is returned?

The Ultimate Culprit - Shared, Mutable State Most of your development has been in imperative languages. The fundamental operation is assignment to change state. Assignable variables are mutable. May be exposed as public (bad karma). May be exposed via interface methods (medium warm karma). Things get tricky very fast when > 1 thread can invoke a mutating function. If we call increment() 10,000 times and then call getcount(), what value is returned? If the Counter object is accessed sequentially by one thread then 10,000. If multiple threads are accessing it concurrently then???

The Perils of Interleaving What we humans see as instructions: What the Java VM is actually executing: public Counter(); 1: invokespecial #1 // Method java/lang/object."<init>":()v 4: aload_0 5: iconst_0 6: putfield #2 // Field count:i 9: return public void increment(); 1: dup 2: getfield #2 // Field count:i 5: iconst_1 6: iadd 7: putfield #2 // Field count:i 10: return public int getcount(); 1: getfield #2 // Field count:i 4: ireturn Viewing Java bytecodes from a class file: javap c Counter > Counter.bc

Mind the Critical Section What we humans see as instructions: What the Java VM is actually executing: public Counter(); 1: invokespecial #1 // Method java/lang/object."<init>":()v 4: aload_0 5: iconst_0 6: putfield #2 // Field count:i 9: return Source Code view of the Critical Section public void increment(); 1: dup 2: getfield #2 // Field count:i 5: iconst_1 6: iadd 7: putfield #2 // Field count:i 10: return public int getcount(); 1: getfield #2 // Field count:i 4: ireturn

Mind the Critical Section What we humans see as instructions: What the Java VM is actually executing: public Counter(); 1: invokespecial #1 // Method java/lang/object."<init>":()v 4: aload_0 5: iconst_0 6: putfield #2 // Field count:i 9: return Source Code view of the Critical Section VM view of the Critical Section public void increment(); 1: dup 2: getfield #2 // Field count:i 5: iconst_1 6: iadd 7: putfield #2 // Field count:i 10: return public int getcount(); 1: getfield #2 // Field count:i 4: ireturn

Mind the Critical Section(s) What we humans see as instructions: What the Java VM is actually executing: public Counter(); 1: invokespecial #1 // Method java/lang/object."<init>":()v 4: aload_0 5: iconst_0 6: putfield #2 // Field count:i 9: return Source Code view of the Critical Section VM view of the Critical Section Also a Critical Section! public void increment(); 1: dup 2: getfield #2 // Field count:i 5: iconst_1 6: iadd 7: putfield #2 // Field count:i 10: return public int getcount(); 1: getfield #2 // Field count:i 4: ireturn

The Ultimate Culprit - Shared, Mutable State Most of your development has been in imperative languages. The fundamental operation is assignment to change state. Assignable variables are mutable. May be exposed as public (bad karma). May be exposed via interface methods (medium warm karma). Things get tricky very fast when > 1 thread can invoke a mutating function. Three basic approaches: Provide mechanisms to support controlled access to shared, mutable state. (Locks semaphores) Hide shared state behind sequential access. (Messaging channels) Make things immutable (functional programming) (We ll look at the first two approaches in this course)

The Ultimate Culprit - Shared, Mutable State Provide mechanisms to support controlled access to shared, mutable state. Java uses the keyword synchronized to provide exclusive access to a method or block of code. First thread to enter a synchronized section of code obtains that object s lock, and releases it when exiting. Other threads will become blocked until the lock is released. // Shared Mutable Resource public synchronized void increment() { // Critical Section protected // by the object s lock public synchronized int getcount() {