Chords in C#. Introduction Polyphonic C# is an extension to the C# language Extension is aimed at providing in-language concurrency support CS 5204 –

Slides:



Advertisements
Similar presentations
1 Polyphonic C# Nick Benton Luca Cardelli Cédric Fournet Microsoft Research.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
CSCI 160 Midterm Review Rasanjalee DM.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Modern Concurrency Abstractions for C# Advanced Software Tools Seminar December, 2004 presented by: Guy Gueta.
Chapter 10 Classes Continued
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Java versus C# An introduction to C# and Visual Studio.
Modern Concurrency Abstractions for C# by Nick Benton, Luca Cardelli & C´EDRIC FOURNET Microsoft Research.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
The Java Programming Language
ECE122 Feb. 22, Any question on Vehicle sample code?
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter -6 Polymorphism
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Features of.net Language independent Object oriented program Multi threading Exception handling Drag and drop Linq
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
1 Concurrency Abstractions in C#. Concurrency in C# CS 5204 – Fall, Motivation Concurrency critical factor in behavior/performance affects semantics.
These materials where developed by Martin Schray
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Re-Intro to Object Oriented Programming
Classes and Inheritance
Inheritance and Polymorphism
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Lecture 14 Writing Classes part 2 Richard Gesick.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Pass by Reference, const, readonly, struct
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Conditional Statements
Lecture 22 Inheritance Richard Gesick.
CIS 199 Final Review.
Class.
Corresponds with Chapter 5
SPL – PS3 C++ Classes.
CS 240 – Advanced Programming Concepts
Threads and concurrency / Safety
Presentation transcript:

Chords in C#

Introduction Polyphonic C# is an extension to the C# language Extension is aimed at providing in-language concurrency support CS 5204 – Fall, 2009 Agenda: 1.Extension Syntax 2.Rules 3.How it works 4.Translation of new constructs to traditional C# 5.An example (Stock Server : Active Object)

Chords in C# CS 5204 – Fall, 2009 async methodName(argumentType stuff) { //stuff to do } New Syntax No return: Just schedule this method for execution in another thread

Chords in C# New Syntax CS 5204 – Fall, 2009 public int Grab(int id) & public async Release() { //Method body } Method Declaration1Method Declaration 2 Separator Chord body executes when all of the methods in the header have been called Chord

Chords in C# Syntax Rules – Method Declarations CS 5204 – Fall, 2009 async method(out int i) { //stuff } “ref” and “out” parameter modifiers cannot be used in async methods because by the time this method is executed, who knows what the caller is doing?

Chords in C# Syntax Rules – Chord Declarations CS 5204 – Fall, 2009 async put(int i) &void get()&void calculate() { //stuff } Only one synchronous method is allowed in a chord declaration. Otherwise, in which thread is the body executed? This decision could have behavioral effects.

Chords in C# Syntax Rules – Chord Declarations CS 5204 – Fall, 2009 int get() & async trigger() { return 4; } If a method header has a return value, the body can return a value of that type. If no type is provided then an empty return statement can be used. Matching Types

Chords in C# Syntax Rules – Chord Declarations CS 5204 – Fall, 2009 void get(int i) & async calculate(int i) { //stuff } All formals appearing in method-headers must have distinct identifiers. Otherwise, there would be translation issues… we’ll see how chords are translated into conventional C# later!

Chords in C# Syntax Rules – Chord Declarations CS 5204 – Fall, 2009 void set(int i) & async set(int k) { //stuff } Two method headers within the same chord declaration may not have both the same method name and argument types. Otherwise, which function to call at runtime?

Chords in C# Syntax Rules – Chord Declarations CS 5204 – Fall, 2009 static void work(int i) & async set(int k) { //stuff } All method headers within a chord declaration must be either instance declarations or static declaration: Never a mix.

