Threadsafety

Now completly working on all platforms
This commit is contained in:
Skycoder42
2016-02-07 17:32:12 +01:00
parent 83cfcb058d
commit d769b95b09
4 changed files with 79 additions and 43 deletions
+6
View File
@@ -200,4 +200,10 @@ void HotTestWidget::on_threadEnableCheckBox_clicked()
this->hotkey_4->moveToThread(this->thread4); this->hotkey_4->moveToThread(this->thread4);
this->hotkey_5->moveToThread(this->thread5); 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);
} }
+1 -21
View File
@@ -927,7 +927,7 @@
<item> <item>
<widget class="QLabel" name="label_7"> <widget class="QLabel" name="label_7">
<property name="text"> <property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Use this test to determine whether shortcuts on multiple threads are supported or not. Internally, the backend of the QHotkey class is &lt;span style=&quot; font-weight:600;&quot;&gt;threadsafe&lt;/span&gt;, and thus QHotkey is reentrant and can be used on any thread. However, some operating systems might not support creating hotkeys from different threads. &lt;/p&gt;&lt;p&gt;To enable threading, check the box below and return to the playground. &lt;span style=&quot; font-style:italic;&quot;&gt;Hotkey 4 and 5&lt;/span&gt; will now each run on their own thread. &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;This test was designed to try out multi-threaded shortcuts. The QHotkey class is completly &lt;span style=&quot; font-weight:600;&quot;&gt;threadsafe&lt;/span&gt;, but this test can help to see if it acutally works (It does).&lt;/p&gt;&lt;p&gt;If activated, &lt;span style=&quot; font-style:italic;&quot;&gt;Hotkey 4 and Hotkey 5 &lt;/span&gt;of the Playground will each run on their own thread. This means:&lt;/p&gt;&lt;ul style=&quot;margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;&quot;&gt;&lt;li style=&quot; margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; text-decoration: underline;&quot;&gt;Mainthread:&lt;/span&gt; Hotkey 1, 2, 3&lt;/li&gt;&lt;li style=&quot; margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; text-decoration: underline;&quot;&gt;Second thread:&lt;/span&gt; Hotkey 4&lt;/li&gt;&lt;li style=&quot; margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; text-decoration: underline;&quot;&gt;Third thread:&lt;/span&gt; Hotkey 5&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;Note:&lt;/span&gt; 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.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property> </property>
<property name="textFormat"> <property name="textFormat">
<enum>Qt::RichText</enum> <enum>Qt::RichText</enum>
@@ -950,26 +950,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QLabel" name="label_8">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;
&lt;b&gt;Results so far:&lt;/b&gt;&lt;/br&gt;
&lt;ul&gt;
&lt;li&gt;&lt;u&gt;Windows:&lt;/u&gt; 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.&lt;/li&gt;
&lt;li&gt;&lt;u&gt;Mac:&lt;/u&gt; Works fine.&lt;/li&gt;
&lt;li&gt;&lt;u&gt;Linux:&lt;/u&gt; Works fine.&lt;/li&gt;
&lt;/ul&gt;&lt;br/&gt;
&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item> <item>
<spacer name="verticalSpacer_7"> <spacer name="verticalSpacer_7">
<property name="orientation"> <property name="orientation">
+62 -18
View File
@@ -3,10 +3,9 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QAbstractEventDispatcher> #include <QAbstractEventDispatcher>
#include <QMetaMethod> #include <QMetaMethod>
#include <QThread>
#include <QDebug> #include <QDebug>
#define LOCKER QMutexLocker locker(&this->mutex)
QHotkey::QHotkey(QObject *parent) : QHotkey::QHotkey(QObject *parent) :
QObject(parent), QObject(parent),
key(Qt::Key_unknown), key(Qt::Key_unknown),
@@ -136,7 +135,6 @@ bool QHotkey::setRegistered(bool registered)
// ---------- QHotkeyPrivate implementation ---------- // ---------- QHotkeyPrivate implementation ----------
QHotkeyPrivate::QHotkeyPrivate() : QHotkeyPrivate::QHotkeyPrivate() :
mutex(QMutex::NonRecursive),
shortcuts() shortcuts()
{ {
Q_ASSERT_X(qApp, Q_FUNC_INFO, "QHotkey requires QCoreApplication to be instantiated"); Q_ASSERT_X(qApp, Q_FUNC_INFO, "QHotkey requires QCoreApplication to be instantiated");
@@ -151,6 +149,21 @@ QHotkeyPrivate::~QHotkeyPrivate()
qApp->eventDispatcher()->removeNativeEventFilter(this); 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) bool QHotkeyPrivate::hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
{ {
return this->shortcuts.contains(this->nativeShortcut(keycode, modifiers)); return this->shortcuts.contains(this->nativeShortcut(keycode, modifiers));
@@ -160,9 +173,53 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
{ {
if(hotkey->registered) if(hotkey->registered)
return false; 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; QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut;
LOCKER;
if(!this->shortcuts.contains(shortcut)) { if(!this->shortcuts.contains(shortcut)) {
if(!this->registerShortcut(shortcut)) if(!this->registerShortcut(shortcut))
return false; return false;
@@ -170,17 +227,13 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
this->shortcuts.insert(shortcut, hotkey); this->shortcuts.insert(shortcut, hotkey);
hotkey->registered = true; hotkey->registered = true;
emit hotkey->registeredChanged(true);
return true; return true;
} }
bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey) bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
{ {
if(!hotkey->registered)
return false;
QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut; QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut;
LOCKER;
if(this->shortcuts.remove(shortcut, hotkey) == 0) if(this->shortcuts.remove(shortcut, hotkey) == 0)
return false; return false;
hotkey->registered = false; hotkey->registered = false;
@@ -190,12 +243,3 @@ bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
else else
return true; 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);
}
+10 -4
View File
@@ -7,8 +7,10 @@
#include <QMutex> #include <QMutex>
#include <QGlobalStatic> #include <QGlobalStatic>
class QHotkeyPrivate : public QAbstractNativeEventFilter class QHotkeyPrivate : public QObject, public QAbstractNativeEventFilter
{ {
Q_OBJECT
public: public:
QHotkeyPrivate();//singleton!!! QHotkeyPrivate();//singleton!!!
~QHotkeyPrivate(); ~QHotkeyPrivate();
@@ -16,7 +18,7 @@ public:
static QHotkeyPrivate *instance(); static QHotkeyPrivate *instance();
static inline bool testValid(QHotkey::NativeShortcut nativeShortcut); 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); bool hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
@@ -32,8 +34,12 @@ protected:
virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
virtual bool unregisterShortcut(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: private:
QMutex mutex;
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts; QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
}; };
@@ -50,7 +56,7 @@ inline bool QHotkeyPrivate::testValid(QHotkey::NativeShortcut nativeShortcut)
return (nativeShortcut.first != 0); 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)}; return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)};
} }