70
1 Concurrency in C Concurrency in C Nick Benton Nick Benton

1 Concurrency in C Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

Embed Size (px)

Citation preview

Page 1: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

1

Concurrency in CConcurrency in C

Nick BentonNick Benton

Page 2: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

2

PPT: PeoplePPT: People

• Luca Cardelli• Nick Benton• Karthik Bhargavan• Gavin Bierman• Byron Cook• Cédric Fournet• Georges Gonthier• Andy Gordon

• Tony Hoare• Andrew Kennedy• Simon Marlow• Simon Peyton

Jones• Claudio Russo• Don Syme• + visitors

Page 3: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

3

Exciting Times…Exciting Times…

• C# and Java are strongly typed, garbage collected languages with language-level security mechanisms • this was the preserve of academic languages ten

years ago!• Parametric polymorphism in C# 2.0 and Java 5• Mixed-language programming is really happening• Concurrency is going mainstream• Using advanced analysis tools for finding bugs and

checking correctness properties is becoming routine• Contracts and behavioural types are hot topics

– Only go back ten years in academia• Security matters

Page 4: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

4

PPT: Some Other ProjectsPPT: Some Other Projects

• Generics for C# and Microsoft® .NET • SML.NET, Haskell, F#• Stack-walking Security in .NET• Formal Tools for Securing Web Services• Query Languages for Semi-structured Data• First-class Functions for Microsoft® Excel• Languages for Systems Biology• Software Transactional Memory

Page 5: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

5

Why CWhy C??

• Target domain: distributed Web applications

• Common “three tier” architecture:1. Data services tier (database)

2. Middle tier (main application) – C#/Java

3. User interface tier (XML/HTML)

• Concurrency scenario: asynchronous events and message passing• Web services, workflow orchestrations, …

Page 6: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

6

The RealityThe Reality

• It’s about both– DATA

• Objects, XML, relations– CONTROL

• Asynchrony, concurrency• Even though our languages are “modern”, support for

these features is pretty primitive• Data access is weakly typed, mostly string based • Control uses concepts from the ’60s (Monitors, etc.)• Essence: these important features are left to APIs

Page 7: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

7

The Guiding PrincipleThe Guiding Principle

• Provide better level of abstraction • Make invariants and intentions more apparent

(part of the interface)• Give stronger compile-time guarantees (types)• Enable different implementations and

optimizations• Expose structure for other tools to exploit

(example: static analysis)

Put (really) important features in the Put (really) important features in the language itself, rather than in librarieslanguage itself, rather than in libraries

Page 8: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

8

The CThe C Project in a Slide Project in a Slide

• We’re building an experimental language with– C# at its core – First-class support for common data models– A better model of concurrency for both local and distributed

concurrency

