2014-11-19 05:17:17 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "DecodePool.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2018-02-13 14:43:31 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2015-05-15 03:08:26 +03:00
|
|
|
#include "mozilla/Monitor.h"
|
2018-02-09 23:17:26 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2014-11-19 05:17:17 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIThreadPool.h"
|
2015-05-15 03:08:26 +03:00
|
|
|
#include "nsThreadManager.h"
|
2015-01-18 12:27:16 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2014-11-19 05:17:17 +03:00
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "prsystem.h"
|
2016-12-09 03:20:13 +03:00
|
|
|
#include "nsIXULRuntime.h"
|
2014-11-19 05:17:17 +03:00
|
|
|
|
|
|
|
#include "gfxPrefs.h"
|
|
|
|
|
|
|
|
#include "Decoder.h"
|
2016-06-29 23:43:19 +03:00
|
|
|
#include "IDecodingTask.h"
|
2014-11-19 05:17:17 +03:00
|
|
|
#include "RasterImage.h"
|
|
|
|
|
|
|
|
using std::max;
|
|
|
|
using std::min;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// DecodePool implementation.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/* static */ StaticRefPtr<DecodePool> DecodePool::sSingleton;
|
2015-07-09 01:52:51 +03:00
|
|
|
/* static */ uint32_t DecodePool::sNumCores = 0;
|
2015-05-15 03:08:26 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(DecodePool, nsIObserver)
|
|
|
|
|
|
|
|
struct Work
|
|
|
|
{
|
|
|
|
enum class Type {
|
2016-06-29 23:43:19 +03:00
|
|
|
TASK,
|
2015-05-15 03:08:26 +03:00
|
|
|
SHUTDOWN
|
|
|
|
} mType;
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
RefPtr<IDecodingTask> mTask;
|
2014-11-19 05:17:17 +03:00
|
|
|
};
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
class DecodePoolImpl
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-05-15 03:08:26 +03:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(DecodePoolImpl)
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DecodePoolImpl)
|
|
|
|
|
2018-02-13 14:43:31 +03:00
|
|
|
DecodePoolImpl(uint8_t aMaxThreads,
|
|
|
|
uint8_t aMaxIdleThreads,
|
2018-02-09 23:17:26 +03:00
|
|
|
TimeDuration aIdleTimeout)
|
2015-05-15 03:08:26 +03:00
|
|
|
: mMonitor("DecodePoolImpl")
|
2018-02-13 14:43:30 +03:00
|
|
|
, mThreads(aMaxThreads)
|
2018-02-13 14:43:31 +03:00
|
|
|
, mIdleTimeout(aIdleTimeout)
|
|
|
|
, mMaxIdleThreads(aMaxIdleThreads)
|
2018-02-13 14:43:30 +03:00
|
|
|
, mAvailableThreads(aMaxThreads)
|
|
|
|
, mIdleThreads(0)
|
2015-05-15 03:08:26 +03:00
|
|
|
, mShuttingDown(false)
|
2018-02-13 14:43:30 +03:00
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
bool success = CreateThread();
|
|
|
|
MOZ_RELEASE_ASSERT(success, "Must create first image decoder thread!");
|
|
|
|
}
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
/// Shut down the provided decode pool thread.
|
2018-02-13 14:43:31 +03:00
|
|
|
void ShutdownThread(nsIThread* aThisThread, bool aShutdownIdle)
|
2015-05-15 03:08:26 +03:00
|
|
|
{
|
2018-02-13 14:43:31 +03:00
|
|
|
{
|
|
|
|
// If this is an idle thread shutdown, then we need to remove it from the
|
|
|
|
// worker array. Process shutdown will move the entire array.
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
if (!mShuttingDown) {
|
|
|
|
++mAvailableThreads;
|
|
|
|
DebugOnly<bool> removed = mThreads.RemoveElement(aThisThread);
|
|
|
|
MOZ_ASSERT(aShutdownIdle);
|
|
|
|
MOZ_ASSERT(mAvailableThreads < mThreads.Capacity());
|
|
|
|
MOZ_ASSERT(removed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
// Threads have to be shut down from another thread, so we'll ask the
|
|
|
|
// main thread to do it for us.
|
2018-02-13 14:43:31 +03:00
|
|
|
SystemGroup::Dispatch(TaskCategory::Other,
|
|
|
|
NewRunnableMethod("DecodePoolImpl::ShutdownThread",
|
|
|
|
aThisThread, &nsIThread::Shutdown));
|
2015-05-15 03:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Requests shutdown. New work items will be dropped on the floor, and all
|
|
|
|
* decode pool threads will be shut down once existing work items have been
|
|
|
|
* processed.
|
|
|
|
*/
|
2018-02-13 14:43:30 +03:00
|
|
|
void Shutdown()
|
2015-05-15 03:08:26 +03:00
|
|
|
{
|
2018-02-13 14:43:30 +03:00
|
|
|
nsTArray<nsCOMPtr<nsIThread>> threads;
|
|
|
|
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mShuttingDown = true;
|
|
|
|
mAvailableThreads = 0;
|
|
|
|
threads.SwapElements(mThreads);
|
|
|
|
mMonitor.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0 ; i < threads.Length() ; ++i) {
|
|
|
|
threads[i]->Shutdown();
|
|
|
|
}
|
2015-05-15 03:08:26 +03:00
|
|
|
}
|
|
|
|
|
2018-03-27 17:57:01 +03:00
|
|
|
bool IsShuttingDown() const
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
return mShuttingDown;
|
|
|
|
}
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
/// Pushes a new decode work item.
|
2016-06-29 23:43:19 +03:00
|
|
|
void PushWork(IDecodingTask* aTask)
|
2015-05-15 03:08:26 +03:00
|
|
|
{
|
2016-06-29 23:43:19 +03:00
|
|
|
MOZ_ASSERT(aTask);
|
|
|
|
RefPtr<IDecodingTask> task(aTask);
|
2015-05-15 03:08:26 +03:00
|
|
|
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
if (mShuttingDown) {
|
|
|
|
// Drop any new work on the floor if we're shutting down.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
if (task->Priority() == TaskPriority::eHigh) {
|
2018-05-30 22:15:35 +03:00
|
|
|
mHighPriorityQueue.AppendElement(std::move(task));
|
2015-05-08 05:12:00 +03:00
|
|
|
} else {
|
2018-05-30 22:15:35 +03:00
|
|
|
mLowPriorityQueue.AppendElement(std::move(task));
|
2018-02-13 14:43:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 14:43:30 +03:00
|
|
|
// If there are pending tasks, create more workers if and only if we have
|
|
|
|
// not exceeded the capacity, and any previously created workers are ready.
|
|
|
|
if (mAvailableThreads) {
|
|
|
|
size_t pending = mHighPriorityQueue.Length() + mLowPriorityQueue.Length();
|
|
|
|
if (pending > mIdleThreads) {
|
|
|
|
CreateThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
mMonitor.Notify();
|
|
|
|
}
|
|
|
|
|
2018-02-13 14:43:31 +03:00
|
|
|
Work StartWork(bool aShutdownIdle)
|
2018-02-13 14:43:30 +03:00
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
// The thread was already marked as idle when it was created. Once it gets
|
|
|
|
// its first work item, it is assumed it is busy performing that work until
|
|
|
|
// it blocks on the monitor once again.
|
|
|
|
MOZ_ASSERT(mIdleThreads > 0);
|
|
|
|
--mIdleThreads;
|
2018-02-13 14:43:31 +03:00
|
|
|
return PopWorkLocked(aShutdownIdle);
|
2018-02-13 14:43:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 14:43:31 +03:00
|
|
|
Work PopWork(bool aShutdownIdle)
|
2018-02-13 14:43:30 +03:00
|
|
|
{
|
2018-02-13 20:56:26 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2018-02-13 14:43:31 +03:00
|
|
|
return PopWorkLocked(aShutdownIdle);
|
2018-02-13 14:43:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Pops a new work item, blocking if necessary.
|
2018-02-13 14:43:31 +03:00
|
|
|
Work PopWorkLocked(bool aShutdownIdle)
|
2018-02-13 14:43:30 +03:00
|
|
|
{
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2015-05-15 03:08:26 +03:00
|
|
|
|
2018-02-09 23:17:26 +03:00
|
|
|
TimeDuration timeout = mIdleTimeout;
|
2015-05-15 03:08:26 +03:00
|
|
|
do {
|
2018-02-13 20:56:26 +03:00
|
|
|
if (!mHighPriorityQueue.IsEmpty()) {
|
2016-06-29 23:43:19 +03:00
|
|
|
return PopWorkFromQueue(mHighPriorityQueue);
|
2015-05-08 05:12:00 +03:00
|
|
|
}
|
2015-05-15 03:08:26 +03:00
|
|
|
|
2018-02-13 20:56:26 +03:00
|
|
|
if (!mLowPriorityQueue.IsEmpty()) {
|
2016-06-29 23:43:19 +03:00
|
|
|
return PopWorkFromQueue(mLowPriorityQueue);
|
2015-05-15 03:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mShuttingDown) {
|
2018-02-13 14:43:31 +03:00
|
|
|
return CreateShutdownWork();
|
2015-05-15 03:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do; block until some work is available.
|
2018-02-13 14:43:31 +03:00
|
|
|
if (!aShutdownIdle) {
|
|
|
|
// This thread was created before we hit the idle thread maximum. It
|
|
|
|
// will never shutdown until the process itself is torn down.
|
|
|
|
++mIdleThreads;
|
|
|
|
MOZ_ASSERT(mIdleThreads <= mThreads.Capacity());
|
|
|
|
mMonitor.Wait();
|
|
|
|
} else {
|
|
|
|
// This thread should shutdown if it is idle. If we have waited longer
|
|
|
|
// than the timeout period without having done any work, then we should
|
|
|
|
// shutdown the thread.
|
2018-02-09 23:17:26 +03:00
|
|
|
if (timeout.IsZero()) {
|
2018-02-13 14:43:31 +03:00
|
|
|
return CreateShutdownWork();
|
|
|
|
}
|
|
|
|
|
|
|
|
++mIdleThreads;
|
|
|
|
MOZ_ASSERT(mIdleThreads <= mThreads.Capacity());
|
|
|
|
|
2018-02-09 23:17:26 +03:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
2018-02-13 14:43:31 +03:00
|
|
|
mMonitor.Wait(timeout);
|
2018-02-09 23:17:26 +03:00
|
|
|
TimeDuration delta = TimeStamp::Now() - now;
|
2018-02-13 14:43:31 +03:00
|
|
|
if (delta > timeout) {
|
|
|
|
timeout = 0;
|
2018-02-09 23:17:26 +03:00
|
|
|
} else if (timeout != TimeDuration::Forever()) {
|
2018-02-13 14:43:31 +03:00
|
|
|
timeout -= delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 14:43:30 +03:00
|
|
|
MOZ_ASSERT(mIdleThreads > 0);
|
|
|
|
--mIdleThreads;
|
2015-05-15 03:08:26 +03:00
|
|
|
} while (true);
|
|
|
|
}
|
2015-01-18 12:27:16 +03:00
|
|
|
|
2018-02-13 20:56:26 +03:00
|
|
|
~DecodePoolImpl() { }
|
2018-02-13 14:43:30 +03:00
|
|
|
|
2018-02-13 14:43:30 +03:00
|
|
|
bool CreateThread();
|
|
|
|
|
2018-02-13 20:56:26 +03:00
|
|
|
Work PopWorkFromQueue(nsTArray<RefPtr<IDecodingTask>>& aQueue)
|
2015-05-08 05:12:00 +03:00
|
|
|
{
|
|
|
|
Work work;
|
2016-06-29 23:43:19 +03:00
|
|
|
work.mType = Work::Type::TASK;
|
2018-03-13 16:51:33 +03:00
|
|
|
work.mTask = aQueue.PopLastElement();
|
2015-05-08 05:12:00 +03:00
|
|
|
|
2018-02-13 14:43:31 +03:00
|
|
|
return work;
|
|
|
|
}
|
|
|
|
|
2018-02-13 14:43:31 +03:00
|
|
|
Work CreateShutdownWork() const
|
|
|
|
{
|
|
|
|
Work work;
|
|
|
|
work.mType = Work::Type::SHUTDOWN;
|
|
|
|
return work;
|
|
|
|
}
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
nsThreadPoolNaming mThreadNaming;
|
2015-01-18 12:27:16 +03:00
|
|
|
|
2018-02-13 14:43:30 +03:00
|
|
|
// mMonitor guards everything below.
|
2018-03-27 17:57:01 +03:00
|
|
|
mutable Monitor mMonitor;
|
2018-02-13 20:56:26 +03:00
|
|
|
nsTArray<RefPtr<IDecodingTask>> mHighPriorityQueue;
|
|
|
|
nsTArray<RefPtr<IDecodingTask>> mLowPriorityQueue;
|
2018-02-13 14:43:30 +03:00
|
|
|
nsTArray<nsCOMPtr<nsIThread>> mThreads;
|
2018-02-09 23:17:26 +03:00
|
|
|
TimeDuration mIdleTimeout;
|
2018-02-13 14:43:31 +03:00
|
|
|
uint8_t mMaxIdleThreads; // Maximum number of workers when idle.
|
2018-02-13 14:43:30 +03:00
|
|
|
uint8_t mAvailableThreads; // How many new threads can be created.
|
|
|
|
uint8_t mIdleThreads; // How many created threads are waiting.
|
2015-05-15 03:08:26 +03:00
|
|
|
bool mShuttingDown;
|
|
|
|
};
|
|
|
|
|
2018-02-13 14:43:30 +03:00
|
|
|
class DecodePoolWorker final : public Runnable
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
2015-01-18 12:27:16 +03:00
|
|
|
public:
|
2018-02-13 14:43:31 +03:00
|
|
|
explicit DecodePoolWorker(DecodePoolImpl* aImpl,
|
|
|
|
bool aShutdownIdle)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("image::DecodePoolWorker")
|
|
|
|
, mImpl(aImpl)
|
2018-02-13 14:43:31 +03:00
|
|
|
, mShutdownIdle(aShutdownIdle)
|
2016-11-25 10:00:10 +03:00
|
|
|
{ }
|
2015-05-15 03:08:26 +03:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2015-01-18 12:27:16 +03:00
|
|
|
{
|
2015-05-15 03:08:26 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
nsCOMPtr<nsIThread> thisThread;
|
2016-06-10 09:04:49 +03:00
|
|
|
nsThreadManager::get().GetCurrentThread(getter_AddRefs(thisThread));
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2018-02-13 14:43:31 +03:00
|
|
|
Work work = mImpl->StartWork(mShutdownIdle);
|
2015-05-15 03:08:26 +03:00
|
|
|
do {
|
|
|
|
switch (work.mType) {
|
2016-06-29 23:43:19 +03:00
|
|
|
case Work::Type::TASK:
|
|
|
|
work.mTask->Run();
|
2018-02-13 14:43:30 +03:00
|
|
|
work.mTask = nullptr;
|
2015-05-15 03:08:26 +03:00
|
|
|
break;
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
case Work::Type::SHUTDOWN:
|
2018-02-13 14:43:31 +03:00
|
|
|
mImpl->ShutdownThread(thisThread, mShutdownIdle);
|
2017-10-04 01:11:18 +03:00
|
|
|
PROFILER_UNREGISTER_THREAD();
|
2015-05-15 03:08:26 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown work type");
|
|
|
|
}
|
2018-02-13 14:43:30 +03:00
|
|
|
|
2018-02-13 14:43:31 +03:00
|
|
|
work = mImpl->PopWork(mShutdownIdle);
|
2015-05-15 03:08:26 +03:00
|
|
|
} while (true);
|
|
|
|
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Exiting thread without Work::Type::SHUTDOWN");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DecodePoolImpl> mImpl;
|
2018-02-13 14:43:31 +03:00
|
|
|
bool mShutdownIdle;
|
2015-05-15 03:08:26 +03:00
|
|
|
};
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2018-02-13 14:43:30 +03:00
|
|
|
bool DecodePoolImpl::CreateThread()
|
|
|
|
{
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mAvailableThreads > 0);
|
|
|
|
|
2018-02-13 14:43:31 +03:00
|
|
|
bool shutdownIdle = mThreads.Length() >= mMaxIdleThreads;
|
|
|
|
nsCOMPtr<nsIRunnable> worker = new DecodePoolWorker(this, shutdownIdle);
|
2018-02-13 14:43:30 +03:00
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsresult rv = NS_NewNamedThread(mThreadNaming.GetNextThreadName("ImgDecoder"),
|
2018-03-08 04:33:17 +03:00
|
|
|
getter_AddRefs(thread), worker,
|
|
|
|
nsIThreadManager::kThreadPoolStackSize);
|
2018-02-13 14:43:30 +03:00
|
|
|
if (NS_FAILED(rv) || !thread) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Should successfully create image decoding threads");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mThreads.AppendElement(std::move(thread));
|
2018-02-13 14:43:30 +03:00
|
|
|
--mAvailableThreads;
|
|
|
|
++mIdleThreads;
|
|
|
|
MOZ_ASSERT(mIdleThreads <= mThreads.Capacity());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-18 12:27:16 +03:00
|
|
|
/* static */ void
|
|
|
|
DecodePool::Initialize()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-11-01 20:45:40 +03:00
|
|
|
sNumCores = max<int32_t>(PR_GetNumberOfProcessors(), 1);
|
2015-01-18 12:27:16 +03:00
|
|
|
DecodePool::Singleton();
|
|
|
|
}
|
|
|
|
|
2014-11-19 05:17:17 +03:00
|
|
|
/* static */ DecodePool*
|
|
|
|
DecodePool::Singleton()
|
|
|
|
{
|
|
|
|
if (!sSingleton) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
sSingleton = new DecodePool();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
2015-07-09 01:52:51 +03:00
|
|
|
/* static */ uint32_t
|
|
|
|
DecodePool::NumberOfCores()
|
|
|
|
{
|
|
|
|
return sNumCores;
|
|
|
|
}
|
|
|
|
|
2014-11-19 05:17:17 +03:00
|
|
|
DecodePool::DecodePool()
|
2018-02-13 14:43:30 +03:00
|
|
|
: mMutex("image::DecodePool")
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
2015-05-15 03:08:26 +03:00
|
|
|
// Determine the number of threads we want.
|
2015-01-08 11:29:41 +03:00
|
|
|
int32_t prefLimit = gfxPrefs::ImageMTDecodingLimit();
|
|
|
|
uint32_t limit;
|
|
|
|
if (prefLimit <= 0) {
|
2015-07-09 01:52:51 +03:00
|
|
|
int32_t numCores = NumberOfCores();
|
2015-05-21 04:54:16 +03:00
|
|
|
if (numCores <= 1) {
|
|
|
|
limit = 1;
|
|
|
|
} else if (numCores == 2) {
|
|
|
|
// On an otherwise mostly idle system, having two image decoding threads
|
|
|
|
// doubles decoding performance, so it's worth doing on dual-core devices,
|
|
|
|
// even if under load we can't actually get that level of parallelism.
|
|
|
|
limit = 2;
|
|
|
|
} else {
|
|
|
|
limit = numCores - 1;
|
|
|
|
}
|
2015-01-08 11:29:41 +03:00
|
|
|
} else {
|
|
|
|
limit = static_cast<uint32_t>(prefLimit);
|
|
|
|
}
|
2015-11-01 20:45:40 +03:00
|
|
|
if (limit > 32) {
|
|
|
|
limit = 32;
|
|
|
|
}
|
2016-12-09 03:20:13 +03:00
|
|
|
// The parent process where there are content processes doesn't need as many
|
|
|
|
// threads for decoding images.
|
2017-04-24 12:40:12 +03:00
|
|
|
if (limit > 4 && XRE_IsE10sParentProcess()) {
|
2016-12-09 03:20:13 +03:00
|
|
|
limit = 4;
|
|
|
|
}
|
2015-01-08 11:29:41 +03:00
|
|
|
|
2018-02-13 14:43:31 +03:00
|
|
|
// The maximum number of idle threads allowed.
|
|
|
|
uint32_t idleLimit;
|
|
|
|
|
|
|
|
// The timeout period before shutting down idle threads.
|
|
|
|
int32_t prefIdleTimeout = gfxPrefs::ImageMTDecodingIdleTimeout();
|
2018-02-09 23:17:26 +03:00
|
|
|
TimeDuration idleTimeout;
|
2018-02-13 14:43:31 +03:00
|
|
|
if (prefIdleTimeout <= 0) {
|
2018-02-09 23:17:26 +03:00
|
|
|
idleTimeout = TimeDuration::Forever();
|
2018-02-13 14:43:31 +03:00
|
|
|
idleLimit = limit;
|
|
|
|
} else {
|
2018-02-09 23:17:26 +03:00
|
|
|
idleTimeout = TimeDuration::FromMilliseconds(prefIdleTimeout);
|
2018-02-13 14:43:31 +03:00
|
|
|
idleLimit = (limit + 1) / 2;
|
|
|
|
}
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
// Initialize the thread pool.
|
2018-02-13 14:43:31 +03:00
|
|
|
mImpl = new DecodePoolImpl(limit, idleLimit, idleTimeout);
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-01-18 12:27:16 +03:00
|
|
|
// Initialize the I/O thread.
|
|
|
|
nsresult rv = NS_NewNamedThread("ImageIO", getter_AddRefs(mIOThread));
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv) && mIOThread,
|
|
|
|
"Should successfully create image I/O thread");
|
|
|
|
|
2015-01-08 11:29:41 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obsSvc = services::GetObserverService();
|
|
|
|
if (obsSvc) {
|
|
|
|
obsSvc->AddObserver(this, "xpcom-shutdown-threads", false);
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DecodePool::~DecodePool()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Must shut down DecodePool on main thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DecodePool::Observe(nsISupports*, const char* aTopic, const char16_t*)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(strcmp(aTopic, "xpcom-shutdown-threads") == 0, "Unexpected topic");
|
|
|
|
|
2015-01-18 12:27:16 +03:00
|
|
|
nsCOMPtr<nsIThread> ioThread;
|
2014-11-19 05:17:17 +03:00
|
|
|
|
|
|
|
{
|
2015-01-18 12:27:16 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
ioThread.swap(mIOThread);
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 14:43:30 +03:00
|
|
|
mImpl->Shutdown();
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-01-18 12:27:16 +03:00
|
|
|
if (ioThread) {
|
|
|
|
ioThread->Shutdown();
|
|
|
|
}
|
|
|
|
|
2014-11-19 05:17:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:57:01 +03:00
|
|
|
bool
|
|
|
|
DecodePool::IsShuttingDown() const
|
|
|
|
{
|
|
|
|
return mImpl->IsShuttingDown();
|
|
|
|
}
|
|
|
|
|
2014-11-19 05:17:17 +03:00
|
|
|
void
|
2016-06-29 23:43:19 +03:00
|
|
|
DecodePool::AsyncRun(IDecodingTask* aTask)
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
2016-06-29 23:43:19 +03:00
|
|
|
MOZ_ASSERT(aTask);
|
|
|
|
mImpl->PushWork(aTask);
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
|
2016-12-22 22:15:41 +03:00
|
|
|
bool
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
DecodePool::SyncRunIfPreferred(IDecodingTask* aTask, const nsCString& aURI)
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-06-29 23:43:19 +03:00
|
|
|
MOZ_ASSERT(aTask);
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2017-10-13 08:12:57 +03:00
|
|
|
AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING(
|
|
|
|
"DecodePool::SyncRunIfPreferred", GRAPHICS, aURI);
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
if (aTask->ShouldPreferSyncRun()) {
|
|
|
|
aTask->Run();
|
2016-12-22 22:15:41 +03:00
|
|
|
return true;
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
AsyncRun(aTask);
|
2016-12-22 22:15:41 +03:00
|
|
|
return false;
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:11:36 +03:00
|
|
|
void
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
DecodePool::SyncRunIfPossible(IDecodingTask* aTask, const nsCString& aURI)
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
2015-01-16 02:11:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-06-29 23:43:19 +03:00
|
|
|
MOZ_ASSERT(aTask);
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
|
2017-10-13 08:12:57 +03:00
|
|
|
AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING(
|
|
|
|
"DecodePool::SyncRunIfPossible", GRAPHICS, aURI);
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
aTask->Run();
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
|
2015-01-18 12:27:16 +03:00
|
|
|
already_AddRefed<nsIEventTarget>
|
|
|
|
DecodePool::GetIOEventTarget()
|
|
|
|
{
|
|
|
|
MutexAutoLock threadPoolLock(mMutex);
|
2018-10-02 00:38:01 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> target = mIOThread;
|
2015-01-18 12:27:16 +03:00
|
|
|
return target.forget();
|
|
|
|
}
|
|
|
|
|
2014-11-19 05:17:17 +03:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|