Exceptions and side-effects in atomic blocks Tim Harris.

Slides:



Advertisements
Similar presentations
Copyright 2008 Sun Microsystems, Inc Better Expressiveness for HTM using Split Hardware Transactions Yossi Lev Brown University & Sun Microsystems Laboratories.
Advertisements

Concurrent programming for dummies (and smart people too) Tim Harris & Keir Fraser.
Software Transactional Memory and Conditional Critical Regions Word-Based Systems.
Written by: Dr. JJ Shepherd
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.
Road Map Introduction to object oriented programming. Classes
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
Run-Time Storage Organization
Run time vs. Compile time
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Language Support for Lightweight transactions Tim Harris & Keir Fraser Presented by Narayanan Sundaram 04/28/2008.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Ranga Rodrigo. Class is central to object oriented programming.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Java and C++, The Difference An introduction Unit - 00.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Problems with Send and Receive Low level –programmer is engaged in I/O –server often not modular –takes 2 calls to get what you want (send, followed by.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
Exceptions Handling Exceptionally Sticky Problems.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Introduction to Exception Handling and Defensive Programming.
Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.
C++ Memory Overview 4 major memory segments Key differences from Java
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
COP4020 Programming Languages Exception Handling Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Optimistic Design CDP 1. Guarded Methods Do something based on the fact that one or more objects have particular states Make a set of purchases assuming.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Handling Exceptionally Sticky Problems
Java Programming Language
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
COP4020 Programming Languages
Chapter 7: User-Defined Functions II
CSC 143 Java Errors and Exceptions.
Handling Exceptionally Sticky Problems
Computer Science 340 Software Design & Testing
References Revisted (Ch 5)
CMSC 202 Exceptions.
Defensive Programming
Presentation transcript:

Exceptions and side-effects in atomic blocks Tim Harris

Atomic blocks void put(int val) { atomic (!this.full) { this.full = true; this.val = val; } int get() { int result; atomic (this.full) { this.full = false; return this.val; }  Basic syntax: ‘ atomic (cond) { statements; } ’  Block until the condition is true…  …then execute the statements (doing all this as a single atomic step)  The statements can access fields & local variables, invoke methods, instantiate objects etc.

Atomic blocks (ii) General principle in single-threaded code:  atomic (E) { S } behaves the same as {E;S}  after blocking until a state in which E is known to yield true The compiler and VM are responsible for making this work well in multi-threaded systems:  first implementation used a software transactional memory  memory accesses in E and S remain thread-private  most non-conflicting operations can commit in parallel

Atomic blocks (iii)  A motivating (and stylized!) example: void serverLoop(ServerSocket s) { while (true) { Socket c = s.acceptConnection(); Thread t = new Thread() { public void run() { atomic { dealWithClient(c); }  Try to deal with non-conflicting client requests concurrently  If something goes wrong (e.g. client triggers DivisionByZero) then try to contain failure  atomic means “all or nothing”

Aborting atomic blocks  What about this code to move an object between collections: boolean move(Collection s, Collection d, Object o) { atomic { if (!s.remove(o)) { return false; } else { if (d.insert(o)) { return true; } else { ? } Option I: put it back again  What if source rejects it too?  Conceptually read-only but underlying transaction is not if (s.insert(o)) { return false; } else { ??? } Option II: abort the transaction  Should STM be exposed?  Does it prevent non-STM implementations? STM.abort(); return false; Option III: use exceptions  Semantics don’t involve STM  Make AbError checked => don’t ordinarily need log for roll-back throw new AbError(); throws AbError

Aborting atomic blocks (ii)  Here still be dragons: void badException() throws AbError { atomic { AbError e = new AbError() { Object payload = new String(“Aborted atomic block“); }; throw e; } try { badException(); } catch (AbError exn) { … }  Solution: propagated exception behaves as a deep-copy of the one thrown “Allocated in …

External side-effects  Original implementation forbade native methods  Some I/O ‘lucky’ and absorbed by buffers  Want a more general solution – e.g.  Full range of operations potentially available  Controllable semantics, e.g. for debugging or client/server example  Appropriate level for buffering and conflict detection

External side-effects (ii)  General model:  Allow a customized library implementation (or wrapper) to tell if it’s executed within an atomic block  Allow it to temporarily execute ‘global’ operations, e.g. to perform its own buffering of output associated with the block  Register call-backs to receive notification when the block is committed or aborted  Many similar problems to exception propagation:  Code running outside the block mustn’t see partial updates made within the block

Execution contexts  Execution contexts represent the views that different threads may have on the heap at the same time  Invariant: objects and stack frames can only hold references to objects in ‘more permanent’ contexts o1 HeapStack Execution outside atomic block o3 Execution in specialized library o1o2 Execution within atomic block

External actions public class ExampleAction { static int x = 0; static VoidExternalAction printX = new VoidExternalAction() { public void action(Context caller_context) { System.out.println (“x=“ + x); }}; static void increment_x() { atomic { x++; printX.doAction(); } printX.doAction(); }

Combined example void doCombinedUpdate(Connection conn, int new_value, boolean do_commit) throws … { atomic { PreparedStatement pst; pst = conn.prepareStatement (“UPDATE TESTDATA “ “SET field = ? WHERE ID = 1“); pst.setInt(1, newValue); pst.executeUpdate(); shadow = new_value; if (!do_commit) { throw new AbError (“Not committed“); }

 Performance evaluation in a real application  Experiment with ‘external object’ abstraction  Export and bind to in RPC-like style  May make parameter passing mode more intuitive  Avoid problems of writing lots of wrappers  Static analysis for avoiding taking copies  Parameters usually seem to be handed off to callee  Result usually seems to be handed off to caller  Allocate & initialise directly in target context Future directions