cpa 2016 communicating generators in javascript
play

CPA 2016 Communicating Generators in JavaScript Kurt Micallef ( - PowerPoint PPT Presentation

CPA 2016 Communicating Generators in JavaScript Kurt Micallef ( kurtmica@live.com ) Kevin Vella ( kevin.vella@um.edu.mt ) Department of Computer Science University of Malta 1 of 21 Problems and Opportunities 1 Single-threaded, event-driven


  1. CPA 2016 Communicating Generators in JavaScript Kurt Micallef ( kurtmica@live.com ) Kevin Vella ( kevin.vella@um.edu.mt ) Department of Computer Science University of Malta 1 of 21

  2. Problems and Opportunities 1 Single-threaded, event-driven JavaScript limits the scope for concurrency. 2 of 21

  3. Problems and Opportunities 1 Single-threaded, event-driven JavaScript limits the scope for concurrency. 2 JavaScript is a ubiquitous computing technology, running in browsers, server runtimes (Node.js) and worker contexts. 2 of 21

  4. JavaScript Generators Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances. 1 1 Mozilla Developer Network 3 of 21

  5. JavaScript Generators Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances. 1 1 var generatorFunction = function* (){ var ret = yield 1; 2 return ret; 3 4 }; 1 Mozilla Developer Network 3 of 21

  6. JavaScript Generators Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances. 1 1 var generatorFunction = function* (){ var ret = yield 1; 2 return ret; 3 4 }; 5 6 var generator = generatorFunction (); 1 Mozilla Developer Network 3 of 21

  7. JavaScript Generators Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances. 1 1 var generatorFunction = function* (){ var ret = yield 1; 2 return ret; 3 4 }; 5 6 var generator = generatorFunction (); 7 var x = generator.next () 1 Mozilla Developer Network 3 of 21

  8. JavaScript Generators Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances. 1 1 var generatorFunction = function* (){ var ret = yield 1; 2 return ret; 3 4 }; 5 6 var generator = generatorFunction (); 7 var x = generator.next ().value; // x = 1 1 Mozilla Developer Network 3 of 21

  9. JavaScript Generators Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances. 1 1 var generatorFunction = function* (){ var ret = yield 1; 2 return ret; 3 4 }; 5 6 var generator = generatorFunction (); 7 var x = generator.next ().value; // x = 1 8 var y = generator.next (2).value; // y = 2 1 Mozilla Developer Network 3 of 21

  10. Generators 1 var delegate = function* (){ yield 1; 2 3 }; 4 of 21

  11. Generators 1 var delegate = function* (){ yield 1; 2 3 }; 4 5 var generator = (function* (){ yield* delegate (); 6 7 }()); 4 of 21

  12. Generators 1 var delegate = function* (){ yield 1; 2 3 }; 4 5 var generator = (function* (){ yield* delegate (); 6 7 }()); 8 9 var x = generator.next ().value; // x = 1 4 of 21

  13. Problems and Opportunities (revisited) 1 Single-threaded, event-driven JavaScript limits the scope for concurrency. 2 JavaScript is a ubiquitous computing technology, running in browsers, server runtimes (Node.js) and worker contexts. 5 of 21

  14. Problems and Opportunities (revisited) 1 Single-threaded, event-driven JavaScript limits the scope for concurrency. • However JavaScript generators enable the dynamic execution of a function. 2 JavaScript is a ubiquitous computing technology, running in browsers, server runtimes (Node.js) and worker contexts. 5 of 21

  15. Problems and Opportunities (revisited) 1 Single-threaded, event-driven JavaScript limits the scope for concurrency. • However JavaScript generators enable the dynamic execution of a function. • These can be repurposed as co-generators to provide co-operative multitasking in a CSP demeanour. 2 JavaScript is a ubiquitous computing technology, running in browsers, server runtimes (Node.js) and worker contexts. 5 of 21

  16. The CSP Environment and Dispatcher • Generators are initialised in a CSP environment, and execute together as co-generators. • These are contained within a function scope, the dispatcher. 2 Except CSP environment creation and channel creation. 6 of 21

  17. The CSP Environment and Dispatcher • Generators are initialised in a CSP environment, and execute together as co-generators. • These are contained within a function scope, the dispatcher. g n g 1 g 0 Dispatcher Figure: Execution flow of co-generators. 2 Except CSP environment creation and channel creation. 6 of 21

  18. The CSP Environment and Dispatcher • Generators are initialised in a CSP environment, and execute together as co-generators. • These are contained within a function scope, the dispatcher. g n g 1 g 0 Dispatcher Figure: Execution flow of co-generators. • All API functions 2 must be: • Called within a CSP environment. • Prefixed with a yield . 2 Except CSP environment creation and channel creation. 6 of 21

  19. The CSP Environment and Dispatcher • Generators are initialised in a CSP environment, and execute together as co-generators. • These are contained within a function scope, the dispatcher. g n g 1 g 0 Dispatcher Figure: Execution flow of co-generators. • All API functions 2 must be: • Called within a CSP environment. • Prefixed with a yield . • yield on its own is effectively a part of the API. 2 Except CSP environment creation and channel creation. 6 of 21

  20. API functions: process creation 1 csp.csp( function* (){ }, 2 // ... 3 function* (){ } 4 5 ); Similar to occam ’s top-level PAR . 7 of 21

  21. API functions: process creation 1 csp.csp( function* (){ }, 2 // ... 3 function* (){ } 4 5 ); Similar to occam ’s top-level PAR . 1 csp.csp(function* (){ yield csp.fork( 2 function* (){ }, 3 // ... 4 function* (){ } 5 ); 6 7 }); 7 of 21

  22. API functions: process creation 1 csp.csp( function* (){ }, 2 // ... 3 function* (){ } 4 5 ); Similar to occam ’s top-level PAR . 1 csp.csp(function* (){ 1 csp.csp(function* (){ yield csp.fork( yield csp.co( 2 2 function* (){ }, function* (){ }, 3 3 // ... // ... 4 4 function* (){ } function* (){ } 5 5 ); ); 6 6 7 }); 7 }); Similar to occam ’s PAR . 7 of 21

  23. API functions: Channel communication 1 var channel = new csp.Channel (); 2 3 csp.csp(function* (){ var x = yield channel.recv (); // x = 1 4 5 }, function* (){ yield channel.send (1); 6 7 }); 8 of 21

  24. API functions: Timeouts 1 csp.csp(function* (){ // ... 2 yield csp.timeout(csp.clock () + 1000); 3 // continue after current time + 1 second 4 5 }); Similar behaviour to occam ’s TIMER s. 9 of 21

  25. API functions: Timeouts 1 csp.csp(function* (){ // ... 2 yield csp.timeout(csp.clock () + 1000); 3 // continue after current time + 1 second 4 5 }); Similar behaviour to occam ’s TIMER s. 1 csp.csp(function* (){ // ... 2 yield csp.sleep (1000); 3 // continue after current time + 1 second 4 5 }); Similar to popular programming languages’ Thread.sleep() . 9 of 21

  26. API functions: Choice 1 var channel = new csp.Channel (); 2 3 csp.csp(function* (){ yield csp.choice ({ 4 recv: channel , 5 action: function* (x) { /* ... */ } 6 }, { 7 timeout: 1000 , 8 action: function* () { /* ... */ } 9 }, { 10 boolean: true , 11 action: function* () { /* ... */ } 12 }); 13 14 }); Similar to occam ’s ALT . 10 of 21

  27. Problems and Opportunities (re-revisited) 1 Single-threaded, event-driven JavaScript limits the scope for concurrency. 2 JavaScript is a ubiquitous computing technology, running in browsers, server runtimes (Node.js) and worker contexts. 11 of 21

  28. Problems and Opportunities (re-revisited) 1 Single-threaded, event-driven JavaScript limits the scope for concurrency. 2 JavaScript is a ubiquitous computing technology, running in browsers, server runtimes (Node.js) and worker contexts. • CSP environments can be distributed over several distinct JavaScript instances to achieve parallel execution. 11 of 21

  29. External Channels • External channels extend across JavaScript instances by overlying various communication mechanisms. JavaScript JavaScript External Channel communication communication CSP environment CSP environment object object generator generator 12 of 21

  30. External Channels • External channels extend across JavaScript instances by overlying various communication mechanisms. JavaScript JavaScript External Channel communication communication CSP environment CSP environment object object generator generator • JavaScript environments investigated: browsers, Node.js, and workers. • Transport mechanisms used: socket.io (over WebSockets), Web Workers, and Cluster Workers. 12 of 21

  31. External Channels – DistributedChannel External channel implementation over socket.io (WebSocket). 1 http.createServer ().listen (8000); 2 io.on("connection", function (s){ var channel = new csp. DistributedChannel (s,"id"); 3 4 csp.csp(function* (){ 5 var x = yield channel.recv (); 6 }); 7 8 }); 1 var s = io.connect("http :// serverhost :8000/"); 2 var channel = new csp. DistributedChannel (s,"id"); 3 4 csp.csp(function* (){ yield channel.send (1); 5 6 }); Listing: Channel communication between distributed co-generators. 13 of 21

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend