AlbumCoverLoader: Process tasks using timer

Helps reduce memory growth.
This commit is contained in:
Jonas Kvinge 2024-09-03 22:01:13 +02:00
parent 82079fcf70
commit 155485173b
2 changed files with 28 additions and 4 deletions

View File

@ -1,6 +1,6 @@
/*
* Strawberry Music Player
* Copyright 2019-2023, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2019-2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -18,6 +18,7 @@
*/
#include <memory>
#include <chrono>
#include <QtGlobal>
#include <QObject>
@ -31,6 +32,7 @@
#include <QUrl>
#include <QFile>
#include <QImage>
#include <QTimer>
#include <QNetworkReply>
#include <QNetworkRequest>
@ -44,6 +46,7 @@
#include "albumcoverloaderresult.h"
#include "albumcoverimageresult.h"
using namespace std::literals::chrono_literals;
using std::make_shared;
namespace {
@ -53,6 +56,7 @@ constexpr int kMaxRedirects = 3;
AlbumCoverLoader::AlbumCoverLoader(QObject *parent)
: QObject(parent),
network_(new NetworkAccessManager(this)),
timer_process_tasks_(new QTimer(this)),
stop_requested_(false),
load_image_async_id_(1),
original_thread_(nullptr) {
@ -61,6 +65,10 @@ AlbumCoverLoader::AlbumCoverLoader(QObject *parent)
original_thread_ = thread();
timer_process_tasks_->setSingleShot(false);
timer_process_tasks_->setInterval(10ms);
QObject::connect(timer_process_tasks_, &QTimer::timeout, this, &AlbumCoverLoader::ProcessTasks);
}
void AlbumCoverLoader::ExitAsync() {
@ -165,18 +173,31 @@ quint64 AlbumCoverLoader::EnqueueTask(TaskPtr task) {
tasks_.enqueue(task);
}
QMetaObject::invokeMethod(this, &AlbumCoverLoader::ProcessTasks, Qt::QueuedConnection);
QMetaObject::invokeMethod(this, &AlbumCoverLoader::StartProcessTasks, Qt::QueuedConnection);
return task->id;
}
void AlbumCoverLoader::StartProcessTasks() {
if (!timer_process_tasks_->isActive()) {
timer_process_tasks_->start();
}
}
void AlbumCoverLoader::ProcessTasks() {
TaskPtr task;
{
QMutexLocker l(&mutex_load_image_async_);
if (tasks_.isEmpty()) return;
if (tasks_.isEmpty()) {
if (timer_process_tasks_->isActive()) {
timer_process_tasks_->stop();
}
return;
}
task = tasks_.dequeue();
}

View File

@ -1,6 +1,6 @@
/*
* Strawberry Music Player
* Copyright 2019-2023, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2019-2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -39,6 +39,7 @@
#include "albumcoverimageresult.h"
class QThread;
class QTimer;
class QNetworkReply;
class NetworkAccessManager;
@ -119,11 +120,13 @@ class AlbumCoverLoader : public QObject {
private Q_SLOTS:
void Exit();
void StartProcessTasks();
void ProcessTasks();
void LoadRemoteImageFinished(QNetworkReply *reply, AlbumCoverLoader::TaskPtr task, const AlbumCoverLoaderResult::Type result_type, const QUrl &cover_url);
private:
SharedPtr<NetworkAccessManager> network_;
QTimer *timer_process_tasks_;
bool stop_requested_;
QMutex mutex_load_image_async_;
QQueue<TaskPtr> tasks_;