Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
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// Qt-Security score:significant reason:default
4
5#ifndef QEVENTDISPATCHER_WASM_P_H
6#define QEVENTDISPATCHER_WASM_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
20#include "private/qtimerinfo_unix_p.h"
21#include "private/qwasmsuspendresumecontrol_p.h"
22#include <QtCore/qloggingcategory.h>
23#include <QtCore/qwaitcondition.h>
24
25#include <chrono>
26#include <mutex>
27#include <optional>
28#include <tuple>
29#include <memory>
30
32
34Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcherTimers)
35
36class Q_CORE_EXPORT QEventDispatcherWasm : public QAbstractEventDispatcherV2
37{
38 Q_OBJECT
39public:
40 QEventDispatcherWasm(std::shared_ptr<QWasmSuspendResumeControl> suspendResume = std::shared_ptr<QWasmSuspendResumeControl>());
41 ~QEventDispatcherWasm();
42
43 bool processEvents(QEventLoop::ProcessEventsFlags flags) override;
44
45 void registerTimer(Qt::TimerId timerId, Duration interval, Qt::TimerType timerType,
46 QObject *object) override final;
47 bool unregisterTimer(Qt::TimerId timerId) override final;
48 bool unregisterTimers(QObject *object) override final;
49 QList<TimerInfoV2> timersForObject(QObject *object) const override final;
50 Duration remainingTime(Qt::TimerId timerId) const override final;
51
52 void interrupt() override;
53 void wakeUp() override;
54
55 void registerSocketNotifier(QSocketNotifier *notifier) override;
56 void unregisterSocketNotifier(QSocketNotifier *notifier) override;
57 static void socketSelect(int timeout, int socket, bool waitForRead, bool waitForWrite,
58 bool *selectForRead, bool *selectForWrite, bool *socketDisconnect);
59
60 static void onTimer();
61 static void onWakeup();
62 static void onProcessNativeEventsResume();
63protected:
64 virtual bool sendPostedEvents();
65
66private:
67 bool sendAllEvents(QEventLoop::ProcessEventsFlags flag);
68 bool isMainThreadEventDispatcher();
69 bool isSecondaryThreadEventDispatcher();
70 bool isValidEventDispatcher();
71 static bool isValidEventDispatcherPointer(QEventDispatcherWasm *eventDispatcher);
72
73 bool sendTimerEvents();
74 bool sendNativeEvents(QEventLoop::ProcessEventsFlags flags);
75
76 void handleNonAsyncifyErrorCases(QEventLoop::ProcessEventsFlags flags);
77
78 bool wait(int timeout);
79 void processEventsWait();
80 void asyncifyWait(std::optional<std::chrono::milliseconds> timeout);
81 bool secondaryThreadWait(std::optional<std::chrono::milliseconds> timeout);
82
83 void updateNativeTimer();
84
85 static QEventDispatcherWasm *g_mainThreadEventDispatcher;
86 static std::shared_ptr<QWasmSuspendResumeControl> g_customMainThreadSuspendResumeControl;
87 static QWasmSuspendResumeControl *g_mainThreadSuspendResumeControl;
88 bool m_interrupted = false;
89 bool m_wakeup = false;
90
91 std::unique_ptr<QTimerInfoList> m_timerInfo;
92 std::chrono::time_point<std::chrono::steady_clock> m_timerTargetTime;
93
94 std::unique_ptr<QWasmTimer> m_nativeTimer;
95 std::unique_ptr<QWasmTimer> m_wakeupTimer;
96 std::unique_ptr<QWasmTimer> m_suspendTimer;
97
98 bool m_wakeFromSuspendTimer = false;
99 bool m_isSendingNativeEvents = false;
100
101#if QT_CONFIG(thread)
102 std::mutex m_mutex;
103 bool m_wakeUpCalled = false;
104 std::condition_variable m_moreEvents;
105
106 static QVector<QEventDispatcherWasm *> g_secondaryThreadEventDispatchers;
107 static std::mutex g_staticDataMutex;
108
109 // Note on mutex usage: the global g_staticDataMutex protects the global (g_ prefixed) data,
110 // while the per eventdispatcher m_mutex protects the state accociated with blocking and waking
111 // that eventdispatcher thread. The locking order is g_staticDataMutex first, then m_mutex.
112#endif
113
114 friend class QWasmSocket;
115};
116
117#endif // QEVENTDISPATCHER_WASM_P_H
static bool useAsyncify()
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcQIORing)