Peter,

There is a way to do this - I'm just learning how myself. There is
a proxy mechanism that allows creating and talking to a component
that is owned by a different thread and event queue.

If you do some digging in the mozilla code, there is a proxy manager
(I think thats what it is called). You should either pass to or have
the other thread request a proxy object to your object. This does mean
that the worker thread will be blocked whenever calling a proxied
method that is stuck waiting for the main UI thread to service the
main event queue.

You could also experiment with having the object created on the
worker thread's event queue and have the main UI thread access
it via proxy.

I think the proxy mechanism offers the ability to either invoke the
message syncronously and return the real object's nsresult value or
it can merely post the method invocation to the event queue and
continue on immediately - watch out for automatic variables that
get popped off the stack before the asyncronous call completes -
ouch!

The requirements for a component to be proxy-able are the same
as making them scriptable (ie. interfaces defined in an IDL file).

For any MSCOM programmers out there: I think creating a worker
thread and then creating multiple proxied objects that get served
by the worker thread's event queue is analogous to what MSCOM
calls an Apartment Threading model.

Sometimes I wonder if it isn't easier to just code everything as
threadsafe/reentrant from the start.

Regards,
Rick

Peter Lee wrote:

> I'm currently making use of xpcom for some transport related things...
>
> I do the following in the main thread...
>
> NS_InitXPCOM(&m_serviceManager, binpath);
>
> Get some services
> rv = m_serviceManager->GetService(kEventQueueServiceCID,
> kEventQueueServiceIID,
>                                      (nsISupports**) &m_eventQService);
> rv = m_eventQService->CreateThreadEventQueue();
> rv = m_serviceManager->GetService(kIOServiceCID, kIOServiceIID,
>                                       (nsISupports**) &m_ioService);
>
> Next I use IOService to create new channel and do some things... this all
> works so long as it all happens on the main thread.
>
> However, there are some synchronous operations that might happen on a
> different thread... as soon as I call m_ioService->NewChannel on another
> thread I get assertions about owning thread etc..
>
> Obviously I can't pass interfaces between threads like this...  So my
> question is what is the right way?  In the COM world I would marshal the
> interface using different api's...  Is there some way in xpcom to marshal
> these interfaces between threads ?
>
> Insight appreciated,
>
> -pete


Reply via email to