Index: sources/storage/usbwatcher.cpp =================================================================== diff -u -rfeb3423b373dc2a2c4267ef9fcb4d924d738423d -r5e78f0799b46963feb5756decb1a27b952cd19b3 --- sources/storage/usbwatcher.cpp (.../usbwatcher.cpp) (revision feb3423b373dc2a2c4267ef9fcb4d924d738423d) +++ sources/storage/usbwatcher.cpp (.../usbwatcher.cpp) (revision 5e78f0799b46963feb5756decb1a27b952cd19b3) @@ -18,6 +18,7 @@ #include // Qt +#include #include // Project @@ -28,23 +29,34 @@ // namespace using namespace Storage; -// Singleton -SINGLETON_INIT(USBWatcher) - -USBWatcher::USBWatcher(QObject *parent) : QObject(parent) -{ +USBWatcher::USBWatcher(QObject *parent) : QObject(parent) { startTimer(_checkInterval); } bool USBWatcher::init() { + // runs in main thread + Q_ASSERT_X(QThread::currentThread() == qApp->thread() , "_USBWatcher::init", "The Class initialization must be done in Main Thread" ); + _USBWatcher_Thread.setObjectName("USB Watcher Thread"); + connect(qApp, &QApplication::aboutToQuit, this, &USBWatcher::quit); + _USBWatcher_Thread.start(); + moveToThread(&_USBWatcher_Thread); + + + // runs in USBWatcher thread initConnections(); + return true; } void USBWatcher::quit() { + // runs in Logger thread + moveToThread(qApp->thread()); + // runs in main thread + _USBWatcher_Thread.quit(); + _USBWatcher_Thread.wait(); } void USBWatcher::initConnections() @@ -63,6 +75,7 @@ } bool USBWatcher::usbSeek(QString &vDevice) { + PRINT_THREAD_NAME; QString device = ""; for (char a = 'a'; a <= 'z'; a++) { device = QString("/dev/sd%1%2").arg(a).arg('1'); @@ -77,11 +90,13 @@ void USBWatcher::timerEvent(QTimerEvent *) { + PRINT_THREAD_NAME; usbcheck(); } void USBWatcher::usbcheck() { + PRINT_THREAD_NAME; QString device = ""; if (usbSeek(device)) { if (! _umounted ) { @@ -122,6 +137,7 @@ bool USBWatcher::usbMount(const QString &vDevice) { + PRINT_THREAD_NAME; bool ok; _usbDrive = vDevice.toLatin1().constData(); ok = mount(_usbDrive, USB_Mount_Point, USB_File_System, 0, "") == 0; @@ -138,6 +154,7 @@ bool USBWatcher::usbUmount(const QString &vDevice) { + PRINT_THREAD_NAME; bool ok; ok = umount(vDevice.toLatin1().constData()) == 0; if (ok) { @@ -146,13 +163,14 @@ emit didUSBDriveUmount(); } else { // the error is irrelevant, commented out for now - usbError(vDevice); + //usbError(vDevice); } return ok; } void USBWatcher::usbRemove() { + PRINT_THREAD_NAME; usbUmount(USB_Mount_Point); _umounted = false; _removed = true;