From 6a11f80f72cd34e207ebd4b92201617564d21ecb Mon Sep 17 00:00:00 2001 From: craig Date: Wed, 29 Feb 2012 18:11:06 +0000 Subject: [PATCH] Sleep --- devices/fsdevice.cpp | 9 +-------- devices/mtpdevice.cpp | 9 +-------- devices/utils.cpp | 20 ++++++++++++++++++++ devices/utils.h | 4 ++++ gui/mainwindow.cpp | 12 +++--------- http/httpserver.cpp | 13 ++++--------- 6 files changed, 33 insertions(+), 34 deletions(-) diff --git a/devices/fsdevice.cpp b/devices/fsdevice.cpp index ea51e7120..1e97ed03f 100644 --- a/devices/fsdevice.cpp +++ b/devices/fsdevice.cpp @@ -50,11 +50,6 @@ static const QLatin1String constCantataCacheFile("/.cache.xml"); -struct Thread : public QThread -{ - static void sleep() { QThread::msleep(100); } -}; - MusicScanner::MusicScanner(const QString &f) : QThread(0) , folder(f) @@ -78,9 +73,7 @@ void MusicScanner::run() void MusicScanner::stop() { stopRequested=true; - quit(); - for(int i=0; i<10 && isRunning(); ++i) - Thread::sleep(); + Utils::stopThread(this); } MusicLibraryItemRoot * MusicScanner::takeLibrary() diff --git a/devices/mtpdevice.cpp b/devices/mtpdevice.cpp index df80e9676..642e0927d 100644 --- a/devices/mtpdevice.cpp +++ b/devices/mtpdevice.cpp @@ -525,16 +525,9 @@ MtpDevice::MtpDevice(DevicesModel *m, Solid::Device &dev) QTimer::singleShot(0, this, SLOT(rescan())); } -struct Thread : public QThread -{ - static void sleep() { QThread::msleep(100); } -}; - MtpDevice::~MtpDevice() { - thread->quit(); - for(int i=0; i<10 && thread->isRunning(); ++i) - Thread::sleep(); + Utils::stopThread(thread); thread->deleteLater(); thread=0; deleteTemp(); diff --git a/devices/utils.cpp b/devices/utils.cpp index 8e69f6d6e..326e047a2 100644 --- a/devices/utils.cpp +++ b/devices/utils.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #ifdef ENABLE_KDE_SUPPORT #include #endif @@ -192,3 +193,22 @@ bool Utils::createDir(const QString &dir, const QString &base) return status; } + +struct Thread : public QThread +{ + static void sleep(int msecs) { QThread::msleep(msecs); } +}; + +void Utils::msleep(int msecs) +{ + Thread::sleep(msecs); +} + +void Utils::stopThread(QThread *thread) +{ + thread->quit(); + for(int i=0; i<10 && thread->isRunning(); ++i) { + sleep(); + } +} + diff --git a/devices/utils.h b/devices/utils.h index 7763c7363..cff065872 100644 --- a/devices/utils.h +++ b/devices/utils.h @@ -27,6 +27,7 @@ #include class QString; +class QThread; namespace Utils { @@ -39,6 +40,9 @@ namespace Utils extern void cleanDir(const QString &dir, const QString &base, const QString &coverFile, int level=0); extern void setFilePerms(const QString &file); extern bool createDir(const QString &dir, const QString &base); + extern void msleep(int msecs); + inline void sleep() { msleep(100); } + extern void stopThread(QThread *thread); }; #endif diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 7d27753c7..958c7d20b 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -66,6 +66,7 @@ #include "mpdparseutils.h" #include "settings.h" #include "config.h" +#include "utils.h" #include "musiclibrarymodel.h" #include "musiclibraryitemalbum.h" #include "librarypage.h" @@ -920,11 +921,6 @@ MainWindow::MainWindow(QWidget *parent) } } -struct Thread : public QThread -{ - static void sleep() { QThread::msleep(100); } -}; - MainWindow::~MainWindow() { if (dock) { @@ -959,11 +955,9 @@ MainWindow::~MainWindow() disconnect(MPDConnection::self(), 0, 0, 0); if (Settings::self()->stopOnExit()) { emit stop(); - Thread::sleep(); + Utils::sleep(); } - mpdThread->quit(); - for(int i=0; i<10 && mpdThread->isRunning(); ++i) - Thread::sleep(); + Utils::stopThread(mpdThread); } void MainWindow::load(const QList &urls) diff --git a/http/httpserver.cpp b/http/httpserver.cpp index f0bd6dd4a..cceaf318e 100644 --- a/http/httpserver.cpp +++ b/http/httpserver.cpp @@ -23,6 +23,7 @@ #include "httpserver.h" #include "httpsocket.h" +#include "utils.h" #include #include #ifdef ENABLE_KDE_SUPPORT @@ -30,11 +31,6 @@ K_GLOBAL_STATIC(HttpServer, instance) #endif -struct Thread : public QThread -{ - static void sleep() { QThread::msleep(100); } -}; - HttpServer * HttpServer::self() { #ifdef ENABLE_KDE_SUPPORT @@ -56,10 +52,9 @@ void HttpServer::stop() } if (thread) { - thread->quit(); - for(int i=0; i<10 && thread->isRunning(); ++i) { - Thread::sleep(); - } + Utils::stopThread(thread); + thread->deleteLater(); + thread=0; } }