/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* 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/. */
// 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<nsTHashMap<nsCStringHashKey, SharedThreadPool*>> sPools;
return sPools->WithEntryHandle(
aName, [&](auto&& entry) -> already_AddRefed<SharedThreadPool> { if (entry) {
pool = entry.Data(); if (NS_FAILED(pool->EnsureThreadLimitIsAtLeast(aThreadLimit))) {
NS_WARNING("Failed to set limits on thread pool");
}
} else {
nsCOMPtr<nsIThreadPool> threadPool(CreateThreadPool(aName)); if (NS_WARN_IF(!threadPool)) {
sPools->Remove(aName); // XXX entry.Remove() return nullptr;
}
pool = new SharedThreadPool(aName, threadPool);
// 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.
nsresult rv = pool->SetThreadLimit(aThreadLimit); if (NS_WARN_IF(NS_FAILED(rv))) {
sPools->Remove(aName); // XXX entry.Remove() return nullptr;
}
// 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.
NS_DispatchToMainThread(NewRunnableMethod("nsIThreadPool::Shutdown", mPool,
&nsIThreadPool::Shutdown));
// Stabilize refcount, so that if something in the dtor QIs, it won't explode.
mRefCnt = 1; deletethis; return 0;
}
nsresult SharedThreadPool::EnsureThreadLimitIsAtLeast(uint32_t aLimit) { // We limit the number of threads that we use. Note that we // 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;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.