From 1acd19bbe86ece3712fc7f8780d742afeb9d653d Mon Sep 17 00:00:00 2001 From: "dougt%netscape.com" Date: Thu, 26 Apr 2001 05:30:26 +0000 Subject: [PATCH] Fixes a problem where hreadpool optimistically kills worker threads. r=darin@netscape.com, sr=waterson@netscape.com, b=76198 git-svn-id: svn://10.0.0.236/trunk@93114 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/xpcom/threads/nsThread.cpp | 31 ++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/mozilla/xpcom/threads/nsThread.cpp b/mozilla/xpcom/threads/nsThread.cpp index f5146d704f2..810888e89f6 100644 --- a/mozilla/xpcom/threads/nsThread.cpp +++ b/mozilla/xpcom/threads/nsThread.cpp @@ -561,30 +561,50 @@ nsThreadPool::GetRequest(nsIThread* currentThread) return request; } + if (mShuttingDown) + break; + // no requests, and we're not shutting down yet... // if we have more than the minimum required threads already then - // we can just go away + // then we may be able to go away. PRUint32 threadCnt; rv = mThreads->Count(&threadCnt); if (NS_FAILED(rv)) break; if (threadCnt > mMinThreads) { + // to avoid multiple thread spawns/exits, we need to + // wait for some period of time while waiting for any + // additional requests. If this this wait yeilds no + // request, then we can exit. + // + // TODO: determine what the optimal timeout value is. + // For now, just use 5 seconds. + + PRIntervalTime interval = PR_SecondsToInterval(5); PR_LOG(nsIThreadLog, PR_LOG_DEBUG, + ("nsIThreadPool thread %p waiting for %d seconds before exiting (%d threads in pool)\n", + currentThread, interval, threadCnt)); + + (void) PR_WaitCondVar( mRequestAdded, interval); + + rv = mRequests->Count(&requestCnt); + if (NS_FAILED(rv) || requestCnt == 0) { + PR_LOG(nsIThreadLog, PR_LOG_DEBUG, ("nsIThreadPool thread %p: %d threads in pool, min = %d, exiting...\n", currentThread, threadCnt, mMinThreads)); RemoveThread(currentThread); return nsnull; // causes nsThreadPoolRunnable::Run to quit } - + } + else + { PR_LOG(nsIThreadLog, PR_LOG_DEBUG, ("nsIThreadPool thread %p waiting (%d threads in pool)\n", currentThread, threadCnt)); - if (mShuttingDown) - break; - (void)PR_WaitCondVar(mRequestAdded, PR_INTERVAL_NO_TIMEOUT); } + } // no requests, we are going to dump the thread. PR_LOG(nsIThreadLog, PR_LOG_DEBUG, ("nsIThreadPool thread %p -- no more requests, exiting...\n", @@ -724,7 +744,6 @@ nsThreadPool::Init(PRUint32 minThreadCount, return NS_ERROR_OUT_OF_MEMORY; } - nsresult nsThreadPool::AddThread() {