From d769b95b095014ed87bb0829e689c6fb289e6406 Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Sun, 7 Feb 2016 17:32:12 +0100 Subject: [PATCH] Threadsafety Now completly working on all platforms --- HotkeyTest/hottestwidget.cpp | 6 +++ HotkeyTest/hottestwidget.ui | 22 +--------- QHotkey/qhotkey.cpp | 80 ++++++++++++++++++++++++++++-------- QHotkey/qhotkey_p.h | 14 +++++-- 4 files changed, 79 insertions(+), 43 deletions(-) diff --git a/HotkeyTest/hottestwidget.cpp b/HotkeyTest/hottestwidget.cpp index c341b64..88d0d57 100644 --- a/HotkeyTest/hottestwidget.cpp +++ b/HotkeyTest/hottestwidget.cpp @@ -200,4 +200,10 @@ void HotTestWidget::on_threadEnableCheckBox_clicked() this->hotkey_4->moveToThread(this->thread4); this->hotkey_5->moveToThread(this->thread5); + + QApplication::processEvents(); + Q_ASSERT(this->hotkey_4->thread() == this->thread4); + Q_ASSERT(this->hotkey_5->thread() == this->thread5); + + this->ui->tabWidget->setCurrentIndex(0); } diff --git a/HotkeyTest/hottestwidget.ui b/HotkeyTest/hottestwidget.ui index 63a3a22..adb6e4c 100644 --- a/HotkeyTest/hottestwidget.ui +++ b/HotkeyTest/hottestwidget.ui @@ -927,7 +927,7 @@ - <html><head/><body><p>Use this test to determine whether shortcuts on multiple threads are supported or not. Internally, the backend of the QHotkey class is <span style=" font-weight:600;">threadsafe</span>, and thus QHotkey is reentrant and can be used on any thread. However, some operating systems might not support creating hotkeys from different threads. </p><p>To enable threading, check the box below and return to the playground. <span style=" font-style:italic;">Hotkey 4 and 5</span> will now each run on their own thread. </p></body></html> + <html><head/><body><p>This test was designed to try out multi-threaded shortcuts. The QHotkey class is completly <span style=" font-weight:600;">threadsafe</span>, but this test can help to see if it acutally works (It does).</p><p>If activated, <span style=" font-style:italic;">Hotkey 4 and Hotkey 5 </span>of the Playground will each run on their own thread. This means:</p><ul style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;"><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" text-decoration: underline;">Mainthread:</span> Hotkey 1, 2, 3</li><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" text-decoration: underline;">Second thread:</span> Hotkey 4</li><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" text-decoration: underline;">Third thread:</span> Hotkey 5</li></ul><p><span style=" font-weight:600;">Note:</span> The two hotkeys will be moved to the threads. For simplicity-reasons, you can't move them back in this test (But its possible, just not done here). Restart the test to get them back.</p></body></html> Qt::RichText @@ -950,26 +950,6 @@ - - - - <html><head/><body><p> -<b>Results so far:</b></br> -<ul> - <li><u>Windows:</u> Registering on multiple threads is possible, but: The thread that registered must unregister too and events will be send to the thread registered from, not the main thread.</li> - <li><u>Mac:</u> Works fine.</li> - <li><u>Linux:</u> Works fine.</li> -</ul><br/> -</p></body></html> - - - Qt::RichText - - - true - - - diff --git a/QHotkey/qhotkey.cpp b/QHotkey/qhotkey.cpp index 89bc036..4086779 100644 --- a/QHotkey/qhotkey.cpp +++ b/QHotkey/qhotkey.cpp @@ -3,10 +3,9 @@ #include #include #include +#include #include -#define LOCKER QMutexLocker locker(&this->mutex) - QHotkey::QHotkey(QObject *parent) : QObject(parent), key(Qt::Key_unknown), @@ -136,7 +135,6 @@ bool QHotkey::setRegistered(bool registered) // ---------- QHotkeyPrivate implementation ---------- QHotkeyPrivate::QHotkeyPrivate() : - mutex(QMutex::NonRecursive), shortcuts() { Q_ASSERT_X(qApp, Q_FUNC_INFO, "QHotkey requires QCoreApplication to be instantiated"); @@ -151,6 +149,21 @@ QHotkeyPrivate::~QHotkeyPrivate() qApp->eventDispatcher()->removeNativeEventFilter(this); } +QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) +{ + Qt::ConnectionType conType = (QThread::currentThread() == this->thread() ? + Qt::DirectConnection : + Qt::BlockingQueuedConnection); + QHotkey::NativeShortcut res; + if(!QMetaObject::invokeMethod(this, "nativeShortcutInvoked", conType, + Q_RETURN_ARG(QHotkey::NativeShortcut, res), + Q_ARG(Qt::Key, keycode), + Q_ARG(Qt::KeyboardModifiers, modifiers))) { + return QHotkey::NativeShortcut(); + } else + return res; +} + bool QHotkeyPrivate::hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) { return this->shortcuts.contains(this->nativeShortcut(keycode, modifiers)); @@ -160,9 +173,53 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey) { if(hotkey->registered) return false; + + Qt::ConnectionType conType = (QThread::currentThread() == this->thread() ? + Qt::DirectConnection : + Qt::BlockingQueuedConnection); + bool res = false; + if(!QMetaObject::invokeMethod(this, "addShortcutInvoked", conType, + Q_RETURN_ARG(bool, res), + Q_ARG(QHotkey*, hotkey))) { + return false; + } else { + if(res) + emit hotkey->registeredChanged(true); + return res; + } +} + +bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey) +{ + if(!hotkey->registered) + return false; + + Qt::ConnectionType conType = (QThread::currentThread() == this->thread() ? + Qt::DirectConnection : + Qt::BlockingQueuedConnection); + bool res = false; + if(!QMetaObject::invokeMethod(this, "removeShortcutInvoked", conType, + Q_RETURN_ARG(bool, res), + Q_ARG(QHotkey*, hotkey))) { + return false; + } else { + if(res) + emit hotkey->registeredChanged(false); + return res; + } +} + +void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut) +{ + QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::activated); + for(QHotkey *hkey : this->shortcuts.values(shortcut)) + signal.invoke(hkey, Qt::QueuedConnection); +} + +bool QHotkeyPrivate::addShortcutInvoked(QHotkey *hotkey) +{ QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut; - LOCKER; if(!this->shortcuts.contains(shortcut)) { if(!this->registerShortcut(shortcut)) return false; @@ -170,17 +227,13 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey) this->shortcuts.insert(shortcut, hotkey); hotkey->registered = true; - emit hotkey->registeredChanged(true); return true; } -bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey) +bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey) { - if(!hotkey->registered) - return false; QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut; - LOCKER; if(this->shortcuts.remove(shortcut, hotkey) == 0) return false; hotkey->registered = false; @@ -190,12 +243,3 @@ bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey) else return true; } - -void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut) -{ - LOCKER; - - QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::activated); - for(QHotkey *hkey : this->shortcuts.values(shortcut)) - signal.invoke(hkey, Qt::QueuedConnection); -} diff --git a/QHotkey/qhotkey_p.h b/QHotkey/qhotkey_p.h index 3d703d3..7d49163 100644 --- a/QHotkey/qhotkey_p.h +++ b/QHotkey/qhotkey_p.h @@ -7,8 +7,10 @@ #include #include -class QHotkeyPrivate : public QAbstractNativeEventFilter +class QHotkeyPrivate : public QObject, public QAbstractNativeEventFilter { + Q_OBJECT + public: QHotkeyPrivate();//singleton!!! ~QHotkeyPrivate(); @@ -16,7 +18,7 @@ public: static QHotkeyPrivate *instance(); static inline bool testValid(QHotkey::NativeShortcut nativeShortcut); - inline QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers); + QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers); bool hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers); @@ -32,8 +34,12 @@ protected: virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement +private:/*functions*/ + Q_INVOKABLE bool addShortcutInvoked(QHotkey *hotkey); + Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey); + Q_INVOKABLE inline QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers); + private: - QMutex mutex; QMultiHash shortcuts; }; @@ -50,7 +56,7 @@ inline bool QHotkeyPrivate::testValid(QHotkey::NativeShortcut nativeShortcut) return (nativeShortcut.first != 0); } -inline QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) { +inline QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers) { return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)}; }