• Second principle: RISCy language extension– Don’t bake complex solutions into language– Choose minimal pieces from which solutions can be built– Those pieces having already been shown effective in `purer’

more academic settings– Impedance match to new setting (and even user profile)

Page 9: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

9

Cw Data (Cw Data (veryvery briefly) briefly)

• New types:1. Streams (T* )2. Choice (choice{int;string;})3. Tuples (struct{int i;string s;})4. Content classes5. Nullables (T?)

• Minimal extensions to allow (fairly) high fidelity, strongly typed, mapping of both relational and semistructured data

• XML literals (OK, so that’s not very RISCy)

• Generalized member access (cf. XPath)• SQL-style select queries

Page 10: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

10

But today’s topic is…But today’s topic is…

ConcurrencyConcurrency

Page 11: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

11

Asynchrony Is Where It’s AtAsynchrony Is Where It’s At

• Distribution → concurrency + latency → asynchrony → more concurrency

• Message-passing, event-based programming, dataflow models

• For programming languages, coordination (orchestration) languages and frameworks, workflow

Page 12: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

12

.NET Today.NET Today

• Java-style “monitors”

• Operating system shared memory primitives

• Clunky delegate-based asynchronous calling model

• Hard to understand, use and get right

– Different models at different scales

– Support for asynchrony all on the caller side – little help building code to handle messages (must be thread-safe, reactive, and deadlock-free)

Page 13: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

13

CC Concurrency Concurrency

• New concurrency model and language constructs

• Based on the join calculus– A foundational process calculus like the -calculus but better

suited to asynchronous, distributed systems

• A single model which works both for– local concurrency (multiple threads on a single machine)

– distributed concurrency (asynchronous messaging over LAN or WAN)

• It is different

• But it’s also simple

Page 14: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

14

CC Concurrency in One Slide Concurrency in One Slide

• Objects have both Objects have both synchronoussynchronous and and asynchronousasynchronous methods methods

• Values are passed Values are passed byby ordinary method calls: ordinary method calls:

– If the method is synchronous, the caller blocksIf the method is synchronous, the caller blocks until the method until the method returns some result (as usual)returns some result (as usual)

– If the method is async, the call completes at once and returns If the method is async, the call completes at once and returns voidvoid

• A class defines a collection of A class defines a collection of chordschords (synchronization (synchronization patternspatterns), ), which define what happens once a particular which define what happens once a particular setset of methods have of methods have been invoked. One method may appear in several chords.been invoked. One method may appear in several chords.

– When pending method calls match a pattern, its body runs.When pending method calls match a pattern, its body runs.

– If If there is no match, the invocations are queued up.there is no match, the invocations are queued up.

– If there are several matches, an unspecified pattern is selected.If there are several matches, an unspecified pattern is selected.

– If a pattern containing If a pattern containing onlyonly async methods fires, the body runs in async methods fires, the body runs in a new thread.a new thread.

Page 15: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

15

A Simple BufferA Simple Buffer(for use by producer/consumer threads)(for use by producer/consumer threads)

class Buffer { async put(string s);string get() & put(string s) {

return s;}

}

Page 16: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

16

A Simple BufferA Simple Buffer

class Buffer { async put(string s);string get() & put(string s) {

return s;}

}•An ordinary (synchronous) method with no arguments, returning a string•An ordinary (synchronous) method with no An ordinary (synchronous) method with no arguments, returning a stringarguments, returning a string

Page 17: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

17

A Simple BufferA Simple Buffer

class Buffer { async put(string s);string get() & put(string s) {

return s;}

}•An ordinary (synchronous) method with no arguments, returning a string

•An asynchronous method (hence returning no An asynchronous method (hence returning no result), with a string argumentresult), with a string argument

Page 18: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

18

A Simple BufferA Simple Buffer

class Buffer { async put(string s);string get() & put(string s) {

return s;}

}•An ordinary (synchronous) method with no arguments, returning a string

•An asynchronous method (hence returning no result), with a string argument

•Joined together in a chordJoined together in a chord

Page 19: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

19

A Simple BufferA Simple Buffer

class Buffer { async put(string s);string get() & put(string s) {

return s;}

} • Calls toCalls to put()put() return immediately, but are internally return immediately, but are internally queued if there’s no waitingqueued if there’s no waiting get()get()• Calls toCalls to get()get() block until/unless there’s a matchingblock until/unless there’s a matching put()put() • When there’s a match the body runs, returning the When there’s a match the body runs, returning the argument of theargument of the put()put() to the caller ofto the caller of get()get()• Exactly which pairs of calls are matched up is Exactly which pairs of calls are matched up is unspecifiedunspecified

Page 20: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

20

A Simple BufferA Simple Buffer

class Buffer { async put(string s);string get() & put(string s) {

return s;}

} • Does this example involve spawning any threads?Does this example involve spawning any threads?• No. Though the calls will usually come from different pre-No. Though the calls will usually come from different pre-existing threads.existing threads.

• Is it thread-safe? You don’t seem to have locked anything…Is it thread-safe? You don’t seem to have locked anything…• Yes. The chord compiles into code which uses locks. (And that Yes. The chord compiles into code which uses locks. (And that doesn’tdoesn’t mean everything is synchronized on the object.) mean everything is synchronized on the object.)

• Which method gets the returned result?Which method gets the returned result?• The synchronous one; there is at most one of these in a chord.The synchronous one; there is at most one of these in a chord.

Page 21: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

21

Reader/WriterReader/Writer

…using threads and mutexes in Modula 3An introduction to programming with threads

Andrew D. Birrell, January 1989

Page 22: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

22

Reader/Writer in Five ChordsReader/Writer in Five Chords

public class ReaderWriter { private async idle(); private async s(int n); public ReaderWriter() {idle();} public void Exclusive() & idle() {} public void ReleaseExclusive() { idle(); }

public void Shared() & idle() { s(1);} & s(int n) { s(n+1);}

public void ReleaseShared() & s(int n) { if (n == 1) idle(); else s(n-1); }}

A single private message represents the state: none ↔ idle() ↔ s(1) ↔ s(2) ↔ s(3) …

Page 23: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

23

Asynch Requests/ResponsesAsynch Requests/Responses

• Service exposes an async method which takes parameters and somewhere to put the result:– a buffer, or a channel, or

– here, an asynchronous delegate, used as a callback

public delegate async IntCB(int v); // the callback

public class Service {

public async request(String arg, IntCB callback){

… // compute result

callback(result); // respond!

} }

Page 24: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

24

Joining ResponsesJoining Responses

class Join2 {async first(int r);async second(int r);struct{int;int;} waitAll() &

first(int r1) & second(int r2) {

return new{r1,r2};}

}// client code:struct{int;int;} results;Join2 x = new Join2();service1.request(arg1, new IntCB(x.first));service2.request(arg2, new IntCB(x.second));

// do something useful in the meantime// now wait until both results have come back

results = x.waitAll();// do something with results

Page 25: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

25

Selecting ResponsesSelecting Responses

class Select2 {async reply(int r);int waitOne() & reply(int r) {

return r;}

}// client code:int result;Select2 x = new Select2();service1.request(arg1, new IntCB(x.reply));service2.request(arg2, new IntCB(x.reply));

// do something useful in the meantime// now wait until one result has come back

result = x.waitOne();// do something with result

Page 26: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

26

Active ObjectsActive Objects

public abstract class ActiveObject : MarshalByRefObject {

protected bool done;

abstract protected void processmessage();

public ActiveObject () {

done = false; mainloop(); }

async mainloop() {

while (!done) { processmessage(); }

}

}

Page 27: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

27

……continuedcontinued

class Stock : ActiveObject {

public async bid(BidOffer thebid);

public async register(Client who);

override protected void processmessage()

& bid(BidOffer thebid) {

// process bid messages

}

& register(Client who) {

// process registration requests

}

}

Page 28: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

28

Extending C# with chordsExtending C# with chords

• Syntax extensions:Syntax extensions:– Declarations for asynchronous methodsDeclarations for asynchronous methods– Definitions of (synchronous) methods Definitions of (synchronous) methods

generalized to chord definitionsgeneralized to chord definitions– whenwhen for purely asynchronous chords for purely asynchronous chords

• Interesting well-formedness conditions:Interesting well-formedness conditions:1.1. Can’t have two synchronous methods in a single Can’t have two synchronous methods in a single

patternpattern

2.2. The inheritance restrictionThe inheritance restriction

3.3. ““ref” and “out” parameters cannot appear in ref” and “out” parameters cannot appear in async headersasync headers

Page 29: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

29

• JoCaml allows multiple synchronous methods to be JoCaml allows multiple synchronous methods to be joined, as in the following rendezvousjoined, as in the following rendezvous

• But in which thread does the body run? In C#, But in which thread does the body run? In C#, thread identity is “very” observable, since threads thread identity is “very” observable, since threads are the holders of particular re-entrant locks. So we are the holders of particular re-entrant locks. So we rule this out in the interests of keeping rule this out in the interests of keeping && commutative. (Of course, it’s still easy to code up commutative. (Of course, it’s still easy to code up an asymmetric rendezvous in Polyphonic C#.)an asymmetric rendezvous in Polyphonic C#.)

Why ≤1 synchronous method in a chord?Why ≤1 synchronous method in a chord?

int f(int x) & int g(int y) {

return y to f;

return x to g;

}

Page 30: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

30

The problem with inheritanceThe problem with inheritanceclass C {

virtual async g();

virtual async h();

virtual void f() & g() {…}

& h() {…}

}

class D : C {

override async g();

}

void m(C x) { x.g(); x.f();}

m(new D());

•We’ve “half” overridden fWe’ve “half” overridden f

•Too easy to create Too easy to create deadlock or async leakagedeadlock or async leakage

Page 31: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

31

The inheritance restrictionThe inheritance restriction

• Two methods are Two methods are co-declaredco-declared if there’s a chord with if there’s a chord with both of them inboth of them in

• Inheritance and concurrency don’t mix well.Inheritance and concurrency don’t mix well.Our restriction is simple; it could be made less Our restriction is simple; it could be made less restrictive.restrictive.

Whenever a method is overridden,Whenever a method is overridden,every co-declared method must also be overriddenevery co-declared method must also be overridden

private async g()

public virtual void f() & g() {…}

Page 32: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

32

Types etc.Types etc.

• async is a subtype of is a subtype of voidvoid• Allow covariant return types on those two:Allow covariant return types on those two:

– An An asyncasync method may override a method may override a voidvoid one one– A A voidvoid delegate may be created from an delegate may be created from an asyncasync method method

– An An asyncasync method may implement a method may implement a voidvoid method in an interfacemethod in an interface

• asyncasync methods are given the methods are given the [OneWay][OneWay] attribute, so remote calls are non-blockingattribute, so remote calls are non-blocking

Page 33: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

33

ImplementationImplementation

• 44thth version… version… • Built on CCI (also used for Spec#), runs in VSBuilt on CCI (also used for Spec#), runs in VS• Introduce queues for pending calls (holding blocked threads Introduce queues for pending calls (holding blocked threads

for sync methods, arguments for asyncs)for sync methods, arguments for asyncs)• Generated code (using brief lock to protect queue state) Generated code (using brief lock to protect queue state)

looks for matches and then eitherlooks for matches and then either– Enqueues args (async no match)Enqueues args (async no match)– Enqueues thread and blocks (sync no match)Enqueues thread and blocks (sync no match)– Dequeues other args and continues (sync match)Dequeues other args and continues (sync match)– Wakes up blocked thread (async match with sync)Wakes up blocked thread (async match with sync)– Spawns new thread (async match all async)Spawns new thread (async match all async)

• Efficient – bitmasks to look for matches, no PulseAlls,…Efficient – bitmasks to look for matches, no PulseAlls,…• We’ve changed the underlying implementation to match We’ve changed the underlying implementation to match

changes in CLR performance (sleeping/interrupting -> changes in CLR performance (sleeping/interrupting -> waiting/pulsing)waiting/pulsing)

Page 34: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

34

Predictable Demo: Predictable Demo: Dining PhilosophersDining Philosophers

eating

eating

waitingto eat

waitingto eat thinking

Page 35: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

35

Code ExtractCode Extract

class Room { private async hasspaces(int n); private async isfull();

public Room (int size) { hasspaces(size); } public void enter() & hasspaces(int n) { if (n > 1) hasspaces(n-1); else isfull(); }public void leave() & hasspaces(int n) { hasspaces(n+1); } & isfull() { hasspaces(1); }}

Page 36: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

36

DemonstrationDemonstrationDining philosophersDining philosophers

Page 37: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

37

Other SamplesOther Samples

• Web service combinators (Cardelli & Web service combinators (Cardelli & Davies)Davies)

• Adaptive scheduler (cf. Larus & Adaptive scheduler (cf. Larus & Parkes), Parkes),

• Accessing web services (Terraserver), Accessing web services (Terraserver),

• Active objects and remoting (stock Active objects and remoting (stock trader)trader)

• Santa Claus problem (Trono)Santa Claus problem (Trono)

Page 38: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

38

InfluenceInfluence

• 3 internal projects use joins of some 3 internal projects use joins of some formform

• ExternalExternal– MC#MC#

– NemerleNemerle

– QUT web service languageQUT web service language

– SprySpry

Page 39: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

39

Current and future workCurrent and future work

• Extension with genericsExtension with generics• Limited pattern-matching on message Limited pattern-matching on message

contentscontents– Been prototyped (Melgratti) but very hard to Been prototyped (Melgratti) but very hard to

compile efficientlycompile efficiently– Direct syntactic support for timeouts along Direct syntactic support for timeouts along

this line would be nice…this line would be nice…• Adding joinable transactions with explicit Adding joinable transactions with explicit

compensations (Bruni, Montanari)compensations (Bruni, Montanari)• Behavioural typesBehavioural types• Integration/compilation with STM???Integration/compilation with STM???

Page 40: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

40

SummarySummary

• A clean, simple, new model for A clean, simple, new model for asynchronous concurrency in C#asynchronous concurrency in C#– Declarative, local synchronizationDeclarative, local synchronization– Model good for both local and distributed Model good for both local and distributed

settings settings – Efficiently compiled to queues and automataEfficiently compiled to queues and automata– Easier to express and enforce concurrency Easier to express and enforce concurrency

invariantsinvariants– Compatible with existing constructs, though Compatible with existing constructs, though

they constrain our design somewhatthey constrain our design somewhat– Minimalist design – pieces to build whatever Minimalist design – pieces to build whatever

complex synchronization behaviours you needcomplex synchronization behaviours you need– Solid foundationsSolid foundations– Works well in practiceWorks well in practice

Page 41: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

41

Compiler ReleaseCompiler Release

• Freely available (v.1.0.2) from http://research.microsoft.com/comega

• Over 7000 downloads so far • Please download and play!

Page 42: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

42

For More InformationFor More Information

• http://research.microsoft.com/comega– Papers, compiler, links

• http://channel9.msdn.com/ ShowPost.aspx?PostID=23947 – Channel 9 video

• http://www.omegaengine.com/– A public discussion forum

– Not run by Microsoft/Microsoft Research

Page 43: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

43

CreditsCredits

Nick Benton Nick Benton

Gavin Bierman Gavin Bierman

Luca CardelliLuca Cardelli

CCéédric Fournetdric Fournet

Erik Meijer Erik Meijer

Claudio Russo Claudio Russo

Wolfram SchulteWolfram Schulte

Herman Venter Herman Venter

Mark Shinwell, Hernan Melgratti, and all the other Mark Shinwell, Hernan Melgratti, and all the other folks who’ve contributed to the implementationfolks who’ve contributed to the implementation

Page 44: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

44

Questions?Questions?

Page 45: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

45

One-shot TimeoutBufferOne-shot TimeoutBuffer

class TimeoutBuffer { async empty(); async has(Object o); async timeout();

TimeoutBuffer(int delay) {Timer t = new Timer(new

TimerCallBack(this.tick),delay);empty();

}void put(Object o) & empty() {has(o);}

& timeout() {timeout();}void tick() & empty() {timeout();}

& has(Object o) {has(o);}Object get() & timeout() {timeout(); throw new TimeOutExn();} & has(Object o) {has(o); return o;}

}

Page 46: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

46

Fairer reader/writer lockFairer reader/writer lock

class ReaderWriterFairclass ReaderWriterFair {{ ReaderWriter() { idle(); }ReaderWriter() { idle(); } private int n = 0; // protected by s() or t()private int n = 0; // protected by s() or t()

public void Shared() & async idle() { n=1; s(); }public void Shared() & async idle() { n=1; s(); } public void Shared() & async s() { n++; s(); }public void Shared() & async s() { n++; s(); } public void ReleaseShared() & async s() { public void ReleaseShared() & async s() { if (--n == 0) idle(); else s(); if (--n == 0) idle(); else s(); }} public void Exclusive() & async idle() {}public void Exclusive() & async idle() {} public void ReleaseExclusive() { idle(); } public void ReleaseExclusive() { idle(); } public void ReleaseShared() & async t() { public void ReleaseShared() & async t() { if (--n == 0) idleExclusive(); else t(); if (--n == 0) idleExclusive(); else t(); }} public void Exclusive() & async s() { public void Exclusive() & async s() { t(); wait(); t(); wait(); }} void wait() & async idleExclusive() {}void wait() & async idleExclusive() {} }}

Page 47: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

47

Current Data Access in C#Current Data Access in C#

SqlConnection conn = new SqlConnection(…);conn.Open();Console.WriteLine("Please enter a city: ");string input = Console.ReadLine();SqlCommand cmd = new SqlCommand("SELECT * FROM Customers

WHERE city='"+input+"'", conn);SqlDataReader results = cmd.ExecuteReader();Console.WriteLine("Customers from: "+input);while (results.Read()){ string Name = (string)results["ContactName"]; string Phone = (string)results["Phone"]; Console.WriteLine(Name + " -- "+Phone);}conn.Close();

Weak Weak TypeType

Weak Weak TypeType

Queries storedQueries storedas stringsas strings

Queries storedQueries storedas stringsas strings

Runtime typeRuntime typeconversionconversion

Runtime typeRuntime typeconversionconversion

Using string toUsing string toproject attributesproject attributesUsing string toUsing string to

project attributesproject attributes

Page 48: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

48

API Access (Continued)API Access (Continued)

• Storing queries as strings is not only ugly but also a security risk– Imagine where city=“’ OR 1=1 --”

• This will return the entire relation!!

• This is the source of many security loopholes in Web-based databases

Page 49: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

49

Use Parameters and Typed Datasets…Use Parameters and Typed Datasets…

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customers WHERE City= @city",conn );

SqlParameter cityParam =da.SelectCommand.Parameters.Add("@city", SqlDbType.VarChar, 80);

cityParam.Value = input;

NorthwindDataSet ds = new NorthwindDataSet();da.Fill(ds, ds.Customers.TableName);

foreach (NorthwindDataSet.CustomersRow dr in ds.Customers.Rows){

string Name = dr.ContactName;string Phone = dr.Phone;Console.WriteLine( Name + " -- " + Phone);

}

Page 50: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

50

Or Use Stored Procedures…Or Use Stored Procedures…

CREATE PROCEDURE CustomersForCity @City nvarchar(80)AS SELECT * FROM Customers WHERE City = @City

SqlCommand cmd = new SqlCommand("dbo.CustomersForCity",conn );cmd.CommandType = CommandType.StoredProcedure;SqlParameter cityParam = cmd.Parameters.Add("@City",

SqlDbType.VarChar, 80);cityParam.Value = input;

SqlDataAdapter da = new SqlDataAdapter( cmd );NorthwindDataSet ds = new NorthwindDataSet();da.Fill(ds, ds.Customers.TableName );

foreach (NorthwindDataSet.CustomersRow dr in ds.Customers.Rows){

string Name = dr.ContactName;string Phone = dr.Phone;Console.WriteLine( Name + " -- " + Phone);

}

Page 51: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

51

Current XML Access in C#Current XML Access in C#

static void UseXPathDocument(string reviewsLocation, string bibLocation, XmlTextWriter xtw) {  XPathDocument bibDoc = new XPathDocument(bibLocation);  XPathDocument reviewsDoc = new XPathDocument(reviewsLocation);  xtw.WriteStartElement("books-with-prices");  XPathNodeIterator books = bibDoc.CreateNavigator().Select("/bib/book");  while (books.MoveNext()) {    XPathNodeIterator entries = reviewsDoc.CreateNavigator().Select("/reviews/entry");    XPathNodeIterator bookTitleItr = books.Current.SelectChildren("title","");    bookTitleItr.MoveNext();    XPathNodeIterator bookPriceItr = books.Current.SelectChildren("price","");    bookPriceItr.MoveNext();    while(entries.MoveNext()) {      XPathNodeIterator entryTitleItr = entries.Current.SelectChildren("title","");      entryTitleItr.MoveNext();      XPathNodeIterator entryPriceItr = entries.Current.SelectChildren("price","");      entryPriceItr.MoveNext();        if (entryTitleItr.Current.Value == bookTitleItr.Current.Value) {        WriteBook(xtw,bookTitleItr.Current.Value,entryPriceItr.Current.Value                    ,bookPriceItr.Current.Value);      }    }   }  xtw.WriteEndElement();}

Page 52: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

52

Can’t We Do Better?Can’t We Do Better?

Page 53: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

53

Minimal Extensions to C#Minimal Extensions to C#

• New types:

1. Streams (T* )2. Choice (choice{int;string;})3. Tuples (struct{int i;string s;})4. Content classes5. Nullables (T?)6. async (subtype of void)

• XML literals• Chords • Generalized member access (cf. XPath)• SQL-style select queries

Page 54: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

54

StreamsStreams

• New type: T* (sequence of T)

• Close relative of IEnumerable<T>

• Streams generated by statement blocks that yield values

• Streams consumed by foreach loop

• No nested streams (no T**)– Just like in XQuery/XPath

Page 55: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

55

Streams ExampleStreams Example

public static int* FromTo(int s, int e) { for (i = s; i <= e; i++) yield return i;

} int* OneToTen = FromTo(1,10); foreach(int j in OneToTen) {

Console.WriteLine(j);};

Page 56: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

56

Why Streams?Why Streams?

• We use streams to represent– The (homogenous) sequence of rows from a

database table

– The iteration operator from XML Schema/DTDs<!ELEMENT foo (bar*)>

Page 57: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

57

Anonymous structsAnonymous structs

• New type:

• Constructed in obvious way:

• Project using dot notation:

• Like ML records but ordered, duplicates allowed

struct{int i; string s; string s;} tup = new{i=42, s=“Gavin”, s=“Nick”};

int temp = tup.i;

struct{int i; string s; string s;}

Page 58: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

58

Why Anonymous structs?Why Anonymous structs?

• We use anonymous structs to represent – A row (of heterogeneous fields) from a SQL table

– Ordered sequences of XML Schema/DTD elements, example:<!ELEMENT foo (bar,pop,pop)>

Page 59: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

59

Union TypesUnion Types

• New type: choice{T;S;}

• A value of this type is either a value of type T or a value of type S, example:

• Why?– To represent choice in XML Schema/DTD

<!ELEMENT foo (bar | pop)>

choice{string; Button; int;} u = “hello”;u = new Button(); u = 42;

Page 60: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

60

Content ClassContent Class

• Like a normal class, except it contains a single unnamed member

• Useful for XML fidelity

public class book { struct{ string title; choice{struct{editor editor;}*; struct{author author;}*; } string publisher; decimal price; }}

Page 61: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

61

bib = <bib> <book year="1994"> <title>TCP/IP Illustrated</title> <author><last>Stevens</last> <first>W.</first> </author> <publisher>Addison-Wesley</publisher> <price> 65.95</price> </book> <book> <title>The Economics of Technology and Content for Digital TV</title> <editor><last>Gerbarg</last> <first>Darcy</first> <affiliation>CITI</affiliation> </editor> <publisher>Kluwer Academic</publisher> <price>129.95</price> </book> </bib>;

XML LiteralsXML Literals

• Now we have the types, we can write (strongly typed) XML literals in our code:

<!ELEMENT bib (book*)><!ELEMENT book (title, (author* | editor*),

publisher, price)><!ELEMENT title (#PCDATA)><!ELEMENT publisher (#PCDATA)><!ELEMENT price (#PCDATA)>

public class bib { struct{ book book; }*; }public class book { struct {string title; choice { struct { editor editor;}*; struct { author author;}*;} string publisher; decimal price;} }

Page 62: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

62

Generalized Member Access (GMA)Generalized Member Access (GMA)

• A key design feature of C

• Observe: C can encode XML-like values

• Want: some query-like facility

• Solution: extend the dot notation behaviour (to match “/” from XPath)

Page 63: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

63

Generalized Member AccessGeneralized Member Access

1. Sequences

2. Anonymous struct types

3. Choice types

string* ss;ss.ToUpper();

struct{int i; string s; string s;} x;x.i; x.s;

choice{string;Person;int;} y;y.Length;

choice{int;Float;}?choice{int;Float;}?choice{int;Float;}?choice{int;Float;}?

string*string*string*string*

intintintint

string*string*string*string*

Page 64: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

64

XQuery Use Case ThreeXQuery Use Case Three

For each book in the bibliography, list the title and authors, grouped inside a “result” element

for $b in $bs/bookreturn <result> {$b/title} {$b/author} <result>

foreach (b in bs.book){ yield return <result> {b.title} {b.author} </result>;}

XXQQUUEERRYY

CCωω

Page 65: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

65

Path ExpressionsPath Expressions

• GMA allows us to write OQL-like path expressions:

static int* FromTo(int s, int e) { for (i = s; i <= e; i++) yield return i; }FromTo(0,100).{return it.ToString("x");}

.ToUpper() .{Console.WriteLine(it);};

Page 66: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

66

Demonstration OneDemonstration OneData access in Data access in CC

Page 67: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

67

SQL-Like QueriesSQL-Like Queries

• We support first-class SQL queries

String conn = “ ...Initial Catalog=Northwind; ";Database DB = new Database(conn);Console.WriteLine("Please enter a city: ");string input = Console.ReadLine();res = select * from DB.Customers where City==input ;Console.WriteLine("Customers from: "+input);res.{Console.WriteLine(it.ContactName + " --- " + it.Phone);};

Page 68: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

68

Other WorkOther Work

• The type system and operational semantics of (the data access fragment of) C have been formally (i.e. mathematically) specified– To be presented at ECOOP 2005

• C has other data access goodies– More XPath-like extensions

– More SQL extensions

Page 69: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

69

SummarySummary

• CC extends C# with first class support for data access and extends C# with first class support for data access and asynchronous concurrencyasynchronous concurrency

• Data access extensionsData access extensions– Model good for both relational (SQL) and semi-structured (XML) Model good for both relational (SQL) and semi-structured (XML)

datadata

– Compile to either in-memory operations or external queriesCompile to either in-memory operations or external queries

– Based on ideas from functional programming, but tweaked for Based on ideas from functional programming, but tweaked for compatibility with objects and XPathcompatibility with objects and XPath

• Concurrency modelConcurrency model– Model good for both local and distributed settings Model good for both local and distributed settings

– Efficiently compiled to queues and automataEfficiently compiled to queues and automata

– Based on join-calculus, but tweaked for compatibility with objectsBased on join-calculus, but tweaked for compatibility with objects

Page 70: 1 Concurrency in C  Nick Benton. 2 PPT: People Luca Cardelli Nick Benton Karthik Bhargavan Gavin Bierman Byron Cook Cédric Fournet Georges Gonthier Andy

70

Relationship with C#Relationship with C#

• Note: C is not C# 3/4/5… !– We’re a research project

– We’re not sponsored by the C# or VS team

– [Actually, the C# team look at lots of research internal and external]

"The C# team is excited about C"The C# team is excited about C and other C#-based and other C#-based research projects that Mresearch projects that MSR-SR-Cambridge is working on. We Cambridge is working on. We have no current plans to extend the C# language in this have no current plans to extend the C# language in this direction, but will continue to observe the progress of Cdirection, but will continue to observe the progress of C and other MSR-Cambridge projects." and other MSR-Cambridge projects."

Scott Wiltamuth, C# Product Unit ManagerScott Wiltamuth, C# Product Unit Manager