author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qcore_unix_p.h" |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
43 |
#include "qelapsedtimer.h" |
0 | 44 |
|
45 |
#ifndef Q_OS_VXWORKS |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
46 |
# if !defined(Q_OS_HPUX) || defined(__ia64) |
0 | 47 |
# include <sys/select.h> |
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
48 |
# endif |
0 | 49 |
# include <sys/time.h> |
50 |
#else |
|
51 |
# include <selectLib.h> |
|
52 |
#endif |
|
53 |
||
54 |
#include <stdlib.h> |
|
55 |
||
56 |
#ifdef Q_OS_MAC |
|
57 |
#include <mach/mach_time.h> |
|
58 |
#endif |
|
59 |
||
60 |
QT_BEGIN_NAMESPACE |
|
61 |
||
62 |
static inline bool time_update(struct timeval *tv, const struct timeval &start, |
|
63 |
const struct timeval &timeout) |
|
64 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
65 |
if (!QElapsedTimer::isMonotonic()) { |
0 | 66 |
// we cannot recalculate the timeout without a monotonic clock as the time may have changed |
67 |
return false; |
|
68 |
} |
|
69 |
||
70 |
// clock source is monotonic, so we can recalculate how much timeout is left |
|
71 |
struct timeval now = qt_gettime(); |
|
72 |
*tv = timeout + start - now; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
73 |
return tv->tv_sec >= 0; |
0 | 74 |
} |
75 |
||
76 |
int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept, |
|
77 |
const struct timeval *orig_timeout) |
|
78 |
{ |
|
79 |
if (!orig_timeout) { |
|
80 |
// no timeout -> block forever |
|
81 |
register int ret; |
|
82 |
EINTR_LOOP(ret, select(nfds, fdread, fdwrite, fdexcept, 0)); |
|
83 |
return ret; |
|
84 |
} |
|
85 |
||
86 |
timeval start = qt_gettime(); |
|
87 |
timeval timeout = *orig_timeout; |
|
88 |
||
89 |
// loop and recalculate the timeout as needed |
|
90 |
int ret; |
|
91 |
forever { |
|
92 |
ret = ::select(nfds, fdread, fdwrite, fdexcept, &timeout); |
|
93 |
if (ret != -1 || errno != EINTR) |
|
94 |
return ret; |
|
95 |
||
96 |
// recalculate the timeout |
|
97 |
if (!time_update(&timeout, start, *orig_timeout)) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
98 |
// timeout during update |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
99 |
// or clock reset, fake timeout error |
0 | 100 |
return 0; |
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
QT_END_NAMESPACE |