From 6d38faff99467cbabe628d90bb89f02da54b33dc Mon Sep 17 00:00:00 2001 From: "darin%meer.net" Date: Thu, 16 Feb 2006 19:36:31 +0000 Subject: [PATCH] make things work better git-svn-id: svn://10.0.0.236/branches/THREADS_20060213_BRANCH@190227 18797224-902f-48f8-a5cc-f745e15eee43 --- .../base/src/nsSocketTransportService2.cpp | 8 +- mozilla/netwerk/build/nsNetModule.cpp | 2 +- mozilla/netwerk/cache/src/nsCacheService.cpp | 55 ++-- mozilla/netwerk/dns/src/nsDNSService2.cpp | 12 +- .../protocol/http/src/nsHttpTransaction.cpp | 4 - .../protocol/http/src/nsHttpTransaction.h | 1 - mozilla/netwerk/test/PropertiesTest.cpp | 11 - mozilla/netwerk/test/TestCallbacks.cpp | 22 +- mozilla/netwerk/test/TestCommon.h | 34 +++ mozilla/netwerk/test/TestIOThreads.cpp | 40 +-- .../netwerk/test/TestIncrementalDownload.cpp | 47 +--- mozilla/netwerk/test/TestPageLoad.cpp | 24 +- mozilla/netwerk/test/TestPerf.cpp | 43 +-- mozilla/netwerk/test/TestProtocols.cpp | 46 +--- mozilla/netwerk/test/TestServ.cpp | 20 +- mozilla/netwerk/test/TestSocketTransport.cpp | 61 +---- mozilla/netwerk/test/TestStreamChannel.cpp | 56 +--- mozilla/netwerk/test/TestStreamLoader.cpp | 19 +- mozilla/netwerk/test/TestStreamPump.cpp | 59 +---- mozilla/netwerk/test/TestStreamTransport.cpp | 56 +--- mozilla/netwerk/test/TestUpload.cpp | 20 +- mozilla/xpcom/base/nsConsoleService.cpp | 5 +- mozilla/xpcom/base/nsMemoryImpl.cpp | 12 +- mozilla/xpcom/build/nsXPComInit.cpp | 11 +- .../xpcom/components/nsCategoryManager.cpp | 5 +- mozilla/xpcom/proxy/src/nsProxyEvent.cpp | 8 +- mozilla/xpcom/tests/TestPipes.cpp | 14 +- mozilla/xpcom/tests/TestThreads.cpp | 16 +- mozilla/xpcom/threads/Makefile.in | 6 +- mozilla/xpcom/threads/TimerThread.cpp | 5 +- mozilla/xpcom/threads/nsIThread.idl | 17 -- mozilla/xpcom/threads/nsIThreadManager.idl | 4 +- mozilla/xpcom/threads/nsRunnable.h | 60 ----- mozilla/xpcom/threads/nsTaskQueue.h | 2 +- mozilla/xpcom/threads/nsThread.cpp | 5 +- mozilla/xpcom/threads/nsThread.h | 2 +- mozilla/xpcom/threads/nsThreadManager.cpp | 2 +- mozilla/xpcom/threads/nsThreadManager.h | 14 - .../{nsRunnable.cpp => nsThreadUtils.cpp} | 40 ++- mozilla/xpcom/threads/nsThreadUtils.h | 245 ++++++++++++++++++ mozilla/xpcom/threads/nsTimerImpl.cpp | 4 +- 41 files changed, 448 insertions(+), 669 deletions(-) delete mode 100644 mozilla/xpcom/threads/nsRunnable.h rename mozilla/xpcom/threads/{nsRunnable.cpp => nsThreadUtils.cpp} (66%) create mode 100644 mozilla/xpcom/threads/nsThreadUtils.h diff --git a/mozilla/netwerk/base/src/nsSocketTransportService2.cpp b/mozilla/netwerk/base/src/nsSocketTransportService2.cpp index 2ced12226a1..82d11bd31cd 100644 --- a/mozilla/netwerk/base/src/nsSocketTransportService2.cpp +++ b/mozilla/netwerk/base/src/nsSocketTransportService2.cpp @@ -497,9 +497,7 @@ nsSocketTransportService::Run() for (;;) { // process all pending tasks - do { - rv = thread->RunNextTask(nsIThread::RUN_NO_WAIT); - } while (rv != NS_BASE_STREAM_WOULD_BLOCK); + NS_RunPendingTasks(thread); // now that our task queue is empty, check to see if we should exit { @@ -523,9 +521,7 @@ nsSocketTransportService::Run() // Final pass over the event queue. This makes sure that events posted by // socket detach handlers get processed. - do { - rv = thread->RunNextTask(nsIThread::RUN_NO_WAIT); - } while (rv != NS_BASE_STREAM_WOULD_BLOCK); + NS_RunPendingTasks(thread); gSocketThread = nsnull; diff --git a/mozilla/netwerk/build/nsNetModule.cpp b/mozilla/netwerk/build/nsNetModule.cpp index f17169c04b6..bcf5cff2792 100644 --- a/mozilla/netwerk/build/nsNetModule.cpp +++ b/mozilla/netwerk/build/nsNetModule.cpp @@ -79,7 +79,7 @@ NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsDNSService, Init) NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsProtocolProxyService, Init) #include "nsStreamTransportService.h" -NS_GENERIC_FACTORY_CONSTRUCTOR(nsStreamTransportService) +NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsStreamTransportService, Init) #include "nsSocketTransportService2.h" #undef LOG diff --git a/mozilla/netwerk/cache/src/nsCacheService.cpp b/mozilla/netwerk/cache/src/nsCacheService.cpp index 8bd036b6ea5..2cb65271a6a 100644 --- a/mozilla/netwerk/cache/src/nsCacheService.cpp +++ b/mozilla/netwerk/cache/src/nsCacheService.cpp @@ -105,7 +105,7 @@ public: virtual ~nsCacheProfilePrefObserver() {} nsresult Install(); - nsresult Remove(); + void Remove(); nsresult ReadPrefs(nsIPrefBranch* branch); PRBool DiskCacheEnabled(); @@ -189,46 +189,31 @@ nsCacheProfilePrefObserver::Install() } -nsresult +void nsCacheProfilePrefObserver::Remove() { - nsresult rv, rv2 = NS_OK; - // remove Observer Service observers - nsCOMPtr observerService = do_GetService("@mozilla.org/observer-service;1", &rv); - if (NS_FAILED(rv)) return rv; - - rv = observerService->RemoveObserver(this, "profile-before-change"); - if (NS_FAILED(rv)) rv2 = rv; - - rv = observerService->RemoveObserver(this, "profile-after-change"); - if (NS_FAILED(rv)) rv2 = rv; - - rv = observerService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID); - if (NS_FAILED(rv)) rv2 = rv; - + nsCOMPtr obs = + do_GetService("@mozilla.org/observer-service;1"); + if (obs) { + obs->RemoveObserver(this, "profile-before-change"); + obs->RemoveObserver(this, "profile-after-change"); + obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID); + } // remove Pref Service observers - nsCOMPtr prefInternal = do_GetService(NS_PREFSERVICE_CONTRACTID); + nsCOMPtr prefs = + do_GetService(NS_PREFSERVICE_CONTRACTID); + if (prefs) { + // remove Disk cache pref observers + prefs->RemoveObserver(DISK_CACHE_ENABLE_PREF, this); + prefs->RemoveObserver(DISK_CACHE_CAPACITY_PREF, this); + prefs->RemoveObserver(DISK_CACHE_DIR_PREF, this); - // remove Disk cache pref observers - rv = prefInternal->RemoveObserver(DISK_CACHE_ENABLE_PREF, this); - if (NS_FAILED(rv)) rv2 = rv; - - rv = prefInternal->RemoveObserver(DISK_CACHE_CAPACITY_PREF, this); - if (NS_FAILED(rv)) rv2 = rv; - - rv = prefInternal->RemoveObserver(DISK_CACHE_DIR_PREF, this); - if (NS_FAILED(rv)) rv2 = rv; - - // remove Memory cache pref observers - rv = prefInternal->RemoveObserver(MEMORY_CACHE_ENABLE_PREF, this); - if (NS_FAILED(rv)) rv2 = rv; - - rv = prefInternal->RemoveObserver(MEMORY_CACHE_CAPACITY_PREF, this); - // if (NS_FAILED(rv)) rv2 = rv; - - return NS_SUCCEEDED(rv) ? rv2 : rv; + // remove Memory cache pref observers + prefs->RemoveObserver(MEMORY_CACHE_ENABLE_PREF, this); + prefs->RemoveObserver(MEMORY_CACHE_CAPACITY_PREF, this); + } } diff --git a/mozilla/netwerk/dns/src/nsDNSService2.cpp b/mozilla/netwerk/dns/src/nsDNSService2.cpp index 75babc69a4d..6aa567e1a93 100644 --- a/mozilla/netwerk/dns/src/nsDNSService2.cpp +++ b/mozilla/netwerk/dns/src/nsDNSService2.cpp @@ -326,11 +326,13 @@ nsDNSService::Init() return NS_ERROR_OUT_OF_MEMORY; // register as prefs observer - prefs->AddObserver(kPrefDnsCacheEntries, this, PR_FALSE); - prefs->AddObserver(kPrefDnsCacheExpiration, this, PR_FALSE); - prefs->AddObserver(kPrefEnableIDN, this, PR_FALSE); - prefs->AddObserver(kPrefIPv4OnlyDomains, this, PR_FALSE); - prefs->AddObserver(kPrefDisableIPv6, this, PR_FALSE); + if (prefs) { + prefs->AddObserver(kPrefDnsCacheEntries, this, PR_FALSE); + prefs->AddObserver(kPrefDnsCacheExpiration, this, PR_FALSE); + prefs->AddObserver(kPrefEnableIDN, this, PR_FALSE); + prefs->AddObserver(kPrefIPv4OnlyDomains, this, PR_FALSE); + prefs->AddObserver(kPrefDisableIPv6, this, PR_FALSE); + } } // we have to null out mIDN since we might be getting re-initialized diff --git a/mozilla/netwerk/protocol/http/src/nsHttpTransaction.cpp b/mozilla/netwerk/protocol/http/src/nsHttpTransaction.cpp index 3d4681b2a74..1073bf37d64 100644 --- a/mozilla/netwerk/protocol/http/src/nsHttpTransaction.cpp +++ b/mozilla/netwerk/protocol/http/src/nsHttpTransaction.cpp @@ -132,7 +132,6 @@ nsHttpTransaction::nsHttpTransaction() , mRestartCount(0) , mCaps(0) , mClosed(PR_FALSE) - , mDestroying(PR_FALSE) , mConnected(PR_FALSE) , mHaveStatusLine(PR_FALSE) , mHaveAllHeaders(PR_FALSE) @@ -939,9 +938,6 @@ nsHttpTransaction::DeleteSelfOnConsumerThread() { LOG(("nsHttpTransaction::DeleteSelfOnConsumerThread [this=%x]\n", this)); - NS_ASSERTION(!mDestroying, "deleting self again"); - mDestroying = PR_TRUE; - PRBool val; if (NS_SUCCEEDED(mConsumerTarget->IsOnCurrentThread(&val)) && val) delete this; diff --git a/mozilla/netwerk/protocol/http/src/nsHttpTransaction.h b/mozilla/netwerk/protocol/http/src/nsHttpTransaction.h index bd6fc675aa4..55655dc5c59 100644 --- a/mozilla/netwerk/protocol/http/src/nsHttpTransaction.h +++ b/mozilla/netwerk/protocol/http/src/nsHttpTransaction.h @@ -192,7 +192,6 @@ private: // state flags PRUint32 mClosed : 1; - PRUint32 mDestroying : 1; PRUint32 mConnected : 1; PRUint32 mHaveStatusLine : 1; PRUint32 mHaveAllHeaders : 1; diff --git a/mozilla/netwerk/test/PropertiesTest.cpp b/mozilla/netwerk/test/PropertiesTest.cpp index a9958054154..b87e4e0c6f2 100644 --- a/mozilla/netwerk/test/PropertiesTest.cpp +++ b/mozilla/netwerk/test/PropertiesTest.cpp @@ -39,7 +39,6 @@ #include "TestCommon.h" #include "nsXPCOM.h" #include "nsStringAPI.h" -#include "nsIEventQueueService.h" #include "nsIPersistentProperties2.h" #include "nsIServiceManager.h" #include "nsIComponentRegistrar.h" @@ -57,7 +56,6 @@ static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID); static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); -static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); /***************************************************************************/ @@ -69,7 +67,6 @@ main(int argc, char* argv[]) nsresult ret; - nsCOMPtr servMan; NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull); nsCOMPtr registrar = do_QueryInterface(servMan); @@ -81,18 +78,10 @@ main(int argc, char* argv[]) nsCOMPtr service(do_GetService(kIOServiceCID, &ret)); if (NS_FAILED(ret)) return ret; - nsCOMPtr eventQService = - do_GetService(kEventQueueServiceCID, &ret); - if (NS_FAILED(ret)) return ret; - nsIChannel *channel = nsnull; ret = service->NewChannel(NS_LITERAL_CSTRING(TEST_URL), nsnull, nsnull, &channel); if (NS_FAILED(ret)) return ret; - nsIEventQueue *eventQ = nsnull; - ret = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &eventQ); - if (NS_FAILED(ret)) return ret; - ret = channel->Open(&in); if (NS_FAILED(ret)) return ret; diff --git a/mozilla/netwerk/test/TestCallbacks.cpp b/mozilla/netwerk/test/TestCallbacks.cpp index f18141347ac..37cfbf2ebc9 100644 --- a/mozilla/netwerk/test/TestCallbacks.cpp +++ b/mozilla/netwerk/test/TestCallbacks.cpp @@ -43,7 +43,6 @@ #include "nspr.h" #include "nscore.h" #include "nsCOMPtr.h" -#include "nsIEventQueueService.h" #include "nsIIOService.h" #include "nsIServiceManager.h" #include "nsIStreamListener.h" @@ -58,12 +57,10 @@ #include "nsNetUtil.h" #include "nsStringAPI.h" -static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); -static int gKeepRunning = 0; -static nsIEventQueue* gEventQ = nsnull; static PRBool gError = PR_FALSE; +static PRInt32 gKeepRunning = 0; #define NS_IEQUALS_IID \ { 0x11c5c8ee, 0x1dd2, 0x11b2, \ @@ -201,7 +198,8 @@ Consumer::~Consumer() { } fprintf(stderr, "Consumer::~Consumer -> out\n\n"); - gKeepRunning--; + if (--gKeepRunning == 0) + QuitPumpingEvents(); } nsresult @@ -256,14 +254,6 @@ int main(int argc, char *argv[]) { rv = NS_InitXPCOM2(nsnull, nsnull, nsnull); if (NS_FAILED(rv)) return rv; - // Create the Event Queue for this thread... - nsCOMPtr eventQService = - do_GetService(kEventQueueServiceCID, &rv); - if (NS_FAILED(rv)) return rv; - - rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); - if (NS_FAILED(rv)) return rv; - if (cmdLineURL) { rv = StartLoad(argv[1]); } else { @@ -272,11 +262,7 @@ int main(int argc, char *argv[]) { if (NS_FAILED(rv)) return rv; // Enter the message pump to allow the URL load to proceed. - while ( gKeepRunning ) { - PLEvent *gEvent; - gEventQ->WaitForEvent(&gEvent); - gEventQ->HandleEvent(gEvent); - } + PumpEvents(); NS_ShutdownXPCOM(nsnull); if (gError) { diff --git a/mozilla/netwerk/test/TestCommon.h b/mozilla/netwerk/test/TestCommon.h index 711ad8d32a8..51f9d811dc4 100644 --- a/mozilla/netwerk/test/TestCommon.h +++ b/mozilla/netwerk/test/TestCommon.h @@ -37,9 +37,43 @@ #ifndef TestCommon_h__ #define TestCommon_h__ +#include "nsThreadUtils.h" + inline int test_common_init(int *argc, char ***argv) { return 0; } +//----------------------------------------------------------------------------- + +static PRBool gKeepPumpingEvents = PR_FALSE; + +class nsQuitPumpingEvent : public nsIRunnable { +public: + NS_DECL_ISUPPORTS + NS_IMETHOD Run() { + gKeepPumpingEvents = PR_FALSE; + return NS_OK; + } +}; +NS_IMPL_THREADSAFE_ISUPPORTS1(nsQuitPumpingEvent, nsIRunnable) + +static void PumpEvents() +{ + nsCOMPtr thread = do_GetCurrentThread(); + + gKeepPumpingEvents = PR_TRUE; + while (gKeepPumpingEvents) + thread->RunNextTask(nsIThread::RUN_NORMAL); +} + +static void QuitPumpingEvents() +{ + // Dispatch a task that toggles gKeepPumpingEvents so that we flush all + // of the pending tasks before exiting from PumpEvents. + nsCOMPtr thread = do_GetMainThread(); + nsCOMPtr task = new nsQuitPumpingEvent(); + thread->Dispatch(task, NS_DISPATCH_NORMAL); +} + #endif diff --git a/mozilla/netwerk/test/TestIOThreads.cpp b/mozilla/netwerk/test/TestIOThreads.cpp index 32fc19c94f1..59d7964b306 100644 --- a/mozilla/netwerk/test/TestIOThreads.cpp +++ b/mozilla/netwerk/test/TestIOThreads.cpp @@ -38,9 +38,9 @@ #include "nsXPCOM.h" #include "nsIServiceManager.h" #include "nsServiceManagerUtils.h" -#include "nsIEventTarget.h" +#include "nsIDispatchTarget.h" #include "nsCOMPtr.h" -#include "plevent.h" +#include "nsNetCID.h" #include "prlog.h" #if defined(PR_LOGGING) @@ -51,33 +51,37 @@ static PRLogModuleInfo *gTestLog = nsnull; #endif #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) -PR_STATIC_CALLBACK(void *) HandleEvent(PLEvent *event) -{ - LOG(("HandleEvent:%d\n", NS_PTR_TO_INT32(event->owner))); - return nsnull; -} +class nsIOEvent : public nsIRunnable { +public: + NS_DECL_ISUPPORTS -PR_STATIC_CALLBACK(void) DestroyEvent(PLEvent *event) -{ - delete event; -} + nsIOEvent(int i) : mIndex(i) {} + + NS_IMETHOD Run() { + LOG(("Run [%d]\n", mIndex)); + return NS_OK; + } + +private: + int mIndex; +}; +NS_IMPL_THREADSAFE_ISUPPORTS1(nsIOEvent, nsIRunnable) static nsresult RunTest() { nsresult rv; - nsCOMPtr target = do_GetService("@mozilla.org/network/io-thread-pool;1", &rv); + nsCOMPtr target = + do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv); if (NS_FAILED(rv)) return rv; for (int i=0; i<10; ++i) { - PLEvent *event = new PLEvent(); - PL_InitEvent(event, (void *) i, HandleEvent, DestroyEvent); - LOG(("PostEvent:%d\n", i)); - if (NS_FAILED(target->PostEvent(event))) - PL_DestroyEvent(event); + nsCOMPtr event = new nsIOEvent(i); + LOG(("Dispatch %d\n", i)); + target->Dispatch(event, NS_DISPATCH_NORMAL); } - return rv; + return NS_OK; } int main(int argc, char **argv) diff --git a/mozilla/netwerk/test/TestIncrementalDownload.cpp b/mozilla/netwerk/test/TestIncrementalDownload.cpp index 5bb0bbe1ed5..a5065a06d00 100644 --- a/mozilla/netwerk/test/TestIncrementalDownload.cpp +++ b/mozilla/netwerk/test/TestIncrementalDownload.cpp @@ -37,54 +37,18 @@ * ***** END LICENSE BLOCK ***** */ #include +#include "TestCommon.h" #include "nsNetUtil.h" #include "nsIIncrementalDownload.h" #include "nsIRequestObserver.h" #include "nsIProgressEventSink.h" -#include "nsIEventQueueService.h" -#include "nsIEventQueue.h" +#include "nsThreadUtils.h" #include "nsAutoPtr.h" #include "prprf.h" #include "prenv.h" //----------------------------------------------------------------------------- -static nsresult SetupEventQ() -{ - nsresult rv; - - nsCOMPtr eqs = - do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv); - if (NS_FAILED(rv)) - return rv; - - return eqs->CreateMonitoredThreadEventQueue(); -} - -static nsresult PumpEvents() -{ - nsresult rv; - - nsCOMPtr eqs = - do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv); - if (NS_FAILED(rv)) - return rv; - - nsCOMPtr eq; - rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(eq)); - if (NS_FAILED(rv)) - return rv; - - return eq->EventLoop(); -} - -static void QuitPumpingEvents() -{ - PR_Interrupt(PR_GetCurrentThread()); -} - -//----------------------------------------------------------------------------- - class FetchObserver : public nsIRequestObserver , public nsIProgressEventSink { @@ -160,15 +124,12 @@ DoIncrementalFetch(const char *uriSpec, const char *resultPath, PRInt32 chunkSiz if (NS_FAILED(rv)) return rv; - rv = SetupEventQ(); - if (NS_FAILED(rv)) - return rv; - rv = download->Start(observer, nsnull); if (NS_FAILED(rv)) return rv; - return PumpEvents(); + PumpEvents(); + return NS_OK; } int diff --git a/mozilla/netwerk/test/TestPageLoad.cpp b/mozilla/netwerk/test/TestPageLoad.cpp index 6ef63550386..36d79f4fd56 100644 --- a/mozilla/netwerk/test/TestPageLoad.cpp +++ b/mozilla/netwerk/test/TestPageLoad.cpp @@ -37,7 +37,6 @@ #include "TestCommon.h" #include "nsNetUtil.h" -#include "nsIEventQueueService.h" #include "nsIServiceManager.h" #include "nsIInterfaceRequestor.h" #include "nsIInterfaceRequestorUtils.h" @@ -65,9 +64,6 @@ nsresult auxLoad(char *uriBuf); } \ PR_END_MACRO -static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); -static nsIEventQueue* gEventQ = nsnull; -static PRBool gKeepRunning = PR_FALSE; static nsCString globalStream; //static char urlBuf[256]; static nsCOMPtr baseURI; @@ -77,6 +73,8 @@ static nsCOMPtr uriList; static int numStart=0; static int numFound=0; +static PRInt32 gKeepRunning = 0; + //--------writer fun---------------------- @@ -186,7 +184,8 @@ NS_IMETHODIMP MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status) { //printf(">>> OnStopRequest status=%x\n", status); - gKeepRunning--; + if (--gKeepRunning == 0) + QuitPumpingEvents(); return NS_OK; } @@ -371,17 +370,6 @@ int main(int argc, char **argv) uriList = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID, &rv); RETURN_IF_FAILED(rv, "do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID)"); - // 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("Loading necko ... \n"); nsCOMPtr chan; nsCOMPtr listener = new MyListener(); @@ -400,9 +388,7 @@ int main(int argc, char **argv) rv = chan->AsyncOpen(listener, nsnull); RETURN_IF_FAILED(rv, "AsyncOpen"); - while (gKeepRunning) { - gEventQ->ProcessPendingEvents(); - } + PumpEvents(); finish = PR_Now(); PRUint32 totalTime32; diff --git a/mozilla/netwerk/test/TestPerf.cpp b/mozilla/netwerk/test/TestPerf.cpp index 6352c12e60e..4516d920f9b 100644 --- a/mozilla/netwerk/test/TestPerf.cpp +++ b/mozilla/netwerk/test/TestPerf.cpp @@ -2,7 +2,6 @@ #include #include "nsCRT.h" /* should be "plstr.h"? */ #include "nsNetUtil.h" -#include "nsIEventQueueService.h" #include "nsIServiceManager.h" #include "nsIComponentRegistrar.h" #include "nsISupportsArray.h" @@ -53,8 +52,6 @@ load_sync(nsISupportsArray *urls) //----------------------------------------------------------------------------- static int gRequestCount = 0; -static nsIEventQueue *gEventQ = 0; - class MyListener : public nsIStreamListener { @@ -95,18 +92,6 @@ MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx, return NS_OK; } -static void *PR_CALLBACK -ShutdownEvent_Handler(PLEvent *ev) -{ - return nsnull; -} - -static void PR_CALLBACK -ShutdownEvent_Cleanup(PLEvent *ev) -{ - delete ev; -} - NS_IMETHODIMP MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status) { @@ -117,11 +102,7 @@ MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status) } if (--gRequestCount == 0) { // post shutdown event - PLEvent *ev = new PLEvent; - PL_InitEvent(ev, nsnull, - ShutdownEvent_Handler, - ShutdownEvent_Cleanup); - gEventQ->PostEvent(ev); + QuitPumpingEvents(); } return NS_OK; } @@ -149,29 +130,9 @@ load_async_1(nsISupports *element, void *data) static nsresult load_async(nsISupportsArray *urls) { - nsresult rv; - - // Create the Event Queue for this thread... - nsCOMPtr eqs( - do_GetService("@mozilla.org/event-queue-service;1", &rv) ); - if (NS_FAILED(rv)) return rv; - - rv = eqs->CreateMonitoredThreadEventQueue(); - if (NS_FAILED(rv)) return rv; - - rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); - if (NS_FAILED(rv)) return rv; - urls->EnumerateForwards(load_async_1, nsnull); - while (1) { - PLEvent *ev; - gEventQ->WaitForEvent(&ev); - if (gRequestCount == 0) - break; - gEventQ->HandleEvent(ev); - } - NS_RELEASE(gEventQ); + PumpEvents(); return NS_OK; } diff --git a/mozilla/netwerk/test/TestProtocols.cpp b/mozilla/netwerk/test/TestProtocols.cpp index 123e5811abb..52ccca12843 100644 --- a/mozilla/netwerk/test/TestProtocols.cpp +++ b/mozilla/netwerk/test/TestProtocols.cpp @@ -57,7 +57,6 @@ #include "nspr.h" #include "nscore.h" #include "nsCOMPtr.h" -#include "nsIEventQueueService.h" #include "nsIIOService.h" #include "nsIServiceManager.h" #include "nsIStreamListener.h" @@ -97,13 +96,11 @@ static PRLogModuleInfo *gTestLog = nsnull; #endif #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) -static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); //static PRTime gElapsedTime; // enable when we time it... static int gKeepRunning = 0; static PRBool gVerbose = PR_FALSE; -static nsIEventQueue* gEventQ = nsnull; static PRBool gAskUserForInput = PR_FALSE; static PRBool gResume = PR_FALSE; static PRUint64 gStartAt = 0; @@ -491,29 +488,6 @@ InputTestConsumer::OnDataAvailable(nsIRequest *request, return NS_OK; } -PR_STATIC_CALLBACK(void) DecrementDestroyHandler(PLEvent *self) -{ - PR_Free(self); -} - - -PR_STATIC_CALLBACK(void*) DecrementEventHandler(PLEvent *self) -{ - gKeepRunning--; - return nsnull; -} - -void FireDecrement() -{ - PLEvent *event = PR_NEW(PLEvent); - PL_InitEvent(event, - nsnull, - DecrementEventHandler, - DecrementDestroyHandler); - - gEventQ->PostEvent(event); -} - NS_IMETHODIMP InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context, nsresult aStatus) @@ -559,7 +533,8 @@ InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context, LOG(("\nFinished loading: UNKNOWN URL. Status Code: %x\n", aStatus)); } - FireDecrement(); + if (--gKeepRunning == 0) + QuitPumpingEvents(); return NS_OK; } @@ -695,7 +670,7 @@ nsresult StartLoadingURL(const char* aUrlString) info); if (NS_SUCCEEDED(rv)) { - gKeepRunning += 1; + gKeepRunning++; } else { LOG(("ERROR: AsyncOpen failed [rv=%x]\n", rv)); @@ -818,13 +793,6 @@ main(int argc, char* argv[]) if (NS_FAILED(rv)) return rv; { - // Create the Event Queue for this thread... - nsCOMPtr eventQService = - do_GetService(kEventQueueServiceCID, &rv); - if (NS_FAILED(rv)) return rv; - - eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); - int i; LOG(("Trying to load:\n")); for (i=1; iWaitForEvent(&gEvent); - gEventQ->HandleEvent(gEvent); - } + // Enter the message pump to allow the URL load to proceed. + PumpEvents(); } // this scopes the nsCOMPtrs // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM NS_ShutdownXPCOM(nsnull); diff --git a/mozilla/netwerk/test/TestServ.cpp b/mozilla/netwerk/test/TestServ.cpp index 0fbf0084720..dbb4e7a06a0 100644 --- a/mozilla/netwerk/test/TestServ.cpp +++ b/mozilla/netwerk/test/TestServ.cpp @@ -38,8 +38,6 @@ #include "TestCommon.h" #include #include "nsIServiceManager.h" -#include "nsIEventQueueService.h" -#include "nsIEventQueue.h" #include "nsIServerSocket.h" #include "nsISocketTransport.h" #include "nsNetUtil.h" @@ -55,9 +53,6 @@ static PRLogModuleInfo *gTestLog = nsnull; #endif #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) -static PRBool gKeepRunning = PR_TRUE; -static nsIEventQueue* gEventQ = nsnull; - class MySocketListener : public nsIServerSocketListener { public: @@ -117,7 +112,7 @@ NS_IMETHODIMP MySocketListener::OnStopListening(nsIServerSocket *serv, nsresult status) { LOG(("MySocketListener::OnStopListening [serv=%p status=%x]\n", serv, status)); - gKeepRunning = PR_FALSE; + QuitPumpingEvents(); return NS_OK; } @@ -167,13 +162,6 @@ main(int argc, char* argv[]) if (NS_FAILED(rv)) return rv; { - // Create the Event Queue for this thread... - nsCOMPtr eqs = - do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv); - if (NS_FAILED(rv)) return rv; - - eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); - rv = MakeServer(atoi(argv[1])); if (NS_FAILED(rv)) { LOG(("MakeServer failed [rv=%x]\n", rv)); @@ -181,11 +169,7 @@ main(int argc, char* argv[]) } // Enter the message pump to allow the URL load to proceed. - while (gKeepRunning) { - PLEvent *event; - gEventQ->WaitForEvent(&event); - gEventQ->HandleEvent(event); - } + PumpEvents(); } // this scopes the nsCOMPtrs // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM NS_ShutdownXPCOM(nsnull); diff --git a/mozilla/netwerk/test/TestSocketTransport.cpp b/mozilla/netwerk/test/TestSocketTransport.cpp index ffdfac41fa9..562db706b63 100644 --- a/mozilla/netwerk/test/TestSocketTransport.cpp +++ b/mozilla/netwerk/test/TestSocketTransport.cpp @@ -54,8 +54,6 @@ #include "nsIDNSService.h" #include "nsIFileStreams.h" #include "nsIStreamListener.h" -#include "nsIEventQueueService.h" -#include "nsIEventQueue.h" #include "nsILocalFile.h" #include "nsNetUtil.h" #include "nsAutoLock.h" @@ -74,39 +72,6 @@ static PRLogModuleInfo *gTestLog = nsnull; //////////////////////////////////////////////////////////////////////////////// static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID); -static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); - -PRBool gDone = PR_FALSE; -nsIEventQueue* gEventQ = nsnull; - -//////////////////////////////////////////////////////////////////////////////// - -static void *PR_CALLBACK -DoneEvent_Handler(PLEvent *ev) -{ - gDone = PR_TRUE; - return nsnull; -} - -static void PR_CALLBACK -DoneEvent_Cleanup(PLEvent *ev) -{ - delete ev; -} - -static void -PostDoneEvent() -{ - LOG(("PostDoneEvent\n")); - - PLEvent *ev = new PLEvent(); - - PL_InitEvent(ev, nsnull, - DoneEvent_Handler, - DoneEvent_Cleanup); - - gEventQ->PostEvent(ev); -} //////////////////////////////////////////////////////////////////////////////// @@ -169,7 +134,7 @@ public: if (NS_FAILED(rv) || (n == 0)) { if (rv != NS_BASE_STREAM_WOULD_BLOCK) { - PostDoneEvent(); + QuitPumpingEvents(); return NS_OK; } } @@ -261,16 +226,8 @@ RunTest(nsISocketTransportService *sts, rv = asyncOut->AsyncWait(handler, 0, 0, nsnull); - if (NS_SUCCEEDED(rv)) { - PLEvent* event; - gDone = PR_FALSE; - while (!gDone) { - rv = gEventQ->WaitForEvent(&event); - if (NS_FAILED(rv)) return rv; - rv = gEventQ->HandleEvent(event); - if (NS_FAILED(rv)) return rv; - } - } + if (NS_SUCCEEDED(rv)) + PumpEvents(); NS_RELEASE(handler); @@ -309,16 +266,6 @@ main(int argc, char* argv[]) do_GetService(NS_DNSSERVICE_CONTRACTID, &rv); if (NS_FAILED(rv)) return rv; - nsCOMPtr eventQService = - do_GetService(kEventQueueServiceCID, &rv); - if (NS_FAILED(rv)) return rv; - - rv = eventQService->CreateMonitoredThreadEventQueue(); - if (NS_FAILED(rv)) return rv; - - rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); - if (NS_FAILED(rv)) return rv; - nsCOMPtr sts = do_GetService(kSocketTransportServiceCID, &rv); if (NS_FAILED(rv)) return rv; @@ -387,8 +334,6 @@ main(int argc, char* argv[]) LOG(("calling Shutdown on socket transport service:\n")); sts->Shutdown(); - NS_RELEASE(gEventQ); - // give background threads a chance to finish whatever work they may // be doing. LOG(("waiting 1 second before exiting...\n")); diff --git a/mozilla/netwerk/test/TestStreamChannel.cpp b/mozilla/netwerk/test/TestStreamChannel.cpp index 58a8fb5863a..05f8a4cd734 100644 --- a/mozilla/netwerk/test/TestStreamChannel.cpp +++ b/mozilla/netwerk/test/TestStreamChannel.cpp @@ -51,8 +51,6 @@ #include "nsStringAPI.h" #include "nsIFileStreams.h" #include "nsIStreamListener.h" -#include "nsIEventQueueService.h" -#include "nsIEventQueue.h" #include "nsILocalFile.h" #include "nsNetUtil.h" #include "nsAutoLock.h" @@ -73,39 +71,6 @@ static PRLogModuleInfo *gTestLog = nsnull; //////////////////////////////////////////////////////////////////////////////// static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID); -static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); - -PRBool gDone = PR_FALSE; -nsIEventQueue *gEventQ = nsnull; - -//////////////////////////////////////////////////////////////////////////////// - -static void *PR_CALLBACK -DoneEvent_Handler(PLEvent *ev) -{ - gDone = PR_TRUE; - return nsnull; -} - -static void PR_CALLBACK -DoneEvent_Cleanup(PLEvent *ev) -{ - delete ev; -} - -static void -PostDoneEvent() -{ - LOG(("PostDoneEvent\n")); - - PLEvent *ev = new PLEvent(); - - PL_InitEvent(ev, nsnull, - DoneEvent_Handler, - DoneEvent_Cleanup); - - gEventQ->PostEvent(ev); -} //////////////////////////////////////////////////////////////////////////////// @@ -151,7 +116,7 @@ public: NS_IMETHOD OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status) { LOG(("MyListener::OnStopRequest [status=%x]\n", status)); - PostDoneEvent(); + QuitPumpingEvents(); return NS_OK; } }; @@ -225,15 +190,7 @@ RunTest(nsIFile *file) rv = chan->AsyncOpen(new MyListener(), nsnull); if (NS_FAILED(rv)) return rv; - gDone = PR_FALSE; - while (!gDone) { - PLEvent *event; - rv = gEventQ->WaitForEvent(&event); - if (NS_FAILED(rv)) return rv; - rv = gEventQ->HandleEvent(event); - if (NS_FAILED(rv)) return rv; - } - + PumpEvents(); return NS_OK; } @@ -264,13 +221,6 @@ main(int argc, char* argv[]) gTestLog = PR_NewLogModule("Test"); #endif - nsCOMPtr eventQService = - do_GetService(kEventQueueServiceCID, &rv); - if (NS_FAILED(rv)) return rv; - - rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); - if (NS_FAILED(rv)) return rv; - nsCOMPtr file; rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(file)); if (NS_FAILED(rv)) return rv; @@ -278,8 +228,6 @@ main(int argc, char* argv[]) rv = RunTest(file); NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed"); - NS_RELEASE(gEventQ); - // give background threads a chance to finish whatever work they may // be doing. PR_Sleep(PR_SecondsToInterval(1)); diff --git a/mozilla/netwerk/test/TestStreamLoader.cpp b/mozilla/netwerk/test/TestStreamLoader.cpp index 5bf2a26b7af..55e56219aa5 100644 --- a/mozilla/netwerk/test/TestStreamLoader.cpp +++ b/mozilla/netwerk/test/TestStreamLoader.cpp @@ -1,7 +1,7 @@ #include #include "TestCommon.h" #include "nsNetUtil.h" -#include "nsEventQueueUtils.h" +#include "nsThreadUtils.h" #include "prlog.h" #if defined(PR_LOGGING) @@ -12,9 +12,6 @@ static PRLogModuleInfo *gTestLog = nsnull; #endif #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) -static PRBool gKeepRunning = PR_TRUE; -static nsIEventQueue* gEventQ = nsnull; - class MyStreamLoaderObserver : public nsIStreamLoaderObserver { public: @@ -37,7 +34,7 @@ MyStreamLoaderObserver::OnStreamComplete(nsIStreamLoader *loader, loader->GetRequest(getter_AddRefs(request)); LOG((" request=%p\n", request.get())); - gKeepRunning = PR_FALSE; + QuitPumpingEvents(); return NS_OK; } @@ -60,11 +57,6 @@ int main(int argc, char **argv) return -1; { - // Create the Event Queue for this thread... - rv = NS_GetMainEventQ(&gEventQ); - if (NS_FAILED(rv)) - return -1; - nsCOMPtr uri; rv = NS_NewURI(getter_AddRefs(uri), nsDependentCString(argv[1])); if (NS_FAILED(rv)) @@ -84,12 +76,7 @@ int main(int argc, char **argv) if (NS_FAILED(rv)) return -1; - // Enter the message pump to allow the URL load to proceed. - while (gKeepRunning) { - PLEvent *e; - gEventQ->WaitForEvent(&e); - gEventQ->HandleEvent(e); - } + PumpEvents(); } // this scopes the nsCOMPtrs // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM NS_ShutdownXPCOM(nsnull); diff --git a/mozilla/netwerk/test/TestStreamPump.cpp b/mozilla/netwerk/test/TestStreamPump.cpp index a32a7283433..4d73376ca26 100644 --- a/mozilla/netwerk/test/TestStreamPump.cpp +++ b/mozilla/netwerk/test/TestStreamPump.cpp @@ -52,8 +52,6 @@ #include "nsStringAPI.h" #include "nsIFileStreams.h" #include "nsIStreamListener.h" -#include "nsIEventQueueService.h" -#include "nsIEventQueue.h" #include "nsILocalFile.h" #include "nsNetUtil.h" #include "nsAutoLock.h" @@ -74,42 +72,6 @@ static PRLogModuleInfo *gTestLog = nsnull; //////////////////////////////////////////////////////////////////////////////// -static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); - -PRBool gDone = PR_FALSE; -nsIEventQueue *gEventQ = nsnull; - -//////////////////////////////////////////////////////////////////////////////// - -static void *PR_CALLBACK -DoneEvent_Handler(PLEvent *ev) -{ - gDone = PR_TRUE; - return nsnull; -} - -static void PR_CALLBACK -DoneEvent_Cleanup(PLEvent *ev) -{ - delete ev; -} - -static void -PostDoneEvent() -{ - LOG(("PostDoneEvent\n")); - - PLEvent *ev = new PLEvent(); - - PL_InitEvent(ev, nsnull, - DoneEvent_Handler, - DoneEvent_Cleanup); - - gEventQ->PostEvent(ev); -} - -//////////////////////////////////////////////////////////////////////////////// - class MyListener : public nsIStreamListener { public: @@ -155,7 +117,7 @@ public: NS_IMETHOD OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status) { LOG(("MyListener::OnStopRequest [status=%x]\n", status)); - PostDoneEvent(); + QuitPumpingEvents(); return NS_OK; } }; @@ -187,15 +149,7 @@ RunTest(nsIFile *file, PRInt64 offset, PRInt64 length) rv = pump->AsyncRead(new MyListener(), nsnull); if (NS_FAILED(rv)) return rv; - gDone = PR_FALSE; - while (!gDone) { - PLEvent *event; - rv = gEventQ->WaitForEvent(&event); - if (NS_FAILED(rv)) return rv; - rv = gEventQ->HandleEvent(event); - if (NS_FAILED(rv)) return rv; - } - + PumpEvents(); return NS_OK; } @@ -238,13 +192,6 @@ main(int argc, char* argv[]) gTestLog = PR_NewLogModule("Test"); #endif - nsCOMPtr eventQService = - do_GetService(kEventQueueServiceCID, &rv); - if (NS_FAILED(rv)) return rv; - - rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); - if (NS_FAILED(rv)) return rv; - nsCOMPtr file; rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(file)); if (NS_FAILED(rv)) return rv; @@ -252,8 +199,6 @@ main(int argc, char* argv[]) rv = RunTest(file, offset, length); NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed"); - NS_RELEASE(gEventQ); - // give background threads a chance to finish whatever work they may // be doing. PR_Sleep(PR_SecondsToInterval(1)); diff --git a/mozilla/netwerk/test/TestStreamTransport.cpp b/mozilla/netwerk/test/TestStreamTransport.cpp index 57faf86e34c..ad2b9463f89 100644 --- a/mozilla/netwerk/test/TestStreamTransport.cpp +++ b/mozilla/netwerk/test/TestStreamTransport.cpp @@ -51,8 +51,6 @@ #include "nsStringAPI.h" #include "nsIFileStreams.h" #include "nsIStreamListener.h" -#include "nsIEventQueueService.h" -#include "nsIEventQueue.h" #include "nsILocalFile.h" #include "nsNetUtil.h" #include "nsAutoLock.h" @@ -72,39 +70,6 @@ static PRLogModuleInfo *gTestLog = nsnull; //////////////////////////////////////////////////////////////////////////////// static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID); -static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); - -PRBool gDone = PR_FALSE; -nsIEventQueue* gEventQ = nsnull; - -//////////////////////////////////////////////////////////////////////////////// - -static void *PR_CALLBACK -DoneEvent_Handler(PLEvent *ev) -{ - gDone = PR_TRUE; - return nsnull; -} - -static void PR_CALLBACK -DoneEvent_Cleanup(PLEvent *ev) -{ - delete ev; -} - -static void -PostDoneEvent() -{ - LOG(("PostDoneEvent\n")); - - PLEvent *ev = new PLEvent(); - - PL_InitEvent(ev, nsnull, - DoneEvent_Handler, - DoneEvent_Cleanup); - - gEventQ->PostEvent(ev); -} //////////////////////////////////////////////////////////////////////////////// @@ -162,7 +127,7 @@ public: mInput = 0; // post done copying event - PostDoneEvent(); + QuitPumpingEvents(); } void Process_Locked() @@ -277,15 +242,7 @@ RunTest(nsIFile *srcFile, nsIFile *destFile) rv = copier->AsyncCopy(srcTransport, destTransport); if (NS_FAILED(rv)) return rv; - PLEvent* event; - - gDone = PR_FALSE; - while (!gDone) { - rv = gEventQ->WaitForEvent(&event); - if (NS_FAILED(rv)) return rv; - rv = gEventQ->HandleEvent(event); - if (NS_FAILED(rv)) return rv; - } + PumpEvents(); NS_RELEASE(copier); return NS_OK; @@ -361,13 +318,6 @@ main(int argc, char* argv[]) gTestLog = PR_NewLogModule("Test"); #endif - nsCOMPtr eventQService = - do_GetService(kEventQueueServiceCID, &rv); - if (NS_FAILED(rv)) return rv; - - rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); - if (NS_FAILED(rv)) return rv; - nsCOMPtr srcFile; rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(srcFile)); if (NS_FAILED(rv)) return rv; @@ -396,8 +346,6 @@ main(int argc, char* argv[]) rv = RunBlockingTest(srcFile, destFile); NS_ASSERTION(NS_SUCCEEDED(rv), "RunBlockingTest failed"); - NS_RELEASE(gEventQ); - // give background threads a chance to finish whatever work they may // be doing. PR_Sleep(PR_SecondsToInterval(1)); diff --git a/mozilla/netwerk/test/TestUpload.cpp b/mozilla/netwerk/test/TestUpload.cpp index c4f80c85cc5..b2542ce357c 100644 --- a/mozilla/netwerk/test/TestUpload.cpp +++ b/mozilla/netwerk/test/TestUpload.cpp @@ -41,14 +41,12 @@ #endif #include "nsIComponentRegistrar.h" -#include "nsIEventQueueService.h" #include "nsIIOService.h" #include "nsIServiceManager.h" #include "nsNetUtil.h" #include "nsIUploadChannel.h" -static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); #include "prlog.h" @@ -60,9 +58,6 @@ static PRLogModuleInfo *gTestLog = nsnull; #endif #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) -static int gKeepRunning = 1; -static nsIEventQueue* gEventQ = nsnull; - //----------------------------------------------------------------------------- // InputTestConsumer //----------------------------------------------------------------------------- @@ -126,7 +121,7 @@ InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context, nsresult aStatus) { LOG(("InputTestConsumer::OnStopRequest [status=%x]\n", aStatus)); - gKeepRunning = PR_FALSE; + QuitPumpingEvents(); return NS_OK; } @@ -158,13 +153,6 @@ main(int argc, char* argv[]) if (registrar) registrar->AutoRegister(nsnull); - // Create the Event Queue for this thread... - nsCOMPtr eventQService = - do_GetService(kEventQueueServiceCID, &rv); - if (NS_FAILED(rv)) return rv; - - eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); - nsCOMPtr ioService(do_GetService(kIOServiceCID, &rv)); // first thing to do is create ourselves a stream that // is to be uploaded. @@ -200,11 +188,7 @@ main(int argc, char* argv[]) channel->AsyncOpen(listener, nsnull); - while ( gKeepRunning ) { - PLEvent *gEvent; - gEventQ->WaitForEvent(&gEvent); - gEventQ->HandleEvent(gEvent); - } + PumpEvents(); } // this scopes the nsCOMPtrs // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM rv = NS_ShutdownXPCOM(nsnull); diff --git a/mozilla/xpcom/base/nsConsoleService.cpp b/mozilla/xpcom/base/nsConsoleService.cpp index 3a9993405b6..53fe7d098c5 100644 --- a/mozilla/xpcom/base/nsConsoleService.cpp +++ b/mozilla/xpcom/base/nsConsoleService.cpp @@ -46,7 +46,7 @@ #include "nsIServiceManager.h" #include "nsIProxyObjectManager.h" #include "nsSupportsArray.h" -#include "nsThreadManager.h" +#include "nsThreadUtils.h" #include "nsConsoleService.h" #include "nsConsoleMessage.h" @@ -324,8 +324,7 @@ nsConsoleService::GetProxyForListener(nsIConsoleListener* aListener, * Would it be better to catch that case and leave the listener unproxied? */ - nsCOMPtr thread; - nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thread)); + nsCOMPtr thread = do_GetCurrentThread(); rv = proxyManager->GetProxyForObject(thread, NS_GET_IID(nsIConsoleListener), diff --git a/mozilla/xpcom/base/nsMemoryImpl.cpp b/mozilla/xpcom/base/nsMemoryImpl.cpp index e0a210a0479..5f2cfd402d8 100644 --- a/mozilla/xpcom/base/nsMemoryImpl.cpp +++ b/mozilla/xpcom/base/nsMemoryImpl.cpp @@ -37,13 +37,11 @@ #include "nsXPCOM.h" #include "nsMemoryImpl.h" -#include "nsThreadManager.h" +#include "nsThreadUtils.h" #include "nsIObserverService.h" -#include "nsIRunnable.h" #include "nsIServiceManager.h" #include "nsISupportsArray.h" -#include "nsIThread.h" #include "prmem.h" #include "prcvar.h" @@ -237,7 +235,7 @@ nsMemoryImpl::FlushMemory(const PRUnichar* aReason, PRBool aImmediate) // They've asked us to run the flusher *immediately*. We've // got to be on the UI main thread for us to be able to do // that...are we? - if (!nsThreadManager::IsMainThread()) { + if (!NS_IsMainThread()) { NS_ERROR("can't synchronously flush memory: not on UI thread"); return NS_ERROR_FAILURE; } @@ -259,8 +257,7 @@ nsMemoryImpl::FlushMemory(const PRUnichar* aReason, PRBool aImmediate) rv = RunFlushers(aReason); } else { - nsCOMPtr thread; - nsThreadManager::get()->GetMainThread(getter_AddRefs(thread)); + nsCOMPtr thread = do_GetMainThread(); if (thread) { sFlushEvent.mReason = aReason; rv = thread->Dispatch(&sFlushEvent, NS_DISPATCH_NORMAL); @@ -400,8 +397,7 @@ MemoryFlusher::Init() sCVar = PR_NewCondVar(sLock); if (!sCVar) return NS_ERROR_FAILURE; - return nsThreadManager::NewThread(NS_LITERAL_CSTRING("xpcom.memoryflusher"), - this, &sThread); + return NS_NewThread("xpcom.memoryflusher", this, &sThread); } void diff --git a/mozilla/xpcom/build/nsXPComInit.cpp b/mozilla/xpcom/build/nsXPComInit.cpp index 15b1c8c2bc1..d3bb57b07a6 100644 --- a/mozilla/xpcom/build/nsXPComInit.cpp +++ b/mozilla/xpcom/build/nsXPComInit.cpp @@ -702,6 +702,13 @@ NS_ShutdownXPCOM(nsIServiceManager* servMgr) NotifyObservers(nsnull, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID, nsnull); + if (thread) + NS_RunPendingTasks(thread); + + // Shutdown the timer thread and all timers that might still be alive before + // shutting down the component manager + nsTimerImpl::Shutdown(); + if (thread) NS_RunPendingTasks(thread); @@ -767,10 +774,6 @@ NS_ShutdownXPCOM(nsIServiceManager* servMgr) NS_ShutdownNativeCharsetUtils(); #endif - // Shutdown the timer thread and all timers that might still be alive before - // shutting down the component manager - nsTimerImpl::Shutdown(); - // Shutdown xpcom. This will release all loaders and cause others holding // a refcount to the component manager to release it. if (nsComponentManagerImpl::gComponentManager) { diff --git a/mozilla/xpcom/components/nsCategoryManager.cpp b/mozilla/xpcom/components/nsCategoryManager.cpp index 01c32be5d92..ce1abfb56ff 100644 --- a/mozilla/xpcom/components/nsCategoryManager.cpp +++ b/mozilla/xpcom/components/nsCategoryManager.cpp @@ -59,7 +59,7 @@ #include "nsQuickSort.h" #include "nsEnumeratorUtils.h" #include "nsIProxyObjectManager.h" -#include "nsThreadManager.h" +#include "nsThreadUtils.h" class nsIComponentLoaderManager; @@ -545,8 +545,7 @@ nsCategoryManager::NotifyObservers( const char *aTopic, if (!observerService) return; - nsCOMPtr mainThread; - nsThreadManager::get()->GetMainThread(getter_AddRefs(mainThread)); + nsCOMPtr mainThread = do_GetMainThread(); nsCOMPtr obsProxy; NS_GetProxyForObject(mainThread, diff --git a/mozilla/xpcom/proxy/src/nsProxyEvent.cpp b/mozilla/xpcom/proxy/src/nsProxyEvent.cpp index 1fb3dfbc6de..c9c0c41f184 100644 --- a/mozilla/xpcom/proxy/src/nsProxyEvent.cpp +++ b/mozilla/xpcom/proxy/src/nsProxyEvent.cpp @@ -53,7 +53,6 @@ #include "nsProxyEvent.h" #include "nsProxyEventPrivate.h" #include "nsIProxyObjectManager.h" -#include "nsRunnable.h" #include "nsCRT.h" #include "pratom.h" @@ -62,7 +61,7 @@ #include "nsIComponentManager.h" #include "nsMemory.h" -#include "nsThreadManager.h" +#include "nsThreadUtils.h" #include "nsIAtom.h" //hack! Need a way to define a component as threadsafe (ie. sta). @@ -205,8 +204,6 @@ nsProxyObjectCallInfo::~nsProxyObjectCallInfo() mOwner = nsnull; - PR_FREEIF(mEvent); - if (mParameterList) free((void*) mParameterList); } @@ -456,8 +453,7 @@ nsProxyObject::PostAndWait(nsProxyObjectCallInfo *proxyInfo) PRBool eventLoopCreated = PR_FALSE; nsresult rv; - nsCOMPtr thread; - nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thread)); + nsCOMPtr thread = do_GetCurrentThread(); if (!thread) { // XXX create a nsIThread wrapper for this thread. diff --git a/mozilla/xpcom/tests/TestPipes.cpp b/mozilla/xpcom/tests/TestPipes.cpp index 523965be8b5..d93d3922941 100644 --- a/mozilla/xpcom/tests/TestPipes.cpp +++ b/mozilla/xpcom/tests/TestPipes.cpp @@ -41,7 +41,7 @@ #include "nsIInputStream.h" #include "nsIOutputStream.h" #include "nsIServiceManager.h" -#include "nsThreadManager.h" +#include "nsThreadUtils.h" #include "prprf.h" #include "prinrval.h" #include "plstr.h" @@ -135,8 +135,7 @@ TestPipe(nsIInputStream* in, nsIOutputStream* out) if (receiver == nsnull) return NS_ERROR_OUT_OF_MEMORY; NS_ADDREF(receiver); - rv = nsThreadManager::NewThread(NS_LITERAL_CSTRING("TestPipe"), - receiver, &thread); + rv = NS_NewThread("TestPipe", receiver, &thread); if (NS_FAILED(rv)) return rv; PRUint32 total = 0; @@ -249,8 +248,7 @@ TestShortWrites(nsIInputStream* in, nsIOutputStream* out) if (receiver == nsnull) return NS_ERROR_OUT_OF_MEMORY; NS_ADDREF(receiver); - rv = nsThreadManager::NewThread(NS_LITERAL_CSTRING("TestShortWrites"), - receiver, &thread); + rv = NS_NewThread("TestShortWrites", receiver, &thread); if (NS_FAILED(rv)) return rv; PRUint32 total = 0; @@ -475,8 +473,7 @@ TestChainedPipes() if (pump == nsnull) return NS_ERROR_OUT_OF_MEMORY; NS_ADDREF(pump); - rv = nsThreadManager::NewThread(NS_LITERAL_CSTRING("TestChainedPipes.pump"), - pump, &thread); + rv = NS_NewThread("TestChainedPipes.pump", pump, &thread); if (NS_FAILED(rv)) return rv; nsIThread* receiverThread; @@ -484,8 +481,7 @@ TestChainedPipes() if (receiver == nsnull) return NS_ERROR_OUT_OF_MEMORY; NS_ADDREF(receiver); - rv = nsThreadManager::NewThread(NS_LITERAL_CSTRING("TestChainedPipes.receiver"), - receiver, &receiverThread); + rv = NS_NewThread("TestChainedPipes.receiver", receiver, &receiverThread); if (NS_FAILED(rv)) return rv; PRUint32 total = 0; diff --git a/mozilla/xpcom/tests/TestThreads.cpp b/mozilla/xpcom/tests/TestThreads.cpp index 57f6debd3f5..7cb3d87713d 100644 --- a/mozilla/xpcom/tests/TestThreads.cpp +++ b/mozilla/xpcom/tests/TestThreads.cpp @@ -36,9 +36,7 @@ * * ***** END LICENSE BLOCK ***** */ -#include "nsIThread.h" -#include "nsIRunnable.h" -#include "nsThreadManager.h" +#include "nsThreadUtils.h" #include #include #include "nspr.h" @@ -52,7 +50,7 @@ public: NS_IMETHOD Run() { nsCOMPtr thread; - nsresult rv = nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thread)); + nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread)); if (NS_FAILED(rv)) { printf("failed to get current thread\n"); return rv; @@ -81,15 +79,14 @@ TestThreads() nsresult rv; nsCOMPtr runner; - rv = nsThreadManager::NewThread(NS_LITERAL_CSTRING("TestThreads"), - new nsRunner(0), getter_AddRefs(runner)); + rv = NS_NewThread("TestThreads", new nsRunner(0), getter_AddRefs(runner)); if (NS_FAILED(rv)) { printf("failed to create thread\n"); return rv; } nsCOMPtr thread; - rv = nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thread)); + rv = NS_GetCurrentThread(getter_AddRefs(thread)); if (NS_FAILED(rv)) { printf("failed to get current thread\n"); return rv; @@ -154,10 +151,7 @@ static int Stress(int loops, int threads) for (k = 0; k < threads; k++) { nsCOMPtr t; - nsCString name("thread:"); - name.AppendInt(k); - nsresult rv = nsThreadManager::NewThread(name, new nsStressRunner(k), - getter_AddRefs(t)); + nsresult rv = NS_NewThread("", new nsStressRunner(k), getter_AddRefs(t)); NS_ASSERTION(NS_SUCCEEDED(rv), "can't create thread"); NS_ADDREF(array[k] = t); } diff --git a/mozilla/xpcom/threads/Makefile.in b/mozilla/xpcom/threads/Makefile.in index 0590c7acf4d..926d8c2b39d 100644 --- a/mozilla/xpcom/threads/Makefile.in +++ b/mozilla/xpcom/threads/Makefile.in @@ -59,7 +59,7 @@ CPPSRCS = \ nsAutoLock.cpp \ nsTaskQueue.cpp \ nsEnvironment.cpp \ - nsRunnable.cpp \ + nsThreadUtils.cpp \ nsThread.cpp \ nsThreadManager.cpp \ nsThreadPool.cpp \ @@ -71,8 +71,8 @@ CPPSRCS = \ EXPORTS = \ nsAutoLock.h \ nsProcess.h \ - nsRunnable.h \ - nsThreadManager.h \ + nsThreadUtils.h \ + nsTaskQueue.h \ $(NULL) XPIDLSRCS = \ diff --git a/mozilla/xpcom/threads/TimerThread.cpp b/mozilla/xpcom/threads/TimerThread.cpp index b97222827a3..0885914894a 100644 --- a/mozilla/xpcom/threads/TimerThread.cpp +++ b/mozilla/xpcom/threads/TimerThread.cpp @@ -42,7 +42,7 @@ #include "TimerThread.h" #include "nsAutoLock.h" -#include "nsThreadManager.h" +#include "nsThreadUtils.h" #include "pratom.h" #include "nsIObserverService.h" @@ -111,8 +111,7 @@ nsresult TimerThread::Init() if (NS_SUCCEEDED(rv)) { // We hold on to mThread to keep the thread alive. - rv = nsThreadManager::NewThread(NS_LITERAL_CSTRING("xpcom.timer"), this, - getter_AddRefs(mThread)); + rv = NS_NewThread("xpcom.timer", this, getter_AddRefs(mThread)); if (NS_FAILED(rv)) { mThread = nsnull; } diff --git a/mozilla/xpcom/threads/nsIThread.idl b/mozilla/xpcom/threads/nsIThread.idl index 0d5e66fb2f6..3514c617c99 100644 --- a/mozilla/xpcom/threads/nsIThread.idl +++ b/mozilla/xpcom/threads/nsIThread.idl @@ -68,20 +68,3 @@ interface nsIThread : nsIDispatchTarget const unsigned long RUN_NORMAL = 0; const unsigned long RUN_NO_WAIT = 1; }; - -%{C++ -// Run all pending tasks for a given thread before returning. -static inline nsresult -NS_RunPendingTasks(nsIThread *thread) -{ - nsresult rv; - do { - rv = thread->RunNextTask(nsIThread::RUN_NO_WAIT); - } while (NS_SUCCEEDED(rv)); - - if (rv == NS_BASE_STREAM_WOULD_BLOCK) - rv = NS_OK; - - return rv; -} -%} diff --git a/mozilla/xpcom/threads/nsIThreadManager.idl b/mozilla/xpcom/threads/nsIThreadManager.idl index 23551734eb8..cb0a2a97a6b 100644 --- a/mozilla/xpcom/threads/nsIThreadManager.idl +++ b/mozilla/xpcom/threads/nsIThreadManager.idl @@ -59,12 +59,12 @@ interface nsIThreadManager : nsISupports /** * Get the main thread. */ - nsIThread getMainThread(); + readonly attribute nsIThread mainThread; /** * Get the current thread. */ - nsIThread getCurrentThread(); + readonly attribute nsIThread currentThread; /** * Set an external nsIThread instance (or null) as the nsIThread for the diff --git a/mozilla/xpcom/threads/nsRunnable.h b/mozilla/xpcom/threads/nsRunnable.h deleted file mode 100644 index 149e0e8c685..00000000000 --- a/mozilla/xpcom/threads/nsRunnable.h +++ /dev/null @@ -1,60 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim:set ts=2 sw=2 sts=2 et cindent: */ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Mozilla code. - * - * The Initial Developer of the Original Code is Google Inc. - * Portions created by the Initial Developer are Copyright (C) 2006 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Darin Fisher - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ - -#ifndef nsRunnable_h__ -#define nsRunnable_h__ - -#include "nsIRunnable.h" - -// This class is designed to be subclassed. - -class nsRunnable : public nsIRunnable -{ -public: - NS_DECL_ISUPPORTS - NS_DECL_NSIRUNNABLE - - nsRunnable() { - } - -protected: - virtual ~nsRunnable() { - } -}; - -#endif // nsRunnable_h__ diff --git a/mozilla/xpcom/threads/nsTaskQueue.h b/mozilla/xpcom/threads/nsTaskQueue.h index 8f7b677e8ad..41b3711aee7 100644 --- a/mozilla/xpcom/threads/nsTaskQueue.h +++ b/mozilla/xpcom/threads/nsTaskQueue.h @@ -44,7 +44,7 @@ #include "nsIRunnable.h" // A threadsafe FIFO task queue... -class nsTaskQueue +class NS_COM nsTaskQueue { public: nsTaskQueue(); diff --git a/mozilla/xpcom/threads/nsThread.cpp b/mozilla/xpcom/threads/nsThread.cpp index eee87a58720..cf7ee984bdc 100644 --- a/mozilla/xpcom/threads/nsThread.cpp +++ b/mozilla/xpcom/threads/nsThread.cpp @@ -341,7 +341,6 @@ NS_IMETHODIMP nsThread::RunNextTask(PRUint32 flags) { NS_ENSURE_STATE(PR_GetCurrentThread() == mThread); - NS_ENSURE_STATE(mActive); NS_ENSURE_ARG(flags == RUN_NORMAL || flags == RUN_NO_WAIT); nsCOMPtr obs; @@ -354,7 +353,7 @@ nsThread::RunNextTask(PRUint32 flags) obs->OnBeforeRunNextTask(this, flags); nsCOMPtr task; - if (flags == RUN_NORMAL) { + if (flags == RUN_NORMAL && mActive) { if (obs) obs->OnWaitNextTask(this, flags); mTasks.WaitPendingTask(getter_AddRefs(task)); @@ -368,6 +367,8 @@ nsThread::RunNextTask(PRUint32 flags) task->Run(); } else if (flags & RUN_NO_WAIT) { rv = NS_BASE_STREAM_WOULD_BLOCK; + } else { + rv = NS_ERROR_UNEXPECTED; } if (obs) diff --git a/mozilla/xpcom/threads/nsThread.h b/mozilla/xpcom/threads/nsThread.h index 2d90c302bc5..0a02e7307c8 100644 --- a/mozilla/xpcom/threads/nsThread.h +++ b/mozilla/xpcom/threads/nsThread.h @@ -42,7 +42,7 @@ #include "nsIThreadInternal.h" #include "nsISupportsPriority.h" #include "nsTaskQueue.h" -#include "nsRunnable.h" +#include "nsThreadUtils.h" #include "nsString.h" #include "nsCOMPtr.h" diff --git a/mozilla/xpcom/threads/nsThreadManager.cpp b/mozilla/xpcom/threads/nsThreadManager.cpp index 3effb4719c7..15c5245d532 100644 --- a/mozilla/xpcom/threads/nsThreadManager.cpp +++ b/mozilla/xpcom/threads/nsThreadManager.cpp @@ -92,7 +92,7 @@ nsThreadManager::Init() void nsThreadManager::Shutdown() { - NS_ASSERTION(IsMainThread(), "shutdown not called from main thread"); + NS_ASSERTION(NS_IsMainThread(), "shutdown not called from main thread"); // We move the threads from the hashtable to a list, so that we avoid // holding the hashtable lock while calling nsIThread::Shutdown. diff --git a/mozilla/xpcom/threads/nsThreadManager.h b/mozilla/xpcom/threads/nsThreadManager.h index 93cbf04469d..93fe21e1ad7 100644 --- a/mozilla/xpcom/threads/nsThreadManager.h +++ b/mozilla/xpcom/threads/nsThreadManager.h @@ -61,20 +61,6 @@ public: // thread of the application process. void Shutdown(); - static PRBool IsMainThread() { - PRBool result; - get()->IsMainThread(&result); - return result; - } - - static nsresult NewThread(const nsACString &name, nsIRunnable *runnable, - nsIThread **result) { - nsresult rv = get()->NewThread(name, result); - if (NS_SUCCEEDED(rv)) - rv = (*result)->Dispatch(runnable, NS_DISPATCH_NORMAL); - return rv; - } - private: nsThreadManager() : mCurThreadIndex(0) {} ~nsThreadManager() {} diff --git a/mozilla/xpcom/threads/nsRunnable.cpp b/mozilla/xpcom/threads/nsThreadUtils.cpp similarity index 66% rename from mozilla/xpcom/threads/nsRunnable.cpp rename to mozilla/xpcom/threads/nsThreadUtils.cpp index a61b15e667e..3f7a1b80b3c 100644 --- a/mozilla/xpcom/threads/nsRunnable.cpp +++ b/mozilla/xpcom/threads/nsThreadUtils.cpp @@ -36,7 +36,8 @@ * * ***** END LICENSE BLOCK ***** */ -#include "nsRunnable.h" +#include "nsThreadUtils.h" +#include "nsThreadManager.h" NS_IMPL_THREADSAFE_ISUPPORTS1(nsRunnable, nsIRunnable) @@ -46,3 +47,40 @@ nsRunnable::Run() // Do nothing return NS_OK; } + +//----------------------------------------------------------------------------- + +NS_COM NS_METHOD +NS_NewThread(const nsACString &name, nsIRunnable *runnable, nsIThread **result) +{ + nsresult rv = nsThreadManager::get()->nsThreadManager::NewThread(name, result); + if (NS_SUCCEEDED(rv)) + rv = (*result)->Dispatch(runnable, NS_DISPATCH_NORMAL); + return rv; +} + +NS_COM NS_METHOD +NS_GetCurrentThread(nsIThread **result) +{ + return nsThreadManager::get()->nsThreadManager::GetCurrentThread(result); +} + +NS_COM NS_METHOD +NS_GetMainThread(nsIThread **result) +{ + return nsThreadManager::get()->nsThreadManager::GetCurrentThread(result); +} + +NS_COM NS_METHOD +NS_GetThread(const nsACString &name, nsIThread **result) +{ + return nsThreadManager::get()->nsThreadManager::GetThread(name, result); +} + +NS_COM PRBool +NS_IsMainThread() +{ + PRBool result = PR_FALSE; + nsThreadManager::get()->nsThreadManager::IsMainThread(&result); + return result; +} diff --git a/mozilla/xpcom/threads/nsThreadUtils.h b/mozilla/xpcom/threads/nsThreadUtils.h new file mode 100644 index 00000000000..df5beef6a65 --- /dev/null +++ b/mozilla/xpcom/threads/nsThreadUtils.h @@ -0,0 +1,245 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla code. + * + * The Initial Developer of the Original Code is Google Inc. + * Portions created by the Initial Developer are Copyright (C) 2006 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsThreadUtils_h__ +#define nsThreadUtils_h__ + +#include "nsIRunnable.h" +#include "nsIThread.h" +#include "nsCOMPtr.h" +#include "nsStringGlue.h" + +class nsIThread; + +//----------------------------------------------------------------------------- +// This class is designed to be subclassed. + +#ifdef MOZILLA_INTERNAL_API +#undef IMETHOD_VISIBILITY +#define IMETHOD_VISIBILITY NS_VISIBILITY_DEFAULT + +class NS_COM nsRunnable : public nsIRunnable +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_NSIRUNNABLE + + nsRunnable() { + } + +protected: + virtual ~nsRunnable() { + } +}; + +#undef IMETHOD_VISIBILITY +#define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN + +//----------------------------------------------------------------------------- +// These methods are alternatives to the methods on nsIThreadManager, provided +// for convenience. + +/** + * Create a new thread, and optionally provide an initial task for the thread. + * @param name + * The name of the thread (may be empty). + * @param runnable + * The initial task to run on this thread. + * @param result + * The resulting thread. + */ +extern NS_COM NS_METHOD +NS_NewThread(const nsACString &name, nsIRunnable *runnable, nsIThread **result); + +/** + * Equivalent to nsIThreadManager::GetCurrentThread. + */ +extern NS_COM NS_METHOD +NS_GetCurrentThread(nsIThread **result); + +/** + * Equivalent to nsIThreadManager::GetMainThread. + */ +extern NS_COM NS_METHOD +NS_GetMainThread(nsIThread **result); + +/** + * Equivalent to nsIThreadManager::GetThread. + */ +extern NS_COM NS_METHOD +NS_GetThread(const nsACString &name, nsIThread **result); + +/** + * Equivalent to nsIThreadManager::IsMainThread. + */ +extern NS_COM PRBool +NS_IsMainThread(); + +#else // MOZILLA_INTERNAL_API +#include "nsXPCOMCID.h" +#include "nsIThreadManager.h" +#include "nsServiceManagerUtils.h" + +/** + * XPCOM glue compatible versions + */ + +inline NS_METHOD +NS_NewThread(const nsACString &name, nsIRunnable *runnable, nsIThread **result) { + nsresult rv; + nsCOMPtr mgr = + do_GetService(NS_THREADMANAGER_CONTRACTID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + rv = mgr->NewThread(name, result); + NS_ENSURE_SUCCESS(rv, rv); + + if (runnable) + rv = (*result)->Dispatch(runnable, NS_DISPATCH_NORMAL); + return rv; +} + +inline NS_METHOD +NS_GetCurrentThread(nsIThread **result) { + nsresult rv; + nsCOMPtr mgr = + do_GetService(NS_THREADMANAGER_CONTRACTID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + return mgr->GetCurrentThread(result); +} + +inline NS_METHOD +NS_GetMainThread(nsIThread **result) { + nsresult rv; + nsCOMPtr mgr = + do_GetService(NS_THREADMANAGER_CONTRACTID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + return mgr->GetMainThread(result); +} + +inline NS_METHOD +NS_GetThread(const nsACString &name, nsIThread **result) { + nsresult rv; + nsCOMPtr mgr = + do_GetService(NS_THREADMANAGER_CONTRACTID, &rv); + NS_ENSURE_SUCCESS(rv, rv); + + return mgr->GetThread(name, result); +} + +inline PRBool +NS_IsMainThread() { + PRBool result = PR_FALSE; + nsCOMPtr mgr = + do_GetService(NS_THREADMANAGER_CONTRACTID); + if (mgr) + mgr->IsMainThread(&result); + return result; +} + + +#endif // MOZILLA_INTERNAL_API + +/** + * Variant on NS_NewThread. + */ +inline NS_METHOD +NS_NewThread(const char *name, nsIRunnable *runnable, nsIThread **result) { + return NS_NewThread(nsDependentCString(name), runnable, result); +} + +/** + * Variant on NS_GetThread. + */ +inline NS_METHOD +NS_GetThread(const char *name, nsIThread **result) { + return NS_GetThread(nsDependentCString(name), result); +} + +/** + * Helpers that work with nsCOMPtr. + */ + +inline already_AddRefed +do_GetCurrentThread() { + nsIThread *thread = nsnull; + NS_GetCurrentThread(&thread); + return already_AddRefed(thread); +} + +inline already_AddRefed +do_GetMainThread() { + nsIThread *thread = nsnull; + NS_GetMainThread(&thread); + return already_AddRefed(thread); +} + +inline already_AddRefed +do_GetThread(const nsACString &name) { + nsIThread *thread = nsnull; + NS_GetThread(name, &thread); + return already_AddRefed(thread); +} + +inline already_AddRefed +do_GetThread(const char *name) { + nsIThread *thread = nsnull; + NS_GetThread(name, &thread); + return already_AddRefed(thread); +} + +/** + * Run all pending tasks for a given thread before returning. + */ +inline NS_METHOD +NS_RunPendingTasks(nsIThread *thread) +{ + nsresult rv; + do { + rv = thread->RunNextTask(nsIThread::RUN_NO_WAIT); + } while (NS_SUCCEEDED(rv)); + + if (rv == NS_BASE_STREAM_WOULD_BLOCK) + rv = NS_OK; + + return rv; +} + +#endif // nsThreadUtils_h__ diff --git a/mozilla/xpcom/threads/nsTimerImpl.cpp b/mozilla/xpcom/threads/nsTimerImpl.cpp index 09ba994cae7..d0c6c23d619 100644 --- a/mozilla/xpcom/threads/nsTimerImpl.cpp +++ b/mozilla/xpcom/threads/nsTimerImpl.cpp @@ -44,7 +44,7 @@ #include "nsAutoPtr.h" #include "nsVoidArray.h" #include "nsThreadManager.h" -#include "nsRunnable.h" +#include "nsThreadUtils.h" #include "prmem.h" static PRInt32 gGenerator = 0; @@ -603,7 +603,7 @@ nsresult nsTimerManager::AddIdleTimer(nsITimer* timer) NS_IMETHODIMP nsTimerManager::FireNextIdleTimer() { - if (!gFireOnIdle || !nsThreadManager::IsMainThread()) { + if (!gFireOnIdle || !NS_IsMainThread()) { return NS_OK; }