1 | // Copyright (C) 2016 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_UNIX_P_H |
5 | #define QEVENTDISPATCHER_UNIX_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 | |
18 | #include "QtCore/qabstracteventdispatcher.h" |
19 | #include "QtCore/qlist.h" |
20 | #include "private/qabstracteventdispatcher_p.h" |
21 | #include "private/qcore_unix_p.h" |
22 | #include "QtCore/qvarlengtharray.h" |
23 | #include "QtCore/qhash.h" |
24 | #include "private/qtimerinfo_unix_p.h" |
25 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | class QEventDispatcherUNIXPrivate; |
29 | |
30 | struct Q_CORE_EXPORT QSocketNotifierSetUNIX final |
31 | { |
32 | inline QSocketNotifierSetUNIX() noexcept; |
33 | |
34 | inline bool isEmpty() const noexcept; |
35 | inline short events() const noexcept; |
36 | |
37 | QSocketNotifier *notifiers[3]; |
38 | }; |
39 | |
40 | Q_DECLARE_TYPEINFO(QSocketNotifierSetUNIX, Q_PRIMITIVE_TYPE); |
41 | |
42 | struct QThreadPipe |
43 | { |
44 | QThreadPipe(); |
45 | ~QThreadPipe(); |
46 | |
47 | bool init(); |
48 | pollfd prepare() const; |
49 | |
50 | void wakeUp(); |
51 | int check(const pollfd &pfd); |
52 | |
53 | // note for eventfd(7) support: |
54 | // if fds[1] is -1, then eventfd(7) is in use and is stored in fds[0] |
55 | int fds[2]; |
56 | QAtomicInt wakeUps; |
57 | |
58 | #if defined(Q_OS_VXWORKS) |
59 | static const int len_name = 20; |
60 | char name[len_name]; |
61 | #endif |
62 | }; |
63 | |
64 | class Q_CORE_EXPORT QEventDispatcherUNIX : public QAbstractEventDispatcher |
65 | { |
66 | Q_OBJECT |
67 | Q_DECLARE_PRIVATE(QEventDispatcherUNIX) |
68 | |
69 | public: |
70 | explicit QEventDispatcherUNIX(QObject *parent = nullptr); |
71 | ~QEventDispatcherUNIX(); |
72 | |
73 | bool processEvents(QEventLoop::ProcessEventsFlags flags) override; |
74 | |
75 | void registerSocketNotifier(QSocketNotifier *notifier) final; |
76 | void unregisterSocketNotifier(QSocketNotifier *notifier) final; |
77 | |
78 | void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) final; |
79 | bool unregisterTimer(int timerId) final; |
80 | bool unregisterTimers(QObject *object) final; |
81 | QList<TimerInfo> registeredTimers(QObject *object) const final; |
82 | |
83 | int remainingTime(int timerId) final; |
84 | |
85 | void wakeUp() override; |
86 | void interrupt() final; |
87 | |
88 | protected: |
89 | QEventDispatcherUNIX(QEventDispatcherUNIXPrivate &dd, QObject *parent = nullptr); |
90 | }; |
91 | |
92 | class Q_CORE_EXPORT QEventDispatcherUNIXPrivate : public QAbstractEventDispatcherPrivate |
93 | { |
94 | Q_DECLARE_PUBLIC(QEventDispatcherUNIX) |
95 | |
96 | public: |
97 | QEventDispatcherUNIXPrivate(); |
98 | ~QEventDispatcherUNIXPrivate(); |
99 | |
100 | int activateTimers(); |
101 | |
102 | void markPendingSocketNotifiers(); |
103 | int activateSocketNotifiers(); |
104 | void setSocketNotifierPending(QSocketNotifier *notifier); |
105 | |
106 | QThreadPipe threadPipe; |
107 | QList<pollfd> pollfds; |
108 | |
109 | QHash<int, QSocketNotifierSetUNIX> socketNotifiers; |
110 | QList<QSocketNotifier *> pendingNotifiers; |
111 | |
112 | QTimerInfoList timerList; |
113 | QAtomicInt interrupt; // bool |
114 | }; |
115 | |
116 | inline QSocketNotifierSetUNIX::QSocketNotifierSetUNIX() noexcept |
117 | { |
118 | notifiers[0] = nullptr; |
119 | notifiers[1] = nullptr; |
120 | notifiers[2] = nullptr; |
121 | } |
122 | |
123 | inline bool QSocketNotifierSetUNIX::isEmpty() const noexcept |
124 | { |
125 | return !notifiers[0] && !notifiers[1] && !notifiers[2]; |
126 | } |
127 | |
128 | inline short QSocketNotifierSetUNIX::events() const noexcept |
129 | { |
130 | short result = 0; |
131 | |
132 | if (notifiers[0]) |
133 | result |= POLLIN; |
134 | |
135 | if (notifiers[1]) |
136 | result |= POLLOUT; |
137 | |
138 | if (notifiers[2]) |
139 | result |= POLLPRI; |
140 | |
141 | return result; |
142 | } |
143 | |
144 | QT_END_NAMESPACE |
145 | |
146 | #endif // QEVENTDISPATCHER_UNIX_P_H |
147 |