commit cb30aa7f107ba0aa0b141dc42be58ab3bbab57d9
parent 2a143f4805d100e0a1e0e65c0693194c40c82146
Author: cfillion <cfillion@users.noreply.github.com>
Date: Sun, 6 Dec 2015 16:09:49 -0500
refactor the download queue
Diffstat:
3 files changed, 60 insertions(+), 21 deletions(-)
diff --git a/src/download.cpp b/src/download.cpp
@@ -19,10 +19,7 @@ Download::Download(const string &name, const string &url)
Download::~Download()
{
- if(m_threadHandle) {
- WaitForSingleObject(m_threadHandle, INFINITE);
- CloseHandle(m_threadHandle);
- }
+ wait();
}
void Download::reset()
@@ -33,6 +30,14 @@ void Download::reset()
m_contents.clear();
}
+void Download::wait()
+{
+ if(m_threadHandle) {
+ WaitForSingleObject(m_threadHandle, INFINITE);
+ CloseHandle(m_threadHandle);
+ }
+}
+
void Download::TimerTick()
{
Download *dl = Download::NextFinished();
@@ -194,14 +199,14 @@ void Download::abort()
m_aborted = true;
}
-DownloadQueue::~DownloadQueue()
+DownloadQueue::DownloadQueue()
+ : m_current(0)
{
- while(!m_queue.empty()) {
- Download *download = m_queue.front();
- delete download;
+}
- m_queue.pop();
- }
+DownloadQueue::~DownloadQueue()
+{
+ abort();
}
void DownloadQueue::push(Download *dl)
@@ -209,15 +214,44 @@ void DownloadQueue::push(Download *dl)
m_onPush(dl);
dl->onFinish([=]() {
- m_queue.pop();
+ m_current = 0;
delete dl;
- if(!m_queue.empty())
- m_queue.front()->start();
+ start();
});
m_queue.push(dl);
- if(m_queue.size() == 1)
- dl->start();
+ start();
+}
+
+void DownloadQueue::start()
+{
+ if(m_queue.empty())
+ return;
+
+ if(!m_current) {
+ m_current = m_queue.front();
+ m_queue.pop();
+
+ m_current->start();
+ }
+}
+
+void DownloadQueue::abort()
+{
+ if(m_current)
+ m_current->abort();
+
+ clear();
+}
+
+void DownloadQueue::clear()
+{
+ while(!m_queue.empty()) {
+ Download *download = m_queue.front();
+ delete download;
+
+ m_queue.pop();
+ }
}
diff --git a/src/download.hpp b/src/download.hpp
@@ -31,6 +31,7 @@ public:
void start();
void abort();
+ void wait();
private:
static WDL_Mutex s_mutex;
@@ -66,19 +67,23 @@ public:
typedef boost::signals2::signal<void (Download *)> Signal;
typedef Signal::slot_type Callback;
- DownloadQueue() {}
+ DownloadQueue();
DownloadQueue(const DownloadQueue &) = delete;
~DownloadQueue();
void push(Download *);
+ void start();
+ void abort();
- size_t size() const { return m_queue.size(); }
- bool empty() const { return m_queue.empty(); }
+ bool idle() const { return m_queue.empty() && !m_current; }
void onPush(const Callback &callback) { m_onPush.connect(callback); }
private:
+ void clear();
+
Download::Queue m_queue;
+ Download *m_current;
Signal m_onPush;
};
diff --git a/src/transaction.cpp b/src/transaction.cpp
@@ -67,7 +67,7 @@ void Transaction::fetch(const Remote &remote)
void Transaction::prepare()
{
- if(!m_queue.empty())
+ if(!m_queue.idle())
return;
for(Database *db : m_databases) {
@@ -102,7 +102,7 @@ void Transaction::run()
void Transaction::cancel()
{
- ShowMessageBox("Not Implemented", "Cancel transaction", 0);
+ m_queue.abort();
}
void Transaction::install(Package *pkg)
@@ -146,7 +146,7 @@ Path Transaction::installPath(Package *pkg) const
void Transaction::finish()
{
- if(!m_queue.empty())
+ if(!m_queue.idle())
return;
m_onFinish();