mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 11:37:56 -04:00
Download user actions have priority over updates
This commit is contained in:
parent
462fe8e9b4
commit
3c04e055bf
@ -5,13 +5,13 @@
|
|||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
#include <QMutexLocker>
|
#include <QMutexLocker>
|
||||||
#include <QQueue>
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
#include <QWaitCondition>
|
#include <QWaitCondition>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <queue>
|
||||||
|
|
||||||
#include <kiwix/downloader.h>
|
#include <kiwix/downloader.h>
|
||||||
|
|
||||||
@ -20,34 +20,36 @@
|
|||||||
typedef QMap<QString, QVariant> DownloadInfo;
|
typedef QMap<QString, QVariant> DownloadInfo;
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class ThreadSafeQueue
|
class ThreadSafePriorityQueue
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void enqueue(const T& x)
|
void enqueue(const T& x)
|
||||||
{
|
{
|
||||||
const QMutexLocker threadSafetyGuarantee(&m_mutex);
|
const QMutexLocker threadSafetyGuarantee(&m_mutex);
|
||||||
m_queue.enqueue(x);
|
m_queue.push(x);
|
||||||
m_queueIsNotEmpty.wakeAll();
|
m_queueIsNotEmpty.wakeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
T dequeue()
|
T dequeue()
|
||||||
{
|
{
|
||||||
const QMutexLocker threadSafetyGuarantee(&m_mutex);
|
const QMutexLocker threadSafetyGuarantee(&m_mutex);
|
||||||
if ( m_queue.isEmpty() )
|
if ( m_queue.empty() )
|
||||||
m_queueIsNotEmpty.wait(&m_mutex);
|
m_queueIsNotEmpty.wait(&m_mutex);
|
||||||
|
|
||||||
return m_queue.dequeue();
|
const T ret = m_queue.top();
|
||||||
|
m_queue.pop();
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isEmpty() const
|
bool isEmpty() const
|
||||||
{
|
{
|
||||||
const QMutexLocker threadSafetyGuarantee(&m_mutex);
|
const QMutexLocker threadSafetyGuarantee(&m_mutex);
|
||||||
return m_queue.isEmpty();
|
return m_queue.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private: // data
|
private: // data
|
||||||
mutable QMutex m_mutex;
|
mutable QMutex m_mutex;
|
||||||
QQueue<T> m_queue;
|
std::priority_queue<T> m_queue;
|
||||||
QWaitCondition m_queueIsNotEmpty;
|
QWaitCondition m_queueIsNotEmpty;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -55,11 +57,11 @@ class DownloadState
|
|||||||
{
|
{
|
||||||
public: // types
|
public: // types
|
||||||
enum Action {
|
enum Action {
|
||||||
|
UPDATE,
|
||||||
START,
|
START,
|
||||||
PAUSE,
|
PAUSE,
|
||||||
RESUME,
|
RESUME,
|
||||||
CANCEL,
|
CANCEL
|
||||||
UPDATE
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum Status {
|
enum Status {
|
||||||
@ -165,9 +167,13 @@ private: // types
|
|||||||
{
|
{
|
||||||
Action action;
|
Action action;
|
||||||
QString bookId;
|
QString bookId;
|
||||||
|
|
||||||
|
bool operator<(const Request& other) const {
|
||||||
|
return this->action < other.action;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef ThreadSafeQueue<Request> RequestQueue;
|
typedef ThreadSafePriorityQueue<Request> RequestQueue;
|
||||||
|
|
||||||
private: // functions
|
private: // functions
|
||||||
void processDownloadActions();
|
void processDownloadActions();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user