Activate your free membership today | Log-in

Friday, September 4th, 2009

Stratified JavaScript; Concurrency features in JavaScript

Category: JavaScript

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:
 var elem = document.getElementById("animated_element");
  var x = 0;
  while (true) {
    elem.style.left = x;
    x = (x + 10) % 200;
    hold(100);
  }
 

Fork-Join Parallelism

JAVASCRIPT:
 function animate(elemname, duration_ms, step_ms) {
    var elem = document.getElementById(elemname);
    var start_time = new Date();
    var x = 0;
    do {
      elem.style.left = x;
      x = (x + 10) % 200;
      hold(step_ms);
    } while (duration_ms> new Date() - start_time);
  }

  function par(a, b) {
    alert("all done");
  }

  par(animate("animated_element_1", 10000, 100),
      animate("animated_element_2", 7000, 80));
 

Exploratory parallelism

JAVASCRIPT:
animate("animated_element_1", 10000, 100) @
animate("animated_element_2", 7000, 80);

alert("all done");
 

Suspend/Resume

JAVASCRIPT:
function waitForEvent(event, elem) {
    suspend {
      var rv;
      var listener_func = function(e) {
        rv = e;
        resume();
      };
      elem.addEventListener(event, listener_func, false);
    }
    finally {
      elem.removeEventListener(event, listener_func, false);
    }
    return rv;
}
 

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.

Posted by Dion Almaer at 6:11 am 1 Comment

1 Comment »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.