| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). |
| 4 |
** All rights reserved. |
| 5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
| 6 |
** |
| 7 |
** This file is part of the QtCore module of the Qt Toolkit. |
| 8 |
** |
| 9 |
** $QT_BEGIN_LICENSE:LGPL$ |
| 10 |
** GNU Lesser General Public License Usage |
| 11 |
** This file may be used under the terms of the GNU Lesser General Public |
| 12 |
** License version 2.1 as published by the Free Software Foundation and |
| 13 |
** appearing in the file LICENSE.LGPL included in the packaging of this |
| 14 |
** file. Please review the following information to ensure the GNU Lesser |
| 15 |
** General Public License version 2.1 requirements will be met: |
| 16 |
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 17 |
** |
| 18 |
** In addition, as a special exception, Nokia gives you certain additional |
| 19 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
| 20 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 21 |
** |
| 22 |
** GNU General Public License Usage |
| 23 |
** Alternatively, this file may be used under the terms of the GNU General |
| 24 |
** Public License version 3.0 as published by the Free Software Foundation |
| 25 |
** and appearing in the file LICENSE.GPL included in the packaging of this |
| 26 |
** file. Please review the following information to ensure the GNU General |
| 27 |
** Public License version 3.0 requirements will be met: |
| 28 |
** http://www.gnu.org/copyleft/gpl.html. |
| 29 |
** |
| 30 |
** Other Usage |
| 31 |
** Alternatively, this file may be used in accordance with the terms and |
| 32 |
** conditions contained in a signed written agreement between you and Nokia. |
| 33 |
** |
| 34 |
** |
| 35 |
** |
| 36 |
** |
| 37 |
** |
| 38 |
** $QT_END_LICENSE$ |
| 39 |
** |
| 40 |
****************************************************************************/ |
| 41 |
|
| 42 |
#include "qcore_unix_p.h" |
| 43 |
#include "qelapsedtimer.h" |
| 44 |
|
| 45 |
#ifdef Q_OS_NACL |
| 46 |
#elif !defined (Q_OS_VXWORKS) |
| 47 |
# if !defined(Q_OS_HPUX) || defined(__ia64) |
| 48 |
# include <sys/select.h> |
| 49 |
# endif |
| 50 |
# include <sys/time.h> |
| 51 |
#else |
| 52 |
# include <selectLib.h> |
| 53 |
#endif |
| 54 |
|
| 55 |
#include <stdlib.h> |
| 56 |
|
| 57 |
#ifdef Q_OS_MAC |
| 58 |
#include <mach/mach_time.h> |
| 59 |
#endif |
| 60 |
|
| 61 |
QT_BEGIN_NAMESPACE |
| 62 |
|
| 63 |
static inline bool time_update(struct timeval *tv, const struct timeval &start, |
| 64 |
const struct timeval &timeout) |
| 65 |
{ |
| 66 |
if (!QElapsedTimer::isMonotonic()) { |
| 67 |
// we cannot recalculate the timeout without a monotonic clock as the time may have changed |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
// clock source is monotonic, so we can recalculate how much timeout is left |
| 72 |
struct timeval now = qt_gettime(); |
| 73 |
*tv = timeout + start - now; |
| 74 |
return tv->tv_sec >= 0; |
| 75 |
} |
| 76 |
|
| 77 |
int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept, |
| 78 |
const struct timeval *orig_timeout) |
| 79 |
{ |
| 80 |
if (!orig_timeout) { |
| 81 |
// no timeout -> block forever |
| 82 |
register int ret; |
| 83 |
EINTR_LOOP(ret, select(nfds, fdread, fdwrite, fdexcept, 0)); |
| 84 |
return ret; |
| 85 |
} |
| 86 |
|
| 87 |
timeval start = qt_gettime(); |
| 88 |
timeval timeout = *orig_timeout; |
| 89 |
|
| 90 |
// loop and recalculate the timeout as needed |
| 91 |
int ret; |
| 92 |
forever { |
| 93 |
ret = ::select(nfds, fdread, fdwrite, fdexcept, &timeout); |
| 94 |
if (ret != -1 || errno != EINTR) |
| 95 |
return ret; |
| 96 |
|
| 97 |
// recalculate the timeout |
| 98 |
if (!time_update(&timeout, start, *orig_timeout)) { |
| 99 |
// timeout during update |
| 100 |
// or clock reset, fake timeout error |
| 101 |
return 0; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
QT_END_NAMESPACE |