2014-01-23 04:21:05 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2015-08-03 18:44:10 +00:00
|
|
|
#include "mozilla/SharedThreadPool.h"
|
2014-01-23 04:21:05 +00:00
|
|
|
#include "mozilla/Monitor.h"
|
2015-08-03 18:39:45 +00:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2015-08-03 21:27:12 +00:00
|
|
|
#include "mozilla/Services.h"
|
2014-01-23 04:21:05 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2015-08-03 21:27:12 +00:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
2014-01-23 04:21:05 +00:00
|
|
|
#ifdef XP_WIN
|
2014-08-20 21:23:22 +00:00
|
|
|
#include "ThreadPoolCOMListener.h"
|
2014-01-23 04:21:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// Created and destroyed on the main thread.
|
|
|
|
static StaticAutoPtr<ReentrantMonitor> sMonitor;
|
|
|
|
|
|
|
|
// Hashtable, maps thread pool name to SharedThreadPool instance.
|
|
|
|
// Modified only on the main thread.
|
|
|
|
static StaticAutoPtr<nsDataHashtable<nsCStringHashKey, SharedThreadPool*>> sPools;
|
|
|
|
|
|
|
|
static already_AddRefed<nsIThreadPool>
|
|
|
|
CreateThreadPool(const nsCString& aName);
|
|
|
|
|
2015-08-03 21:27:12 +00:00
|
|
|
class SharedThreadPoolShutdownObserver : public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
protected:
|
|
|
|
virtual ~SharedThreadPoolShutdownObserver() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(SharedThreadPoolShutdownObserver, nsIObserver, nsISupports)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SharedThreadPoolShutdownObserver::Observe(nsISupports* aSubject, const char *aTopic,
|
|
|
|
const char16_t *aData)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(!strcmp(aTopic, "xpcom-shutdown-threads"));
|
|
|
|
SharedThreadPool::SpinUntilEmpty();
|
|
|
|
sMonitor = nullptr;
|
|
|
|
sPools = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-23 04:21:05 +00:00
|
|
|
void
|
2015-06-24 23:01:15 +00:00
|
|
|
SharedThreadPool::InitStatics()
|
2014-01-23 04:21:05 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-06-24 23:01:15 +00:00
|
|
|
MOZ_ASSERT(!sMonitor && !sPools);
|
2014-01-23 04:21:05 +00:00
|
|
|
sMonitor = new ReentrantMonitor("SharedThreadPool");
|
|
|
|
sPools = new nsDataHashtable<nsCStringHashKey, SharedThreadPool*>();
|
2015-08-03 21:27:12 +00:00
|
|
|
nsCOMPtr<nsIObserverService> obsService = mozilla::services::GetObserverService();
|
|
|
|
nsCOMPtr<nsIObserver> obs = new SharedThreadPoolShutdownObserver();
|
|
|
|
obsService->AddObserver(obs, "xpcom-shutdown-threads", false);
|
2014-01-23 04:21:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 23:01:15 +00:00
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
SharedThreadPool::IsEmpty()
|
2014-01-23 04:21:05 +00:00
|
|
|
{
|
2015-06-24 23:01:15 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(*sMonitor);
|
|
|
|
return !sPools->Count();
|
2014-01-23 04:21:05 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 22:53:53 +00:00
|
|
|
/* static */
|
|
|
|
void
|
2015-06-24 23:01:15 +00:00
|
|
|
SharedThreadPool::SpinUntilEmpty()
|
2014-02-17 22:53:53 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-06-24 23:01:15 +00:00
|
|
|
while (!IsEmpty()) {
|
|
|
|
sMonitor->AssertNotCurrentThreadIn();
|
|
|
|
NS_ProcessNextEvent(NS_GetCurrentThread(), true);
|
2014-02-17 22:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 14:00:52 +00:00
|
|
|
already_AddRefed<SharedThreadPool>
|
2014-02-17 22:53:52 +00:00
|
|
|
SharedThreadPool::Get(const nsCString& aName, uint32_t aThreadLimit)
|
2014-01-23 04:21:05 +00:00
|
|
|
{
|
2015-06-24 23:01:15 +00:00
|
|
|
MOZ_ASSERT(sMonitor && sPools);
|
2014-01-23 04:21:05 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(*sMonitor);
|
|
|
|
SharedThreadPool* pool = nullptr;
|
2014-02-17 22:53:52 +00:00
|
|
|
nsresult rv;
|
2014-01-23 04:21:05 +00:00
|
|
|
if (!sPools->Get(aName, &pool)) {
|
|
|
|
nsCOMPtr<nsIThreadPool> threadPool(CreateThreadPool(aName));
|
|
|
|
NS_ENSURE_TRUE(threadPool, nullptr);
|
|
|
|
pool = new SharedThreadPool(aName, threadPool);
|
2014-02-17 22:53:52 +00:00
|
|
|
|
|
|
|
// Set the thread and idle limits. Note that we don't rely on the
|
|
|
|
// EnsureThreadLimitIsAtLeast() call below, as the default thread limit
|
|
|
|
// is 4, and if aThreadLimit is less than 4 we'll end up with a pool
|
|
|
|
// with 4 threads rather than what we expected; so we'll have unexpected
|
|
|
|
// behaviour.
|
|
|
|
rv = pool->SetThreadLimit(aThreadLimit);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
rv = pool->SetIdleThreadLimit(aThreadLimit);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
2014-01-23 04:21:05 +00:00
|
|
|
sPools->Put(aName, pool);
|
2014-02-17 22:53:52 +00:00
|
|
|
} else if (NS_FAILED(pool->EnsureThreadLimitIsAtLeast(aThreadLimit))) {
|
|
|
|
NS_WARNING("Failed to set limits on thread pool");
|
2014-01-23 04:21:05 +00:00
|
|
|
}
|
2014-02-17 22:53:52 +00:00
|
|
|
|
2014-01-23 04:21:05 +00:00
|
|
|
MOZ_ASSERT(pool);
|
|
|
|
RefPtr<SharedThreadPool> instance(pool);
|
|
|
|
return instance.forget();
|
|
|
|
}
|
|
|
|
|
2014-03-27 20:38:33 +00:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) SharedThreadPool::AddRef(void)
|
2014-01-23 04:21:05 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(sMonitor);
|
|
|
|
ReentrantMonitorAutoEnter mon(*sMonitor);
|
|
|
|
MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt");
|
|
|
|
nsrefcnt count = ++mRefCnt;
|
|
|
|
NS_LOG_ADDREF(this, count, "SharedThreadPool", sizeof(*this));
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-03-27 20:38:33 +00:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) SharedThreadPool::Release(void)
|
2014-01-23 04:21:05 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(sMonitor);
|
2015-06-24 23:12:30 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(*sMonitor);
|
|
|
|
nsrefcnt count = --mRefCnt;
|
|
|
|
NS_LOG_RELEASE(this, count, "SharedThreadPool");
|
|
|
|
if (count) {
|
|
|
|
return count;
|
2014-01-23 04:21:05 +00:00
|
|
|
}
|
2015-06-24 23:12:30 +00:00
|
|
|
|
|
|
|
// Remove SharedThreadPool from table of pools.
|
|
|
|
sPools->Remove(mName);
|
|
|
|
MOZ_ASSERT(!sPools->Get(mName));
|
|
|
|
|
|
|
|
// Dispatch an event to the main thread to call Shutdown() on
|
|
|
|
// the nsIThreadPool. The Runnable here will add a refcount to the pool,
|
|
|
|
// and when the Runnable releases the nsIThreadPool it will be deleted.
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableMethod(mPool, &nsIThreadPool::Shutdown);
|
2015-08-03 21:40:06 +00:00
|
|
|
NS_DispatchToMainThread(r);
|
2015-06-24 23:12:30 +00:00
|
|
|
|
|
|
|
// Stabilize refcount, so that if something in the dtor QIs, it won't explode.
|
|
|
|
mRefCnt = 1;
|
|
|
|
delete this;
|
|
|
|
return 0;
|
2014-01-23 04:21:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE(SharedThreadPool, nsIThreadPool, nsIEventTarget)
|
2014-01-23 04:21:05 +00:00
|
|
|
|
|
|
|
SharedThreadPool::SharedThreadPool(const nsCString& aName,
|
|
|
|
nsIThreadPool* aPool)
|
|
|
|
: mName(aName)
|
|
|
|
, mPool(aPool)
|
|
|
|
, mRefCnt(0)
|
|
|
|
{
|
2014-02-17 22:53:53 +00:00
|
|
|
MOZ_COUNT_CTOR(SharedThreadPool);
|
2014-01-23 04:21:05 +00:00
|
|
|
mEventTarget = do_QueryInterface(aPool);
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedThreadPool::~SharedThreadPool()
|
|
|
|
{
|
2014-02-17 22:53:53 +00:00
|
|
|
MOZ_COUNT_DTOR(SharedThreadPool);
|
2014-01-23 04:21:05 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 22:53:52 +00:00
|
|
|
nsresult
|
|
|
|
SharedThreadPool::EnsureThreadLimitIsAtLeast(uint32_t aLimit)
|
|
|
|
{
|
2015-08-03 18:39:45 +00:00
|
|
|
// We limit the number of threads that we use. Note that we
|
2014-02-17 22:53:52 +00:00
|
|
|
// set the thread limit to the same as the idle limit so that we're not
|
|
|
|
// constantly creating and destroying threads (see Bug 881954). When the
|
|
|
|
// thread pool threads shutdown they dispatch an event to the main thread
|
|
|
|
// to call nsIThread::Shutdown(), and if we're very busy that can take a
|
|
|
|
// while to run, and we end up with dozens of extra threads. Note that
|
|
|
|
// threads that are idle for 60 seconds are shutdown naturally.
|
|
|
|
uint32_t existingLimit = 0;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = mPool->GetThreadLimit(&existingLimit);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (aLimit > existingLimit) {
|
|
|
|
rv = mPool->SetThreadLimit(aLimit);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mPool->GetIdleThreadLimit(&existingLimit);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (aLimit > existingLimit) {
|
|
|
|
rv = mPool->SetIdleThreadLimit(aLimit);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-23 04:21:05 +00:00
|
|
|
static already_AddRefed<nsIThreadPool>
|
|
|
|
CreateThreadPool(const nsCString& aName)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIThreadPool> pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
rv = pool->SetName(aName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
2015-08-03 18:39:45 +00:00
|
|
|
rv = pool->SetThreadStackSize(SharedThreadPool::kStackSize);
|
2014-02-17 22:53:53 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
2014-01-23 04:21:05 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// Ensure MSCOM is initialized on the thread pools threads.
|
|
|
|
nsCOMPtr<nsIThreadPoolListener> listener = new MSCOMInitThreadPoolListener();
|
|
|
|
rv = pool->SetListener(listener);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return pool.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|