mozilla

Revision 19270 of Using web workers

  • Revision slug: Talk:DOM/Using_web_workers
  • Revision title: Using web workers
  • Revision id: 19270
  • Created:
  • Creator: FunPackedShow
  • Is current revision? No
  • Comment 110 words added

Revision Content

Workers in extensions - Threadsafety

Workers in extensions don't have access to the the Components object from their thread making it nearly impossible to create XPCOM objects in a worker. Objects passed to workers will always be sent as JSON making it impossible to inject one into the worker thread. As such I can't think of any special reason you'd have to worry about threadsafety in extensions differently than normal web pages. If someone does need to use XPCOM objects in threaded code they can use the Thread Manager instead, and will need to look into thread-safety for the created objects.

I'm certainly not an expert on workers, threads or on Mozilla's use of them though, so I'm refraining from changing this article. Maybe there is some extra worry about threads in extensions, or some way to create Components/Services that I'm not aware of.

Posting messages both ways

I know that I can postMessage from a worker to its 'parent' by calling postMessage(). Can I post from the parent back to the worker by worker.postMessage() ?

Similarly, I can set worker.onmessage = function(){ ... } to handle messages from the worker. Am I right in thinking that you can (inside the worker) set onmessage = function(){ ... } to handle messages sent TO the worker as above?

I couldn't find any (explicit) info on this on MDC, so maybe this is worth adding.

Revision Source

<h3>Workers in extensions - Threadsafety</h3>
<p>Workers in extensions don't have access to the the <a class="internal" href="/en/Components_object" title="En/Components object">Components object</a> from their thread making it nearly impossible to create <a class="internal" href="/en/XPCOM" title="En/XPCOM">XPCOM</a> objects in a worker. Objects passed to workers will always be sent as <a class="internal" href="/en/JSON" title="En/JSON">JSON</a> making it impossible to inject one into the worker thread. As such I can't think of any special reason you'd have to worry about threadsafety in extensions differently than normal web pages. If someone does need to use <a class="internal" href="/en/XPCOM" title="En/XPCOM">XPCOM</a> objects in threaded code they can use <a class="internal" href="/en/The_Thread_Manager" title="En/The Thread Manager">the Thread Manager</a> instead, and will need to look into thread-safety for the created objects.</p>
<p>I'm certainly not an expert on workers, threads or on Mozilla's use of them though, so I'm refraining from changing this article. Maybe there is some extra worry about threads in extensions, or some way to create Components/Services that I'm not aware of.</p>
<h2>Posting messages both ways</h2>
<p>I know that I can postMessage from a worker to its 'parent' by calling postMessage(). Can I post from the parent back to the worker by worker.postMessage() ?</p>
<p>Similarly, I can set worker.onmessage = function(){ ... } to handle messages from the worker. Am I right in thinking that you can (inside the worker) set onmessage = function(){ ... } to handle messages sent TO the worker as above?</p>
<p>I couldn't find any (explicit) info on this on MDC, so maybe this is worth adding.</p>
Revert to this revision