porting ipc/ipcd
git-svn-id: svn://10.0.0.236/branches/THREADS_20060307_BRANCH@192081 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
@@ -44,7 +44,7 @@
|
||||
#include "ipcm.h"
|
||||
|
||||
#include "nsIFile.h"
|
||||
#include "nsEventQueueUtils.h"
|
||||
#include "nsThreadUtils.h"
|
||||
#include "nsDirectoryServiceUtils.h"
|
||||
#include "nsDirectoryServiceDefs.h"
|
||||
#include "nsCOMPtr.h"
|
||||
@@ -81,8 +81,8 @@ public:
|
||||
// this may be null
|
||||
nsCOMPtr<ipcIMessageObserver> observer;
|
||||
|
||||
// the message observer is called via this event queue
|
||||
nsCOMPtr<nsIEventQueue> eventQ;
|
||||
// the message observer is called on this thread
|
||||
nsCOMPtr<nsIThread> thread;
|
||||
|
||||
// incoming messages are added to this list
|
||||
ipcMessageQ pendingQ;
|
||||
@@ -130,9 +130,9 @@ ipcTargetData::SetObserver(ipcIMessageObserver *aObserver, PRBool aOnCurrentThre
|
||||
observer = aObserver;
|
||||
|
||||
if (aOnCurrentThread)
|
||||
NS_GetCurrentEventQ(getter_AddRefs(eventQ));
|
||||
NS_GetCurrentThread(getter_AddRefs(thread));
|
||||
else
|
||||
eventQ = nsnull;
|
||||
thread = nsnull;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
@@ -400,65 +400,27 @@ WaitTarget(const nsID &aTarget,
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
static void
|
||||
PostEvent(nsIEventTarget *eventTarget, PLEvent *ev)
|
||||
{
|
||||
if (!ev)
|
||||
return;
|
||||
|
||||
nsresult rv = eventTarget->PostEvent(ev);
|
||||
if (NS_FAILED(rv))
|
||||
{
|
||||
NS_WARNING("PostEvent failed");
|
||||
PL_DestroyEvent(ev);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
PostEventToMainThread(PLEvent *ev)
|
||||
{
|
||||
nsCOMPtr<nsIEventQueue> eventQ;
|
||||
NS_GetMainEventQ(getter_AddRefs(eventQ));
|
||||
if (!eventQ)
|
||||
{
|
||||
NS_WARNING("unable to get reference to main event queue");
|
||||
PL_DestroyEvent(ev);
|
||||
return;
|
||||
}
|
||||
PostEvent(eventQ, ev);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
class ipcEvent_ClientState : public PLEvent
|
||||
class ipcEvent_ClientState : public nsRunnable
|
||||
{
|
||||
public:
|
||||
ipcEvent_ClientState(PRUint32 aClientID, PRUint32 aClientState)
|
||||
: mClientID(aClientID)
|
||||
, mClientState(aClientState)
|
||||
{
|
||||
PL_InitEvent(this, nsnull, HandleEvent, DestroyEvent);
|
||||
}
|
||||
|
||||
PR_STATIC_CALLBACK(void *) HandleEvent(PLEvent *ev)
|
||||
NS_IMETHOD Run()
|
||||
{
|
||||
// maybe we've been shutdown!
|
||||
if (!gClientState)
|
||||
return nsnull;
|
||||
|
||||
ipcEvent_ClientState *self = (ipcEvent_ClientState *) ev;
|
||||
|
||||
for (PRInt32 i=0; i<gClientState->clientObservers.Count(); ++i)
|
||||
gClientState->clientObservers[i]->OnClientStateChange(self->mClientID,
|
||||
self->mClientState);
|
||||
gClientState->clientObservers[i]->OnClientStateChange(mClientID,
|
||||
mClientState);
|
||||
return nsnull;
|
||||
}
|
||||
|
||||
PR_STATIC_CALLBACK(void) DestroyEvent(PLEvent *ev)
|
||||
{
|
||||
delete (ipcEvent_ClientState *) ev;
|
||||
}
|
||||
|
||||
private:
|
||||
PRUint32 mClientID;
|
||||
PRUint32 mClientState;
|
||||
@@ -466,48 +428,55 @@ private:
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
class ipcEvent_ProcessPendingQ : public PLEvent
|
||||
class ipcEvent_ProcessPendingQ : public nsRunnable
|
||||
{
|
||||
public:
|
||||
ipcEvent_ProcessPendingQ(const nsID &aTarget)
|
||||
: mTarget(aTarget)
|
||||
{
|
||||
PL_InitEvent(this, nsnull, HandleEvent, DestroyEvent);
|
||||
}
|
||||
|
||||
PR_STATIC_CALLBACK(void *) HandleEvent(PLEvent *ev)
|
||||
NS_IMETHOD Run()
|
||||
{
|
||||
ProcessPendingQ(((ipcEvent_ProcessPendingQ *) ev)->mTarget);
|
||||
return nsnull;
|
||||
}
|
||||
|
||||
PR_STATIC_CALLBACK(void) DestroyEvent(PLEvent *ev)
|
||||
{
|
||||
delete (ipcEvent_ProcessPendingQ *) ev;
|
||||
ProcessPendingQ(mTarget);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
private:
|
||||
const nsID mTarget;
|
||||
};
|
||||
|
||||
static void
|
||||
RunEvent(void *arg)
|
||||
{
|
||||
nsIRunnable *ev = NS_STATIC_CAST(nsIRunnable *, arg);
|
||||
ev->Run();
|
||||
NS_RELEASE(ev);
|
||||
}
|
||||
|
||||
static void
|
||||
CallProcessPendingQ(const nsID &target, ipcTargetData *td)
|
||||
{
|
||||
// we assume that we are inside td's monitor
|
||||
|
||||
PLEvent *ev = new ipcEvent_ProcessPendingQ(target);
|
||||
nsIRunnable *ev = new ipcEvent_ProcessPendingQ(target);
|
||||
if (!ev)
|
||||
return;
|
||||
NS_ADDREF(ev);
|
||||
|
||||
nsresult rv;
|
||||
|
||||
if (td->eventQ)
|
||||
rv = td->eventQ->PostEvent(ev);
|
||||
else
|
||||
rv = IPC_DoCallback((ipcCallbackFunc) PL_HandleEvent, ev);
|
||||
if (td->thread)
|
||||
{
|
||||
rv = td->thread->Dispatch(ev, NS_DISPATCH_NORMAL);
|
||||
NS_RELEASE(ev);
|
||||
}
|
||||
else
|
||||
{
|
||||
rv = IPC_DoCallback(RunEvent, ev);
|
||||
}
|
||||
|
||||
if (NS_FAILED(rv))
|
||||
PL_DestroyEvent(ev);
|
||||
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "failed to process pending queue");
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
@@ -1087,8 +1056,10 @@ IPC_OnMessageAvailable(ipcMessage *msg)
|
||||
case IPCM_MSG_PSH_CLIENT_STATE:
|
||||
{
|
||||
ipcMessageCast<ipcmMessageClientState> status(msg);
|
||||
PostEventToMainThread(new ipcEvent_ClientState(status->ClientID(),
|
||||
status->ClientState()));
|
||||
nsCOMPtr<nsIRunnable> ev =
|
||||
new ipcEvent_ClientState(status->ClientID(),
|
||||
status->ClientState());
|
||||
NS_DispatchToMainThread(ev);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,6 @@ const ipcIService = Components.interfaces.ipcIService;
|
||||
const ipcIDConnectService = Components.interfaces.ipcIDConnectService;
|
||||
const nsIFile = Components.interfaces.nsIFile;
|
||||
const nsILocalFile = Components.interfaces.nsILocalFile;
|
||||
const nsIEventQueueService = Components.interfaces.nsIEventQueueService;
|
||||
|
||||
// XXX use directory service for this
|
||||
const TEST_PATH = "/tmp";
|
||||
@@ -92,14 +91,6 @@ function doTest()
|
||||
dump("localObj.equals(file) = " + localObj.equals(file) + "\n");
|
||||
}
|
||||
|
||||
function setupEventQ()
|
||||
{
|
||||
var eqs = Components.classes["@mozilla.org/event-queue-service;1"]
|
||||
.getService(nsIEventQueueService);
|
||||
eqs.createMonitoredThreadEventQueue();
|
||||
}
|
||||
|
||||
setupEventQ();
|
||||
findServer();
|
||||
dump("\n---------------------------------------------------\n");
|
||||
doTest();
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
#include "ipcIDConnectService.h"
|
||||
#include "ipcCID.h"
|
||||
|
||||
#include "nsIEventQueueService.h"
|
||||
#include "nsThreadUtils.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIComponentRegistrar.h"
|
||||
|
||||
@@ -63,8 +63,6 @@
|
||||
} \
|
||||
PR_END_MACRO
|
||||
|
||||
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
||||
static nsIEventQueue* gEventQ = nsnull;
|
||||
static PRBool gKeepRunning = PR_TRUE;
|
||||
|
||||
static ipcIService *gIpcServ = nsnull;
|
||||
@@ -219,17 +217,6 @@ int main(int argc, char **argv)
|
||||
if (registrar)
|
||||
registrar->AutoRegister(nsnull);
|
||||
|
||||
// Create the Event Queue for this thread...
|
||||
nsCOMPtr<nsIEventQueueService> eqs =
|
||||
do_GetService(kEventQueueServiceCID, &rv);
|
||||
RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
|
||||
|
||||
rv = eqs->CreateMonitoredThreadEventQueue();
|
||||
RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
|
||||
|
||||
rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
||||
RETURN_IF_FAILED(rv, "GetThreadEventQueue");
|
||||
|
||||
nsCOMPtr<ipcIService> ipcServ(do_GetService(IPC_SERVICE_CONTRACTID, &rv));
|
||||
RETURN_IF_FAILED(rv, "do_GetService(ipcServ)");
|
||||
NS_ADDREF(gIpcServ = ipcServ);
|
||||
@@ -244,20 +231,16 @@ int main(int argc, char **argv)
|
||||
gIpcServ->AddName("DConnectServer");
|
||||
}
|
||||
|
||||
PLEvent *ev;
|
||||
nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
|
||||
while (gKeepRunning)
|
||||
{
|
||||
gEventQ->WaitForEvent(&ev);
|
||||
gEventQ->HandleEvent(ev);
|
||||
}
|
||||
thread->ProcessNextEvent();
|
||||
|
||||
NS_RELEASE(gIpcServ);
|
||||
|
||||
printf("*** processing remaining events\n");
|
||||
|
||||
// process any remaining events
|
||||
while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
|
||||
gEventQ->HandleEvent(ev);
|
||||
NS_ProcessPendingEvents(thread);
|
||||
|
||||
printf("*** done\n");
|
||||
} // this scopes the nsCOMPtrs
|
||||
|
||||
@@ -41,9 +41,7 @@
|
||||
* can connect and control this process.
|
||||
*/
|
||||
|
||||
const ipcIService = Components.interfaces.ipcIService;
|
||||
const nsIEventQueueService = Components.interfaces.nsIEventQueueService;
|
||||
const nsIEventQueue = Components.interfaces.nsIEventQueue;
|
||||
const ipcIService = Components.interfaces.ipcIService;
|
||||
|
||||
function registerServer()
|
||||
{
|
||||
@@ -53,13 +51,13 @@ function registerServer()
|
||||
|
||||
function runEventQ()
|
||||
{
|
||||
var eqs = Components.classes["@mozilla.org/event-queue-service;1"]
|
||||
.getService(nsIEventQueueService);
|
||||
eqs.createMonitoredThreadEventQueue();
|
||||
var queue = eqs.getSpecialEventQueue(eqs.CURRENT_THREAD_EVENT_QUEUE);
|
||||
var thread =
|
||||
Components.classes["@mozilla.org/thread-manager;1"].
|
||||
getService().currentThread;
|
||||
|
||||
// this never returns
|
||||
queue.eventLoop();
|
||||
while (true)
|
||||
thread.processNextEvent();
|
||||
}
|
||||
|
||||
registerServer();
|
||||
|
||||
@@ -48,6 +48,7 @@ MOZILLA_INTERNAL_API = 1
|
||||
REQUIRES = ipcd \
|
||||
nspr \
|
||||
xpcom \
|
||||
string \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = \
|
||||
|
||||
@@ -47,7 +47,7 @@
|
||||
#include "ipcILockService.h"
|
||||
#include "ipcLockCID.h"
|
||||
#include "nsServiceManagerUtils.h"
|
||||
#include "nsIEventQueueService.h"
|
||||
#include "nsThreadUtils.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsXPCOM.h"
|
||||
#include "prproces.h"
|
||||
@@ -144,22 +144,6 @@ static const char *kLockNames[] = {
|
||||
|
||||
static nsresult DoTest()
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
nsCOMPtr<nsIEventQueueService> eqs =
|
||||
do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
rv = eqs->CreateMonitoredThreadEventQueue();
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
nsCOMPtr<nsIEventQueue> eq;
|
||||
rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(eq));
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
nsCOMPtr<ipcILockService> lockService =
|
||||
do_GetService(IPC_LOCKSERVICE_CONTRACTID);
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
|
||||
// core & xpcom ns includes
|
||||
#include "nsDebug.h"
|
||||
#include "nsIEventQueueService.h"
|
||||
#include "nsThreadUtils.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIComponentRegistrar.h"
|
||||
#include "nsString.h"
|
||||
@@ -80,8 +80,6 @@ PRUint32 dataLen = 10; // includes the null terminator for "test data
|
||||
} \
|
||||
PR_END_MACRO
|
||||
|
||||
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
||||
static nsIEventQueue* gEventQ = nsnull;
|
||||
static PRBool gKeepRunning = PR_TRUE;
|
||||
//static PRInt32 gMsgCount = 0;
|
||||
static ipcIService *gIpcServ = nsnull;
|
||||
@@ -195,16 +193,6 @@ int main(PRInt32 argc, char *argv[])
|
||||
if (registrar)
|
||||
registrar->AutoRegister(nsnull);
|
||||
|
||||
// Create the Event Queue for this thread...
|
||||
nsCOMPtr<nsIEventQueueService> eqs = do_GetService(kEventQueueServiceCID, &rv);
|
||||
RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
|
||||
|
||||
rv = eqs->CreateMonitoredThreadEventQueue();
|
||||
RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
|
||||
|
||||
rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
||||
RETURN_IF_FAILED(rv, "GetThreadEventQueue");
|
||||
|
||||
// Need to make sure the ipc system has been started
|
||||
printf("tmModuleTest: getting ipc service\n");
|
||||
nsCOMPtr<ipcIService> ipcServ(do_GetService("@mozilla.org/ipc/service;1", &rv));
|
||||
@@ -229,6 +217,7 @@ int main(PRInt32 argc, char *argv[])
|
||||
gTransServ->Attach(nsDependentCString(queueName), observ, PR_TRUE);
|
||||
printf("tmModuleTest: observing queue [%s]\n", queueName);
|
||||
|
||||
nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
|
||||
|
||||
// run specific patterns based on the mode
|
||||
int i = 0; // wasn't working inside the cases
|
||||
@@ -242,7 +231,7 @@ int main(PRInt32 argc, char *argv[])
|
||||
}
|
||||
// listen for events
|
||||
while (gKeepRunning)
|
||||
gEventQ->ProcessPendingEvents();
|
||||
thread->ProcessNextEvent();
|
||||
printf("tmModuleTest: end standard\n");
|
||||
break;
|
||||
case 'b':
|
||||
@@ -253,7 +242,7 @@ int main(PRInt32 argc, char *argv[])
|
||||
}
|
||||
// listen for events
|
||||
while (gKeepRunning)
|
||||
gEventQ->ProcessPendingEvents();
|
||||
thread->ProcessNextEvent();
|
||||
printf("tmModuleTest: end broadcast\n");
|
||||
break;
|
||||
case 'f':
|
||||
@@ -270,7 +259,7 @@ int main(PRInt32 argc, char *argv[])
|
||||
}
|
||||
// listen for events
|
||||
while (gKeepRunning)
|
||||
gEventQ->ProcessPendingEvents();
|
||||
thread->ProcessNextEvent();
|
||||
// detach
|
||||
gTransServ->Detach(nsDependentCString(queueName));
|
||||
printf("tmModuleTest: end flush\n");
|
||||
@@ -289,7 +278,7 @@ int main(PRInt32 argc, char *argv[])
|
||||
printf("tmModuleTest: start listener\n");
|
||||
// listen for events
|
||||
while (gKeepRunning)
|
||||
gEventQ->ProcessPendingEvents();
|
||||
thread->ProcessNextEvent();
|
||||
printf("tmModuleTest: end listener\n");
|
||||
break;
|
||||
default :
|
||||
@@ -305,9 +294,8 @@ int main(PRInt32 argc, char *argv[])
|
||||
printf("tmModuleTest: processing remaining events\n");
|
||||
|
||||
// process any remaining events
|
||||
PLEvent *ev;
|
||||
while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
|
||||
gEventQ->HandleEvent(ev);
|
||||
|
||||
NS_ProcessPendingEvents(thread);
|
||||
|
||||
printf("tmModuleTest: done\n");
|
||||
} // this scopes the nsCOMPtrs
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
#include "ipcCID.h"
|
||||
#include "ipcLockCID.h"
|
||||
|
||||
#include "nsIEventQueueService.h"
|
||||
#include "nsThreadUtils.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIComponentRegistrar.h"
|
||||
|
||||
@@ -72,8 +72,6 @@ static const nsID kTestTargetID =
|
||||
} \
|
||||
PR_END_MACRO
|
||||
|
||||
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
||||
static nsIEventQueue* gEventQ = nsnull;
|
||||
static PRBool gKeepRunning = PR_TRUE;
|
||||
static ipcIService *gIpcServ = nsnull;
|
||||
static ipcILockService *gIpcLockServ = nsnull;
|
||||
@@ -201,17 +199,6 @@ int main(int argc, char **argv)
|
||||
if (registrar)
|
||||
registrar->AutoRegister(nsnull);
|
||||
|
||||
// Create the Event Queue for this thread...
|
||||
nsCOMPtr<nsIEventQueueService> eqs =
|
||||
do_GetService(kEventQueueServiceCID, &rv);
|
||||
RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
|
||||
|
||||
rv = eqs->CreateMonitoredThreadEventQueue();
|
||||
RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
|
||||
|
||||
rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
||||
RETURN_IF_FAILED(rv, "GetThreadEventQueue");
|
||||
|
||||
printf("*** getting ipc service\n");
|
||||
nsCOMPtr<ipcIService> ipcServ(do_GetService(IPC_SERVICE_CONTRACTID, &rv));
|
||||
RETURN_IF_FAILED(rv, "do_GetService(ipcServ)");
|
||||
@@ -313,19 +300,17 @@ int main(int argc, char **argv)
|
||||
rv = gIpcLockServ->AcquireLock("foo", PR_TRUE);
|
||||
printf("*** sync AcquireLock returned [rv=%x]\n", rv);
|
||||
|
||||
PLEvent *ev;
|
||||
while (gKeepRunning) {
|
||||
gEventQ->WaitForEvent(&ev);
|
||||
gEventQ->HandleEvent(ev);
|
||||
}
|
||||
nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
|
||||
|
||||
while (gKeepRunning)
|
||||
thread->ProcessNextEvent();
|
||||
|
||||
NS_RELEASE(gIpcServ);
|
||||
|
||||
printf("*** processing remaining events\n");
|
||||
|
||||
// process any remaining events
|
||||
while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
|
||||
gEventQ->HandleEvent(ev);
|
||||
NS_ProcessPendingEvents(thread);
|
||||
|
||||
printf("*** done\n");
|
||||
} // this scopes the nsCOMPtrs
|
||||
|
||||
Reference in New Issue
Block a user