| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qcore_unix.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
| 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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
| 25 | ** | - | ||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
| 29 | ** | - | ||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 31 | ** | - | ||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | #include "qcore_unix_p.h" | - | ||||||||||||
| 35 | #include "qelapsedtimer.h" | - | ||||||||||||
| 36 | - | |||||||||||||
| 37 | #include <stdlib.h> | - | ||||||||||||
| 38 | - | |||||||||||||
| 39 | #ifdef Q_OS_MAC | - | ||||||||||||
| 40 | #include <mach/mach_time.h> | - | ||||||||||||
| 41 | #endif | - | ||||||||||||
| 42 | - | |||||||||||||
| 43 | #ifdef Q_OS_BLACKBERRY | - | ||||||||||||
| 44 | #include <qsocketnotifier.h> | - | ||||||||||||
| 45 | #endif // Q_OS_BLACKBERRY | - | ||||||||||||
| 46 | - | |||||||||||||
| 47 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 48 | - | |||||||||||||
| 49 | static inline bool time_update(struct timespec *tv, const struct timespec &start, | - | ||||||||||||
| 50 | const struct timespec &timeout) | - | ||||||||||||
| 51 | { | - | ||||||||||||
| 52 | // clock source is (hopefully) monotonic, so we can recalculate how much timeout is left; | - | ||||||||||||
| 53 | // if it isn't monotonic, we'll simply hope that it hasn't jumped, because we have no alternative | - | ||||||||||||
| 54 | struct timespec now = qt_gettime(); | - | ||||||||||||
| 55 | *tv = timeout + start - now; | - | ||||||||||||
| 56 | return tv->tv_sec >= 0; executed 1564 times by 23 tests: return tv->tv_sec >= 0;Executed by:
| 1564 | ||||||||||||
| 57 | } | - | ||||||||||||
| 58 | - | |||||||||||||
| 59 | int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept, | - | ||||||||||||
| 60 | const struct timespec *orig_timeout) | - | ||||||||||||
| 61 | { | - | ||||||||||||
| 62 | if (!orig_timeout) {
| 763-301848 | ||||||||||||
| 63 | // no timeout -> block forever | - | ||||||||||||
| 64 | int ret; | - | ||||||||||||
| 65 | EINTR_LOOP(ret, select(nfds, fdread, fdwrite, fdexcept, 0)); executed 795 times by 13 tests: end of blockExecuted by:
| 0-795 | ||||||||||||
| 66 | return ret; executed 763 times by 13 tests: return ret;Executed by:
| 763 | ||||||||||||
| 67 | } | - | ||||||||||||
| 68 | - | |||||||||||||
| 69 | timespec start = qt_gettime(); | - | ||||||||||||
| 70 | timespec timeout = *orig_timeout; | - | ||||||||||||
| 71 | - | |||||||||||||
| 72 | // loop and recalculate the timeout as needed | - | ||||||||||||
| 73 | int ret; | - | ||||||||||||
| 74 | forever { | - | ||||||||||||
| 75 | #ifndef Q_OS_QNX | - | ||||||||||||
| 76 | ret = ::pselect(nfds, fdread, fdwrite, fdexcept, &timeout, 0); | - | ||||||||||||
| 77 | #else | - | ||||||||||||
| 78 | timeval timeoutVal; | - | ||||||||||||
| 79 | timeoutVal.tv_sec = timeout.tv_sec; | - | ||||||||||||
| 80 | timeoutVal.tv_usec = timeout.tv_nsec / 1000; | - | ||||||||||||
| 81 | ret = ::select(nfds, fdread, fdwrite, fdexcept, &timeoutVal); | - | ||||||||||||
| 82 | #endif | - | ||||||||||||
| 83 | if (ret != -1 || errno != EINTR)
| 0-301848 | ||||||||||||
| 84 | return ret; executed 301848 times by 52 tests: return ret;Executed by:
| 301848 | ||||||||||||
| 85 | - | |||||||||||||
| 86 | // recalculate the timeout | - | ||||||||||||
| 87 | if (!time_update(&timeout, start, *orig_timeout)) {
| 0-1564 | ||||||||||||
| 88 | // timeout during update | - | ||||||||||||
| 89 | // or clock reset, fake timeout error | - | ||||||||||||
| 90 | return 0; never executed: return 0; | 0 | ||||||||||||
| 91 | } | - | ||||||||||||
| 92 | } executed 1564 times by 23 tests: end of blockExecuted by:
| 1564 | ||||||||||||
| 93 | } never executed: end of block | 0 | ||||||||||||
| 94 | - | |||||||||||||
| 95 | int qt_select_msecs(int nfds, fd_set *fdread, fd_set *fdwrite, int timeout) | - | ||||||||||||
| 96 | { | - | ||||||||||||
| 97 | if (timeout < 0)
| 659-41215 | ||||||||||||
| 98 | return qt_safe_select(nfds, fdread, fdwrite, 0, 0); executed 659 times by 6 tests: return qt_safe_select(nfds, fdread, fdwrite, 0, 0);Executed by:
| 659 | ||||||||||||
| 99 | - | |||||||||||||
| 100 | struct timespec tv; | - | ||||||||||||
| 101 | tv.tv_sec = timeout / 1000; | - | ||||||||||||
| 102 | tv.tv_nsec = (timeout % 1000) * 1000 * 1000; | - | ||||||||||||
| 103 | return qt_safe_select(nfds, fdread, fdwrite, 0, &tv); executed 41215 times by 34 tests: return qt_safe_select(nfds, fdread, fdwrite, 0, &tv);Executed by:
| 41215 | ||||||||||||
| 104 | } | - | ||||||||||||
| 105 | - | |||||||||||||
| 106 | #ifdef Q_OS_BLACKBERRY | - | ||||||||||||
| 107 | // The BlackBerry event dispatcher uses bps_get_event. Unfortunately, already registered | - | ||||||||||||
| 108 | // socket notifiers are disabled by a call to select. This is to rearm the standard streams. | - | ||||||||||||
| 109 | int bb_select(QList<QSocketNotifier *> socketNotifiers, int nfds, fd_set *fdread, fd_set *fdwrite, | - | ||||||||||||
| 110 | int timeout) | - | ||||||||||||
| 111 | { | - | ||||||||||||
| 112 | QList<bool> socketNotifiersEnabled; | - | ||||||||||||
| 113 | socketNotifiersEnabled.reserve(socketNotifiers.count()); | - | ||||||||||||
| 114 | for (int a = 0; a < socketNotifiers.count(); ++a) { | - | ||||||||||||
| 115 | if (socketNotifiers.at(a) && socketNotifiers.at(a)->isEnabled()) { | - | ||||||||||||
| 116 | socketNotifiersEnabled.append(true); | - | ||||||||||||
| 117 | socketNotifiers.at(a)->setEnabled(false); | - | ||||||||||||
| 118 | } else { | - | ||||||||||||
| 119 | socketNotifiersEnabled.append(false); | - | ||||||||||||
| 120 | } | - | ||||||||||||
| 121 | } | - | ||||||||||||
| 122 | - | |||||||||||||
| 123 | const int ret = qt_select_msecs(nfds, fdread, fdwrite, timeout); | - | ||||||||||||
| 124 | - | |||||||||||||
| 125 | for (int a = 0; a < socketNotifiers.count(); ++a) { | - | ||||||||||||
| 126 | if (socketNotifiersEnabled.at(a) == true) | - | ||||||||||||
| 127 | socketNotifiers.at(a)->setEnabled(true); | - | ||||||||||||
| 128 | } | - | ||||||||||||
| 129 | - | |||||||||||||
| 130 | return ret; | - | ||||||||||||
| 131 | } | - | ||||||||||||
| 132 | #endif // Q_OS_BLACKBERRY | - | ||||||||||||
| 133 | - | |||||||||||||
| 134 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |