Activate your free membership today | Log-in

Friday, September 4th, 2009

Stratified JavaScript; Concurrency features in JavaScript

Category: JavaScript

<>p>Alex Fritze has introduced Stratified JavaScript, an experiment that allows us to play with some concurrency features in JavaScript in a cross browser way.

It features:

The ability to pause execution: hold()

JAVASCRIPT:
  1.  
  2.  var elem = document.getElementById("animated_element");
  3.   var x = 0;
  4.   while (true) {
  5.     elem.style.left = x;
  6.     x = (x + 10) % 200;
  7.     hold(100);
  8.   }
  9.  

Fork-Join Parallelism

JAVASCRIPT:
  1.  
  2.  function animate(elemname, duration_ms, step_ms) {
  3.     var elem = document.getElementById(elemname);
  4.     var start_time = new Date();
  5.     var x = 0;
  6.     do {
  7.       elem.style.left = x;
  8.       x = (x + 10) % 200;
  9.       hold(step_ms);
  10.     } while (duration_ms> new Date() - start_time);
  11.   }
  12.  
  13.   function par(a, b) {
  14.     alert("all done");
  15.   }
  16.  
  17.   par(animate("animated_element_1", 10000, 100),
  18.       animate("animated_element_2", 7000, 80));
  19.  

Exploratory parallelism

JAVASCRIPT:
  1.  
  2. animate("animated_element_1", 10000, 100) @
  3. animate("animated_element_2", 7000, 80);
  4.  
  5. alert("all done");
  6.  

Suspend/Resume

JAVASCRIPT:
  1.  
  2. function waitForEvent(event, elem) {
  3.     suspend {
  4.       var rv;
  5.       var listener_func = function(e) {
  6.         rv = e;
  7.         resume();
  8.       };
  9.       elem.addEventListener(event, listener_func, false);
  10.     }
  11.     finally {
  12.       elem.removeEventListener(event, listener_func, false);
  13.     }
  14.     return rv;
  15. }
  16.  

Under the hood, Stratified JavaScript is executed by a runtime based on Oni.

Kris Zyp asked for a comparison to Narrative JavaScript or JavaScript strands and Alex came back with:

The most important enhancement of SJS versus Narrative JavaScript or Strands is the support for 'exploratory parallelism' or 'parallel selection', as embodied by SJS's Alt operator ('@'). There are many problems that are tricky to express with just threads, but which can be expressed elegantly in a structured, compositional way with the use of Alt.

Related Content:

Posted by Dion Almaer at 6:11 am
1 Comment

++++-
4.6 rating from 40 votes

1 Comment »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.