220 likes | 393 Views
Modern Concurrency Abstractions for C# by Nick Benton, Luca Cardelli & C´EDRIC FOURNET Microsoft Research. Outline. Introduction Polyphonic C# overview Specification Programming in Polyphonic C# Related work Future work Conclusion. Introduction. How is concurrency implemented ?
E N D
Modern Concurrency Abstractions for C#byNick Benton, Luca Cardelli & C´EDRIC FOURNETMicrosoft Research
Outline • Introduction • Polyphonic C# overview • Specification • Programming in Polyphonic C# • Related work • Future work • Conclusion
Introduction • How is concurrency implemented ? • Language feature • Libraries • Provides features like – memory management & exceptions • Advantages of having them as language features • Produce better code • Warn programmers of potential & actual problems
They made concurrency as a language feature • Monitors - more asynchronous • Shift in focus – shared memory to event- or message-oriented concurrency • Programming constructs handling asynchronous communication • Polyphonic C#
Polyphonic C# Overview • Extension to C# - new asynchronous concurrency constructs based on join calculus • New constructs – • Asynchronous methods • Chords • Asynchronous methods – • guaranteed to complete essentially immediately • Never returns a result • async keyword is used
Chord – consists of a header and a body • Header - set of method declarations • Body is only executed once all the methods in the header have been called • Method calls are implicitly queued up until/unless there is a matching chord • If buff is instance of Buffer then call to buff.Get() has two possibilities • If there has previously been an unmatched call to buff .Put(s) • If there are no previous unmatched calls to buff .Put(s)
Conversely - call to the asynchronous method buff .Put(s) has two possibilities • If there has previously been an unmatched call to buff .Get() • If there are no pending calls to buff .Get() • Exactly which pairs of calls are matched up is unspecified • Difficulties • In which thread the body runs ? • To which method call the final value is returned ?
Specification • If return-type is async then the formal parameter list formals may not contain any ref or out parameter modifier • At most one method-header may have a non-async return-type. • If the chord has a method-header with return-type type, then body may use return statements with type expressions, otherwise body may use empty return statements. • All the formals appearing in method-headers must have distinct identifiers. • Two method-headers may not have both the same member-name and the same argument type signature.
The method-headers must either all declare instance methods or all declare static methods • 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 • Typing issues – async is a subtype of void • an async method may override a void one, • a void delegate may be created from an async method, and • an async method may implement a void method in an interface
Programming Polyphonic C# • A simple example Put Chord Get Chord
Reader writer lock • Problems with the above code • Fairness problem – starvation(writer)
Implementation • Translation from Polyphonic C# to plain C# • Converting state chords to state automata • synchronization state consists of the pending calls • threads for regular methods • messages for asynchronous methods • synchronization depends on – • presence or absence of pending calls • number of calls • actual parameters
Performance issues – • Complete asynchronous chord is always expensive • asynchronous method • synchronous method • All asynchronous messages are present • Otherwise • Translation – • modularize the translated code by introducing auxiliary classes for queues and bitmasks • Queues – class that represents message queues
Related work • The work is based on join calculus • Join java follows similar approach of using join calculus • Synchronization barriers in dataflow languages modeled in Petrinets are similar to chords.
Future Work • Scope for optimizing the implementation • Lock optimization - There are situations when we could safely ‘fuse’ successive critical sections protected by the same lock. • Queue optimization - at most one pending call on a particular object could be compiled using private fields instead of queues • Thread optimization - Purely asynchronous chords can often be compiled to run in the invoking thread, rather than a new one
Conclusion • Designed and implemented a join calculus-based extension of C# that is simple, expressive, and efficient. • Writing correct concurrent programs is considerably less difficult in Polyphonic C# than in ordinary C# • Implementation is constrained by the underlying threads-and-locks model