| 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 |  | 
|---|
| 110 | void registerSocketNotifier(QSocketNotifier *notifier) final; | 
|---|
| 111 | void unregisterSocketNotifier(QSocketNotifier *notifier) final; | 
|---|
| 112 |  | 
|---|
| 113 | void registerTimer(int timerId, qint64 interval, Qt::TimerType timerType, QObject *object) final; | 
|---|
| 114 | bool unregisterTimer(int timerId) final; | 
|---|
| 115 | bool unregisterTimers(QObject *object) final; | 
|---|
| 116 | QList<TimerInfo> registeredTimers(QObject *object) const final; | 
|---|
| 117 |  | 
|---|
| 118 | int remainingTime(int timerId) final; | 
|---|
| 119 |  | 
|---|
| 120 | void wakeUp() override; | 
|---|
| 121 | void interrupt() final; | 
|---|
| 122 |  | 
|---|
| 123 | protected: | 
|---|
| 124 | QEventDispatcherUNIX(QEventDispatcherUNIXPrivate &dd, QObject *parent = nullptr); | 
|---|
| 125 | }; | 
|---|
| 126 |  | 
|---|
| 127 | class Q_CORE_EXPORT QEventDispatcherUNIXPrivate : public QAbstractEventDispatcherPrivate | 
|---|
| 128 | { | 
|---|
| 129 | Q_DECLARE_PUBLIC(QEventDispatcherUNIX) | 
|---|
| 130 |  | 
|---|
| 131 | public: | 
|---|
| 132 | QEventDispatcherUNIXPrivate(); | 
|---|
| 133 | ~QEventDispatcherUNIXPrivate(); | 
|---|
| 134 |  | 
|---|
| 135 | int activateTimers(); | 
|---|
| 136 |  | 
|---|
| 137 | void markPendingSocketNotifiers(); | 
|---|
| 138 | int activateSocketNotifiers(); | 
|---|
| 139 | void setSocketNotifierPending(QSocketNotifier *notifier); | 
|---|
| 140 |  | 
|---|
| 141 | QThreadPipe threadPipe; | 
|---|
| 142 | QList<pollfd> pollfds; | 
|---|
| 143 |  | 
|---|
| 144 | QHash<int, QSocketNotifierSetUNIX> socketNotifiers; | 
|---|
| 145 | QList<QSocketNotifier *> pendingNotifiers; | 
|---|
| 146 |  | 
|---|
| 147 | QTimerInfoList timerList; | 
|---|
| 148 | QAtomicInt interrupt; // bool | 
|---|
| 149 | }; | 
|---|
| 150 |  | 
|---|
| 151 | inline QSocketNotifierSetUNIX::QSocketNotifierSetUNIX() noexcept | 
|---|
| 152 | { | 
|---|
| 153 | notifiers[0] = nullptr; | 
|---|
| 154 | notifiers[1] = nullptr; | 
|---|
| 155 | notifiers[2] = nullptr; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | inline bool QSocketNotifierSetUNIX::isEmpty() const noexcept | 
|---|
| 159 | { | 
|---|
| 160 | return !notifiers[0] && !notifiers[1] && !notifiers[2]; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | inline short QSocketNotifierSetUNIX::events() const noexcept | 
|---|
| 164 | { | 
|---|
| 165 | short result = 0; | 
|---|
| 166 |  | 
|---|
| 167 | if (notifiers[0]) | 
|---|
| 168 | result |= POLLIN; | 
|---|
| 169 |  | 
|---|
| 170 | if (notifiers[1]) | 
|---|
| 171 | result |= POLLOUT; | 
|---|
| 172 |  | 
|---|
| 173 | if (notifiers[2]) | 
|---|
| 174 | result |= POLLPRI; | 
|---|
| 175 |  | 
|---|
| 176 | return result; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | QT_END_NAMESPACE | 
|---|
| 180 |  | 
|---|
| 181 | #endif // QEVENTDISPATCHER_UNIX_P_H | 
|---|
| 182 |  | 
|---|