| 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) 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 "qcore_unix_p.h" | - | ||||||||||||
| 42 | #include "qelapsedtimer.h" | - | ||||||||||||
| 43 | - | |||||||||||||
| 44 | #include <stdlib.h> | - | ||||||||||||
| 45 | - | |||||||||||||
| 46 | #ifdef Q_OS_MAC | - | ||||||||||||
| 47 | #include <mach/mach_time.h> | - | ||||||||||||
| 48 | #endif | - | ||||||||||||
| 49 | - | |||||||||||||
| 50 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | #if !defined(QT_HAVE_PPOLL) && defined(QT_HAVE_POLLTS) | - | ||||||||||||
| 53 | # define ppoll pollts | - | ||||||||||||
| 54 | # define QT_HAVE_PPOLL | - | ||||||||||||
| 55 | #endif | - | ||||||||||||
| 56 | - | |||||||||||||
| 57 | static inline bool time_update(struct timespec *tv, const struct timespec &start, | - | ||||||||||||
| 58 | const struct timespec &timeout) | - | ||||||||||||
| 59 | { | - | ||||||||||||
| 60 | // clock source is (hopefully) monotonic, so we can recalculate how much timeout is left; | - | ||||||||||||
| 61 | // if it isn't monotonic, we'll simply hope that it hasn't jumped, because we have no alternative | - | ||||||||||||
| 62 | struct timespec now = qt_gettime(); | - | ||||||||||||
| 63 | *tv = timeout + start - now; | - | ||||||||||||
| 64 | return tv->tv_sec >= 0; executed 2108 times by 24 tests:  return tv->tv_sec >= 0;Executed by: 
 | 2108 | ||||||||||||
| 65 | } | - | ||||||||||||
| 66 | - | |||||||||||||
| 67 | #if !defined(QT_HAVE_PPOLL) && defined(QT_HAVE_POLL) | - | ||||||||||||
| 68 | static inline int timespecToMillisecs(const struct timespec *ts) | - | ||||||||||||
| 69 | { | - | ||||||||||||
| 70 | return (ts == NULL) ? -1 : executed 324551 times by 56 tests:  return (ts == __null) ? -1 : (ts->tv_sec * 1000) + (ts->tv_nsec / 1000000);Executed by: 
 | 324551 | ||||||||||||
| 71 | (ts->tv_sec * 1000) + (ts->tv_nsec / 1000000); executed 324551 times by 56 tests:  return (ts == __null) ? -1 : (ts->tv_sec * 1000) + (ts->tv_nsec / 1000000);Executed by: 
 | 324551 | ||||||||||||
| 72 | } | - | ||||||||||||
| 73 | #endif | - | ||||||||||||
| 74 | - | |||||||||||||
| 75 | // defined in qpoll.cpp | - | ||||||||||||
| 76 | int qt_poll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts); | - | ||||||||||||
| 77 | - | |||||||||||||
| 78 | static inline int qt_ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts) | - | ||||||||||||
| 79 | { | - | ||||||||||||
| 80 | #if defined(QT_HAVE_PPOLL) | - | ||||||||||||
| 81 | return ::ppoll(fds, nfds, timeout_ts, Q_NULLPTR); | - | ||||||||||||
| 82 | #elif defined(QT_HAVE_POLL) | - | ||||||||||||
| 83 | return ::poll(fds, nfds, timespecToMillisecs(timeout_ts)); executed 324551 times by 56 tests:  return ::poll(fds, nfds, timespecToMillisecs(timeout_ts));Executed by: 
 | 324551 | ||||||||||||
| 84 | #else | - | ||||||||||||
| 85 | return qt_poll(fds, nfds, timeout_ts); | - | ||||||||||||
| 86 | #endif | - | ||||||||||||
| 87 | } | - | ||||||||||||
| 88 | - | |||||||||||||
| 89 | - | |||||||||||||
| 90 | /*! | - | ||||||||||||
| 91 | \internal | - | ||||||||||||
| 92 | - | |||||||||||||
| 93 | Behaves as close to POSIX poll(2) as practical but may be implemented | - | ||||||||||||
| 94 | using select(2) where necessary. In that case, returns -1 and sets errno | - | ||||||||||||
| 95 | to EINVAL if passed any descriptor greater than or equal to FD_SETSIZE. | - | ||||||||||||
| 96 | */ | - | ||||||||||||
| 97 | int qt_safe_poll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts) | - | ||||||||||||
| 98 | { | - | ||||||||||||
| 99 | if (!timeout_ts) { 
 | 765-321646 | ||||||||||||
| 100 | // no timeout -> block forever | - | ||||||||||||
| 101 | int ret; | - | ||||||||||||
| 102 | EINTR_LOOP(ret, qt_ppoll(fds, nfds, Q_NULLPTR)); executed 797 times by 13 tests:  end of blockExecuted by: 
 
 
 | 0-797 | ||||||||||||
| 103 | return ret; executed 765 times by 13 tests:  return ret;Executed by: 
 | 765 | ||||||||||||
| 104 | } | - | ||||||||||||
| 105 | - | |||||||||||||
| 106 | timespec start = qt_gettime(); | - | ||||||||||||
| 107 | timespec timeout = *timeout_ts; | - | ||||||||||||
| 108 | - | |||||||||||||
| 109 | // loop and recalculate the timeout as needed | - | ||||||||||||
| 110 | forever { | - | ||||||||||||
| 111 | const int ret = qt_ppoll(fds, nfds, &timeout); | - | ||||||||||||
| 112 | if (ret != -1 || errno != EINTR) 
 
 | 0-321646 | ||||||||||||
| 113 | return ret; executed 321646 times by 53 tests:  return ret;Executed by: 
 | 321646 | ||||||||||||
| 114 | - | |||||||||||||
| 115 | // recalculate the timeout | - | ||||||||||||
| 116 | if (!time_update(&timeout, start, *timeout_ts)) { 
 | 0-2108 | ||||||||||||
| 117 | // timeout during update | - | ||||||||||||
| 118 | // or clock reset, fake timeout error | - | ||||||||||||
| 119 | return 0; never executed:  return 0; | 0 | ||||||||||||
| 120 | } | - | ||||||||||||
| 121 | } executed 2108 times by 24 tests:  end of blockExecuted by: 
 | 2108 | ||||||||||||
| 122 | } never executed:  end of block | 0 | ||||||||||||
| 123 | - | |||||||||||||
| 124 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |