2016-05-31 16:07:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangiasyncjob.h"
|
|
|
|
|
#include "clangjobqueue.h"
|
2016-09-07 10:42:12 +02:00
|
|
|
#include "clangdocument.h"
|
|
|
|
|
#include "clangdocuments.h"
|
2016-09-13 13:57:08 +02:00
|
|
|
#include "clangtranslationunits.h"
|
2016-05-31 16:07:09 +02:00
|
|
|
#include "projects.h"
|
|
|
|
|
#include "unsavedfiles.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
|
|
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
2016-09-07 10:42:12 +02:00
|
|
|
JobQueue::JobQueue(Documents &documents, ProjectParts &projectParts)
|
|
|
|
|
: m_documents(documents)
|
2016-05-31 16:07:09 +02:00
|
|
|
, m_projectParts(projectParts)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 12:54:22 +02:00
|
|
|
bool JobQueue::add(const JobRequest &job)
|
2016-05-31 16:07:09 +02:00
|
|
|
{
|
2016-10-06 12:54:22 +02:00
|
|
|
if (m_queue.contains(job)) {
|
|
|
|
|
qCDebug(jobsLog) << "Not adding duplicate request" << job;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isJobRunningForJobRequest(job)) {
|
|
|
|
|
qCDebug(jobsLog) << "Not adding duplicate request for already running job" << job;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-05-31 16:07:09 +02:00
|
|
|
|
2016-10-06 12:54:22 +02:00
|
|
|
qCDebug(jobsLog) << "Adding" << job;
|
2016-05-31 16:07:09 +02:00
|
|
|
m_queue.append(job);
|
2016-10-06 12:54:22 +02:00
|
|
|
|
|
|
|
|
return true;
|
2016-05-31 16:07:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int JobQueue::size() const
|
|
|
|
|
{
|
|
|
|
|
return m_queue.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JobRequests JobQueue::processQueue()
|
|
|
|
|
{
|
2017-06-01 16:18:58 +02:00
|
|
|
removeExpiredRequests();
|
2016-05-31 16:07:09 +02:00
|
|
|
prioritizeRequests();
|
|
|
|
|
const JobRequests jobsToRun = takeJobRequestsToRunNow();
|
|
|
|
|
|
|
|
|
|
return jobsToRun;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 16:18:58 +02:00
|
|
|
void JobQueue::removeExpiredRequests()
|
2016-05-31 16:07:09 +02:00
|
|
|
{
|
|
|
|
|
JobRequests cleanedRequests;
|
|
|
|
|
|
|
|
|
|
foreach (const JobRequest &jobRequest, m_queue) {
|
|
|
|
|
try {
|
2017-06-01 16:18:58 +02:00
|
|
|
if (!isJobRequestExpired(jobRequest))
|
2016-05-31 16:07:09 +02:00
|
|
|
cleanedRequests.append(jobRequest);
|
|
|
|
|
} catch (const std::exception &exception) {
|
|
|
|
|
qWarning() << "Error in Jobs::removeOutDatedRequests for"
|
|
|
|
|
<< jobRequest << ":" << exception.what();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_queue = cleanedRequests;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 16:18:58 +02:00
|
|
|
bool JobQueue::isJobRequestExpired(const JobRequest &jobRequest) const
|
2016-05-31 16:07:09 +02:00
|
|
|
{
|
2017-06-01 16:18:58 +02:00
|
|
|
const JobRequest::ExpirationReasons expirationReasons = jobRequest.expirationReasons;
|
2016-09-07 10:42:12 +02:00
|
|
|
const UnsavedFiles unsavedFiles = m_documents.unsavedFiles();
|
2017-07-28 14:51:03 +02:00
|
|
|
using ExpirationReason = JobRequest::ExpirationReason;
|
2016-05-31 16:07:09 +02:00
|
|
|
|
2017-07-28 14:51:03 +02:00
|
|
|
if (expirationReasons.testFlag(ExpirationReason::UnsavedFilesChanged)) {
|
2016-05-31 16:07:09 +02:00
|
|
|
if (jobRequest.unsavedFilesChangeTimePoint != unsavedFiles.lastChangeTimePoint()) {
|
|
|
|
|
qCDebug(jobsLog) << "Removing due to outdated unsaved files:" << jobRequest;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool projectCheckedAndItExists = false;
|
|
|
|
|
|
2017-07-28 14:51:03 +02:00
|
|
|
if (expirationReasons.testFlag(ExpirationReason::DocumentClosed)) {
|
2016-09-07 10:42:12 +02:00
|
|
|
if (!m_documents.hasDocument(jobRequest.filePath, jobRequest.projectPartId)) {
|
2016-05-31 16:07:09 +02:00
|
|
|
qCDebug(jobsLog) << "Removing due to already closed document:" << jobRequest;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_projectParts.hasProjectPart(jobRequest.projectPartId)) {
|
|
|
|
|
qCDebug(jobsLog) << "Removing due to already closed project:" << jobRequest;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
projectCheckedAndItExists = true;
|
|
|
|
|
|
2016-09-07 10:42:12 +02:00
|
|
|
const Document document
|
|
|
|
|
= m_documents.document(jobRequest.filePath, jobRequest.projectPartId);
|
|
|
|
|
if (!document.isIntact()) {
|
2016-05-31 16:07:09 +02:00
|
|
|
qCDebug(jobsLog) << "Removing due to not intact translation unit:" << jobRequest;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 14:51:03 +02:00
|
|
|
if (expirationReasons.testFlag(ExpirationReason::DocumentRevisionChanged)) {
|
2017-06-07 14:44:32 +02:00
|
|
|
if (document.documentRevision() > jobRequest.documentRevision) {
|
2016-05-31 16:07:09 +02:00
|
|
|
qCDebug(jobsLog) << "Removing due to changed document revision:" << jobRequest;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 14:51:03 +02:00
|
|
|
if (expirationReasons.testFlag(ExpirationReason::ProjectChanged)) {
|
2016-05-31 16:07:09 +02:00
|
|
|
if (!projectCheckedAndItExists && !m_projectParts.hasProjectPart(jobRequest.projectPartId)) {
|
|
|
|
|
qCDebug(jobsLog) << "Removing due to already closed project:" << jobRequest;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ProjectPart &project = m_projectParts.project(jobRequest.projectPartId);
|
|
|
|
|
if (project.lastChangeTimePoint() != jobRequest.projectChangeTimePoint) {
|
|
|
|
|
qCDebug(jobsLog) << "Removing due to outdated project:" << jobRequest;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 10:42:12 +02:00
|
|
|
static int priority(const Document &document)
|
2016-05-31 16:07:09 +02:00
|
|
|
{
|
|
|
|
|
int thePriority = 0;
|
|
|
|
|
|
2016-09-07 10:42:12 +02:00
|
|
|
if (document.isUsedByCurrentEditor())
|
2016-05-31 16:07:09 +02:00
|
|
|
thePriority += 1000;
|
|
|
|
|
|
2016-09-07 10:42:12 +02:00
|
|
|
if (document.isVisibleInEditor())
|
2016-05-31 16:07:09 +02:00
|
|
|
thePriority += 100;
|
|
|
|
|
|
|
|
|
|
return thePriority;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JobQueue::prioritizeRequests()
|
|
|
|
|
{
|
|
|
|
|
const auto lessThan = [this] (const JobRequest &r1, const JobRequest &r2) {
|
|
|
|
|
// TODO: Getting the TU is O(n) currently, so this might become expensive for large n.
|
2016-09-07 10:42:12 +02:00
|
|
|
const Document &t1 = m_documents.document(r1.filePath, r1.projectPartId);
|
|
|
|
|
const Document &t2 = m_documents.document(r2.filePath, r2.projectPartId);
|
2016-05-31 16:07:09 +02:00
|
|
|
|
|
|
|
|
return priority(t1) > priority(t2);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::stable_sort(m_queue.begin(), m_queue.end(), lessThan);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 15:13:23 +02:00
|
|
|
static bool passesPreconditions(const JobRequest &request, const Document &document)
|
|
|
|
|
{
|
|
|
|
|
using Condition = JobRequest::Condition;
|
|
|
|
|
const JobRequest::Conditions conditions = request.conditions;
|
|
|
|
|
|
2017-07-28 15:15:46 +02:00
|
|
|
if (conditions.testFlag(Condition::DocumentSuspended) && !document.isSuspended()) {
|
|
|
|
|
qCDebug(jobsLog) << "Not choosing due to unsuspended document:" << request;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (conditions.testFlag(Condition::DocumentUnsuspended) && document.isSuspended()) {
|
|
|
|
|
qCDebug(jobsLog) << "Not choosing due to suspended document:" << request;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 15:13:23 +02:00
|
|
|
if (conditions.testFlag(Condition::DocumentVisible) && !document.isVisibleInEditor()) {
|
|
|
|
|
qCDebug(jobsLog) << "Not choosing due to invisble document:" << request;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (conditions.testFlag(Condition::DocumentNotVisible) && document.isVisibleInEditor()) {
|
|
|
|
|
qCDebug(jobsLog) << "Not choosing due to visble document:" << request;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (conditions.testFlag(Condition::CurrentDocumentRevision)) {
|
|
|
|
|
if (document.isDirty()) {
|
|
|
|
|
// TODO: If the document is dirty due to a project update,
|
|
|
|
|
// references are processes later than ideal.
|
|
|
|
|
qCDebug(jobsLog) << "Not choosing due to dirty document:" << request;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request.documentRevision != document.documentRevision()) {
|
|
|
|
|
qCDebug(jobsLog) << "Not choosing due to revision mismatch:" << request;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 16:07:09 +02:00
|
|
|
JobRequests JobQueue::takeJobRequestsToRunNow()
|
|
|
|
|
{
|
|
|
|
|
JobRequests jobsToRun;
|
2016-09-13 13:57:08 +02:00
|
|
|
using TranslationUnitIds = QSet<Utf8String>;
|
|
|
|
|
TranslationUnitIds translationUnitsScheduledForThisRun;
|
2016-05-31 16:07:09 +02:00
|
|
|
|
|
|
|
|
QMutableVectorIterator<JobRequest> i(m_queue);
|
|
|
|
|
while (i.hasNext()) {
|
2016-09-13 13:57:08 +02:00
|
|
|
const JobRequest &request = i.next();
|
2016-05-31 16:07:09 +02:00
|
|
|
|
|
|
|
|
try {
|
2016-09-13 13:57:08 +02:00
|
|
|
const Document &document = m_documents.document(request.filePath,
|
|
|
|
|
request.projectPartId);
|
2016-05-31 16:07:09 +02:00
|
|
|
|
2017-07-28 15:13:23 +02:00
|
|
|
if (!passesPreconditions(request, document))
|
2016-05-31 16:07:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
2016-09-13 13:57:08 +02:00
|
|
|
const Utf8String id = document.translationUnit(request.preferredTranslationUnit).id();
|
|
|
|
|
if (translationUnitsScheduledForThisRun.contains(id))
|
2016-05-31 16:07:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
2016-09-13 13:57:08 +02:00
|
|
|
if (isJobRunningForTranslationUnit(id))
|
2016-05-31 16:07:09 +02:00
|
|
|
continue;
|
|
|
|
|
|
2016-09-13 13:57:08 +02:00
|
|
|
translationUnitsScheduledForThisRun.insert(id);
|
|
|
|
|
jobsToRun += request;
|
2016-05-31 16:07:09 +02:00
|
|
|
i.remove();
|
|
|
|
|
} catch (const std::exception &exception) {
|
|
|
|
|
qWarning() << "Error in Jobs::takeJobRequestsToRunNow for"
|
2016-09-13 13:57:08 +02:00
|
|
|
<< request << ":" << exception.what();
|
2016-05-31 16:07:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return jobsToRun;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-13 13:57:08 +02:00
|
|
|
bool JobQueue::isJobRunningForTranslationUnit(const Utf8String &translationUnitId)
|
2016-05-31 16:07:09 +02:00
|
|
|
{
|
2016-10-06 12:54:22 +02:00
|
|
|
if (m_isJobRunningForTranslationUnitHandler)
|
|
|
|
|
return m_isJobRunningForTranslationUnitHandler(translationUnitId);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool JobQueue::isJobRunningForJobRequest(const JobRequest &jobRequest)
|
|
|
|
|
{
|
|
|
|
|
if (m_isJobRunningForJobRequestHandler)
|
|
|
|
|
return m_isJobRunningForJobRequestHandler(jobRequest);
|
2016-05-31 16:07:09 +02:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 12:54:22 +02:00
|
|
|
void JobQueue::setIsJobRunningForTranslationUnitHandler(
|
|
|
|
|
const IsJobRunningForTranslationUnitHandler &isJobRunningHandler)
|
|
|
|
|
{
|
|
|
|
|
m_isJobRunningForTranslationUnitHandler = isJobRunningHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JobQueue::setIsJobRunningForJobRequestHandler(
|
|
|
|
|
const JobQueue::IsJobRunningForJobRequestHandler &isJobRunningHandler)
|
2016-05-31 16:07:09 +02:00
|
|
|
{
|
2016-10-06 12:54:22 +02:00
|
|
|
m_isJobRunningForJobRequestHandler = isJobRunningHandler;
|
2016-05-31 16:07:09 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-09 12:19:09 +02:00
|
|
|
JobRequests &JobQueue::queue()
|
2016-05-31 16:07:09 +02:00
|
|
|
{
|
|
|
|
|
return m_queue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangBackEnd
|