1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
|
/*
SPDX-FileCopyrightText: 2006 Adam Treat <treat@kde.org>
SPDX-FileCopyrightText: 2007 Kris Wong <kris.p.wong@gmail.com>
SPDX-FileCopyrightText: 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "backgroundparser.h"
#include <QCoreApplication>
#include <QList>
#include <QMutex>
#include <QMutexLocker>
#include <QPointer>
#include <QRecursiveMutex>
#include <QTimer>
#include <QThread>
#include <KConfigGroup>
#include <KSharedConfig>
#include <KLocalizedString>
#include <ThreadWeaver/State>
#include <ThreadWeaver/ThreadWeaver>
#include <ThreadWeaver/DebuggingAids>
#include <interfaces/icore.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/ilanguagecontroller.h>
#include <interfaces/ilanguagesupport.h>
#include <interfaces/isession.h>
#include <interfaces/iproject.h>
#include <interfaces/iprojectcontroller.h>
#include <debug.h>
#include "parsejob.h"
using namespace KDevelop;
namespace {
const bool separateThreadForHighPriority = true;
/**
* Elides string in @p path, e.g. "VEEERY/LONG/PATH" -> ".../LONG/PATH"
* - probably much faster than QFontMetrics::elidedText()
* - we do not need a widget context
* - takes path separators into account
*
* @p width Maximum number of characters
*
* TODO: Move to kdevutil?
*/
QString elidedPathLeft(const QString& path, int width)
{
const QLatin1String placeholder("...");
if (path.size() <= width) {
return path;
}
int start = (path.size() - width) + placeholder.size();
int pos = path.indexOf(QDir::separator(), start);
if (pos == -1) {
pos = start; // no separator => just cut off the path at the beginning
}
Q_ASSERT(path.size() - pos >= 0 && path.size() - pos <= width);
QStringRef elidedText = path.rightRef(path.size() - pos);
const QString result = placeholder + elidedText;
return result;
}
/**
* @return true if @p url is non-empty, valid and has a clean path, false otherwise.
*/
inline bool isValidURL(const IndexedString& url)
{
if (url.isEmpty()) {
return false;
}
QUrl original = url.toUrl();
if (!original.isValid() || original.isRelative() || (original.fileName().isEmpty() && original.isLocalFile())) {
qCWarning(LANGUAGE) << "INVALID URL ENCOUNTERED:" << url << original;
return false;
}
QUrl cleaned = original.adjusted(QUrl::NormalizePathSegments);
return original == cleaned;
}
}
struct DocumentParseTarget
{
QPointer<QObject> notifyWhenReady;
int priority;
TopDUContext::Features features;
ParseJob::SequentialProcessingFlags sequentialProcessingFlags;
bool operator==(const DocumentParseTarget& rhs) const
{
return notifyWhenReady == rhs.notifyWhenReady
&& priority == rhs.priority
&& features == rhs.features;
}
};
inline uint qHash(const DocumentParseTarget& target)
{
return target.features * 7 + target.priority * 13 + target.sequentialProcessingFlags * 17
+ static_cast<uint>(reinterpret_cast<quintptr>(target.notifyWhenReady.data()));
}
class DocumentParsePlan
{
public:
ParseJob::SequentialProcessingFlags sequentialProcessingFlags() const
{
//Pick the strictest possible flags
ParseJob::SequentialProcessingFlags ret = ParseJob::IgnoresSequentialProcessing;
for (const DocumentParseTarget& target : m_targets) {
ret |= target.sequentialProcessingFlags;
}
return ret;
}
int priority() const { return m_priority; }
TopDUContext::Features features() const
{
//Pick the best features
TopDUContext::Features ret{};
for (const DocumentParseTarget& target : m_targets) {
ret |= target.features;
}
return ret;
}
QVector<QPointer<QObject>> notifyWhenReady() const
{
QVector<QPointer<QObject>> ret;
for (const DocumentParseTarget& target : m_targets) {
if (target.notifyWhenReady)
ret << target.notifyWhenReady;
}
return ret;
}
const QSet<const DocumentParseTarget>& targets() const
{
return m_targets;
}
void addTarget(const DocumentParseTarget& target)
{
if (target.priority < m_priority) {
m_priority = target.priority;
}
m_targets.insert(target);
}
void removeTargetsForListener(QObject* notifyWhenReady)
{
m_priority = BackgroundParser::WorstPriority;
for (auto it = m_targets.cbegin(); it != m_targets.cend();) {
if (it->notifyWhenReady.data() == notifyWhenReady) {
it = m_targets.erase(it);
} else {
if (it->priority < m_priority) {
m_priority = it->priority;
}
++it;
}
}
}
private:
QSet<const DocumentParseTarget> m_targets;
int m_priority = BackgroundParser::WorstPriority;
};
Q_DECLARE_TYPEINFO(DocumentParseTarget, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(DocumentParsePlan, Q_MOVABLE_TYPE);
class KDevelop::BackgroundParserPrivate
{
public:
enum class AddBehavior
{
AddIfMissing,
OnlyUpdateExisting,
};
BackgroundParserPrivate(BackgroundParser* parser, ILanguageController* languageController)
: m_parser(parser)
, m_languageController(languageController)
, m_shuttingDown(false)
{
parser->d_ptr = this; //Set this so we can safely call back BackgroundParser from within loadSettings()
m_timer.setSingleShot(true);
m_progressTimer.setSingleShot(true);
m_progressTimer.setInterval(500);
ThreadWeaver::setDebugLevel(true, 1);
QObject::connect(&m_timer, &QTimer::timeout, m_parser, &BackgroundParser::parseDocuments);
QObject::connect(&m_progressTimer, &QTimer::timeout, m_parser, &BackgroundParser::updateProgressBar);
}
void startTimerThreadSafe(int delay)
{
QMetaObject::invokeMethod(m_parser, "startTimer", Qt::QueuedConnection, Q_ARG(int, delay));
}
~BackgroundParserPrivate()
{
m_weaver.resume();
m_weaver.finish();
}
// Non-mutex guarded functions, only call with m_mutex acquired.
int currentBestRunningPriority() const
{
int bestRunningPriority = BackgroundParser::WorstPriority;
for (const auto* decorator : m_parseJobs) {
const auto* parseJob = dynamic_cast<const ParseJob*>(decorator->job());
Q_ASSERT(parseJob);
if (parseJob->respectsSequentialProcessing() && parseJob->parsePriority() < bestRunningPriority) {
bestRunningPriority = parseJob->parsePriority();
}
}
return bestRunningPriority;
}
IndexedString nextDocumentToParse() const
{
// Before starting a new job, first wait for all higher-priority ones to finish.
// That way, parse job priorities can be used for dependency handling.
const int bestRunningPriority = currentBestRunningPriority();
for (auto it1 = m_documentsForPriority.begin();
it1 != m_documentsForPriority.end(); ++it1) {
const auto priority = it1.key();
if (priority > m_neededPriority)
break; //The priority is not good enough to be processed right now
if (m_parseJobs.count() >= m_threads && priority > BackgroundParser::NormalPriority && !specialParseJob) {
break; //The additional parsing thread is reserved for higher priority parsing
}
for (const auto& url : it1.value()) {
// When a document is scheduled for parsing while it is being parsed, it will be parsed
// again once the job finished, but not now.
if (m_parseJobs.contains(url)) {
continue;
}
Q_ASSERT(m_documents.contains(url));
const auto& parsePlan = m_documents[url];
// If the current job requires sequential processing, but not all jobs with a better priority have been
// completed yet, it will not be created now.
if (parsePlan.sequentialProcessingFlags() & ParseJob::RequiresSequentialProcessing
&& parsePlan.priority() > bestRunningPriority) {
continue;
}
return url;
}
}
return {};
}
/**
* Create a single delayed parse job
*
* E.g. jobs for documents which have been changed by the user, but also to
* handle initial startup where we parse all project files.
*/
void parseDocumentsInternal()
{
if (m_shuttingDown)
return;
//Only create parse-jobs for up to thread-count * 2 documents, so we don't fill the memory unnecessarily
if (m_parseJobs.count() >= m_threads + 1
|| (m_parseJobs.count() >= m_threads && !separateThreadForHighPriority)) {
return;
}
const auto& url = nextDocumentToParse();
if (!url.isEmpty()) {
qCDebug(LANGUAGE) << "creating parse-job" << url << "new count of active parse-jobs:" <<
m_parseJobs.count() + 1;
const QString elidedPathString = elidedPathLeft(url.str(), 70);
emit m_parser->showMessage(m_parser, i18n("Parsing: %1", elidedPathString));
ThreadWeaver::QObjectDecorator* decorator = nullptr;
{
// copy shared data before unlocking the mutex
const auto parsePlanConstIt = m_documents.constFind(url);
const DocumentParsePlan parsePlan = *parsePlanConstIt;
// we must not lock the mutex while creating a parse job
// this could in turn lock e.g. the DUChain and then
// we have a classic lock order inversion (since, usually,
// we lock first the duchain and then our background parser
// mutex)
// see also: https://bugs.kde.org/show_bug.cgi?id=355100
m_mutex.unlock();
decorator = createParseJob(url, parsePlan);
m_mutex.lock();
}
// iterator might get invalid during the time we didn't have the lock
// search again
const auto parsePlanIt = m_documents.find(url);
if (parsePlanIt != m_documents.end()) {
// Remove all mentions of this document.
for (const auto& target : parsePlanIt->targets()) {
m_documentsForPriority[target.priority].remove(url);
}
m_documents.erase(parsePlanIt);
} else {
qCWarning(LANGUAGE) << "Document got removed during parse job creation:" << url;
}
if (decorator) {
if (m_parseJobs.count() == m_threads + 1 && !specialParseJob)
specialParseJob = decorator; //This parse-job is allocated into the reserved thread
m_parseJobs.insert(url, decorator);
m_weaver.enqueue(ThreadWeaver::JobPointer(decorator));
} else {
--m_maxParseJobs;
}
if (!m_documents.isEmpty()) {
// Only try creating one parse-job at a time, else we might iterate through thousands of files
// without finding a language-support, and block the UI for a long time.
QMetaObject::invokeMethod(m_parser, "parseDocuments", Qt::QueuedConnection);
} else {
// make sure we cleaned up properly
// TODO: also empty m_documentsForPriority when m_documents is empty? or do we want to keep capacity?
Q_ASSERT(std::none_of(m_documentsForPriority.constBegin(), m_documentsForPriority.constEnd(),
[](const QSet<IndexedString>& docs) {
return !docs.isEmpty();
}));
}
}
m_parser->updateProgressData();
}
// NOTE: you must not access any of the data structures that are protected by any of the
// background parser internal mutexes in this method
// see also: https://bugs.kde.org/show_bug.cgi?id=355100
ThreadWeaver::QObjectDecorator* createParseJob(const IndexedString& url, const DocumentParsePlan& parsePlan)
{
///FIXME: use IndexedString in the other APIs as well! Esp. for createParseJob!
QUrl qUrl = url.toUrl();
const auto languages = m_languageController->languagesForUrl(qUrl);
const auto& notifyWhenReady = parsePlan.notifyWhenReady();
for (const auto language : languages) {
if (!language) {
qCWarning(LANGUAGE) << "got zero language for" << qUrl;
continue;
}
ParseJob* job = language->createParseJob(url);
if (!job) {
continue; // Language part did not produce a valid ParseJob.
}
job->setParsePriority(parsePlan.priority());
job->setMinimumFeatures(parsePlan.features());
job->setNotifyWhenReady(notifyWhenReady);
job->setSequentialProcessingFlags(parsePlan.sequentialProcessingFlags());
auto* decorator = new ThreadWeaver::QObjectDecorator(job);
QObject::connect(decorator, &ThreadWeaver::QObjectDecorator::done,
m_parser, &BackgroundParser::parseComplete);
QObject::connect(job, &ParseJob::progress,
m_parser, &BackgroundParser::parseProgress, Qt::QueuedConnection);
// TODO more thinking required here to support multiple parse jobs per url (where multiple language plugins want to parse)
return decorator;
}
if (languages.isEmpty())
qCDebug(LANGUAGE) << "found no languages for url" << qUrl;
else
qCDebug(LANGUAGE) << "could not create parse-job for url" << qUrl;
//Notify that we failed
for (const auto& n : notifyWhenReady) {
if (!n) {
continue;
}
QMetaObject::invokeMethod(n.data(), "updateReady", Qt::QueuedConnection,
Q_ARG(KDevelop::IndexedString, url),
Q_ARG(KDevelop::ReferencedTopDUContext, ReferencedTopDUContext()));
}
return nullptr;
}
void loadSettings()
{
///@todo re-load settings when they have been changed!
Q_ASSERT(ICore::self()->activeSession());
KConfigGroup config(ICore::self()->activeSession()->config(), "Background Parser");
// stay backwards compatible
KConfigGroup oldConfig(KSharedConfig::openConfig(), "Background Parser");
#define BACKWARDS_COMPATIBLE_ENTRY(entry, default) \
config.readEntry(entry, oldConfig.readEntry(entry, default))
m_delay = BACKWARDS_COMPATIBLE_ENTRY("Delay", 500);
m_timer.setInterval(m_delay);
m_threads = 0;
if (qEnvironmentVariableIsSet("KDEV_BACKGROUNDPARSER_MAXTHREADS")) {
m_parser->setThreadCount(qEnvironmentVariableIntValue("KDEV_BACKGROUNDPARSER_MAXTHREADS"));
} else {
m_parser->setThreadCount(BACKWARDS_COMPATIBLE_ENTRY("Number of Threads", QThread::idealThreadCount()));
}
resume();
if (BACKWARDS_COMPATIBLE_ENTRY("Enabled", true)) {
m_parser->enableProcessing();
} else {
m_parser->disableProcessing();
}
}
bool isSuspended() const
{
return m_weaver.state()->stateId() == ThreadWeaver::Suspended ||
m_weaver.state()->stateId() == ThreadWeaver::Suspending;
}
void suspend()
{
qCDebug(LANGUAGE) << "Suspending background parser";
if (isSuspended()) { // Already suspending
qCWarning(LANGUAGE) << "Already suspended or suspending";
return;
}
m_timer.stop();
m_weaver.suspend();
}
void resume()
{
qCDebug(LANGUAGE) << "Resuming background parser";
if (m_timer.isActive() && !isSuspended()) { // Not suspended
qCWarning(LANGUAGE) << "Not suspended";
return;
}
m_timer.start(m_delay);
m_weaver.resume();
}
bool addDocumentListener(const IndexedString& url, TopDUContext::Features features, int priority,
QObject* notifyWhenReady, ParseJob::SequentialProcessingFlags flags, int delay,
AddBehavior addBehavior)
{
qCDebug(LANGUAGE) << "BackgroundParserPrivate::addDocumentListener" << url << url.toUrl();
Q_ASSERT(isValidURL(url));
DocumentParseTarget target;
target.priority = priority;
target.features = features;
target.sequentialProcessingFlags = flags;
target.notifyWhenReady = QPointer<QObject>(notifyWhenReady);
{
QMutexLocker lock(&m_mutex);
auto it = m_documents.find(url);
if (it != m_documents.end()) {
//Update the stored plan
auto currentPrio = it.value().priority();
it.value().addTarget(target);
if (currentPrio > target.priority) {
m_documentsForPriority[currentPrio].remove(url);
m_documentsForPriority[target.priority].insert(url);
}
} else if (addBehavior == AddBehavior::AddIfMissing) {
// qCDebug(LANGUAGE) << "BackgroundParser::addDocument: queuing" << cleanedUrl;
auto& doc = m_documents[url];
doc.addTarget(target);
m_documentsForPriority[doc.priority()].insert(url);
++m_maxParseJobs; //So the progress-bar waits for this document
} else {
return false;
}
if (delay == ILanguageSupport::DefaultDelay) {
delay = m_delay;
}
}
startTimerThreadSafe(delay);
return true;
}
BackgroundParser* m_parser;
ILanguageController* m_languageController;
//Current parse-job that is executed in the additional thread
QPointer<QObject> specialParseJob;
QTimer m_timer;
int m_delay = 500;
int m_threads = 1;
bool m_shuttingDown;
// A list of documents that are planned to be parsed, and their priority
QHash<IndexedString, DocumentParsePlan> m_documents;
// The documents ordered by priority
QMap<int, QSet<IndexedString>> m_documentsForPriority;
// Currently running parse jobs
QHash<IndexedString, ThreadWeaver::QObjectDecorator*> m_parseJobs;
// The url for each managed document. Those may temporarily differ from the real url.
QHash<KTextEditor::Document*, IndexedString> m_managedTextDocumentUrls;
// Projects currently in progress of loading
QSet<IProject*> m_loadingProjects;
ThreadWeaver::Queue m_weaver;
// generic high-level mutex
mutable QRecursiveMutex m_mutex;
// local mutex only protecting m_managed
mutable QMutex m_managedMutex;
// A change tracker for each managed document
QHash<IndexedString, DocumentChangeTracker*> m_managed;
int m_maxParseJobs = 0;
int m_doneParseJobs = 0;
QHash<KDevelop::ParseJob*, float> m_jobProgress;
/// The minimum priority needed for processed jobs
int m_neededPriority = BackgroundParser::WorstPriority;
int m_progressMax = 0;
int m_progressDone = 0;
QTimer m_progressTimer;
};
BackgroundParser::BackgroundParser(ILanguageController* languageController)
: QObject(languageController)
, d_ptr(new BackgroundParserPrivate(this, languageController))
{
Q_ASSERT(ICore::self()->documentController());
connect(
ICore::self()->documentController(), &IDocumentController::documentLoaded, this,
&BackgroundParser::documentLoaded);
connect(
ICore::self()->documentController(), &IDocumentController::documentUrlChanged, this,
&BackgroundParser::documentUrlChanged);
connect(
ICore::self()->documentController(), &IDocumentController::documentClosed, this,
&BackgroundParser::documentClosed);
connect(ICore::self(), &ICore::aboutToShutdown, this, &BackgroundParser::aboutToQuit);
QObject::connect(ICore::self()->projectController(),
&IProjectController::projectAboutToBeOpened,
this, &BackgroundParser::projectAboutToBeOpened);
QObject::connect(ICore::self()->projectController(),
&IProjectController::projectOpened,
this, &BackgroundParser::projectOpened);
QObject::connect(ICore::self()->projectController(),
&IProjectController::projectOpeningAborted,
this, &BackgroundParser::projectOpeningAborted);
}
void BackgroundParser::aboutToQuit()
{
Q_D(BackgroundParser);
d->m_shuttingDown = true;
}
BackgroundParser::~BackgroundParser()
{
delete d_ptr;
}
QString BackgroundParser::statusName() const
{
return i18n("Background Parser");
}
void BackgroundParser::loadSettings()
{
Q_D(BackgroundParser);
d->loadSettings();
}
void BackgroundParser::parseProgress(KDevelop::ParseJob* job, float value, const QString& text)
{
Q_UNUSED(text)
Q_D(BackgroundParser);
d->m_jobProgress[job] = value;
updateProgressData();
}
void BackgroundParser::revertAllRequests(QObject* notifyWhenReady)
{
Q_ASSERT(notifyWhenReady != nullptr);
Q_D(BackgroundParser);
QMutexLocker lock(&d->m_mutex);
for (auto it = d->m_documents.begin(); it != d->m_documents.end();) {
d->m_documentsForPriority[it.value().priority()].remove(it.key());
it->removeTargetsForListener(notifyWhenReady);
if ((*it).targets().isEmpty()) {
it = d->m_documents.erase(it);
--d->m_maxParseJobs;
continue;
}
d->m_documentsForPriority[it.value().priority()].insert(it.key());
++it;
}
}
bool BackgroundParser::addListenerToDocumentIfExist(const IndexedString& url, TopDUContext::Features features, int priority,
QObject* notifyWhenReady, ParseJob::SequentialProcessingFlags flags,
int delay)
{
Q_D(BackgroundParser);
return d->addDocumentListener(url, features, priority, notifyWhenReady, flags, delay,
BackgroundParserPrivate::AddBehavior::OnlyUpdateExisting);
}
void BackgroundParser::addDocument(const IndexedString& url, TopDUContext::Features features, int priority,
QObject* notifyWhenReady, ParseJob::SequentialProcessingFlags flags, int delay)
{
Q_D(BackgroundParser);
d->addDocumentListener(url, features, priority, notifyWhenReady, flags, delay,
BackgroundParserPrivate::AddBehavior::AddIfMissing);
}
void BackgroundParser::removeDocument(const IndexedString& url, QObject* notifyWhenReady)
{
Q_D(BackgroundParser);
Q_ASSERT(isValidURL(url));
QMutexLocker lock(&d->m_mutex);
auto documentParsePlanIt = d->m_documents.find(url);
if (documentParsePlanIt != d->m_documents.end()) {
auto& documentParsePlan = *documentParsePlanIt;
d->m_documentsForPriority[documentParsePlan.priority()].remove(url);
documentParsePlan.removeTargetsForListener(notifyWhenReady);
if (documentParsePlan.targets().isEmpty()) {
d->m_documents.erase(documentParsePlanIt);
--d->m_maxParseJobs;
} else {
//Insert with an eventually different priority
d->m_documentsForPriority[documentParsePlan.priority()].insert(url);
}
}
}
void BackgroundParser::parseDocuments()
{
Q_D(BackgroundParser);
if (d->isSuspended() || !d->m_loadingProjects.empty()) {
startTimer(d->m_delay);
return;
}
QMutexLocker lock(&d->m_mutex);
d->parseDocumentsInternal();
}
void BackgroundParser::parseComplete(const ThreadWeaver::JobPointer& job)
{
Q_D(BackgroundParser);
auto decorator = dynamic_cast<ThreadWeaver::QObjectDecorator*>(job.data());
Q_ASSERT(decorator);
auto* parseJob = dynamic_cast<ParseJob*>(decorator->job());
Q_ASSERT(parseJob);
emit parseJobFinished(parseJob);
{
QMutexLocker lock(&d->m_mutex);
d->m_parseJobs.remove(parseJob->document());
d->m_jobProgress.remove(parseJob);
++d->m_doneParseJobs;
updateProgressData();
}
//Continue creating more parse-jobs
QMetaObject::invokeMethod(this, "parseDocuments", Qt::QueuedConnection);
}
void BackgroundParser::disableProcessing()
{
setNeededPriority(BestPriority);
}
void BackgroundParser::enableProcessing()
{
setNeededPriority(WorstPriority);
}
int BackgroundParser::priorityForDocument(const IndexedString& url) const
{
Q_D(const BackgroundParser);
Q_ASSERT(isValidURL(url));
QMutexLocker lock(&d->m_mutex);
return d->m_documents[url].priority();
}
bool BackgroundParser::isQueued(const IndexedString& url) const
{
Q_D(const BackgroundParser);
Q_ASSERT(isValidURL(url));
QMutexLocker lock(&d->m_mutex);
return d->m_documents.contains(url);
}
int BackgroundParser::queuedCount() const
{
Q_D(const BackgroundParser);
QMutexLocker lock(&d->m_mutex);
return d->m_documents.count();
}
bool BackgroundParser::isIdle() const
{
Q_D(const BackgroundParser);
QMutexLocker lock(&d->m_mutex);
return d->m_documents.isEmpty() && d->m_weaver.isIdle();
}
void BackgroundParser::setNeededPriority(int priority)
{
Q_D(BackgroundParser);
QMutexLocker lock(&d->m_mutex);
d->m_neededPriority = priority;
d->startTimerThreadSafe(d->m_delay);
}
void BackgroundParser::abortAllJobs()
{
Q_D(BackgroundParser);
qCDebug(LANGUAGE) << "Aborting all parse jobs";
d->m_weaver.requestAbort();
}
void BackgroundParser::suspend()
{
Q_D(BackgroundParser);
d->suspend();
emit hideProgress(this);
}
void BackgroundParser::resume()
{
Q_D(BackgroundParser);
d->resume();
updateProgressData();
}
void BackgroundParser::updateProgressData()
{
Q_D(BackgroundParser);
if (d->m_doneParseJobs >= d->m_maxParseJobs) {
if (d->m_doneParseJobs > d->m_maxParseJobs) {
qCDebug(LANGUAGE) << "m_doneParseJobs larger than m_maxParseJobs:" << d->m_doneParseJobs <<
d->m_maxParseJobs;
}
d->m_doneParseJobs = 0;
d->m_maxParseJobs = 0;
} else {
float additionalProgress = 0;
for (float progress : qAsConst(d->m_jobProgress)) {
additionalProgress += progress;
}
d->m_progressMax = d->m_maxParseJobs * 1000;
d->m_progressDone = (additionalProgress + d->m_doneParseJobs) * 1000;
if (!d->m_progressTimer.isActive()) {
d->m_progressTimer.start();
}
}
// Cancel progress updating and hide progress-bar when parsing is done.
if (d->m_doneParseJobs == d->m_maxParseJobs
|| (d->m_neededPriority == BackgroundParser::BestPriority && d->m_weaver.queueLength() == 0)) {
if (d->m_progressTimer.isActive()) {
d->m_progressTimer.stop();
}
emit d->m_parser->hideProgress(d->m_parser);
}
}
ParseJob* BackgroundParser::parseJobForDocument(const IndexedString& document) const
{
Q_D(const BackgroundParser);
Q_ASSERT(isValidURL(document));
QMutexLocker lock(&d->m_mutex);
auto decorator = d->m_parseJobs.value(document);
return decorator ? dynamic_cast<ParseJob*>(decorator->job()) : nullptr;
}
void BackgroundParser::setThreadCount(int threadCount)
{
Q_D(BackgroundParser);
if (d->m_threads != threadCount) {
d->m_threads = threadCount;
d->m_weaver.setMaximumNumberOfThreads(d->m_threads + 1); //1 Additional thread for high-priority parsing
}
}
int BackgroundParser::threadCount() const
{
Q_D(const BackgroundParser);
return d->m_threads;
}
void BackgroundParser::setDelay(int milliseconds)
{
Q_D(BackgroundParser);
if (d->m_delay != milliseconds) {
d->m_delay = milliseconds;
d->m_timer.setInterval(d->m_delay);
}
}
QList<IndexedString> BackgroundParser::managedDocuments()
{
Q_D(BackgroundParser);
QMutexLocker l(&d->m_managedMutex);
return d->m_managed.keys();
}
bool BackgroundParser::waitForIdle() const
{
Q_D(const BackgroundParser);
QList<IndexedString> runningParseJobsUrls;
forever {
{
QMutexLocker lock(&d->m_mutex);
if (d->m_parseJobs.isEmpty()) {
qCDebug(LANGUAGE) << "All parse jobs done" << d->m_parseJobs.keys();
return true;
}
if (d->m_parseJobs.size() != runningParseJobsUrls.size()) {
runningParseJobsUrls = d->m_parseJobs.keys();
qCDebug(LANGUAGE) <<
"Waiting for background parser to get in idle state... -- the following parse jobs are still running:"
<< runningParseJobsUrls;
}
}
QCoreApplication::processEvents();
QThread::msleep(100);
}
return false;
}
DocumentChangeTracker* BackgroundParser::trackerForUrl(const KDevelop::IndexedString& url) const
{
Q_D(const BackgroundParser);
if (url.isEmpty()) {
// this happens e.g. when setting the final location of a problem that is not
// yet associated with a top ctx.
return nullptr;
}
if (!isValidURL(url)) {
qCWarning(LANGUAGE) << "Tracker requested for invalid URL:" << url.toUrl();
}
Q_ASSERT(isValidURL(url));
QMutexLocker l(&d->m_managedMutex);
return d->m_managed.value(url, nullptr);
}
void BackgroundParser::documentClosed(IDocument* document)
{
Q_D(BackgroundParser);
QMutexLocker l(&d->m_mutex);
if (document->textDocument()) {
KTextEditor::Document* textDocument = document->textDocument();
auto documentUrlIt = d->m_managedTextDocumentUrls.find(textDocument);
if (documentUrlIt == d->m_managedTextDocumentUrls.end())
return; // Probably the document had an invalid url, and thus it wasn't added to the background parser
Q_ASSERT(documentUrlIt != d->m_managedTextDocumentUrls.end());
IndexedString url(*documentUrlIt);
QMutexLocker l2(&d->m_managedMutex);
auto urlIt = d->m_managed.find(url);
Q_ASSERT(urlIt != d->m_managed.end());
qCDebug(LANGUAGE) << "removing" << url.str() << "from background parser";
delete *urlIt;
d->m_managedTextDocumentUrls.erase(documentUrlIt);
d->m_managed.erase(urlIt);
}
}
void BackgroundParser::documentLoaded(IDocument* document)
{
Q_D(BackgroundParser);
QMutexLocker l(&d->m_mutex);
if (document->textDocument() && document->textDocument()->url().isValid()) {
KTextEditor::Document* textDocument = document->textDocument();
IndexedString url(document->url());
// Some debugging because we had issues with this
QMutexLocker l2(&d->m_managedMutex);
auto urlIt = d->m_managed.find(url);
if (urlIt != d->m_managed.end() && (*urlIt)->document() == textDocument) {
qCDebug(LANGUAGE) << "Got redundant documentLoaded from" << document->url() << textDocument;
return;
}
qCDebug(LANGUAGE) << "Creating change tracker for " << document->url();
Q_ASSERT(!d->m_managed.contains(url));
Q_ASSERT(!d->m_managedTextDocumentUrls.contains(textDocument));
d->m_managedTextDocumentUrls[textDocument] = url;
d->m_managed.insert(url, new DocumentChangeTracker(textDocument));
} else {
qCDebug(LANGUAGE) << "NOT creating change tracker for" << document->url();
}
}
void BackgroundParser::documentUrlChanged(IDocument* document)
{
documentClosed(document);
// Only call documentLoaded if the file wasn't renamed to a filename that is already tracked.
if (document->textDocument() && !trackerForUrl(IndexedString(document->textDocument()->url())))
documentLoaded(document);
}
void BackgroundParser::startTimer(int delay)
{
Q_D(BackgroundParser);
if (!d->isSuspended()) {
d->m_timer.start(delay);
}
}
void BackgroundParser::projectAboutToBeOpened(IProject* project)
{
Q_D(BackgroundParser);
d->m_loadingProjects.insert(project);
}
void BackgroundParser::projectOpened(IProject* project)
{
Q_D(BackgroundParser);
d->m_loadingProjects.remove(project);
}
void BackgroundParser::projectOpeningAborted(IProject* project)
{
Q_D(BackgroundParser);
d->m_loadingProjects.remove(project);
}
void BackgroundParser::updateProgressBar()
{
Q_D(BackgroundParser);
emit showProgress(this, 0, d->m_progressMax, d->m_progressDone);
}
|