150 likes | 282 Views
Polyphonic JR. Ludwik Ejsmont www.mdstud.chalmers.se/~ludwik/. Polyphonic C# - overview. Asynchronous Methods async postEvent(EventInfo data) { // large method body } Chords public class Buffer { public string Get() & public async Put(string s) { return s; } }.
E N D
Polyphonic JR Ludwik Ejsmont www.mdstud.chalmers.se/~ludwik/
Polyphonic C# - overview • Asynchronous Methods async postEvent(EventInfo data) { // large method body } • Chords public class Buffer { public string Get() & public async Put(string s) { return s; } }
JR - overview • Semaphores
Transformation - Single synchronous method in a chord // Polyphonic version ... f(); ... public void f(..){ //body } // JR version public op void f(..); //declaration not necessary ... call f(); // ‘call’ keyword not necessary ... public void f(..){ // if without declaration we have //body add ‘op’ to definition header }
Transformation- Single asynchronous method in a chord // Polyphonic version ... f(); ... public async f(..){ //body } // JR version public op void f(..); //declaration not necessary ... send f(); // ‘send’ keyword necessary ... public void f(..){ // if without declaration we have //body add ‘op’ to definition header }
Transformation- No the same method used more than once in different chord headers public class Test1 { public async sendTest1(string st1); public async sendTest2(string st2); public string getTest() & sendTest1(string st1) & sendTest2(string st2) { return st1 + st2;} public static void Main() { Test1 test = new Test1(); test.sendTest1("Hello "); test.sendTest2("World "); Console.Write(test.getTest()); } } public class Test1 { public op void sendTest1(String); public op void sendTest2(String); public op String getTest() { String st1, st2; receive sendTest2(st2); receive sendTest1(st1); return st1 + st2 ;} public static void main(String [] args){ Test1 test = new Test1(); send test.sendTest1("Hello "); send test.sendTest2("World "); System.out.println(test.getTest()); } }
Transformation - One synchronous, one asynchronous methods, possibly the same in different chord headers class Room { public Room (int size) { hasspaces(size); } private async hasspaces(int n); public void enter() & hasspaces(int n) { if (n > 1) hasspaces(n-1); else isfull(); } private async isfull(); public void leave() & hasspaces(int n) { hasspaces(n+1); } & isfull() { hasspaces(1); } } class Room { public Room (int size) { send hasspaces(size); } private op void hasspaces(int n); public void enter() { int n; receive hasspaces(n); if (n > 1) send hasspaces(n-1); else send isfull(); } private op void isfull(); public void leave(){ inni void hasspaces(int n){sendhasspaces(n+1);} [] void isfull(){send hasspaces(1);} } }
Transformation - One synchronous, an arbitrary number of asynchronous methods, possibly the same in different chord headers class santa { ... static void waittobewoken() & elvesready() & reindeernotready() { reindeernotready(); … } & reindeerready() { … reindeernotready(); … } ... static void clearreindeernotready() & reindeernotready() { elvesready(); reindeernotready(); } }
Transformation - One synchronous, an arbitrary number of asynchronous methods, possibly the same in different chord headers class ElvesReady {} class ReindeerNotReady{} public class Santa { private static ArrayList er = new ArrayList(); private static ArrayList rnr = new ArrayList(); private static sem mutex = 1; private static void waitToBeWoken() { while(true) { inni void elvesReady() { P(mutex); if (rnr.isEmpty()) { er.add(new ElvesReady()); V(mutex); } else { rnr.remove(0); V(mutex); send reindeerNotReady(); break; } }[] void reindeerNotReady() { P(mutex); if (er.isEmpty()) { rnr.add(new ReindeerNotReady()); V(mutex); } else { er.remove(0); V(mutex); send reindeerNotReady(); break; } } [] void reindeerReady() { send reindeerNotReady(); send reinWaiting(0); delay(); unharness.acceptn(9,"all …"); rcount++; break; } } } private static void clearReindeerNotReady() { P(mutex); if (!rnr.isEmpty()) { rnr.remove(0); V(mutex); } else { V(mutex); receive reindeerNotReady(); } }
Transformation - Only asynchronous messages (more than one) in a chord public class Test3 { public op void m1(); public op void m2(); public op void m3(); String s = "Hello World"; public void m1(){ receive m2(); receive m3(); System.out.println(s); } public static void main(String [] args) { Test3 t3 = new Test3(); send t3.m1(); send t3.m2(); send t3.m3(); } } class Test3{ public async m1(); public async m2(); public async m3(); string s = "I am Test3"; when m1() & m2() & m3() { Console.WriteLine(s); } public static void Main() { T t3 = new Test3(); t3.m1(); t3.m2(); t3.m3(); } }
Other issues • Inheritance • Exception handling • Problems with JR
Conclusions • JR – more extensive, more familiar • Polyphonic C# - a different approach but treated more seriously • Transformation JR – Polyphonic C# seems much harder • Why no popular programming language treats concurrency seriously ?
Sources • http://research.microsoft.com/~nick/polyphony/ • http://research.microsoft.com/comega/ • http://www.cs.ucdavis.edu/~olsson/research/jr/ • R. A. Ollson, A. W. Keen, “The JR Programming Language”, Kulwer Academic Publishers 2004 • www.mdstud.chalmers.se/~ludwik/polyphonicjr.html