Bug 1390681 - Use NS_DISPATCH_AT_END when dispatching crypto tasks to the thread pool, so that we don't launch new threads if all threads are busy. r?bkelly
MozReview-Commit-ID: 8sgZ4LHQbac
--- a/dom/crypto/WebCryptoThreadPool.cpp
+++ b/dom/crypto/WebCryptoThreadPool.cpp
@@ -71,17 +71,24 @@ WebCryptoThreadPool::DispatchInternal(ns
NS_ENSURE_TRUE(pool, NS_ERROR_FAILURE);
nsresult rv = pool->SetName(NS_LITERAL_CSTRING("SubtleCrypto"));
NS_ENSURE_SUCCESS(rv, rv);
pool.swap(mPool);
}
- return mPool->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
+ // Use NS_DISPATCH_AT_END instead of NS_DISPATCH_NORMAL.
+ // NS_DISPATCH_NORMAL causes the thread pool to try hard to service the event
+ // immediately, and if all threads are currently busy, it will spin up a new
+ // thread. But we don't want any extra threads to be created.
+ // NS_DISPATCH_AT_END gives the behavior you'd expect of a thread pool: the
+ // event is simply queued at the end and serviced by the first available
+ // thread.
+ return mPool->Dispatch(aRunnable, NS_DISPATCH_AT_END);
}
void
WebCryptoThreadPool::Shutdown()
{
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
MutexAutoLock lock(mMutex);