mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-22 20:44:15 +00:00
deleteThread and downloadThread threads are handled better and destructed in a cleaner way
This commit is contained in:
parent
84fef00349
commit
825c874718
@ -37,6 +37,10 @@ class deleteThread : public QThread {
|
|||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
~deleteThread(){
|
||||||
|
wait();
|
||||||
|
}
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void deletionFinished(deleteThread*) const;
|
void deletionFinished(deleteThread*) const;
|
||||||
|
|
||||||
|
@ -40,27 +40,44 @@ class downloadThread : public QThread {
|
|||||||
QStringList url_list;
|
QStringList url_list;
|
||||||
QMutex mutex;
|
QMutex mutex;
|
||||||
QWaitCondition condition;
|
QWaitCondition condition;
|
||||||
|
bool abort;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void downloadFinished(const QString& url, const QString& file_path, int return_code, const QString& errorBuffer);
|
void downloadFinished(const QString& url, const QString& file_path, int return_code, const QString& errorBuffer);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
downloadThread(QObject* parent) : QThread(parent){}
|
downloadThread(QObject* parent) : QThread(parent){
|
||||||
|
mutex.lock();
|
||||||
|
abort = false;
|
||||||
|
mutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
~downloadThread(){
|
||||||
|
mutex.lock();
|
||||||
|
abort = true;
|
||||||
|
condition.wakeOne();
|
||||||
|
mutex.unlock();
|
||||||
|
wait();
|
||||||
|
}
|
||||||
|
|
||||||
void downloadUrl(const QString& url){
|
void downloadUrl(const QString& url){
|
||||||
mutex.lock();
|
QMutexLocker locker(&mutex);
|
||||||
qDebug("In Download thread function, mutex locked");
|
qDebug("In Download thread function, mutex locked");
|
||||||
url_list << url;
|
url_list << url;
|
||||||
mutex.unlock();
|
|
||||||
qDebug("In Download thread function, mutex unlocked (url added)");
|
qDebug("In Download thread function, mutex unlocked (url added)");
|
||||||
if(!isRunning()){
|
if(!isRunning()){
|
||||||
qDebug("In Download thread function, Launching thread (was stopped)");
|
qDebug("In Download thread function, Launching thread (was stopped)");
|
||||||
start();
|
start();
|
||||||
|
}else{
|
||||||
|
condition.wakeOne();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
void run(){
|
void run(){
|
||||||
forever{
|
forever{
|
||||||
|
if(abort)
|
||||||
|
return;
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
qDebug("In Download thread RUN, mutex locked");
|
qDebug("In Download thread RUN, mutex locked");
|
||||||
if(url_list.size() != 0){
|
if(url_list.size() != 0){
|
||||||
@ -76,6 +93,8 @@ class downloadThread : public QThread {
|
|||||||
filePath = tmpfile->fileName();
|
filePath = tmpfile->fileName();
|
||||||
}
|
}
|
||||||
delete tmpfile;
|
delete tmpfile;
|
||||||
|
if(abort)
|
||||||
|
return;
|
||||||
FILE *file = fopen((const char*)filePath.toUtf8(), "w");
|
FILE *file = fopen((const char*)filePath.toUtf8(), "w");
|
||||||
if(!file){
|
if(!file){
|
||||||
std::cerr << "Error: could not open temporary file...\n";
|
std::cerr << "Error: could not open temporary file...\n";
|
||||||
@ -140,9 +159,10 @@ class downloadThread : public QThread {
|
|||||||
emit downloadFinished(url, filePath, return_code, QString(errorBuffer));
|
emit downloadFinished(url, filePath, return_code, QString(errorBuffer));
|
||||||
qDebug("In Download thread RUN, signal emitted, ErrorBuffer: %s", errorBuffer);
|
qDebug("In Download thread RUN, signal emitted, ErrorBuffer: %s", errorBuffer);
|
||||||
}else{
|
}else{
|
||||||
|
qDebug("In Download thread RUN, mutex still locked (no urls) -> sleeping");
|
||||||
|
condition.wait(&mutex);
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
qDebug("In Download thread RUN, mutex unlocked (no urls) -> stopping");
|
qDebug("In Download thread RUN, woke up, mutex unlocked");
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user