diff options
author | Morten Johan Sørvig <morten.sorvig@qt.io> | 2018-07-04 00:20:37 +0200 |
---|---|---|
committer | Morten Johan Sørvig <morten.sorvig@qt.io> | 2018-08-09 07:24:15 +0000 |
commit | a09c8b0ee6ddd13251ee1335ede73fd66e007a35 (patch) | |
tree | 9c7cb2af31892edc16b696c86bde948152b8cb2f | |
parent | 489ae6db3c1d4c78b60f67d1c64e6bdb820560eb (diff) |
Make QEventDispatcherCoreFoundaton work on secondary threads
We were using CFRunLoopGetMain() everywhere. Get the
correct run loop using CFRunLoopGetCurrent() during
initialization, and store it.
Event dispatcher initialization must now be delayed
until after the constructor has run, since event dispatchers
may be created on the main thread and then moved to
the target thread. Initialize on first call to processEvents()
where the current thread will be the correct thread.
Use the stored m_runLoop instead of CFRunLoopGetMain().
This is required for wakeUp() and interrupt() which
may be called from another thread.
Change-Id: I6fffcfd4394899c4a12f241c42781979aaf99d5e
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
-rw-r--r-- | src/corelib/kernel/qcfsocketnotifier.cpp | 6 | ||||
-rw-r--r-- | src/corelib/kernel/qeventdispatcher_cf.mm | 23 | ||||
-rw-r--r-- | src/corelib/kernel/qeventdispatcher_cf_p.h | 12 | ||||
-rw-r--r-- | src/plugins/platforms/ios/qioseventdispatcher.mm | 5 |
4 files changed, 30 insertions, 16 deletions
diff --git a/src/corelib/kernel/qcfsocketnotifier.cpp b/src/corelib/kernel/qcfsocketnotifier.cpp index 1fee2aa5fc..920ec9cd86 100644 --- a/src/corelib/kernel/qcfsocketnotifier.cpp +++ b/src/corelib/kernel/qcfsocketnotifier.cpp @@ -86,7 +86,7 @@ CFRunLoopSourceRef qt_mac_add_socket_to_runloop(const CFSocketRef socket) if (!loopSource) return 0; - CFRunLoopAddSource(CFRunLoopGetMain(), loopSource, kCFRunLoopCommonModes); + CFRunLoopAddSource(CFRunLoopGetCurrent(), loopSource, kCFRunLoopCommonModes); return loopSource; } @@ -96,7 +96,7 @@ CFRunLoopSourceRef qt_mac_add_socket_to_runloop(const CFSocketRef socket) void qt_mac_remove_socket_from_runloop(const CFSocketRef socket, CFRunLoopSourceRef runloop) { Q_ASSERT(runloop); - CFRunLoopRemoveSource(CFRunLoopGetMain(), runloop, kCFRunLoopCommonModes); + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runloop, kCFRunLoopCommonModes); CFSocketDisableCallBacks(socket, kCFSocketReadCallBack); CFSocketDisableCallBacks(socket, kCFSocketWriteCallBack); } @@ -188,7 +188,7 @@ void QCFSocketNotifier::registerSocketNotifier(QSocketNotifier *notifier) enableNotifiersObserver = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeSources, true, 0, enableSocketNotifiers, &context); Q_ASSERT(enableNotifiersObserver); - CFRunLoopAddObserver(CFRunLoopGetMain(), enableNotifiersObserver, kCFRunLoopCommonModes); + CFRunLoopAddObserver(CFRunLoopGetCurrent(), enableNotifiersObserver, kCFRunLoopCommonModes); } } diff --git a/src/corelib/kernel/qeventdispatcher_cf.mm b/src/corelib/kernel/qeventdispatcher_cf.mm index 73eeaba03f..8881305b18 100644 --- a/src/corelib/kernel/qeventdispatcher_cf.mm +++ b/src/corelib/kernel/qeventdispatcher_cf.mm @@ -209,8 +209,16 @@ QEventDispatcherCoreFoundation::QEventDispatcherCoreFoundation(QObject *parent) , m_blockedRunLoopTimer(0) , m_overdueTimerScheduled(false) { - m_cfSocketNotifier.setHostEventDispatcher(this); +} + +void QEventDispatcherCoreFoundation::startingUp() +{ + // The following code must run on the event dispatcher thread, so that + // CFRunLoopGetCurrent() returns the correct run loop. + Q_ASSERT(QThread::currentThread() == thread()); + m_runLoop = QCFType<CFRunLoopRef>::constructFromGet(CFRunLoopGetCurrent()); + m_cfSocketNotifier.setHostEventDispatcher(this); m_postedEventsRunLoopSource.addToMode(kCFRunLoopCommonModes); m_runLoopActivityObserver.addToMode(kCFRunLoopCommonModes); } @@ -252,6 +260,7 @@ QEventLoop *QEventDispatcherCoreFoundation::currentEventLoop() const bool QEventDispatcherCoreFoundation::processEvents(QEventLoop::ProcessEventsFlags flags) { QT_APPLE_SCOPED_LOG_ACTIVITY(lcEventDispatcher().isDebugEnabled(), "processEvents"); + bool eventsProcessed = false; if (flags & (QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers)) @@ -413,7 +422,8 @@ bool QEventDispatcherCoreFoundation::processPostedEvents() m_processEvents.processedPostedEvents = true; - qCDebug(lcEventDispatcher) << "Sending posted events for" << m_processEvents.flags; + qCDebug(lcEventDispatcher) << "Sending posted events for" + << QEventLoop::ProcessEventsFlags(m_processEvents.flags.load()); QCoreApplication::sendPostedEvents(); return true; @@ -496,7 +506,7 @@ bool QEventDispatcherCoreFoundation::hasPendingEvents() // 'maybeHasPendingEvents' in our case. extern uint qGlobalPostedEventsCount(); - return qGlobalPostedEventsCount() || !CFRunLoopIsWaiting(CFRunLoopGetMain()); + return qGlobalPostedEventsCount() || !CFRunLoopIsWaiting(m_runLoop); } void QEventDispatcherCoreFoundation::wakeUp() @@ -516,7 +526,8 @@ void QEventDispatcherCoreFoundation::wakeUp() } m_postedEventsRunLoopSource.signal(); - CFRunLoopWakeUp(CFRunLoopGetMain()); + if (m_runLoop) + CFRunLoopWakeUp(m_runLoop); qCDebug(lcEventDispatcher) << "Signaled posted event run-loop source"; } @@ -525,7 +536,7 @@ void QEventDispatcherCoreFoundation::interrupt() { qCDebug(lcEventDispatcher) << "Marking current processEvent as interrupted"; m_processEvents.wasInterrupted = true; - CFRunLoopStop(CFRunLoopGetMain()); + CFRunLoopStop(m_runLoop); } void QEventDispatcherCoreFoundation::flush() @@ -620,7 +631,7 @@ void QEventDispatcherCoreFoundation::updateTimers() processTimers(timer); }); - CFRunLoopAddTimer(CFRunLoopGetMain(), m_runLoopTimer, kCFRunLoopCommonModes); + CFRunLoopAddTimer(m_runLoop, m_runLoopTimer, kCFRunLoopCommonModes); qCDebug(lcEventDispatcherTimers) << "Created new CFRunLoopTimer" << m_runLoopTimer; } else { diff --git a/src/corelib/kernel/qeventdispatcher_cf_p.h b/src/corelib/kernel/qeventdispatcher_cf_p.h index ac9170da89..26191d520c 100644 --- a/src/corelib/kernel/qeventdispatcher_cf_p.h +++ b/src/corelib/kernel/qeventdispatcher_cf_p.h @@ -208,6 +208,7 @@ class Q_CORE_EXPORT QEventDispatcherCoreFoundation : public QAbstractEventDispat public: explicit QEventDispatcherCoreFoundation(QObject *parent = 0); + void startingUp() override; ~QEventDispatcherCoreFoundation(); bool processEvents(QEventLoop::ProcessEventsFlags flags) override; @@ -239,11 +240,11 @@ protected: , processedPostedEvents(false), processedTimers(false) , deferredWakeUp(false), deferredUpdateTimers(false) {} - QEventLoop::ProcessEventsFlags flags; - bool wasInterrupted; - bool processedPostedEvents; - bool processedTimers; - bool deferredWakeUp; + QAtomicInt flags; + QAtomicInteger<char> wasInterrupted; + QAtomicInteger<char> processedPostedEvents; + QAtomicInteger<char> processedTimers; + QAtomicInteger<char> deferredWakeUp; bool deferredUpdateTimers; }; @@ -258,6 +259,7 @@ private: QTimerInfoList m_timerInfoList; CFRunLoopTimerRef m_runLoopTimer; CFRunLoopTimerRef m_blockedRunLoopTimer; + QCFType<CFRunLoopRef> m_runLoop; bool m_overdueTimerScheduled; QCFSocketNotifier m_cfSocketNotifier; diff --git a/src/plugins/platforms/ios/qioseventdispatcher.mm b/src/plugins/platforms/ios/qioseventdispatcher.mm index 6a6e1bd618..776343c5aa 100644 --- a/src/plugins/platforms/ios/qioseventdispatcher.mm +++ b/src/plugins/platforms/ios/qioseventdispatcher.mm @@ -471,8 +471,9 @@ bool QIOSEventDispatcher::processPostedEvents() return false; QT_APPLE_SCOPED_LOG_ACTIVITY(lcEventDispatcher().isDebugEnabled(), "sendWindowSystemEvents"); - qCDebug(lcEventDispatcher) << "Sending window system events for" << m_processEvents.flags; - QWindowSystemInterface::sendWindowSystemEvents(m_processEvents.flags); + QEventLoop::ProcessEventsFlags flags = QEventLoop::ProcessEventsFlags(m_processEvents.flags.load()); + qCDebug(lcEventDispatcher) << "Sending window system events for" << flags; + QWindowSystemInterface::sendWindowSystemEvents(flags); return true; } |