1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QEVENTDISPATCHER_UNIX_P_H
41 #define QEVENTDISPATCHER_UNIX_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include "QtCore/qabstracteventdispatcher.h"
55 #include "QtCore/qlist.h"
56 #include "private/qabstracteventdispatcher_p.h"
57 #include "private/qcore_unix_p.h"
58 #include "QtCore/qvarlengtharray.h"
59 #include "private/qtimerinfo_unix_p.h"
60 
61 QT_BEGIN_NAMESPACE
62 
63 class QEventDispatcherUNIXPrivate;
64 
65 struct Q_CORE_EXPORT QSocketNotifierSetUNIX final
66 {
67     inline QSocketNotifierSetUNIX() noexcept;
68 
69     inline bool isEmpty() const noexcept;
70     inline short events() const noexcept;
71 
72     QSocketNotifier *notifiers[3];
73 };
74 
75 Q_DECLARE_TYPEINFO(QSocketNotifierSetUNIX, Q_PRIMITIVE_TYPE);
76 
77 struct QThreadPipe
78 {
79     QThreadPipe();
80     ~QThreadPipe();
81 
82     bool init();
83     pollfd prepare() const;
84 
85     void wakeUp();
86     int check(const pollfd &pfd);
87 
88     // note for eventfd(7) support:
89     // if fds[1] is -1, then eventfd(7) is in use and is stored in fds[0]
90     int fds[2];
91     QAtomicInt wakeUps;
92 
93 #if defined(Q_OS_VXWORKS)
94     static const int len_name = 20;
95     char name[len_name];
96 #endif
97 };
98 
99 class Q_CORE_EXPORT QEventDispatcherUNIX : public QAbstractEventDispatcher
100 {
101     Q_OBJECT
102     Q_DECLARE_PRIVATE(QEventDispatcherUNIX)
103 
104 public:
105     explicit QEventDispatcherUNIX(QObject *parent = nullptr);
106     ~QEventDispatcherUNIX();
107 
108     bool processEvents(QEventLoop::ProcessEventsFlags flags) override;
109     bool hasPendingEvents() override;
110 
111     void registerSocketNotifier(QSocketNotifier *notifier) final;
112     void unregisterSocketNotifier(QSocketNotifier *notifier) final;
113 
114     void registerTimer(int timerId, int interval, Qt::TimerType timerType, QObject *object) final;
115     bool unregisterTimer(int timerId) final;
116     bool unregisterTimers(QObject *object) final;
117     QList<TimerInfo> registeredTimers(QObject *object) const final;
118 
119     int remainingTime(int timerId) final;
120 
121     void wakeUp() override;
122     void interrupt() final;
123     void flush() override;
124 
125 protected:
126     QEventDispatcherUNIX(QEventDispatcherUNIXPrivate &dd, QObject *parent = nullptr);
127 };
128 
129 class Q_CORE_EXPORT QEventDispatcherUNIXPrivate : public QAbstractEventDispatcherPrivate
130 {
131     Q_DECLARE_PUBLIC(QEventDispatcherUNIX)
132 
133 public:
134     QEventDispatcherUNIXPrivate();
135     ~QEventDispatcherUNIXPrivate();
136 
137     int activateTimers();
138 
139     void markPendingSocketNotifiers();
140     int activateSocketNotifiers();
141     void setSocketNotifierPending(QSocketNotifier *notifier);
142 
143     QThreadPipe threadPipe;
144     QVector<pollfd> pollfds;
145 
146     QHash<int, QSocketNotifierSetUNIX> socketNotifiers;
147     QVector<QSocketNotifier *> pendingNotifiers;
148 
149     QTimerInfoList timerList;
150     QAtomicInt interrupt; // bool
151 };
152 
QSocketNotifierSetUNIX()153 inline QSocketNotifierSetUNIX::QSocketNotifierSetUNIX() noexcept
154 {
155     notifiers[0] = nullptr;
156     notifiers[1] = nullptr;
157     notifiers[2] = nullptr;
158 }
159 
isEmpty()160 inline bool QSocketNotifierSetUNIX::isEmpty() const noexcept
161 {
162     return !notifiers[0] && !notifiers[1] && !notifiers[2];
163 }
164 
events()165 inline short QSocketNotifierSetUNIX::events() const noexcept
166 {
167     short result = 0;
168 
169     if (notifiers[0])
170         result |= POLLIN;
171 
172     if (notifiers[1])
173         result |= POLLOUT;
174 
175     if (notifiers[2])
176         result |= POLLPRI;
177 
178     return result;
179 }
180 
181 QT_END_NAMESPACE
182 
183 #endif // QEVENTDISPATCHER_UNIX_P_H
184