2021-04-30 20:12:10 +02:00
|
|
|
// Copyright (C) 2020-2021 Jakub Melka
|
2020-01-18 14:55:26 +01:00
|
|
|
//
|
2021-08-10 19:22:56 +02:00
|
|
|
// This file is part of PDF4QT.
|
2020-01-18 14:55:26 +01:00
|
|
|
//
|
2021-08-10 19:22:56 +02:00
|
|
|
// PDF4QT is free software: you can redistribute it and/or modify
|
2020-01-18 14:55:26 +01:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
2021-04-30 20:12:10 +02:00
|
|
|
// with the written consent of the copyright owner, any later version.
|
2020-01-18 14:55:26 +01:00
|
|
|
//
|
2021-08-10 19:22:56 +02:00
|
|
|
// PDF4QT is distributed in the hope that it will be useful,
|
2020-01-18 14:55:26 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2021-08-10 19:22:56 +02:00
|
|
|
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
|
2020-01-18 14:55:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
#include "pdfexecutionpolicy.h"
|
|
|
|
|
|
|
|
#include <QThread>
|
2020-11-06 17:30:24 +01:00
|
|
|
#include <QApplication>
|
2020-01-18 14:55:26 +01:00
|
|
|
|
|
|
|
namespace pdf
|
|
|
|
{
|
|
|
|
|
|
|
|
struct PDFExecutionPolicyHolder
|
|
|
|
{
|
2020-11-06 17:30:24 +01:00
|
|
|
PDFExecutionPolicyHolder()
|
|
|
|
{
|
|
|
|
qAddPostRoutine(&PDFExecutionPolicy::finalize);
|
|
|
|
}
|
|
|
|
~PDFExecutionPolicyHolder()
|
|
|
|
{
|
|
|
|
auxiliary.waitForDone();
|
|
|
|
primary.waitForDone();
|
|
|
|
}
|
|
|
|
|
2020-01-18 14:55:26 +01:00
|
|
|
PDFExecutionPolicy policy;
|
2020-11-06 17:30:24 +01:00
|
|
|
QThreadPool primary;
|
|
|
|
QThreadPool auxiliary;
|
2020-01-18 14:55:26 +01:00
|
|
|
} s_execution_policy;
|
|
|
|
|
|
|
|
void PDFExecutionPolicy::setStrategy(Strategy strategy)
|
|
|
|
{
|
|
|
|
s_execution_policy.policy.m_strategy.store(strategy, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PDFExecutionPolicy::isParallelizing(Scope scope)
|
|
|
|
{
|
|
|
|
const Strategy strategy = s_execution_policy.policy.m_strategy.load(std::memory_order_relaxed);
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case Strategy::SingleThreaded:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Strategy::PageMultithreaded:
|
|
|
|
{
|
|
|
|
switch (scope)
|
|
|
|
{
|
|
|
|
case Scope::Page:
|
|
|
|
case Scope::Unknown:
|
|
|
|
return true; // We are parallelizing pages...
|
|
|
|
|
|
|
|
case Scope::Content:
|
2020-11-06 17:30:24 +01:00
|
|
|
return false;
|
2020-01-18 14:55:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Strategy::AlwaysMultithreaded:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It should never go here...
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:30:24 +01:00
|
|
|
int PDFExecutionPolicy::getActiveThreadCount(Scope scope)
|
|
|
|
{
|
|
|
|
return getThreadPool(scope)->activeThreadCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
int PDFExecutionPolicy::getMaxThreadCount(Scope scope)
|
|
|
|
{
|
|
|
|
return getThreadPool(scope)->maxThreadCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFExecutionPolicy::setMaxThreadCount(Scope scope, int count)
|
|
|
|
{
|
|
|
|
// Sanitize value!
|
|
|
|
count = qMax(count, 1);
|
|
|
|
getThreadPool(scope)->setMaxThreadCount(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PDFExecutionPolicy::getIdealThreadCount(Scope scope)
|
|
|
|
{
|
|
|
|
Q_UNUSED(scope);
|
|
|
|
return QThread::idealThreadCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
int PDFExecutionPolicy::getContentStreamCount()
|
|
|
|
{
|
|
|
|
return s_execution_policy.policy.m_contentStreamsCount.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2020-01-18 14:55:26 +01:00
|
|
|
void PDFExecutionPolicy::startProcessingContentStream()
|
|
|
|
{
|
|
|
|
++s_execution_policy.policy.m_contentStreamsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFExecutionPolicy::endProcessingContentStream()
|
|
|
|
{
|
|
|
|
--s_execution_policy.policy.m_contentStreamsCount;
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:30:24 +01:00
|
|
|
void PDFExecutionPolicy::finalize()
|
|
|
|
{
|
|
|
|
s_execution_policy.auxiliary.waitForDone();
|
|
|
|
s_execution_policy.primary.waitForDone();
|
|
|
|
}
|
|
|
|
|
|
|
|
QThreadPool* PDFExecutionPolicy::getThreadPool(PDFExecutionPolicy::Scope scope)
|
|
|
|
{
|
|
|
|
switch (scope)
|
|
|
|
{
|
|
|
|
case Scope::Page:
|
|
|
|
case Scope::Unknown:
|
|
|
|
return &s_execution_policy.primary;
|
|
|
|
|
|
|
|
case Scope::Content:
|
|
|
|
return &s_execution_policy.auxiliary;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-18 14:55:26 +01:00
|
|
|
PDFExecutionPolicy::PDFExecutionPolicy() :
|
|
|
|
m_contentStreamsCount(0),
|
|
|
|
m_strategy(Strategy::PageMultithreaded)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pdf
|