Chords in C# Syntax Rules – Within a Class CS 5204 – Fall, 2009 class c { string work(int i) & async set(int k) { //stuff } int work (int i) & async set(int k){ //stuff } All method headers with the same member name and argument type signature must have the same return type and identical sets of attributes and modifiers.

Chords in C# Syntax Rules – Within a Class CS 5204 – Fall, 2009 When methods are overridden, all their chords must also be completely overridden. class C { virtual void f() & virtual async g() {/*stuff1*/} virtual void f() & virtual async h() {/*stuff2*/} } class D:C { override async g() {/*stuff3*/} override void f() & override async g(){/*new stuff 1*/} override void f() & override async h(){/*new stuff2*/} } WRONG Correct!

Chords in C# class token { public Token(int initial_tokens) { for(int i = 0; i < initial_tokens; i++)Release(); } public int Grab(int id) & public async Release() { return id; } class token { public Token(int initial_tokens) { for(int i = 0; i < initial_tokens; i++)Release(); } public int Grab(int id) & public async Release() { return id; } Chord Methodology CS 5204 – Fall, Thread A Release() Grab() Scan Thread B

Chords in C# Chord Translation - BitMask CS 5204 – Fall, 2009 struct BitMask { private int v ; // = 0; public void set(int m) { v |= m; } public void clear(int m) { v &= ˜m; } public bool match(int m) { return (˜v & m)==0; } }

Chords in C# Chord Translation - VoidQ CS 5204 – Fall, 2009 class voidQ { private int n; public voidQ(){ n = 0;} public void add() {n++;} public void get() {n--;} public bool empty {get{return n==0;}} }

Chords in C# Chord Translation - ThreadQ CS 5204 – Fall, 2009 class threadQ { private bool signalled = false; private int count = 0; public bool empty {get{return count == 0;}} public void yield(object myCurrentLock){ count++; Monitor.Exit(myCurrentLock); lock(this){ while(!signaled) Monitor.Wait(this); signaled = false; } Monitor.Enter(myCurrentLock); count--; } public void wakeup(){ lock(this){ signaled = true; Monitor.Pulse(this); }

Chords in C# Chord Translation – Token Example Class CS 5204 – Fall, 2009 class Token { private const int mGrab = 1 << 0; private const int mRelease = 1 << 1; private threadQ GrabQ = new threadQ(); private voidQ ReleaseQ = new voidQ(); private const int mGrabRelease = mGrab | mRelease; private BitMask s = new BitMask(); private object mlock = ReleaseQ; private void scan() { if (s.match(mGrabRelease)) {GrabQ.wakeup(); return;} } public Token(int initial tokens) { for (int i = 0; i < initial tokens ; i++) Release(); } [OneWay] public void Release() { lock(mlock) { ReleaseQ.add(); if (! s.match(mRelease)) { s.set (mRelease); scan (); }} } public int Grab(int id) { Monitor.Enter(mlock); if (! s.match(mGrab)) goto now; later : GrabQ.yield(mlock); if (GrabQ.empty) s.clear(mGrab); now: if (s.match(mRelease)) { ReleaseQ.get (); if (ReleaseQ.empty) s.clear(mRelease); scan(); Monitor.Exit(mlock); { return id; // source code for the chord }} else{ s.set (mGrab); goto later ; }} } Variables Tokens constructor (see polyphonic code) Release method Grab method Scan method

Chords in C# Example (From Paper) CS 5204 – Fall, 2009 public abstract class ActiveObject { protected bool done; abstract protected void ProcessMessage(); public ActiveObject(){ done = false; mainLoop(); } async mainLoop(){ while (!done){ ProcessMessage(); } public class StockServer:ActiveObject{ private ArrayList clients = new ArrayList(); public async AddClient(Client c) && override protected void ProcessMessage(){ clients.Add(c); } public async WireQuote(Quote q) & override protected void ProcessMessage(){ foreach(Client c in clients){ c.UpdateQuote(q); }