| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qeventdispatcher_unix.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||||||||||||||
| 5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
| 6 | ** | - | ||||||||||||||||||
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
| 8 | ** | - | ||||||||||||||||||
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||
| 10 | ** Commercial License Usage | - | ||||||||||||||||||
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
| 12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
| 13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||
| 17 | ** | - | ||||||||||||||||||
| 18 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
| 20 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
| 22 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
| 25 | ** | - | ||||||||||||||||||
| 26 | ** GNU General Public License Usage | - | ||||||||||||||||||
| 27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
| 28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
| 29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
| 30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
| 32 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
| 33 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
| 36 | ** | - | ||||||||||||||||||
| 37 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 38 | ** | - | ||||||||||||||||||
| 39 | ****************************************************************************/ | - | ||||||||||||||||||
| 40 | - | |||||||||||||||||||
| 41 | #include "qplatformdefs.h" | - | ||||||||||||||||||
| 42 | - | |||||||||||||||||||
| 43 | #include "qcoreapplication.h" | - | ||||||||||||||||||
| 44 | #include "qpair.h" | - | ||||||||||||||||||
| 45 | #include "qsocketnotifier.h" | - | ||||||||||||||||||
| 46 | #include "qthread.h" | - | ||||||||||||||||||
| 47 | #include "qelapsedtimer.h" | - | ||||||||||||||||||
| 48 | - | |||||||||||||||||||
| 49 | #include "qeventdispatcher_unix_p.h" | - | ||||||||||||||||||
| 50 | #include <private/qthread_p.h> | - | ||||||||||||||||||
| 51 | #include <private/qcoreapplication_p.h> | - | ||||||||||||||||||
| 52 | #include <private/qcore_unix_p.h> | - | ||||||||||||||||||
| 53 | - | |||||||||||||||||||
| 54 | #include <errno.h> | - | ||||||||||||||||||
| 55 | #include <stdio.h> | - | ||||||||||||||||||
| 56 | #include <stdlib.h> | - | ||||||||||||||||||
| 57 | - | |||||||||||||||||||
| 58 | #ifndef QT_NO_EVENTFD | - | ||||||||||||||||||
| 59 | # include <sys/eventfd.h> | - | ||||||||||||||||||
| 60 | #endif | - | ||||||||||||||||||
| 61 | - | |||||||||||||||||||
| 62 | // VxWorks doesn't correctly set the _POSIX_... options | - | ||||||||||||||||||
| 63 | #if defined(Q_OS_VXWORKS) | - | ||||||||||||||||||
| 64 | # if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK <= 0) | - | ||||||||||||||||||
| 65 | # undef _POSIX_MONOTONIC_CLOCK | - | ||||||||||||||||||
| 66 | # define _POSIX_MONOTONIC_CLOCK 1 | - | ||||||||||||||||||
| 67 | # endif | - | ||||||||||||||||||
| 68 | # include <pipeDrv.h> | - | ||||||||||||||||||
| 69 | # include <sys/time.h> | - | ||||||||||||||||||
| 70 | #endif | - | ||||||||||||||||||
| 71 | - | |||||||||||||||||||
| 72 | #if (_POSIX_MONOTONIC_CLOCK-0 <= 0) || defined(QT_BOOTSTRAPPED) | - | ||||||||||||||||||
| 73 | # include <sys/times.h> | - | ||||||||||||||||||
| 74 | #endif | - | ||||||||||||||||||
| 75 | - | |||||||||||||||||||
| 76 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 77 | - | |||||||||||||||||||
| 78 | static const char *socketType(QSocketNotifier::Type type) | - | ||||||||||||||||||
| 79 | { | - | ||||||||||||||||||
| 80 | switch (type) { | - | ||||||||||||||||||
| 81 | case QSocketNotifier::Read: never executed: case QSocketNotifier::Read: | 0 | ||||||||||||||||||
| 82 | return "Read"; never executed: return "Read"; | 0 | ||||||||||||||||||
| 83 | case QSocketNotifier::Write: never executed: case QSocketNotifier::Write: | 0 | ||||||||||||||||||
| 84 | return "Write"; never executed: return "Write"; | 0 | ||||||||||||||||||
| 85 | case QSocketNotifier::Exception: never executed: case QSocketNotifier::Exception: | 0 | ||||||||||||||||||
| 86 | return "Exception"; never executed: return "Exception"; | 0 | ||||||||||||||||||
| 87 | } | - | ||||||||||||||||||
| 88 | - | |||||||||||||||||||
| 89 | Q_UNREACHABLE(); | - | ||||||||||||||||||
| 90 | } never executed: end of block | 0 | ||||||||||||||||||
| 91 | - | |||||||||||||||||||
| 92 | QThreadPipe::QThreadPipe() | - | ||||||||||||||||||
| 93 | { | - | ||||||||||||||||||
| 94 | fds[0] = -1; | - | ||||||||||||||||||
| 95 | fds[1] = -1; | - | ||||||||||||||||||
| 96 | #if defined(Q_OS_VXWORKS) | - | ||||||||||||||||||
| 97 | name[0] = '\0'; | - | ||||||||||||||||||
| 98 | #endif | - | ||||||||||||||||||
| 99 | } never executed: end of block | 0 | ||||||||||||||||||
| 100 | - | |||||||||||||||||||
| 101 | QThreadPipe::~QThreadPipe() | - | ||||||||||||||||||
| 102 | { | - | ||||||||||||||||||
| 103 | if (fds[0] >= 0)
| 0 | ||||||||||||||||||
| 104 | close(fds[0]); never executed: close(fds[0]); | 0 | ||||||||||||||||||
| 105 | - | |||||||||||||||||||
| 106 | if (fds[1] >= 0)
| 0 | ||||||||||||||||||
| 107 | close(fds[1]); never executed: close(fds[1]); | 0 | ||||||||||||||||||
| 108 | - | |||||||||||||||||||
| 109 | #if defined(Q_OS_VXWORKS) | - | ||||||||||||||||||
| 110 | pipeDevDelete(name, true); | - | ||||||||||||||||||
| 111 | #endif | - | ||||||||||||||||||
| 112 | } never executed: end of block | 0 | ||||||||||||||||||
| 113 | - | |||||||||||||||||||
| 114 | #if defined(Q_OS_VXWORKS) | - | ||||||||||||||||||
| 115 | static void initThreadPipeFD(int fd) | - | ||||||||||||||||||
| 116 | { | - | ||||||||||||||||||
| 117 | int ret = fcntl(fd, F_SETFD, FD_CLOEXEC); | - | ||||||||||||||||||
| 118 | if (ret == -1) | - | ||||||||||||||||||
| 119 | perror("QEventDispatcherUNIXPrivate: Unable to init thread pipe"); | - | ||||||||||||||||||
| 120 | - | |||||||||||||||||||
| 121 | int flags = fcntl(fd, F_GETFL); | - | ||||||||||||||||||
| 122 | if (flags == -1) | - | ||||||||||||||||||
| 123 | perror("QEventDispatcherUNIXPrivate: Unable to get flags on thread pipe"); | - | ||||||||||||||||||
| 124 | - | |||||||||||||||||||
| 125 | ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); | - | ||||||||||||||||||
| 126 | if (ret == -1) | - | ||||||||||||||||||
| 127 | perror("QEventDispatcherUNIXPrivate: Unable to set flags on thread pipe"); | - | ||||||||||||||||||
| 128 | } | - | ||||||||||||||||||
| 129 | #endif | - | ||||||||||||||||||
| 130 | - | |||||||||||||||||||
| 131 | bool QThreadPipe::init() | - | ||||||||||||||||||
| 132 | { | - | ||||||||||||||||||
| 133 | #if defined(Q_OS_NACL) | - | ||||||||||||||||||
| 134 | // do nothing. | - | ||||||||||||||||||
| 135 | #elif defined(Q_OS_VXWORKS) | - | ||||||||||||||||||
| 136 | qsnprintf(name, sizeof(name), "/pipe/qt_%08x", int(taskIdSelf())); | - | ||||||||||||||||||
| 137 | - | |||||||||||||||||||
| 138 | // make sure there is no pipe with this name | - | ||||||||||||||||||
| 139 | pipeDevDelete(name, true); | - | ||||||||||||||||||
| 140 | - | |||||||||||||||||||
| 141 | // create the pipe | - | ||||||||||||||||||
| 142 | if (pipeDevCreate(name, 128 /*maxMsg*/, 1 /*maxLength*/) != OK) { | - | ||||||||||||||||||
| 143 | perror("QThreadPipe: Unable to create thread pipe device %s", name); | - | ||||||||||||||||||
| 144 | return false; | - | ||||||||||||||||||
| 145 | } | - | ||||||||||||||||||
| 146 | - | |||||||||||||||||||
| 147 | if ((fds[0] = open(name, O_RDWR, 0)) < 0) { | - | ||||||||||||||||||
| 148 | perror("QThreadPipe: Unable to open pipe device %s", name); | - | ||||||||||||||||||
| 149 | return false; | - | ||||||||||||||||||
| 150 | } | - | ||||||||||||||||||
| 151 | - | |||||||||||||||||||
| 152 | initThreadPipeFD(fds[0]); | - | ||||||||||||||||||
| 153 | fds[1] = fds[0]; | - | ||||||||||||||||||
| 154 | #else | - | ||||||||||||||||||
| 155 | # ifndef QT_NO_EVENTFD | - | ||||||||||||||||||
| 156 | if ((fds[0] = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC)) >= 0)
| 0 | ||||||||||||||||||
| 157 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 158 | # endif | - | ||||||||||||||||||
| 159 | if (qt_safe_pipe(fds, O_NONBLOCK) == -1) {
| 0 | ||||||||||||||||||
| 160 | perror("QThreadPipe: Unable to create pipe"); | - | ||||||||||||||||||
| 161 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 162 | } | - | ||||||||||||||||||
| 163 | #endif | - | ||||||||||||||||||
| 164 | - | |||||||||||||||||||
| 165 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 166 | } | - | ||||||||||||||||||
| 167 | - | |||||||||||||||||||
| 168 | pollfd QThreadPipe::prepare() const | - | ||||||||||||||||||
| 169 | { | - | ||||||||||||||||||
| 170 | return qt_make_pollfd(fds[0], POLLIN); never executed: return qt_make_pollfd(fds[0], 0x001); | 0 | ||||||||||||||||||
| 171 | } | - | ||||||||||||||||||
| 172 | - | |||||||||||||||||||
| 173 | void QThreadPipe::wakeUp() | - | ||||||||||||||||||
| 174 | { | - | ||||||||||||||||||
| 175 | if (wakeUps.testAndSetAcquire(0, 1)) {
| 0 | ||||||||||||||||||
| 176 | #ifndef QT_NO_EVENTFD | - | ||||||||||||||||||
| 177 | if (fds[1] == -1) {
| 0 | ||||||||||||||||||
| 178 | // eventfd | - | ||||||||||||||||||
| 179 | eventfd_t value = 1; | - | ||||||||||||||||||
| 180 | int ret; | - | ||||||||||||||||||
| 181 | EINTR_LOOP(ret, eventfd_write(fds[0], value)); never executed: end of block
| 0 | ||||||||||||||||||
| 182 | return; never executed: return; | 0 | ||||||||||||||||||
| 183 | } | - | ||||||||||||||||||
| 184 | #endif | - | ||||||||||||||||||
| 185 | char c = 0; | - | ||||||||||||||||||
| 186 | qt_safe_write(fds[1], &c, 1); | - | ||||||||||||||||||
| 187 | } never executed: end of block | 0 | ||||||||||||||||||
| 188 | } never executed: end of block | 0 | ||||||||||||||||||
| 189 | - | |||||||||||||||||||
| 190 | int QThreadPipe::check(const pollfd &pfd) | - | ||||||||||||||||||
| 191 | { | - | ||||||||||||||||||
| 192 | Q_ASSERT(pfd.fd == fds[0]); | - | ||||||||||||||||||
| 193 | - | |||||||||||||||||||
| 194 | char c[16]; | - | ||||||||||||||||||
| 195 | const int readyread = pfd.revents & POLLIN; | - | ||||||||||||||||||
| 196 | - | |||||||||||||||||||
| 197 | if (readyread) {
| 0 | ||||||||||||||||||
| 198 | // consume the data on the thread pipe so that | - | ||||||||||||||||||
| 199 | // poll doesn't immediately return next time | - | ||||||||||||||||||
| 200 | #if defined(Q_OS_VXWORKS) | - | ||||||||||||||||||
| 201 | ::read(fds[0], c, sizeof(c)); | - | ||||||||||||||||||
| 202 | ::ioctl(fds[0], FIOFLUSH, 0); | - | ||||||||||||||||||
| 203 | #else | - | ||||||||||||||||||
| 204 | # ifndef QT_NO_EVENTFD | - | ||||||||||||||||||
| 205 | if (fds[1] == -1) {
| 0 | ||||||||||||||||||
| 206 | // eventfd | - | ||||||||||||||||||
| 207 | eventfd_t value; | - | ||||||||||||||||||
| 208 | eventfd_read(fds[0], &value); | - | ||||||||||||||||||
| 209 | } else never executed: end of block | 0 | ||||||||||||||||||
| 210 | # endif | - | ||||||||||||||||||
| 211 | { | - | ||||||||||||||||||
| 212 | while (::read(fds[0], c, sizeof(c)) > 0) {} never executed: end of block
| 0 | ||||||||||||||||||
| 213 | } never executed: end of block | 0 | ||||||||||||||||||
| 214 | #endif | - | ||||||||||||||||||
| 215 | - | |||||||||||||||||||
| 216 | if (!wakeUps.testAndSetRelease(1, 0)) {
| 0 | ||||||||||||||||||
| 217 | // hopefully, this is dead code | - | ||||||||||||||||||
| 218 | qWarning("QThreadPipe: internal error, wakeUps.testAndSetRelease(1, 0) failed!"); | - | ||||||||||||||||||
| 219 | } never executed: end of block | 0 | ||||||||||||||||||
| 220 | } never executed: end of block | 0 | ||||||||||||||||||
| 221 | - | |||||||||||||||||||
| 222 | return readyread; never executed: return readyread; | 0 | ||||||||||||||||||
| 223 | } | - | ||||||||||||||||||
| 224 | - | |||||||||||||||||||
| 225 | QEventDispatcherUNIXPrivate::QEventDispatcherUNIXPrivate() | - | ||||||||||||||||||
| 226 | { | - | ||||||||||||||||||
| 227 | if (Q_UNLIKELY(threadPipe.init() == false))
| 0 | ||||||||||||||||||
| 228 | qFatal("QEventDispatcherUNIXPrivate(): Can not continue without a thread pipe"); never executed: QMessageLogger(__FILE__, 228, __PRETTY_FUNCTION__).fatal("QEventDispatcherUNIXPrivate(): Can not continue without a thread pipe"); | 0 | ||||||||||||||||||
| 229 | } never executed: end of block | 0 | ||||||||||||||||||
| 230 | - | |||||||||||||||||||
| 231 | QEventDispatcherUNIXPrivate::~QEventDispatcherUNIXPrivate() | - | ||||||||||||||||||
| 232 | { | - | ||||||||||||||||||
| 233 | // cleanup timers | - | ||||||||||||||||||
| 234 | qDeleteAll(timerList); | - | ||||||||||||||||||
| 235 | } never executed: end of block | 0 | ||||||||||||||||||
| 236 | - | |||||||||||||||||||
| 237 | void QEventDispatcherUNIXPrivate::setSocketNotifierPending(QSocketNotifier *notifier) | - | ||||||||||||||||||
| 238 | { | - | ||||||||||||||||||
| 239 | Q_ASSERT(notifier); | - | ||||||||||||||||||
| 240 | - | |||||||||||||||||||
| 241 | if (pendingNotifiers.contains(notifier))
| 0 | ||||||||||||||||||
| 242 | return; never executed: return; | 0 | ||||||||||||||||||
| 243 | - | |||||||||||||||||||
| 244 | pendingNotifiers << notifier; | - | ||||||||||||||||||
| 245 | } never executed: end of block | 0 | ||||||||||||||||||
| 246 | - | |||||||||||||||||||
| 247 | int QEventDispatcherUNIXPrivate::activateTimers() | - | ||||||||||||||||||
| 248 | { | - | ||||||||||||||||||
| 249 | return timerList.activateTimers(); never executed: return timerList.activateTimers(); | 0 | ||||||||||||||||||
| 250 | } | - | ||||||||||||||||||
| 251 | - | |||||||||||||||||||
| 252 | void QEventDispatcherUNIXPrivate::markPendingSocketNotifiers() | - | ||||||||||||||||||
| 253 | { | - | ||||||||||||||||||
| 254 | for (const pollfd &pfd : qAsConst(pollfds)) { | - | ||||||||||||||||||
| 255 | if (pfd.fd < 0 || pfd.revents == 0)
| 0 | ||||||||||||||||||
| 256 | continue; never executed: continue; | 0 | ||||||||||||||||||
| 257 | - | |||||||||||||||||||
| 258 | auto it = socketNotifiers.find(pfd.fd); | - | ||||||||||||||||||
| 259 | Q_ASSERT(it != socketNotifiers.end()); | - | ||||||||||||||||||
| 260 | - | |||||||||||||||||||
| 261 | const QSocketNotifierSetUNIX &sn_set = it.value(); | - | ||||||||||||||||||
| 262 | - | |||||||||||||||||||
| 263 | static const struct { | - | ||||||||||||||||||
| 264 | QSocketNotifier::Type type; | - | ||||||||||||||||||
| 265 | short flags; | - | ||||||||||||||||||
| 266 | } notifiers[] = { | - | ||||||||||||||||||
| 267 | { QSocketNotifier::Read, POLLIN | POLLHUP | POLLERR }, | - | ||||||||||||||||||
| 268 | { QSocketNotifier::Write, POLLOUT | POLLHUP | POLLERR }, | - | ||||||||||||||||||
| 269 | { QSocketNotifier::Exception, POLLPRI | POLLHUP | POLLERR } | - | ||||||||||||||||||
| 270 | }; | - | ||||||||||||||||||
| 271 | - | |||||||||||||||||||
| 272 | for (const auto &n : notifiers) { | - | ||||||||||||||||||
| 273 | QSocketNotifier *notifier = sn_set.notifiers[n.type]; | - | ||||||||||||||||||
| 274 | - | |||||||||||||||||||
| 275 | if (!notifier)
| 0 | ||||||||||||||||||
| 276 | continue; never executed: continue; | 0 | ||||||||||||||||||
| 277 | - | |||||||||||||||||||
| 278 | if (pfd.revents & POLLNVAL) {
| 0 | ||||||||||||||||||
| 279 | qWarning("QSocketNotifier: Invalid socket %d with type %s, disabling...", | - | ||||||||||||||||||
| 280 | it.key(), socketType(n.type)); | - | ||||||||||||||||||
| 281 | notifier->setEnabled(false); | - | ||||||||||||||||||
| 282 | } never executed: end of block | 0 | ||||||||||||||||||
| 283 | - | |||||||||||||||||||
| 284 | if (pfd.revents & n.flags)
| 0 | ||||||||||||||||||
| 285 | setSocketNotifierPending(notifier); never executed: setSocketNotifierPending(notifier); | 0 | ||||||||||||||||||
| 286 | } never executed: end of block | 0 | ||||||||||||||||||
| 287 | } never executed: end of block | 0 | ||||||||||||||||||
| 288 | - | |||||||||||||||||||
| 289 | pollfds.clear(); | - | ||||||||||||||||||
| 290 | } never executed: end of block | 0 | ||||||||||||||||||
| 291 | - | |||||||||||||||||||
| 292 | int QEventDispatcherUNIXPrivate::activateSocketNotifiers() | - | ||||||||||||||||||
| 293 | { | - | ||||||||||||||||||
| 294 | markPendingSocketNotifiers(); | - | ||||||||||||||||||
| 295 | - | |||||||||||||||||||
| 296 | if (pendingNotifiers.isEmpty())
| 0 | ||||||||||||||||||
| 297 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
| 298 | - | |||||||||||||||||||
| 299 | int n_activated = 0; | - | ||||||||||||||||||
| 300 | QEvent event(QEvent::SockAct); | - | ||||||||||||||||||
| 301 | - | |||||||||||||||||||
| 302 | while (!pendingNotifiers.isEmpty()) {
| 0 | ||||||||||||||||||
| 303 | QSocketNotifier *notifier = pendingNotifiers.takeFirst(); | - | ||||||||||||||||||
| 304 | QCoreApplication::sendEvent(notifier, &event); | - | ||||||||||||||||||
| 305 | ++n_activated; | - | ||||||||||||||||||
| 306 | } never executed: end of block | 0 | ||||||||||||||||||
| 307 | - | |||||||||||||||||||
| 308 | return n_activated; never executed: return n_activated; | 0 | ||||||||||||||||||
| 309 | } | - | ||||||||||||||||||
| 310 | - | |||||||||||||||||||
| 311 | QEventDispatcherUNIX::QEventDispatcherUNIX(QObject *parent) | - | ||||||||||||||||||
| 312 | : QAbstractEventDispatcher(*new QEventDispatcherUNIXPrivate, parent) | - | ||||||||||||||||||
| 313 | { never executed: }end of blocknever executed: end of block | 0 | ||||||||||||||||||
| 314 | - | |||||||||||||||||||
| 315 | QEventDispatcherUNIX::QEventDispatcherUNIX(QEventDispatcherUNIXPrivate &dd, QObject *parent) | - | ||||||||||||||||||
| 316 | : QAbstractEventDispatcher(dd, parent) | - | ||||||||||||||||||
| 317 | { never executed: }end of blocknever executed: end of block | 0 | ||||||||||||||||||
| 318 | - | |||||||||||||||||||
| 319 | QEventDispatcherUNIX::~QEventDispatcherUNIX() | - | ||||||||||||||||||
| 320 | { } | - | ||||||||||||||||||
| 321 | - | |||||||||||||||||||
| 322 | /*! | - | ||||||||||||||||||
| 323 | \internal | - | ||||||||||||||||||
| 324 | */ | - | ||||||||||||||||||
| 325 | void QEventDispatcherUNIX::registerTimer(int timerId, int interval, Qt::TimerType timerType, QObject *obj) | - | ||||||||||||||||||
| 326 | { | - | ||||||||||||||||||
| 327 | #ifndef QT_NO_DEBUG | - | ||||||||||||||||||
| 328 | if (timerId < 1 || interval < 0 || !obj) {
| 0 | ||||||||||||||||||
| 329 | qWarning("QEventDispatcherUNIX::registerTimer: invalid arguments"); | - | ||||||||||||||||||
| 330 | return; never executed: return; | 0 | ||||||||||||||||||
| 331 | } else if (obj->thread() != thread() || thread() != QThread::currentThread()) {
| 0 | ||||||||||||||||||
| 332 | qWarning("QEventDispatcherUNIX::registerTimer: timers cannot be started from another thread"); | - | ||||||||||||||||||
| 333 | return; never executed: return; | 0 | ||||||||||||||||||
| 334 | } | - | ||||||||||||||||||
| 335 | #endif | - | ||||||||||||||||||
| 336 | - | |||||||||||||||||||
| 337 | Q_D(QEventDispatcherUNIX); | - | ||||||||||||||||||
| 338 | d->timerList.registerTimer(timerId, interval, timerType, obj); | - | ||||||||||||||||||
| 339 | } never executed: end of block | 0 | ||||||||||||||||||
| 340 | - | |||||||||||||||||||
| 341 | /*! | - | ||||||||||||||||||
| 342 | \internal | - | ||||||||||||||||||
| 343 | */ | - | ||||||||||||||||||
| 344 | bool QEventDispatcherUNIX::unregisterTimer(int timerId) | - | ||||||||||||||||||
| 345 | { | - | ||||||||||||||||||
| 346 | #ifndef QT_NO_DEBUG | - | ||||||||||||||||||
| 347 | if (timerId < 1) {
| 0 | ||||||||||||||||||
| 348 | qWarning("QEventDispatcherUNIX::unregisterTimer: invalid argument"); | - | ||||||||||||||||||
| 349 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 350 | } else if (thread() != QThread::currentThread()) {
| 0 | ||||||||||||||||||
| 351 | qWarning("QEventDispatcherUNIX::unregisterTimer: timers cannot be stopped from another thread"); | - | ||||||||||||||||||
| 352 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 353 | } | - | ||||||||||||||||||
| 354 | #endif | - | ||||||||||||||||||
| 355 | - | |||||||||||||||||||
| 356 | Q_D(QEventDispatcherUNIX); | - | ||||||||||||||||||
| 357 | return d->timerList.unregisterTimer(timerId); never executed: return d->timerList.unregisterTimer(timerId); | 0 | ||||||||||||||||||
| 358 | } | - | ||||||||||||||||||
| 359 | - | |||||||||||||||||||
| 360 | /*! | - | ||||||||||||||||||
| 361 | \internal | - | ||||||||||||||||||
| 362 | */ | - | ||||||||||||||||||
| 363 | bool QEventDispatcherUNIX::unregisterTimers(QObject *object) | - | ||||||||||||||||||
| 364 | { | - | ||||||||||||||||||
| 365 | #ifndef QT_NO_DEBUG | - | ||||||||||||||||||
| 366 | if (!object) {
| 0 | ||||||||||||||||||
| 367 | qWarning("QEventDispatcherUNIX::unregisterTimers: invalid argument"); | - | ||||||||||||||||||
| 368 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 369 | } else if (object->thread() != thread() || thread() != QThread::currentThread()) {
| 0 | ||||||||||||||||||
| 370 | qWarning("QEventDispatcherUNIX::unregisterTimers: timers cannot be stopped from another thread"); | - | ||||||||||||||||||
| 371 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 372 | } | - | ||||||||||||||||||
| 373 | #endif | - | ||||||||||||||||||
| 374 | - | |||||||||||||||||||
| 375 | Q_D(QEventDispatcherUNIX); | - | ||||||||||||||||||
| 376 | return d->timerList.unregisterTimers(object); never executed: return d->timerList.unregisterTimers(object); | 0 | ||||||||||||||||||
| 377 | } | - | ||||||||||||||||||
| 378 | - | |||||||||||||||||||
| 379 | QList<QEventDispatcherUNIX::TimerInfo> | - | ||||||||||||||||||
| 380 | QEventDispatcherUNIX::registeredTimers(QObject *object) const | - | ||||||||||||||||||
| 381 | { | - | ||||||||||||||||||
| 382 | if (!object) {
| 0 | ||||||||||||||||||
| 383 | qWarning("QEventDispatcherUNIX:registeredTimers: invalid argument"); | - | ||||||||||||||||||
| 384 | return QList<TimerInfo>(); never executed: return QList<TimerInfo>(); | 0 | ||||||||||||||||||
| 385 | } | - | ||||||||||||||||||
| 386 | - | |||||||||||||||||||
| 387 | Q_D(const QEventDispatcherUNIX); | - | ||||||||||||||||||
| 388 | return d->timerList.registeredTimers(object); never executed: return d->timerList.registeredTimers(object); | 0 | ||||||||||||||||||
| 389 | } | - | ||||||||||||||||||
| 390 | - | |||||||||||||||||||
| 391 | /***************************************************************************** | - | ||||||||||||||||||
| 392 | QEventDispatcher implementations for UNIX | - | ||||||||||||||||||
| 393 | *****************************************************************************/ | - | ||||||||||||||||||
| 394 | - | |||||||||||||||||||
| 395 | void QEventDispatcherUNIX::registerSocketNotifier(QSocketNotifier *notifier) | - | ||||||||||||||||||
| 396 | { | - | ||||||||||||||||||
| 397 | Q_ASSERT(notifier); | - | ||||||||||||||||||
| 398 | int sockfd = notifier->socket(); | - | ||||||||||||||||||
| 399 | QSocketNotifier::Type type = notifier->type(); | - | ||||||||||||||||||
| 400 | #ifndef QT_NO_DEBUG | - | ||||||||||||||||||
| 401 | if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
| 0 | ||||||||||||||||||
| 402 | qWarning("QSocketNotifier: socket notifiers cannot be enabled from another thread"); | - | ||||||||||||||||||
| 403 | return; never executed: return; | 0 | ||||||||||||||||||
| 404 | } | - | ||||||||||||||||||
| 405 | #endif | - | ||||||||||||||||||
| 406 | - | |||||||||||||||||||
| 407 | Q_D(QEventDispatcherUNIX); | - | ||||||||||||||||||
| 408 | QSocketNotifierSetUNIX &sn_set = d->socketNotifiers[sockfd]; | - | ||||||||||||||||||
| 409 | - | |||||||||||||||||||
| 410 | if (sn_set.notifiers[type] && sn_set.notifiers[type] != notifier)
| 0 | ||||||||||||||||||
| 411 | qWarning("%s: Multiple socket notifiers for same socket %d and type %s", never executed: QMessageLogger(__FILE__, 411, __PRETTY_FUNCTION__).warning("%s: Multiple socket notifiers for same socket %d and type %s", __PRETTY_FUNCTION__, sockfd, socketType(type)); | 0 | ||||||||||||||||||
| 412 | Q_FUNC_INFO, sockfd, socketType(type)); never executed: QMessageLogger(__FILE__, 411, __PRETTY_FUNCTION__).warning("%s: Multiple socket notifiers for same socket %d and type %s", __PRETTY_FUNCTION__, sockfd, socketType(type)); | 0 | ||||||||||||||||||
| 413 | - | |||||||||||||||||||
| 414 | sn_set.notifiers[type] = notifier; | - | ||||||||||||||||||
| 415 | } never executed: end of block | 0 | ||||||||||||||||||
| 416 | - | |||||||||||||||||||
| 417 | void QEventDispatcherUNIX::unregisterSocketNotifier(QSocketNotifier *notifier) | - | ||||||||||||||||||
| 418 | { | - | ||||||||||||||||||
| 419 | Q_ASSERT(notifier); | - | ||||||||||||||||||
| 420 | int sockfd = notifier->socket(); | - | ||||||||||||||||||
| 421 | QSocketNotifier::Type type = notifier->type(); | - | ||||||||||||||||||
| 422 | #ifndef QT_NO_DEBUG | - | ||||||||||||||||||
| 423 | if (notifier->thread() != thread() || thread() != QThread::currentThread()) {
| 0 | ||||||||||||||||||
| 424 | qWarning("QSocketNotifier: socket notifier (fd %d) cannot be disabled from another thread.\n" | - | ||||||||||||||||||
| 425 | "(Notifier's thread is %s(%p), event dispatcher's thread is %s(%p), current thread is %s(%p))", | - | ||||||||||||||||||
| 426 | sockfd, | - | ||||||||||||||||||
| 427 | notifier->thread() ? notifier->thread()->metaObject()->className() : "QThread", notifier->thread(), | - | ||||||||||||||||||
| 428 | thread() ? thread()->metaObject()->className() : "QThread", thread(), | - | ||||||||||||||||||
| 429 | QThread::currentThread() ? QThread::currentThread()->metaObject()->className() : "QThread", QThread::currentThread()); | - | ||||||||||||||||||
| 430 | return; never executed: return; | 0 | ||||||||||||||||||
| 431 | } | - | ||||||||||||||||||
| 432 | #endif | - | ||||||||||||||||||
| 433 | - | |||||||||||||||||||
| 434 | Q_D(QEventDispatcherUNIX); | - | ||||||||||||||||||
| 435 | - | |||||||||||||||||||
| 436 | d->pendingNotifiers.removeOne(notifier); | - | ||||||||||||||||||
| 437 | - | |||||||||||||||||||
| 438 | auto i = d->socketNotifiers.find(sockfd); | - | ||||||||||||||||||
| 439 | if (i == d->socketNotifiers.end())
| 0 | ||||||||||||||||||
| 440 | return; never executed: return; | 0 | ||||||||||||||||||
| 441 | - | |||||||||||||||||||
| 442 | QSocketNotifierSetUNIX &sn_set = i.value(); | - | ||||||||||||||||||
| 443 | - | |||||||||||||||||||
| 444 | if (sn_set.notifiers[type] == nullptr)
| 0 | ||||||||||||||||||
| 445 | return; never executed: return; | 0 | ||||||||||||||||||
| 446 | - | |||||||||||||||||||
| 447 | if (sn_set.notifiers[type] != notifier) {
| 0 | ||||||||||||||||||
| 448 | qWarning("%s: Multiple socket notifiers for same socket %d and type %s", | - | ||||||||||||||||||
| 449 | Q_FUNC_INFO, sockfd, socketType(type)); | - | ||||||||||||||||||
| 450 | return; never executed: return; | 0 | ||||||||||||||||||
| 451 | } | - | ||||||||||||||||||
| 452 | - | |||||||||||||||||||
| 453 | sn_set.notifiers[type] = nullptr; | - | ||||||||||||||||||
| 454 | - | |||||||||||||||||||
| 455 | if (sn_set.isEmpty())
| 0 | ||||||||||||||||||
| 456 | d->socketNotifiers.erase(i); never executed: d->socketNotifiers.erase(i); | 0 | ||||||||||||||||||
| 457 | } never executed: end of block | 0 | ||||||||||||||||||
| 458 | - | |||||||||||||||||||
| 459 | bool QEventDispatcherUNIX::processEvents(QEventLoop::ProcessEventsFlags flags) | - | ||||||||||||||||||
| 460 | { | - | ||||||||||||||||||
| 461 | Q_D(QEventDispatcherUNIX); | - | ||||||||||||||||||
| 462 | d->interrupt.store(0); | - | ||||||||||||||||||
| 463 | - | |||||||||||||||||||
| 464 | // we are awake, broadcast it | - | ||||||||||||||||||
| 465 | emit awake(); | - | ||||||||||||||||||
| 466 | QCoreApplicationPrivate::sendPostedEvents(0, 0, d->threadData); | - | ||||||||||||||||||
| 467 | - | |||||||||||||||||||
| 468 | const bool include_timers = (flags & QEventLoop::X11ExcludeTimers) == 0; | - | ||||||||||||||||||
| 469 | const bool include_notifiers = (flags & QEventLoop::ExcludeSocketNotifiers) == 0; | - | ||||||||||||||||||
| 470 | const bool wait_for_events = flags & QEventLoop::WaitForMoreEvents; | - | ||||||||||||||||||
| 471 | - | |||||||||||||||||||
| 472 | const bool canWait = (d->threadData->canWaitLocked()
| 0 | ||||||||||||||||||
| 473 | && !d->interrupt.load()
| 0 | ||||||||||||||||||
| 474 | && wait_for_events);
| 0 | ||||||||||||||||||
| 475 | - | |||||||||||||||||||
| 476 | if (canWait)
| 0 | ||||||||||||||||||
| 477 | emit aboutToBlock(); never executed: aboutToBlock(); | 0 | ||||||||||||||||||
| 478 | - | |||||||||||||||||||
| 479 | if (d->interrupt.load())
| 0 | ||||||||||||||||||
| 480 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 481 | - | |||||||||||||||||||
| 482 | timespec *tm = nullptr; | - | ||||||||||||||||||
| 483 | timespec wait_tm = { 0, 0 }; | - | ||||||||||||||||||
| 484 | - | |||||||||||||||||||
| 485 | if (!canWait || (include_timers && d->timerList.timerWait(wait_tm)))
| 0 | ||||||||||||||||||
| 486 | tm = &wait_tm; never executed: tm = &wait_tm; | 0 | ||||||||||||||||||
| 487 | - | |||||||||||||||||||
| 488 | d->pollfds.clear(); | - | ||||||||||||||||||
| 489 | d->pollfds.reserve(1 + (include_notifiers ? d->socketNotifiers.size() : 0)); | - | ||||||||||||||||||
| 490 | - | |||||||||||||||||||
| 491 | if (include_notifiers)
| 0 | ||||||||||||||||||
| 492 | for (auto it = d->socketNotifiers.cbegin(); it != d->socketNotifiers.cend(); ++it)
| 0 | ||||||||||||||||||
| 493 | d->pollfds.append(qt_make_pollfd(it.key(), it.value().events())); never executed: d->pollfds.append(qt_make_pollfd(it.key(), it.value().events())); | 0 | ||||||||||||||||||
| 494 | - | |||||||||||||||||||
| 495 | // This must be last, as it's popped off the end below | - | ||||||||||||||||||
| 496 | d->pollfds.append(d->threadPipe.prepare()); | - | ||||||||||||||||||
| 497 | - | |||||||||||||||||||
| 498 | int nevents = 0; | - | ||||||||||||||||||
| 499 | - | |||||||||||||||||||
| 500 | switch (qt_safe_poll(d->pollfds.data(), d->pollfds.size(), tm)) { | - | ||||||||||||||||||
| 501 | case -1: never executed: case -1: | 0 | ||||||||||||||||||
| 502 | perror("qt_safe_poll"); | - | ||||||||||||||||||
| 503 | break; never executed: break; | 0 | ||||||||||||||||||
| 504 | case 0: never executed: case 0: | 0 | ||||||||||||||||||
| 505 | break; never executed: break; | 0 | ||||||||||||||||||
| 506 | default: never executed: default: | 0 | ||||||||||||||||||
| 507 | nevents += d->threadPipe.check(d->pollfds.takeLast()); | - | ||||||||||||||||||
| 508 | if (include_notifiers)
| 0 | ||||||||||||||||||
| 509 | nevents += d->activateSocketNotifiers(); never executed: nevents += d->activateSocketNotifiers(); | 0 | ||||||||||||||||||
| 510 | break; never executed: break; | 0 | ||||||||||||||||||
| 511 | } | - | ||||||||||||||||||
| 512 | - | |||||||||||||||||||
| 513 | if (include_timers)
| 0 | ||||||||||||||||||
| 514 | nevents += d->activateTimers(); never executed: nevents += d->activateTimers(); | 0 | ||||||||||||||||||
| 515 | - | |||||||||||||||||||
| 516 | // return true if we handled events, false otherwise | - | ||||||||||||||||||
| 517 | return (nevents > 0); never executed: return (nevents > 0); | 0 | ||||||||||||||||||
| 518 | } | - | ||||||||||||||||||
| 519 | - | |||||||||||||||||||
| 520 | bool QEventDispatcherUNIX::hasPendingEvents() | - | ||||||||||||||||||
| 521 | { | - | ||||||||||||||||||
| 522 | extern uint qGlobalPostedEventsCount(); // from qapplication.cpp | - | ||||||||||||||||||
| 523 | return qGlobalPostedEventsCount(); never executed: return qGlobalPostedEventsCount(); | 0 | ||||||||||||||||||
| 524 | } | - | ||||||||||||||||||
| 525 | - | |||||||||||||||||||
| 526 | int QEventDispatcherUNIX::remainingTime(int timerId) | - | ||||||||||||||||||
| 527 | { | - | ||||||||||||||||||
| 528 | #ifndef QT_NO_DEBUG | - | ||||||||||||||||||
| 529 | if (timerId < 1) {
| 0 | ||||||||||||||||||
| 530 | qWarning("QEventDispatcherUNIX::remainingTime: invalid argument"); | - | ||||||||||||||||||
| 531 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
| 532 | } | - | ||||||||||||||||||
| 533 | #endif | - | ||||||||||||||||||
| 534 | - | |||||||||||||||||||
| 535 | Q_D(QEventDispatcherUNIX); | - | ||||||||||||||||||
| 536 | return d->timerList.timerRemainingTime(timerId); never executed: return d->timerList.timerRemainingTime(timerId); | 0 | ||||||||||||||||||
| 537 | } | - | ||||||||||||||||||
| 538 | - | |||||||||||||||||||
| 539 | void QEventDispatcherUNIX::wakeUp() | - | ||||||||||||||||||
| 540 | { | - | ||||||||||||||||||
| 541 | Q_D(QEventDispatcherUNIX); | - | ||||||||||||||||||
| 542 | d->threadPipe.wakeUp(); | - | ||||||||||||||||||
| 543 | } never executed: end of block | 0 | ||||||||||||||||||
| 544 | - | |||||||||||||||||||
| 545 | void QEventDispatcherUNIX::interrupt() | - | ||||||||||||||||||
| 546 | { | - | ||||||||||||||||||
| 547 | Q_D(QEventDispatcherUNIX); | - | ||||||||||||||||||
| 548 | d->interrupt.store(1); | - | ||||||||||||||||||
| 549 | wakeUp(); | - | ||||||||||||||||||
| 550 | } never executed: end of block | 0 | ||||||||||||||||||
| 551 | - | |||||||||||||||||||
| 552 | void QEventDispatcherUNIX::flush() | - | ||||||||||||||||||
| 553 | { } | - | ||||||||||||||||||
| 554 | - | |||||||||||||||||||
| 555 | QT_END_NAMESPACE | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |