2014-06-30 08:39:45 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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
|
2014-05-27 00:15:35 -07:00
|
|
|
static PRLogModuleInfo*
|
2012-10-29 16:32:10 -07:00
|
|
|
GetThreadPoolLog()
|
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
static PRLogModuleInfo* sLog;
|
|
|
|
if (!sLog) {
|
2012-10-29 16:32:10 -07:00
|
|
|
sLog = PR_NewLogModule("nsThreadPool");
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-10-29 16:32:10 -07:00
|
|
|
return sLog;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2013-11-18 18:34:00 -08:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#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)
|
|
|
|
|
2013-07-18 19:31:26 -07:00
|
|
|
NS_IMPL_ADDREF(nsThreadPool)
|
|
|
|
NS_IMPL_RELEASE(nsThreadPool)
|
2013-10-10 13:42:16 -07:00
|
|
|
NS_IMPL_CLASSINFO(nsThreadPool, nullptr, nsIClassInfo::THREADSAFE,
|
2010-06-22 09:59:57 -07:00
|
|
|
NS_THREADPOOL_CID)
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(nsThreadPool, nsIThreadPool, nsIEventTarget,
|
|
|
|
nsIRunnable)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsThreadPool, nsIThreadPool, nsIEventTarget)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsThreadPool::nsThreadPool()
|
|
|
|
: mThreadLimit(DEFAULT_THREAD_LIMIT)
|
|
|
|
, mIdleThreadLimit(DEFAULT_IDLE_THREAD_LIMIT)
|
|
|
|
, mIdleThreadTimeout(DEFAULT_IDLE_THREAD_TIMEOUT)
|
|
|
|
, mIdleCount(0)
|
2014-02-17 23:11:56 -08:00
|
|
|
, mStackSize(nsIThreadManager::DEFAULT_STACK_SIZE)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mShutdown(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsThreadPool::~nsThreadPool()
|
|
|
|
{
|
2013-10-23 11:21:37 -07:00
|
|
|
// Threads keep a reference to the nsThreadPool until they return from Run()
|
|
|
|
// after removing themselves from mThreads.
|
|
|
|
MOZ_ASSERT(mThreads.IsEmpty());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::PutEvent(nsIRunnable* aEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Avoid spawning a new thread while holding the event queue lock...
|
2014-02-17 14:53:53 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool spawnThread = false;
|
2014-02-17 14:53:53 -08:00
|
|
|
uint32_t stackSize = 0;
|
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
|
|
|
|
2014-06-21 03:26:43 -07:00
|
|
|
if (NS_WARN_IF(mShutdown)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2013-10-24 21:40:39 -07:00
|
|
|
LOG(("THRD-P(%p) put [%d %d %d]\n", this, mIdleCount, mThreads.Count(),
|
2007-03-22 10:30:00 -07:00
|
|
|
mThreadLimit));
|
2014-05-27 00:15:35 -07:00
|
|
|
MOZ_ASSERT(mIdleCount <= (uint32_t)mThreads.Count(), "oops");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Make sure we have a thread to service this event.
|
2014-05-27 00:15:35 -07:00
|
|
|
if (mIdleCount == 0 && mThreads.Count() < (int32_t)mThreadLimit) {
|
2011-10-17 07:59:28 -07:00
|
|
|
spawnThread = true;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2013-10-24 21:40:39 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
mEvents.PutEvent(aEvent);
|
2014-02-17 14:53:53 -08:00
|
|
|
stackSize = mStackSize;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("THRD-P(%p) put [spawn=%d]\n", this, spawnThread));
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!spawnThread) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-24 21:40:39 -07:00
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsThreadManager::get()->NewThread(0,
|
2014-02-17 14:53:53 -08:00
|
|
|
stackSize,
|
2013-10-24 21:40:39 -07:00
|
|
|
getter_AddRefs(thread));
|
2014-05-27 00:15:35 -07:00
|
|
|
if (NS_WARN_IF(!thread)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-24 21:40:39 -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());
|
2014-05-27 00:15:35 -07:00
|
|
|
if (mThreads.Count() < (int32_t)mThreadLimit) {
|
2013-10-24 21:40:39 -07:00
|
|
|
mThreads.AppendObject(thread);
|
|
|
|
} else {
|
|
|
|
killThread = true; // okay, we don't need this thread anymore
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-10-24 21:40:39 -07:00
|
|
|
}
|
|
|
|
LOG(("THRD-P(%p) put [%p kill=%d]\n", this, thread.get(), killThread));
|
|
|
|
if (killThread) {
|
|
|
|
// Pending events are processed on the current thread during
|
|
|
|
// nsIThread::Shutdown() execution, so if nsThreadPool::Dispatch() is called
|
|
|
|
// under caller's lock then deadlock could occur. This happens e.g. in case
|
|
|
|
// of nsStreamCopier. To prevent this situation, dispatch a shutdown event
|
|
|
|
// to the current thread instead of calling nsIThread::Shutdown() directly.
|
|
|
|
|
|
|
|
nsRefPtr<nsIRunnable> r = NS_NewRunnableMethod(thread,
|
|
|
|
&nsIThread::Shutdown);
|
|
|
|
NS_DispatchToCurrentThread(r);
|
|
|
|
} else {
|
|
|
|
thread->Dispatch(this, NS_DISPATCH_NORMAL);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::ShutdownThread(nsIThread* aThread)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
LOG(("THRD-P(%p) shutdown async [%p]\n", this, aThread));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
// This method is responsible for calling Shutdown on |aThread|. This must be
|
2007-03-22 10:30:00 -07:00
|
|
|
// done from some other thread, so we use the main thread of the application.
|
|
|
|
|
2013-02-07 21:54:20 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread(), "wrong thread");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
nsRefPtr<nsIRunnable> r = NS_NewRunnableMethod(aThread, &nsIThread::Shutdown);
|
2012-01-11 08:28:21 -08:00
|
|
|
NS_DispatchToMainThread(r);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-24 21:40:39 -07:00
|
|
|
NS_IMETHODIMP
|
2007-03-22 10:30:00 -07:00
|
|
|
nsThreadPool::Run()
|
|
|
|
{
|
|
|
|
LOG(("THRD-P(%p) enter\n", this));
|
|
|
|
|
2012-06-12 10:06:20 -07:00
|
|
|
mThreadNaming.SetThreadPoolName(mName);
|
|
|
|
|
2013-10-24 21:40:39 -07:00
|
|
|
nsCOMPtr<nsIThread> current;
|
|
|
|
nsThreadManager::get()->GetCurrentThread(getter_AddRefs(current));
|
|
|
|
|
|
|
|
bool shutdownThreadOnExit = false;
|
|
|
|
bool exitThread = false;
|
|
|
|
bool wasIdle = false;
|
|
|
|
PRIntervalTime idleSince;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
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.
|
2014-05-27 00:15:35 -07:00
|
|
|
if (mIdleCount > mIdleThreadLimit || (now - idleSince) >= timeout) {
|
2011-10-17 07:59:28 -07:00
|
|
|
exitThread = true;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
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) {
|
2014-05-27 00:15:35 -07:00
|
|
|
if (wasIdle) {
|
2007-03-22 10:30:00 -07:00
|
|
|
--mIdleCount;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2013-10-24 21:40:39 -07:00
|
|
|
shutdownThreadOnExit = mThreads.RemoveObject(current);
|
2007-03-22 10:30:00 -07:00
|
|
|
} 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));
|
2013-10-24 21:40:39 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::Dispatch(nsIRunnable* aEvent, uint32_t aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
LOG(("THRD-P(%p) dispatch [%p %x]\n", this, aEvent, aFlags));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (NS_WARN_IF(mShutdown)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aFlags & DISPATCH_SYNC) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thread));
|
2014-05-27 00:15:35 -07:00
|
|
|
if (NS_WARN_IF(!thread)) {
|
2013-11-19 13:27:37 -08:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsRefPtr<nsThreadSyncDispatch> wrapper =
|
2014-05-27 00:15:35 -07:00
|
|
|
new nsThreadSyncDispatch(thread, aEvent);
|
2007-03-22 10:30:00 -07:00
|
|
|
PutEvent(wrapper);
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
while (wrapper->IsPending()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ProcessNextEvent(thread);
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2014-05-27 00:15:35 -07:00
|
|
|
NS_ASSERTION(aFlags == NS_DISPATCH_NORMAL, "unexpected dispatch flags");
|
|
|
|
PutEvent(aEvent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::IsOnCurrentThread(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-02-17 14:53:52 -08:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2014-06-21 03:26:43 -07:00
|
|
|
if (NS_WARN_IF(mShutdown)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2014-02-17 14:53:52 -08:00
|
|
|
nsIThread* thread = NS_GetCurrentThread();
|
2014-02-17 23:11:56 -08:00
|
|
|
for (uint32_t i = 0; i < static_cast<uint32_t>(mThreads.Count()); ++i) {
|
2014-02-17 14:53:52 -08:00
|
|
|
if (mThreads[i] == thread) {
|
2014-05-27 00:15:35 -07:00
|
|
|
*aResult = true;
|
2014-02-17 14:53:52 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2014-05-27 00:15:35 -07:00
|
|
|
*aResult = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsThreadPool::Shutdown()
|
|
|
|
{
|
2013-10-24 21:40:39 -07:00
|
|
|
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();
|
|
|
|
|
2013-10-24 21:40:39 -07:00
|
|
|
threads.AppendObjects(mThreads);
|
|
|
|
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);
|
2013-10-23 13:31:15 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-24 21:40:39 -07:00
|
|
|
// It's important that we shutdown the threads while outside the event queue
|
|
|
|
// monitor. Otherwise, we could end up dead-locking.
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
for (int32_t i = 0; i < threads.Count(); ++i) {
|
2013-10-24 21:40:39 -07:00
|
|
|
threads[i]->Shutdown();
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2013-10-24 21:40:39 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::GetThreadLimit(uint32_t* aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
*aValue = mThreadLimit;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::SetThreadLimit(uint32_t aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2014-05-27 00:15:35 -07:00
|
|
|
mThreadLimit = aValue;
|
|
|
|
if (mIdleThreadLimit > mThreadLimit) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mIdleThreadLimit = mThreadLimit;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2013-05-16 09:02:46 -07:00
|
|
|
|
2013-10-24 21:40:39 -07:00
|
|
|
if (static_cast<uint32_t>(mThreads.Count()) > mThreadLimit) {
|
2013-05-16 09:02:46 -07:00
|
|
|
mon.NotifyAll(); // wake up threads so they observe this change
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::GetIdleThreadLimit(uint32_t* aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
*aValue = mIdleThreadLimit;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::SetIdleThreadLimit(uint32_t aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2014-05-27 00:15:35 -07:00
|
|
|
mIdleThreadLimit = aValue;
|
|
|
|
if (mIdleThreadLimit > mThreadLimit) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mIdleThreadLimit = mThreadLimit;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2013-05-16 09:02:46 -07:00
|
|
|
|
|
|
|
// Do we need to kill some idle threads?
|
|
|
|
if (mIdleCount > mIdleThreadLimit) {
|
|
|
|
mon.NotifyAll(); // wake up threads so they observe this change
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::GetIdleThreadTimeout(uint32_t* aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
*aValue = mIdleThreadTimeout;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::SetIdleThreadTimeout(uint32_t aValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2013-05-16 09:02:46 -07:00
|
|
|
uint32_t oldTimeout = mIdleThreadTimeout;
|
2014-05-27 00:15:35 -07:00
|
|
|
mIdleThreadTimeout = aValue;
|
2013-05-16 09:02:46 -07:00
|
|
|
|
|
|
|
// Do we need to notify any idle threads that their sleep time has shortened?
|
|
|
|
if (mIdleThreadTimeout < oldTimeout && mIdleCount > 0) {
|
|
|
|
mon.NotifyAll(); // wake up threads so they observe this change
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-08-11 17:01:47 -07:00
|
|
|
|
2014-02-17 14:53:53 -08:00
|
|
|
NS_IMETHODIMP
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::GetThreadStackSize(uint32_t* aValue)
|
2014-02-17 14:53:53 -08:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2014-05-27 00:15:35 -07:00
|
|
|
*aValue = mStackSize;
|
2014-02-17 14:53:53 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 00:15:35 -07:00
|
|
|
nsThreadPool::SetThreadStackSize(uint32_t aValue)
|
2014-02-17 14:53:53 -08:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
|
2014-05-27 00:15:35 -07:00
|
|
|
mStackSize = aValue;
|
2014-02-17 14:53:53 -08:00
|
|
|
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());
|
2014-05-27 00:15:35 -07:00
|
|
|
if (mThreads.Count()) {
|
2012-06-12 10:06:20 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2012-06-12 10:06:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mName = aName;
|
|
|
|
return NS_OK;
|
|
|
|
}
|