summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qeventdispatcher_wasm_p.h
diff options
context:
space:
mode:
authorMorten Johan Sørvig <morten.sorvig@qt.io>2019-09-09 13:04:27 +0200
committerMorten Johan Sørvig <morten.sorvig@qt.io>2021-08-27 15:45:25 +0200
commitecb92aacab2820ecbd4237f0db0fa2311fec32d2 (patch)
treee5d8dcfc21799783ef4fc0c43697208b8537faaa /src/corelib/kernel/qeventdispatcher_wasm_p.h
parent82f14a95b5d2b41a47bc1ba65c2c9b880dd4accb (diff)
wasm: add new event dispatcher implementation
Add QEventDispatcherWasm to QtCore. The event dispatcher supports managing event queue wakeups and timers, both for the main thread or for secondary threads. Blocking in processEvents() (using QEventLoop::WaitForMoreEvents) is supported when running on a secondary thread, or on the main thread when Qt is built with Emscripten’s asyncify support. Code is shared for all both modes as far as possible, with breakout functions which handle main and secondary thread as well as asyncify specifics,. Some functions like wakeUp() can be called from any thread, and needs to take the calling thread into consideration as well. The current asyncify implementation in Emscripten is restricted to one level of suspend, and this restriction carries over to Qt as well. In practice this means we support one level of exec()-like API. Note that this commit does not _enable_ use of the new event dispatcher. This will be done in separate commits. Task-number: QTBUG-76007 Task-number: QTBUG-64020 Change-Id: I77dc9ba34bcff59ef05dd23a46dbf1873cbe6780 Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'src/corelib/kernel/qeventdispatcher_wasm_p.h')
-rw-r--r--src/corelib/kernel/qeventdispatcher_wasm_p.h125
1 files changed, 125 insertions, 0 deletions
diff --git a/src/corelib/kernel/qeventdispatcher_wasm_p.h b/src/corelib/kernel/qeventdispatcher_wasm_p.h
new file mode 100644
index 0000000000..2fcd512773
--- /dev/null
+++ b/src/corelib/kernel/qeventdispatcher_wasm_p.h
@@ -0,0 +1,125 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QEVENTDISPATCHER_WASM_P_H
+#define QEVENTDISPATCHER_WASM_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "qabstracteventdispatcher.h"
+#include "private/qtimerinfo_unix_p.h"
+#include <QtCore/qloggingcategory.h>
+#include <QtCore/qwaitcondition.h>
+
+#include <mutex>
+#include <optional>
+#include <tuple>
+
+QT_BEGIN_NAMESPACE
+
+Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcher);
+Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcherTimers)
+
+class Q_CORE_EXPORT QEventDispatcherWasm : public QAbstractEventDispatcher
+{
+ Q_OBJECT
+public:
+ QEventDispatcherWasm();
+ ~QEventDispatcherWasm();
+
+ bool processEvents(QEventLoop::ProcessEventsFlags flags) override;
+
+ void registerSocketNotifier(QSocketNotifier *notifier) override;
+ void unregisterSocketNotifier(QSocketNotifier *notifier) override;
+
+ void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) override;
+ bool unregisterTimer(int timerId) override;
+ bool unregisterTimers(QObject *object) override;
+ QList<QAbstractEventDispatcher::TimerInfo> registeredTimers(QObject *object) const override;
+ int remainingTime(int timerId) override;
+
+ void interrupt() override;
+ void wakeUp() override;
+
+private:
+ bool isMainThreadEventDispatcher();
+ bool isSecondaryThreadEventDispatcher();
+
+ void pollForNativeEvents();
+ bool waitForForEvents();
+ static void callProcessEvents(void *eventDispatcher);
+
+ void processTimers();
+ void updateNativeTimer();
+ static void callProcessTimers(void *eventDispatcher);
+
+#if QT_CONFIG(thread)
+ void runOnMainThread(std::function<void(void)> fn);
+#endif
+
+ static QEventDispatcherWasm *g_mainThreadEventDispatcher;
+
+ bool m_interrupted = false;
+ bool m_processTimers = false;
+ bool m_pendingProcessEvents = false;
+
+ QTimerInfoList *m_timerInfo = new QTimerInfoList();
+ long m_timerId = 0;
+ uint64_t m_timerTargetTime = 0;
+
+#if QT_CONFIG(thread)
+ std::mutex m_mutex;
+ bool m_wakeUpCalled = false;
+ std::condition_variable m_moreEvents;
+
+ static QVector<QEventDispatcherWasm *> g_secondaryThreadEventDispatchers;
+ static std::mutex g_secondaryThreadEventDispatchersMutex;
+#endif
+};
+
+#endif // QEVENTDISPATCHER_WASM_P_H