Commit 74512fe9 authored by Ivan Čukić's avatar Ivan Čukić 👁
Browse files

Removed the separate thread for resource score cache processing

Reviewers: davidedmundson

Reviewed By: davidedmundson

Subscribers: broulik, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D12576
parent 5451dbae
......@@ -23,7 +23,6 @@
// Qt
#include <QList>
#include <QMutex>
#include <QThread>
// System
......@@ -38,9 +37,14 @@
#include "ResourceScoreCache.h"
class ResourceScoreMaintainer::Private : public QThread {
class ResourceScoreMaintainer::Private {
public:
Private()
{
}
~Private();
typedef QString ApplicationName;
typedef QString ActivityID;
typedef QList<QString> ResourceList;
......@@ -49,50 +53,45 @@ public:
typedef QHash<ActivityID, Applications> ResourceTree;
ResourceTree scheduledResources;
QMutex scheduledResources_mutex;
void run() Q_DECL_OVERRIDE;
void processActivity(const ActivityID &activity,
const Applications &applications);
void processResources();
QTimer processResourcesTimer;
};
ResourceScoreMaintainer::Private::~Private()
{
requestInterruption();
wait(1500); // Enough time for the sleep(1) + processing in run()
}
void ResourceScoreMaintainer::Private::run()
void ResourceScoreMaintainer::Private::processResources()
{
using namespace kamd::utils;
while (!isInterruptionRequested()) {
// initial delay before processing the resources
sleep(1);
ResourceTree resources;
// initial delay before processing the resources
sleep(1);
{
QMutexLocker lock(&scheduledResources_mutex);
std::swap(resources, scheduledResources);
}
ResourceTree resources;
const auto activity = StatsPlugin::self()->currentActivity();
std::swap(resources, scheduledResources);
// Let us first process the events related to the current
// activity so that the stats are available quicker
const auto activity = StatsPlugin::self()->currentActivity();
if (resources.contains(activity)) {
processActivity(activity, resources[activity]);
resources.remove(activity);
}
// Let us first process the events related to the current
// activity so that the stats are available quicker
for_each_assoc(resources,
[this](const ActivityID & activity, const Applications & applications) {
processActivity(activity, applications);
}
);
if (resources.contains(activity)) {
processActivity(activity, resources[activity]);
resources.remove(activity);
}
for_each_assoc(resources,
[this](const ActivityID & activity, const Applications & applications) {
processActivity(activity, applications);
}
);
}
void ResourceScoreMaintainer::Private::processActivity(const ActivityID
......@@ -118,8 +117,11 @@ ResourceScoreMaintainer *ResourceScoreMaintainer::self()
}
ResourceScoreMaintainer::ResourceScoreMaintainer()
: d()
{
d->processResourcesTimer.setInterval(1000);
d->processResourcesTimer.setSingleShot(true);
connect(&d->processResourcesTimer, &QTimer::timeout,
this, [=] { d->processResources(); });
}
ResourceScoreMaintainer::~ResourceScoreMaintainer()
......@@ -129,8 +131,6 @@ ResourceScoreMaintainer::~ResourceScoreMaintainer()
void ResourceScoreMaintainer::processResource(const QString &resource,
const QString &application)
{
QMutexLocker lock(&d->scheduledResources_mutex);
// Checking whether the item is already scheduled for
// processing
......@@ -153,5 +153,5 @@ void ResourceScoreMaintainer::processResource(const QString &resource,
d->scheduledResources[activity][application] << resource;
}
d->start();
d->processResourcesTimer.start();
}
......@@ -20,6 +20,8 @@
#ifndef PLUGINS_SQLITE_RESOURCE_SCORE_MAINTAINER_H
#define PLUGINS_SQLITE_RESOURCE_SCORE_MAINTAINER_H
#include <QObject>
// Utils
#include <utils/d_ptr.h>
......@@ -29,7 +31,7 @@ class QString;
/**
* ResourceScoreMaintainer represents a queue of resource processing requests.
*/
class ResourceScoreMaintainer {
class ResourceScoreMaintainer: public QObject {
public:
static ResourceScoreMaintainer *self();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment