Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
qeventdispatcher_wasm_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QEVENTDISPATCHER_WASM_P_H
5#define QEVENTDISPATCHER_WASM_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
19#include "private/qtimerinfo_unix_p.h"
20#include <QtCore/qloggingcategory.h>
21#include <QtCore/qwaitcondition.h>
22
23#include <chrono>
24#include <mutex>
25#include <optional>
26#include <tuple>
27
28#include <emscripten/proxying.h>
29
31
32Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcher);
33Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcherTimers)
34
36{
38public:
41
42 bool processEvents(QEventLoop::ProcessEventsFlags flags) override;
43
44 void registerSocketNotifier(QSocketNotifier *notifier) override;
45 void unregisterSocketNotifier(QSocketNotifier *notifier) override;
46
47 void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) override;
48 bool unregisterTimer(int timerId) override;
49 bool unregisterTimers(QObject *object) override;
50 QList<QAbstractEventDispatcher::TimerInfo> registeredTimers(QObject *object) const override;
51 int remainingTime(int timerId) override;
52
53 void interrupt() override;
54 void wakeUp() override;
55
56 static void socketSelect(int timeout, int socket, bool waitForRead, bool waitForWrite,
57 bool *selectForRead, bool *selectForWrite, bool *socketDisconnect);
58protected:
59 virtual bool processPostedEvents();
60
61private:
62 bool isMainThreadEventDispatcher();
63 bool isSecondaryThreadEventDispatcher();
64 static bool isValidEventDispatcherPointer(QEventDispatcherWasm *eventDispatcher);
65
66 void handleApplicationExec();
67 void handleDialogExec();
68 bool wait(int timeout = -1);
69 bool wakeEventDispatcherThread();
70 static void callProcessPostedEvents(void *eventDispatcher);
71
72 void processTimers();
73 void updateNativeTimer();
74 static void callProcessTimers(void *eventDispatcher);
75
76 static void setEmscriptenSocketCallbacks();
77 static void clearEmscriptenSocketCallbacks();
78 static void socketError(int fd, int err, const char* msg, void *context);
79 static void socketOpen(int fd, void *context);
80 static void socketListen(int fd, void *context);
81 static void socketConnection(int fd, void *context);
82 static void socketMessage(int fd, void *context);
83 static void socketClose(int fd, void *context);
84
85 static void setSocketState(int socket, bool setReadyRead, bool setReadyWrite);
86 static void clearSocketState(int socket);
87 void waitForSocketState(int timeout, int socket, bool checkRead, bool checkWrite,
88 bool *selectForRead, bool *selectForWrite, bool *socketDisconnect);
89
90 static void run(std::function<void(void)> fn);
91 static void runAsync(std::function<void(void)> fn);
92 static void runOnMainThread(std::function<void(void)> fn);
93 static void runOnMainThreadAsync(std::function<void(void)> fn);
94
95 static QEventDispatcherWasm *g_mainThreadEventDispatcher;
96
97 bool m_interrupted = false;
98 bool m_processTimers = false;
99 bool m_pendingProcessEvents = false;
100
101 QTimerInfoList *m_timerInfo = new QTimerInfoList();
102 long m_timerId = 0;
103 std::chrono::milliseconds m_timerTargetTime{};
104
105#if QT_CONFIG(thread)
106 std::mutex m_mutex;
107 bool m_wakeUpCalled = false;
108 std::condition_variable m_moreEvents;
109
110 static QVector<QEventDispatcherWasm *> g_secondaryThreadEventDispatchers;
111 static std::mutex g_staticDataMutex;
112 static emscripten::ProxyingQueue g_proxyingQueue;
113 static pthread_t g_mainThread;
114
115 // Note on mutex usage: the global g_staticDataMutex protects the global (g_ prefixed) data,
116 // while the per eventdispatcher m_mutex protects the state accociated with blocking and waking
117 // that eventdispatcher thread. The locking order is g_staticDataMutex first, then m_mutex.
118#endif
119
120 static std::multimap<int, QSocketNotifier *> g_socketNotifiers;
121
122 struct SocketReadyState {
123 QEventDispatcherWasm *waiter = nullptr;
124 bool waitForReadyRead = false;
125 bool waitForReadyWrite = false;
126 bool readyRead = false;
127 bool readyWrite = false;
128 };
129 static std::map<int, SocketReadyState> g_socketState;
130};
131
132#endif // QEVENTDISPATCHER_WASM_P_H
DarwinBluetooth::LECBManagerNotifier * notifier
Definition qlist.h:74
\inmodule QtCore
Definition qobject.h:90
\inmodule QtCore
Combined button and popup list for selecting options.
TimerType
static void * context
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLbitfield GLuint64 timeout
[4]
GLbitfield flags
GLuint64 GLenum GLint fd
#define Q_OBJECT
long long qint64
Definition qtypes.h:55
device waitForReadyRead(deadline)
QTcpSocket * socket
[1]
socketLayer waitForWrite()