130 likes | 319 Views
Chords. Concurrency Abstractions in C#. Motivation. Concurrency critical factor in behavior/performance affects semantics of all other constructs advantages of language vs. library Compiler analysis/optimization Clarity of syntax asynchronous communication Occurs at various levels
E N D
Chords Concurrency Abstractions in C# CS5204 – Operating Systems
Motivation Concurrency • critical factor in behavior/performance • affects semantics of all other constructs • advantages of language vs. library • Compiler analysis/optimization • Clarity of syntax • asynchronous communication • Occurs at various levels • Requires language support CS 5204 – Operating Systems
Basic Constructs – Asynchronous Methods Syntax: asyncpostEvent (EventInfo data) { // method body using data } • calls to async method return “immediately” • method body is scheduled for execution in another thread • no return result is possible • similar to sending message/event CS 5204 – Operating Systems
Basic Constructs - Chords Example: public classBuffer { public stringGet() & public asyncPut (strings) { returns; } } • illustrates a single chord with two methods • chord body is executed only when all methods in the chord have been called • non-async method call implicitly blocked/queued until chord completes • async method calls are queued until matched (caller not blocked) • at most one non-async method per chord • non-deterministic selection of method calls matched by chord • chord body executes in thread of non-async caller (unless all methods in chord are async methods, in which case a new thread is created) CS 5204 – Operating Systems
. . . [a] [b] [c] [a] [b] [b] [c] Executing Chords invocation queues (one per method) methods bitmaps (one per chord) execute [a,b] chord execute [b,c] chord CS 5204 – Operating Systems
“Counting” via Methods classToken publicToken (intinitial_tokens) { for (inti=0; i<initial_tokens; i++) Release(); } publicintGrab (intid) & public asyncRelease() { returnid; } } • allows clients to Grab and Release a limited number of tokens • argument on Grab returned to client CS 5204 – Operating Systems
Recording “state” via Methods publicclassOneCell { publicOneCell() { empty(); } publicvoidPut(objecto) & private asyncempty() { contains( o ); } public objectGet() & private asynccontains (objecto) { empty(); returno; } } • methods empty and contains are declared private • methods empty and contains “carries” the state of the cell CS 5204 – Operating Systems
Reader-Writer Example classReaderWriter { ReaderWriter() { idle(); } public voidShared() & asyncidle() { s(1); } public void Shared() & asyncs(intn) { s(n+1); } public voidReleaseShared() & asyncs(intn) { if (n == 1) idle(); elses(n-1); } public voidExclusive() & asyncidle() {} public voidReleaseExclusive() { idle(); } } CS 5204 – Operating Systems
Active Object (Actor): Base Class public abstract classActiveObject { protected booldone; abstract protected voidProcessMessage(); publicActiveObject() { done = false; mainLoop(); } asyncmainLoop() { while( !done) { ProcessMessage(); } } • actor: thread per object; repeatedly processes received messages • note: thread created by call to async mainLoop() • abstract class creates basic actor pattern CS 5204 – Operating Systems
Active Object (Actor): Event Example public classStockServer : ActiveObject { privateArrayListclients = newArrayList(); public async AddClient (Client c) & override protected voidProcessMessage() { clients.Add(c); } public asyncWireQuote (Quoteq) & override protected voidProcessMessage() { foreach (Clientcinclients) { c.UpdateQuote(q); }} public asyncCloseDown() & override protect voidProcessMessage() { done = true; } } • message reception/processing driven by ProcessMessage invocations in mainLoop CS 5204 – Operating Systems
Implementation Outline CS 5204 – Operating Systems
Performance CS 5204 – Operating Systems
Syntactic Extension classReaderWriter { asyncidle(); asyncs(int); ReaderWriter() { idle(); } public voidShared() whenidle() { s(1); } whens(intn) { s(n+1); } public voidReleaseShared() whens(intn) { ifn==1) idle(); elses(n-1); } public void Exlcusive() whenidle() {} public voidReleaseExclusive() { idle(); } } CS 5204 – Operating Systems