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
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 "private/qwasmsuspendresumecontrol_p.h"
21#include <QtCore/qloggingcategory.h>
22#include <QtCore/qwaitcondition.h>
23
24#include <chrono>
25#include <mutex>
26#include <optional>
27#include <tuple>
28#include <memory>
29
31
33Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcherTimers)
34
35class Q_CORE_EXPORT QEventDispatcherWasm : public QAbstractEventDispatcherV2
36{
37 Q_OBJECT
38public:
39 QEventDispatcherWasm(std::shared_ptr<QWasmSuspendResumeControl> suspendResume = std::shared_ptr<QWasmSuspendResumeControl>());
40 ~QEventDispatcherWasm();
41
42 bool processEvents(QEventLoop::ProcessEventsFlags flags) override;
43 bool sendAllEvents(QEventLoop::ProcessEventsFlags flag);
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 registerStartupTask();
61 static void completeStarupTask();
62 static void callOnLoadedIfRequired();
63 virtual void onLoaded();
64
65 static void onTimer();
66 static void onWakeup();
67 static void onProcessNativeEventsResume();
68protected:
69 virtual bool sendPostedEvents();
70
71private:
72 bool isMainThreadEventDispatcher();
73 bool isSecondaryThreadEventDispatcher();
74 bool isValidEventDispatcher();
75 static bool isValidEventDispatcherPointer(QEventDispatcherWasm *eventDispatcher);
76
77 bool sendTimerEvents();
78 bool sendNativeEvents(QEventLoop::ProcessEventsFlags flags);
79
80 void handleNonAsyncifyErrorCases(QEventLoop::ProcessEventsFlags flags);
81
82 bool wait(int timeout);
83 void processEventsWait();
84 void asyncifyWait(std::optional<std::chrono::milliseconds> timeout);
85 bool secondaryThreadWait(std::optional<std::chrono::milliseconds> timeout);
86
87 void updateNativeTimer();
88
89 static QEventDispatcherWasm *g_mainThreadEventDispatcher;
90 static std::shared_ptr<QWasmSuspendResumeControl> g_mainThreadSuspendResumeControl;
91
92 bool m_interrupted = false;
93 bool m_wakeup = false;
94
95 std::unique_ptr<QTimerInfoList> m_timerInfo;
96 std::chrono::time_point<std::chrono::steady_clock> m_timerTargetTime;
97
98 std::unique_ptr<QWasmTimer> m_nativeTimer;
99 std::unique_ptr<QWasmTimer> m_wakeupTimer;
100 std::unique_ptr<QWasmTimer> m_suspendTimer;
101
102 bool m_wakeFromSuspendTimer = false;
103 bool m_isSendingNativeEvents = false;
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
113 // Note on mutex usage: the global g_staticDataMutex protects the global (g_ prefixed) data,
114 // while the per eventdispatcher m_mutex protects the state accociated with blocking and waking
115 // that eventdispatcher thread. The locking order is g_staticDataMutex first, then m_mutex.
116#endif
117
118 friend class QWasmSocket;
119};
120
121#endif // QEVENTDISPATCHER_WASM_P_H
static bool useAsyncify()
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcher)