DOMWorkerThreads: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(New page: == DOM Worker Threads API Proposal == == Abstract == This is an API '''proposal''' for DOM Worker Threads. At this time this document is not complete and is not yet ready for general co...)
 
No edit summary
 
(15 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== DOM Worker Threads API Proposal ==
== Web Worker API Proposal ==


== Abstract ==
== API Proposal ==


This is an API '''proposal''' for DOM Worker Threads.
<blockquote>
<code>
<pre>


At this time this document is not complete and is not yet ready for general consideration or debate. It's just getting started!
[NoInterfaceObject] interface WorkerFactory {
  Worker createWorker(in DOMString scriptURL);
};


Look for a post to mozilla.dev.platform when this proposal is ready to be discussed.
interface Worker {
  void postMessage(in DOMString aMessage);


== Introduction ==
  // event handler attributes
          attribute EventListener onmessage;
          attribute EventListener onerror;
          attribute EventListener onload;
          attribute EventListener onunload;
};


=== Use Cases ===


=== Terminology ===
[NoInterfaceObject] interface WorkerGlobalScope
{
  readonly attribute WorkerGlobalScope self;
  readonly attribute WorkerLocation location;
  readonly attribute boolean closing;
  void close();


A '''worker''' is an execution context for JavaScript that runs off of the main UI thread. The global scope for a worker is almost empty - familiar objects such as <code>window</code> and <code>document</code> are not available, nor is the Mozilla-specific <code>Components</code> object.
  // event handler attributes
          attribute EventListener onunload;


A '''worker pool''' is a collection of related workers. Workers within a pool can communicate with each other freely, but workers cannot communicate with workers from another pool.
  // WorkerUtils
  void importScripts([Variadic] in DOMString urls);
  readonly attribute Storage localStorage;
  Database openDatabase(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize);
  void showNotification(in DOMString title, in DOMString subtitle, in DOMString description);
  void showNotification(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick);
};
 
[NoInterfaceObject] interface WorkerLocation {
  readonly attribute DOMString href;
  readonly attribute DOMString protocol;
  readonly attribute DOMString host;
  readonly attribute DOMString hostname;
  readonly attribute DOMString port;
  readonly attribute DOMString pathname;
  readonly attribute DOMString search;
  readonly attribute DOMString hash;
};


== Conformance ==
</pre>
</code>
</blockquote>


== Dependencies ==
=== Sample usage ===


== Specification Proposal ==
This is a '''very''' suboptimal way of calculating a number in the Fibonacci sequence.


Main page:
<blockquote>
<blockquote>
<code>
<code>
<pre>
<pre>
#include "nsISupports.idl"
worker = createWorker("f.js");
worker.onmessage = function(e) {
  alert("The 100th Fibonacci number is " + e.data);
}
worker.postMessage(100);
</pre>
</code>
</blockquote>


interface nsIDOMWorkerThread;
f.js:
interface nsIScriptError;
<blockquote>
interface nsIURI;
<code>
<pre>
parent.onmessage = function(e) {
  if (e.data <= 1)
    postMessage(e.data);


[scriptable, function, uuid(e50ca05d-1381-4abb-a021-02eb720cfc38)]
  w1 = createWorker("f.js");
interface nsIDOMWorkerMessageListener : nsISupports
  w1.onmessage = handler;
{
   w1.postMessage(e.data - 1);
   /**
 
  * An nsIDOMWorkerThread receives the onMessage callback when another
  w2 = createWorker("f.js");
  * worker posts a message to it.
  w2.onmessage = handler;
  *
   w2.postMessage(e.data - 2);
  * @param aMessage (in DOMString)
}
  *        The message sent from another worker.
  * @param aSource (in nsIDOMWorkerThread)
  *        The worker that sent the message. Useful for a quick response.
  */
   void onMessage(in DOMString aMessage,
                in nsIDOMWorkerThread aSource);
};


[scriptable, function, uuid(9df8422e-25dd-43f4-b9b9-709f9e074647)]
c = 0;
interface nsIDOMWorkerErrorListener : nsISupports
sum = 0;
{
  /**
  * An nsIDOMWorkerPool receives the onError callback when one of its child
  * workers has a parse error or an unhandled exception.
  *
  * @param aMessage (in nsIScriptError)
  *        Details about the error that occurred. See nsIScriptError.
  * @param aSource (in nsIDOMWorkerThread)
  *        The worker that sent the message. Depending on the specific error in
  *        question it may not be possible to use this object (in the case of a
  *        parse error, for instance, aSource will be unusable).
  */
  void onError(in nsIScriptError aError,
              in nsIDOMWorkerThread aSource);
};


[scriptable, uuid(6f19f3ff-2aaa-4504-9b71-dca3c191efed)]
function handler(e) {
interface nsIDOMWorkerThread : nsISupports
   sum += parseInt(e.data);
{
  if (++c == 2) {
   /**
    postMessage(sum);
  * The nsIDOMWorkerMessageListener which handles messages for this worker.
  }
  *
}
  * Developers should set this attribute to a proper object before another
</pre>
  * worker begins sending messages to ensure that all messages are received.
</code>
  *
</blockquote>
  * The main script can set this property directly on the pool object.
  *
  * Worker scripts must set this attribute on the 'thisThread' global object
  * defined in its scope. Setting the messageListener from the pool's script is
  * not permitted.
  */
  attribute nsIDOMWorkerMessageListener messageListener;


  /**
== References ==
  * Sends a message to the worker.
  *
  * @param aMessage (in DOMString)
  *        The message to send. This may be a string or an object that can be
  *        serialized via JSON (see http://developer.mozilla.org/en/docs/JSON).
  */
  void postMessage(in DOMString aMessage);
};


[scriptable, uuid(45312e93-8a3e-4493-9bd9-272a6c23a16c)]
The only thing I've seen so far is the [http://code.google.com/apis/gears/api_workerpool.html Google Gears WorkerPool API]. We would certainly like to provide a API that would make migrating Gears code trivial.
interface nsIDOMWorkerPool : nsIDOMWorkerThread
{
  /**
  * The nsIDOMWorkerErrorListener which handles errors in child threads.
  *
  * Developers should set this attribute to a proper object before calling
  * createWorker in order to catch parse errors as well as runtime exceptions.
  */
  attribute nsIDOMWorkerErrorListener errorListener;


  /**
== API Proposal For Shared Workers ==
  * Create a new worker object by evaluating the given script.
  *
  * @param aSourceScript (in DOMString)
  *        The script to compile. See below for details on the scope in which
  *        the script will run.
  */
  nsIDOMWorkerThread createWorker(in DOMString aSourceScript);


  /**
If we want to support shared workers in the initial release of this spec, here are two proposals that will work with the above initial API.
  * Create a new worker object by evaluating the given script file.
  *
  * @param aSourceScript (in nsIURI)
  *        The script file to compile. See below for details on the scope in
  *        which the script will run. See nsIURI.
  */
  nsIDOMWorkerThread createWorkerFromURI(in nsIURI aSourceScript);
};
</pre>
</code>
</blockquote>


=== The worker pool's scope ===
=== Proposal 1 ===


The scope for the worker pool is the standard DOM scope. All DOM objects such as <code>window</code> and <code>document</code> are available, as well as functions such as <code>alert()</code> and <code>dump()</code>.
<blockquote>
<code>
<pre>


=== The worker's scope ===
[NoInterfaceObject] interface WorkerFactory {
  ...
  Worker createSharedWorker(in DOMString name, in DOMString scriptURL);
};


The worker's scope is far more limited.
[NoInterfaceObject] interface WorkerParent {
  void postMessage(in DOMString aMessage);
  attribute EventListener onmessage;
};


A worker has access to the following objects:
</pre>
</code>
</blockquote>


* <code>thisThread</code>
<code>createSharedWorker</code> creates a new <code>Worker</code> object that interacts with the same <code>WorkerGlobalScope</code> as any previously existing <code>Worker</code>s.
** This is the <code>nsIDOMWorkerThread</code> object representing the current worker. It can be passed as an argument to the <code>postMessage</code> method on another <code>nsIDOMWorkerThread</code> object or on the <code>postMessageToPool()</code> function.


A worker has access to the following global functions:
When a shared <code>Worker</code> receives a message it can send data back to the sender using the <code>Event.source</code> property which is a <code>WorkerParent</code>.


* <code>dump(in DOMString aString)</code>
Using <code>WorkerGlobalScope.postMessage</code> and <code>WorkerGlobalScope.onmessage</code> results in a message being sent to the first context that opened the shared worker (or nothing if that context is dead).
** The same as normal <code>dump()</code> for DOM scopes. <code>aString</code> is output to the console.


* <code>postMessageToPool(in DOMString aMessage)</code>
Downsides with this proposal:
** The same as calling <code>postMessage</code> on the <code>nsIDOMWorkerPool</code> object that created the worker.
* There is no way to communicate back unless first communicated to.
* The first one to instantiate a shared worker get special treatment.


=== Sample usage ===
=== Proposal 2 ===


<blockquote>
<blockquote>
<code>
<code>
<pre>
<pre>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <title>Test threads</title>
  <body>
  <script language="javascript">
    var scriptToRun = 'function messageListener(message, source) { ' +
                      '  dump("Worker: " + message + "\\n"); ' +
                      '} ' +
                      'thisThread.messageListener = messageListener; ' +
                      'for (var i = 0; i < 10000000; i++) { ' +
                      ' /* do something */ ' +
                      '} '+
                      'sendMessageToPool("Done!"); ';


    var wp = navigator.newWorkerPool();
[NoInterfaceObject] interface WorkerFactory {
    wp.messageListener = function(message, source) {
  ...
      dump("Pool: " + message + "\n");
  MessagePort connectToSharedWorker(in DOMString name, in DOMString scriptURL);
    };
};
    wp.errorListener = function(error, source) {
      dump("Pool: error = '" + error + "'\n");
    }


    var threads = []
[NoInterfaceObject] interface WorkerGlobalScope {
    for (var i = 0; i < 10; i++) {
  ...
      var thread = wp.createWorker(scriptToRun);
          attribute EventListener onconnect;
      thread.postMessage("hello");
};
      threads.push(thread);
    }
 
  </script>
  </body>
</html>


</pre>
</pre>
Line 191: Line 160:
</blockquote>
</blockquote>


== Not In This Specification ==
<code>connectToSharedWorker</code> creates a two new <code>MessagePort</code> which are entangled with each other. One of the two ports is returned, and the other is sent to the <code>Worker</code> with the given name. If such a <code>Worker</code> does not yet exist, one is created.
 
== References ==


The only thing I've seen so far is the [http://code.google.com/apis/gears/api_workerpool.html Google Gears WorkerPool API]. We would certainly like to provide a compatibility API that would make migrating Gears code trivial.
The <code>Worker</code> receives the other <code>MessagePort</code> through an <code>onconnect</code> <code>Event</code> fired on the <code>WorkerGlobalScope</code> object.


== Acknowledgements ==
* There is no way to get a reference to the shared <code>Worker</code> object itself.
* Communication for shared workers is different from communication for non-shared ones.

Latest revision as of 16:58, 20 August 2008

Web Worker API Proposal

API Proposal


[NoInterfaceObject] interface WorkerFactory {
  Worker createWorker(in DOMString scriptURL);
};

interface Worker {
  void postMessage(in DOMString aMessage);

  // event handler attributes
           attribute EventListener onmessage;
           attribute EventListener onerror;
           attribute EventListener onload;
           attribute EventListener onunload;
};


[NoInterfaceObject] interface WorkerGlobalScope
{
  readonly attribute WorkerGlobalScope self;
  readonly attribute WorkerLocation location;
  readonly attribute boolean closing;
  void close();

  // event handler attributes
           attribute EventListener onunload;

  // WorkerUtils
  void importScripts([Variadic] in DOMString urls);
  readonly attribute Storage localStorage;
  Database openDatabase(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize);
  void showNotification(in DOMString title, in DOMString subtitle, in DOMString description);
  void showNotification(in DOMString title, in DOMString subtitle, in DOMString description, in VoidCallback onclick);
};

[NoInterfaceObject] interface WorkerLocation {
  readonly attribute DOMString href;
  readonly attribute DOMString protocol;
  readonly attribute DOMString host;
  readonly attribute DOMString hostname;
  readonly attribute DOMString port;
  readonly attribute DOMString pathname;
  readonly attribute DOMString search;
  readonly attribute DOMString hash;
};

Sample usage

This is a very suboptimal way of calculating a number in the Fibonacci sequence.

Main page:

worker = createWorker("f.js");
worker.onmessage = function(e) {
  alert("The 100th Fibonacci number is " + e.data);
}
worker.postMessage(100);

f.js:

parent.onmessage = function(e) {
  if (e.data <= 1)
    postMessage(e.data);

  w1 = createWorker("f.js");
  w1.onmessage = handler;
  w1.postMessage(e.data - 1);
  
  w2 = createWorker("f.js");
  w2.onmessage = handler;
  w2.postMessage(e.data - 2);
}

c = 0;
sum = 0;

function handler(e) {
  sum += parseInt(e.data);
  if (++c == 2) {
    postMessage(sum);
  }
}

References

The only thing I've seen so far is the Google Gears WorkerPool API. We would certainly like to provide a API that would make migrating Gears code trivial.

API Proposal For Shared Workers

If we want to support shared workers in the initial release of this spec, here are two proposals that will work with the above initial API.

Proposal 1


[NoInterfaceObject] interface WorkerFactory {
  ...
  Worker createSharedWorker(in DOMString name, in DOMString scriptURL);
};

[NoInterfaceObject] interface WorkerParent {
  void postMessage(in DOMString aMessage);
  attribute EventListener onmessage;
};

createSharedWorker creates a new Worker object that interacts with the same WorkerGlobalScope as any previously existing Workers.

When a shared Worker receives a message it can send data back to the sender using the Event.source property which is a WorkerParent.

Using WorkerGlobalScope.postMessage and WorkerGlobalScope.onmessage results in a message being sent to the first context that opened the shared worker (or nothing if that context is dead).

Downsides with this proposal:

  • There is no way to communicate back unless first communicated to.
  • The first one to instantiate a shared worker get special treatment.

Proposal 2


[NoInterfaceObject] interface WorkerFactory {
  ...
  MessagePort connectToSharedWorker(in DOMString name, in DOMString scriptURL);
};

[NoInterfaceObject] interface WorkerGlobalScope {
  ...
           attribute EventListener onconnect;  
};

connectToSharedWorker creates a two new MessagePort which are entangled with each other. One of the two ports is returned, and the other is sent to the Worker with the given name. If such a Worker does not yet exist, one is created.

The Worker receives the other MessagePort through an onconnect Event fired on the WorkerGlobalScope object.

  • There is no way to get a reference to the shared Worker object itself.
  • Communication for shared workers is different from communication for non-shared ones.