2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
#include "nsThreadPool.h"
|
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#include "nsThread.h"
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "prinrval.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef PR_LOGGING
|
2012-10-29 16:32:10 -07:00
|
|
|
static PRLogModuleInfo *
|
|
|
|
GetThreadPoolLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("nsThreadPool");
|
|
|
|
return sLog;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2012-10-29 16:32:10 -07:00
|
|
|
#define LOG(args) PR_LOG(GetThreadPoolLog(), PR_LOG_DEBUG, args)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// DESIGN:
|
|
|
|
// o Allocate anonymous threads.
|
|
|
|
// o Use nsThreadPool::Run as the main routine for each thread.
|
|
|
|
// o Each thread waits on the event queue's monitor, checking for
|
|
|
|
// pending events and rescheduling itself as an idle thread.
|
|
|
|
|
|
|
|
#define DEFAULT_THREAD_LIMIT 4
|
|
|
|
#define DEFAULT_IDLE_THREAD_LIMIT 1
|
|
|
|
#define DEFAULT_IDLE_THREAD_TIMEOUT PR_SecondsToInterval(60)
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsThreadPool)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsThreadPool)
|
2010-06-22 09:59:57 -07:00
|
|
|
NS_IMPL_CLASSINFO(nsThreadPool, NULL, nsIClassInfo::THREADSAFE,
|
|
|
|
NS_THREADPOOL_CID)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE3_CI(nsThreadPool, nsIThreadPool, nsIEventTarget,
|
|
|
|
nsIRunnable)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER2(nsThreadPool, nsIThreadPool, nsIEventTarget)
|
|
|
|
|
|
|
|
nsThreadPool::nsThreadPool()
|
|
|
|
: mThreadLimit(DEFAULT_THREAD_LIMIT)
|
|
|
|
, mIdleThreadLimit(DEFAULT_IDLE_THREAD_LIMIT)
|
|
|
|
, mIdleThreadTimeout(DEFAULT_IDLE_THREAD_TIMEOUT)
|
|
|
|
, mIdleCount(0)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mShutdown(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsThreadPool::~nsThreadPool()
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsThreadPool::PutEvent(nsIRunnable *event)
|
|
|
|
{
|
|
|
|
// Avoid spawning a new thread while holding the event queue lock...
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool spawnThread = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
LOG(("THRD-P(%p) put [%d %d %d]\n", this, mIdleCount, mThreads.Count(),
|
|
|
|
mThreadLimit));
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_ASSERTION(mIdleCount <= (uint32_t) mThreads.Count(), "oops");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Make sure we have a thread to service this event.
|
2012-08-22 08:56:38 -07:00
|
|
|
if (mIdleCount == 0 && mThreads.Count() < (int32_t) mThreadLimit)
|
2011-10-17 07:59:28 -07:00
|
|
|
spawnThread = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mEvents.PutEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("THRD-P(%p) put [spawn=%d]\n", this, spawnThread));
|
|
|
|
if (!spawnThread)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
2011-07-26 20:26:47 -07:00
|
|
|
nsThreadManager::get()->NewThread(0,
|
|
|
|
nsIThreadManager::DEFAULT_STACK_SIZE,
|
|
|
|
getter_AddRefs(thread));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_STATE(thread);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool killThread = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2012-08-22 08:56:38 -07:00
|
|
|
if (mThreads.Count() < (int32_t) mThreadLimit) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mThreads.AppendObject(thread);
|
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
killThread = true; // okay, we don't need this thread anymore
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG(("THRD-P(%p) put [%p kill=%d]\n", this, thread.get(), killThread));
|
|
|
|
if (killThread) {
|
|
|
|
thread->Shutdown();
|
|
|
|
} else {
|
|
|
|
thread->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsThreadPool::ShutdownThread(nsIThread *thread)
|
|
|
|
{
|
|
|
|
LOG(("THRD-P(%p) shutdown async [%p]\n", this, thread));
|
|
|
|
|
|
|
|
// This method is responsible for calling Shutdown on |thread|. This must be
|
|
|
|
// done from some other thread, so we use the main thread of the application.
|
|
|
|
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "wrong thread");
|
|
|
|
|
2012-01-11 08:28:21 -08:00
|
|
|
nsRefPtr<nsIRunnable> r = NS_NewRunnableMethod(thread, &nsIThread::Shutdown);
|
|
|
|
NS_DispatchToMainThread(r);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadPool::Run()
|
|
|
|
{
|
|
|
|
LOG(("THRD-P(%p) enter\n", this));
|
|
|
|
|
2012-06-12 10:06:20 -07:00
|
|
|
mThreadNaming.SetThreadPoolName(mName);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIThread> current;
|
|
|
|
nsThreadManager::get()->GetCurrentThread(getter_AddRefs(current));
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool shutdownThreadOnExit = false;
|
|
|
|
bool exitThread = false;
|
|
|
|
bool wasIdle = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRIntervalTime idleSince;
|
|
|
|
|
2008-08-11 17:01:47 -07:00
|
|
|
nsCOMPtr<nsIThreadPoolListener> listener;
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2008-08-11 17:01:47 -07:00
|
|
|
listener = mListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
listener->OnThreadCreated();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
do {
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mEvents.GetPendingEvent(getter_AddRefs(event))) {
|
|
|
|
PRIntervalTime now = PR_IntervalNow();
|
|
|
|
PRIntervalTime timeout = PR_MillisecondsToInterval(mIdleThreadTimeout);
|
|
|
|
|
|
|
|
// If we are shutting down, then don't keep any idle threads
|
|
|
|
if (mShutdown) {
|
2011-10-17 07:59:28 -07:00
|
|
|
exitThread = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
if (wasIdle) {
|
|
|
|
// if too many idle threads or idle for too long, then bail.
|
|
|
|
if (mIdleCount > mIdleThreadLimit || (now - idleSince) >= timeout)
|
2011-10-17 07:59:28 -07:00
|
|
|
exitThread = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
// if would be too many idle threads...
|
|
|
|
if (mIdleCount == mIdleThreadLimit) {
|
2011-10-17 07:59:28 -07:00
|
|
|
exitThread = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
++mIdleCount;
|
|
|
|
idleSince = now;
|
2011-10-17 07:59:28 -07:00
|
|
|
wasIdle = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exitThread) {
|
|
|
|
if (wasIdle)
|
|
|
|
--mIdleCount;
|
|
|
|
shutdownThreadOnExit = mThreads.RemoveObject(current);
|
|
|
|
} else {
|
|
|
|
PRIntervalTime delta = timeout - (now - idleSince);
|
|
|
|
LOG(("THRD-P(%p) waiting [%d]\n", this, delta));
|
|
|
|
mon.Wait(delta);
|
|
|
|
}
|
|
|
|
} else if (wasIdle) {
|
2011-10-17 07:59:28 -07:00
|
|
|
wasIdle = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
--mIdleCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (event) {
|
|
|
|
LOG(("THRD-P(%p) running [%p]\n", this, event.get()));
|
|
|
|
event->Run();
|
|
|
|
}
|
|
|
|
} while (!exitThread);
|
|
|
|
|
2008-08-11 17:01:47 -07:00
|
|
|
if (listener) {
|
|
|
|
listener->OnThreadShuttingDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shutdownThreadOnExit) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ShutdownThread(current);
|
2008-08-11 17:01:47 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
LOG(("THRD-P(%p) leave\n", this));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsThreadPool::Dispatch(nsIRunnable *event, uint32_t flags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
LOG(("THRD-P(%p) dispatch [%p %x]\n", this, event, flags));
|
|
|
|
|
|
|
|
NS_ENSURE_STATE(!mShutdown);
|
|
|
|
|
|
|
|
if (flags & DISPATCH_SYNC) {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thread));
|
|
|
|
NS_ENSURE_STATE(thread);
|
|
|
|
|
|
|
|
nsRefPtr<nsThreadSyncDispatch> wrapper =
|
|
|
|
new nsThreadSyncDispatch(thread, event);
|
|
|
|
PutEvent(wrapper);
|
|
|
|
|
|
|
|
while (wrapper->IsPending())
|
|
|
|
NS_ProcessNextEvent(thread);
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(flags == NS_DISPATCH_NORMAL, "unexpected dispatch flags");
|
|
|
|
PutEvent(event);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsThreadPool::IsOnCurrentThread(bool *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// No one should be calling this method. If this assertion gets hit, then we
|
|
|
|
// need to think carefully about what this method should be returning.
|
|
|
|
NS_NOTREACHED("implement me");
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadPool::Shutdown()
|
|
|
|
{
|
|
|
|
nsCOMArray<nsIThread> threads;
|
2008-08-11 17:01:47 -07:00
|
|
|
nsCOMPtr<nsIThreadPoolListener> listener;
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2011-10-17 07:59:28 -07:00
|
|
|
mShutdown = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
mon.NotifyAll();
|
|
|
|
|
|
|
|
threads.AppendObjects(mThreads);
|
2008-08-14 12:04:31 -07:00
|
|
|
mThreads.Clear();
|
2008-08-11 17:01:47 -07:00
|
|
|
|
|
|
|
// Swap in a null listener so that we release the listener at the end of
|
|
|
|
// this method. The listener will be kept alive as long as the other threads
|
|
|
|
// that were created when it was set.
|
|
|
|
mListener.swap(listener);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// It's important that we shutdown the threads while outside the event queue
|
2008-08-14 12:04:31 -07:00
|
|
|
// monitor. Otherwise, we could end up dead-locking.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < threads.Count(); ++i)
|
2007-03-22 10:30:00 -07:00
|
|
|
threads[i]->Shutdown();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsThreadPool::GetThreadLimit(uint32_t *value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*value = mThreadLimit;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsThreadPool::SetThreadLimit(uint32_t value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2007-03-22 10:30:00 -07:00
|
|
|
mThreadLimit = value;
|
|
|
|
if (mIdleThreadLimit > mThreadLimit)
|
|
|
|
mIdleThreadLimit = mThreadLimit;
|
|
|
|
mon.NotifyAll(); // wake up threads so they observe this change
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsThreadPool::GetIdleThreadLimit(uint32_t *value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*value = mIdleThreadLimit;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsThreadPool::SetIdleThreadLimit(uint32_t value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2007-03-22 10:30:00 -07:00
|
|
|
mIdleThreadLimit = value;
|
|
|
|
if (mIdleThreadLimit > mThreadLimit)
|
|
|
|
mIdleThreadLimit = mThreadLimit;
|
|
|
|
mon.NotifyAll(); // wake up threads so they observe this change
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsThreadPool::GetIdleThreadTimeout(uint32_t *value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*value = mIdleThreadTimeout;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsThreadPool::SetIdleThreadTimeout(uint32_t value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2007-03-22 10:30:00 -07:00
|
|
|
mIdleThreadTimeout = value;
|
|
|
|
mon.NotifyAll(); // wake up threads so they observe this change
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-08-11 17:01:47 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadPool::GetListener(nsIThreadPoolListener** aListener)
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2008-08-11 17:01:47 -07:00
|
|
|
NS_IF_ADDREF(*aListener = mListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadPool::SetListener(nsIThreadPoolListener* aListener)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIThreadPoolListener> swappedListener(aListener);
|
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2008-08-11 17:01:47 -07:00
|
|
|
mListener.swap(swappedListener);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-06-12 10:06:20 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadPool::SetName(const nsACString& aName)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
|
|
|
if (mThreads.Count())
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mName = aName;
|
|
|
|
return NS_OK;
|
|
|
|
}
|