Revision 165302 of User:Mossop

  • Revision slug: User:Mossop
  • Revision title: User:Mossop
  • Revision id: 165302
  • Created:
  • Creator: Mossop
  • Is current revision? No
  • Comment

Revision Content

Work in progress. This code seems to work in general but I found that it would hang the browser often while calling the callbacks.

function myThread(proxy)
{
  this.proxy = proxy;
}

myThread.prototype = {
proxy: null,

// This runs in its own thread, but cannot affect any DOM or UI except by calls on the proxy object
run: function()
{
  // long running code
  this.proxy.method1();
  // long running code
  this.proxy.method2();
  // long running code
  this.proxy.method3();
},

QueryInterface: function(iid)
{
  if (iid.equals(Components.interfaces.nsIRunnable)
   || iid.equals(Components.interfaces.nsISupports))
  {
    return this;
  }
  else
  {
    throw Components.results.NS_ERROR_NO_INTERFACE;
  }
}
}

myComponent = {

startThread: function()
{
  // Retrieves the event queue for the UI thread
  var eQs = Components.classes["@mozilla.org/event-queue-service;1"]
                      .getService(Components.interfaces.nsIEventQueueService);
  var eQueue = eQs.getSpecialEventQueue(Components.interfaces.nsIEventQueueService.CURRENT_THREAD_EVENT_QUEUE);
  
  // Creates a proxy for this object that will make calls on the UI event queue.
  var nsIPOM = Components.interfaces.nsIProxyObjectManager;
  var proxyManager = Components.classes["@mozilla.org/xpcomproxy;1"]
                               .getService(nsIPOM);
  var proxy = proxyManager.getProxyForObject(eQueue, 
                                             Components.interfaces.myComponent, 
                                             this, 
                                             nsIPOM.INVOKE_SYNC + nsIPOM.FORCE_PROXY_CREATION);

  // Creates the thread
  var runnable = new myThread(proxy);
  var nsIThread = Components.interfaces.nsIThread;
  var thread = Components.classes["@mozilla.org/thread;1"]
		         .createInstance(nsIThread);
  
  // Starts the thread
  // These settings came from some source I found, they may not be correct.
  thread.init(runnable, 0, nsIThread.PRIORITY_NORMAL, nsIThread.SCOPE_GLOBAL, nsIThread.STATE_JOINABLE);
},

// Must be declared in the myComponent interface idl
method1: function()
{
  // This runs in the main UI thread
},

// Must be declared in the myComponent interface idl
method2: function()
{
  // This runs in the main UI thread
},

// Must be declared in the myComponent interface idl
method3: function()
{
  // This runs in the main UI thread
},

QueryInterface: function(iid)
{
  if (iid.equals(Components.interfaces.myComponent)
   || iid.equals(Components.interfaces.nsISupports))
  {
    return this;
  }
  else
  {
    throw Components.results.NS_ERROR_NO_INTERFACE;
  }
}
}

Revision Source

<p>Work in progress. This code seems to work in general but I found that it would hang the browser often while calling the callbacks.
</p>
<pre>function myThread(proxy)
{
  this.proxy = proxy;
}

myThread.prototype = {
proxy: null,

// This runs in its own thread, but cannot affect any DOM or UI except by calls on the proxy object
run: function()
{
  // long running code
  this.proxy.method1();
  // long running code
  this.proxy.method2();
  // long running code
  this.proxy.method3();
},

QueryInterface: function(iid)
{
  if (iid.equals(Components.interfaces.nsIRunnable)
   || iid.equals(Components.interfaces.nsISupports))
  {
    return this;
  }
  else
  {
    throw Components.results.NS_ERROR_NO_INTERFACE;
  }
}
}

myComponent = {

startThread: function()
{
  // Retrieves the event queue for the UI thread
  var eQs = Components.classes["@mozilla.org/event-queue-service;1"]
                      .getService(Components.interfaces.nsIEventQueueService);
  var eQueue = eQs.getSpecialEventQueue(Components.interfaces.nsIEventQueueService.CURRENT_THREAD_EVENT_QUEUE);
  
  // Creates a proxy for this object that will make calls on the UI event queue.
  var nsIPOM = Components.interfaces.nsIProxyObjectManager;
  var proxyManager = Components.classes["@mozilla.org/xpcomproxy;1"]
                               .getService(nsIPOM);
  var proxy = proxyManager.getProxyForObject(eQueue, 
                                             Components.interfaces.myComponent, 
                                             this, 
                                             nsIPOM.INVOKE_SYNC + nsIPOM.FORCE_PROXY_CREATION);

  // Creates the thread
  var runnable = new myThread(proxy);
  var nsIThread = Components.interfaces.nsIThread;
  var thread = Components.classes["@mozilla.org/thread;1"]
		         .createInstance(nsIThread);
  
  // Starts the thread
  // These settings came from some source I found, they may not be correct.
  thread.init(runnable, 0, nsIThread.PRIORITY_NORMAL, nsIThread.SCOPE_GLOBAL, nsIThread.STATE_JOINABLE);
},

// Must be declared in the myComponent interface idl
method1: function()
{
  // This runs in the main UI thread
},

// Must be declared in the myComponent interface idl
method2: function()
{
  // This runs in the main UI thread
},

// Must be declared in the myComponent interface idl
method3: function()
{
  // This runs in the main UI thread
},

QueryInterface: function(iid)
{
  if (iid.equals(Components.interfaces.myComponent)
   || iid.equals(Components.interfaces.nsISupports))
  {
    return this;
  }
  else
  {
    throw Components.results.NS_ERROR_NO_INTERFACE;
  }
}
}

</pre>
Revert to this revision