From b369945350896e06543ed991f603114f505dd720 Mon Sep 17 00:00:00 2001 From: "darin%meer.net" Date: Thu, 9 Mar 2006 18:45:31 +0000 Subject: [PATCH] porting ipc/ipcd git-svn-id: svn://10.0.0.236/branches/THREADS_20060307_BRANCH@192081 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/ipc/ipcd/client/src/ipcdclient.cpp | 103 +++++++----------- .../extensions/dconnect/test/TestClient.js | 9 -- .../extensions/dconnect/test/TestDConnect.cpp | 25 +---- .../extensions/dconnect/test/TestServer.js | 14 +-- .../ipc/ipcd/extensions/lock/test/Makefile.in | 1 + .../extensions/lock/test/TestIPCLocks.cpp | 18 +-- .../transmngr/test/tmModuleTest.cpp | 28 ++--- mozilla/ipc/ipcd/test/TestIPC.cpp | 27 +---- 8 files changed, 63 insertions(+), 162 deletions(-) diff --git a/mozilla/ipc/ipcd/client/src/ipcdclient.cpp b/mozilla/ipc/ipcd/client/src/ipcdclient.cpp index aa28e969e5b..2cc3d90c478 100644 --- a/mozilla/ipc/ipcd/client/src/ipcdclient.cpp +++ b/mozilla/ipc/ipcd/client/src/ipcdclient.cpp @@ -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 observer; - // the message observer is called via this event queue - nsCOMPtr eventQ; + // the message observer is called on this thread + nsCOMPtr 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 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; iclientObservers.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 status(msg); - PostEventToMainThread(new ipcEvent_ClientState(status->ClientID(), - status->ClientState())); + nsCOMPtr ev = + new ipcEvent_ClientState(status->ClientID(), + status->ClientState()); + NS_DispatchToMainThread(ev); return; } } diff --git a/mozilla/ipc/ipcd/extensions/dconnect/test/TestClient.js b/mozilla/ipc/ipcd/extensions/dconnect/test/TestClient.js index 8403fcdd4ee..646b985d638 100644 --- a/mozilla/ipc/ipcd/extensions/dconnect/test/TestClient.js +++ b/mozilla/ipc/ipcd/extensions/dconnect/test/TestClient.js @@ -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(); diff --git a/mozilla/ipc/ipcd/extensions/dconnect/test/TestDConnect.cpp b/mozilla/ipc/ipcd/extensions/dconnect/test/TestDConnect.cpp index 8eabe13c60c..9c09120bd2f 100644 --- a/mozilla/ipc/ipcd/extensions/dconnect/test/TestDConnect.cpp +++ b/mozilla/ipc/ipcd/extensions/dconnect/test/TestDConnect.cpp @@ -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 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 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 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 diff --git a/mozilla/ipc/ipcd/extensions/dconnect/test/TestServer.js b/mozilla/ipc/ipcd/extensions/dconnect/test/TestServer.js index a72e133bc9f..56cb8244448 100644 --- a/mozilla/ipc/ipcd/extensions/dconnect/test/TestServer.js +++ b/mozilla/ipc/ipcd/extensions/dconnect/test/TestServer.js @@ -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(); diff --git a/mozilla/ipc/ipcd/extensions/lock/test/Makefile.in b/mozilla/ipc/ipcd/extensions/lock/test/Makefile.in index 93d1f48d0e8..4e77914eb0c 100644 --- a/mozilla/ipc/ipcd/extensions/lock/test/Makefile.in +++ b/mozilla/ipc/ipcd/extensions/lock/test/Makefile.in @@ -48,6 +48,7 @@ MOZILLA_INTERNAL_API = 1 REQUIRES = ipcd \ nspr \ xpcom \ + string \ $(NULL) CPPSRCS = \ diff --git a/mozilla/ipc/ipcd/extensions/lock/test/TestIPCLocks.cpp b/mozilla/ipc/ipcd/extensions/lock/test/TestIPCLocks.cpp index 907a1e76dc2..b723e43645b 100644 --- a/mozilla/ipc/ipcd/extensions/lock/test/TestIPCLocks.cpp +++ b/mozilla/ipc/ipcd/extensions/lock/test/TestIPCLocks.cpp @@ -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 eqs = - do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv); - if (NS_FAILED(rv)) - return rv; - - rv = eqs->CreateMonitoredThreadEventQueue(); - if (NS_FAILED(rv)) - return rv; - - nsCOMPtr eq; - rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(eq)); - if (NS_FAILED(rv)) - return rv; - nsCOMPtr lockService = do_GetService(IPC_LOCKSERVICE_CONTRACTID); diff --git a/mozilla/ipc/ipcd/extensions/transmngr/test/tmModuleTest.cpp b/mozilla/ipc/ipcd/extensions/transmngr/test/tmModuleTest.cpp index 5d1dccd4d65..8a5bd2714fd 100644 --- a/mozilla/ipc/ipcd/extensions/transmngr/test/tmModuleTest.cpp +++ b/mozilla/ipc/ipcd/extensions/transmngr/test/tmModuleTest.cpp @@ -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 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 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 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 diff --git a/mozilla/ipc/ipcd/test/TestIPC.cpp b/mozilla/ipc/ipcd/test/TestIPC.cpp index 1100d674f0e..02e89148de3 100644 --- a/mozilla/ipc/ipcd/test/TestIPC.cpp +++ b/mozilla/ipc/ipcd/test/TestIPC.cpp @@ -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 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 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 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