src/corelib/kernel/qcore_unix.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** 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"
       
    43 
       
    44 #ifndef Q_OS_VXWORKS
       
    45 #  include <sys/select.h>
       
    46 #  include <sys/time.h>
       
    47 #else
       
    48 #  include <selectLib.h>
       
    49 #endif
       
    50 
       
    51 #include <stdlib.h>
       
    52 
       
    53 #ifdef Q_OS_MAC
       
    54 #include <mach/mach_time.h>
       
    55 #endif
       
    56 
       
    57 #if !defined(QT_NO_CLOCK_MONOTONIC)
       
    58 # if defined(QT_BOOTSTRAPPED)
       
    59 #  define QT_NO_CLOCK_MONOTONIC
       
    60 # endif
       
    61 #endif
       
    62 
       
    63 QT_BEGIN_NAMESPACE
       
    64 
       
    65 bool qt_gettime_is_monotonic()
       
    66 {
       
    67 #if (_POSIX_MONOTONIC_CLOCK-0 > 0) || defined(Q_OS_MAC)
       
    68     return true;
       
    69 #else
       
    70     static int returnValue = 0;
       
    71 
       
    72     if (returnValue == 0) {
       
    73 #  if (_POSIX_MONOTONIC_CLOCK-0 < 0)
       
    74         returnValue = -1;
       
    75 #  elif (_POSIX_MONOTONIC_CLOCK == 0)
       
    76         // detect if the system support monotonic timers
       
    77         long x = sysconf(_SC_MONOTONIC_CLOCK);
       
    78         returnValue = (x >= 200112L) ? 1 : -1;
       
    79 #  endif
       
    80     }
       
    81 
       
    82     return returnValue != -1;
       
    83 #endif
       
    84 }
       
    85 
       
    86 timeval qt_gettime()
       
    87 {
       
    88     timeval tv;
       
    89 #if defined(Q_OS_MAC)
       
    90     static mach_timebase_info_data_t info = {0,0};
       
    91     if (info.denom == 0)
       
    92         mach_timebase_info(&info);
       
    93 
       
    94     uint64_t cpu_time = mach_absolute_time();
       
    95     uint64_t nsecs = cpu_time * (info.numer / info.denom);
       
    96     tv.tv_sec = nsecs / 1000000000ull;
       
    97     tv.tv_usec = (nsecs / 1000) - (tv.tv_sec * 1000000);
       
    98     return tv;
       
    99 #elif (_POSIX_MONOTONIC_CLOCK-0 > 0)
       
   100     timespec ts;
       
   101     clock_gettime(CLOCK_MONOTONIC, &ts);
       
   102     tv.tv_sec = ts.tv_sec;
       
   103     tv.tv_usec = ts.tv_nsec / 1000;
       
   104     return tv;
       
   105 #else
       
   106 #  if !defined(QT_NO_CLOCK_MONOTONIC) && !defined(QT_BOOTSTRAPPED)
       
   107     if (qt_gettime_is_monotonic()) {
       
   108         timespec ts;
       
   109         clock_gettime(CLOCK_MONOTONIC, &ts);
       
   110         tv.tv_sec = ts.tv_sec;
       
   111         tv.tv_usec = ts.tv_nsec / 1000;
       
   112         return tv;
       
   113     }
       
   114 #  endif
       
   115     // use gettimeofday
       
   116     ::gettimeofday(&tv, 0);
       
   117     return tv;
       
   118 #endif
       
   119 }
       
   120 
       
   121 static inline bool time_update(struct timeval *tv, const struct timeval &start,
       
   122                                const struct timeval &timeout)
       
   123 {
       
   124     if (!qt_gettime_is_monotonic()) {
       
   125         // we cannot recalculate the timeout without a monotonic clock as the time may have changed
       
   126         return false;
       
   127     }
       
   128 
       
   129     // clock source is monotonic, so we can recalculate how much timeout is left
       
   130     struct timeval now = qt_gettime();
       
   131     *tv = timeout + start - now;
       
   132     return true;
       
   133 }
       
   134 
       
   135 int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept,
       
   136                    const struct timeval *orig_timeout)
       
   137 {
       
   138     if (!orig_timeout) {
       
   139         // no timeout -> block forever
       
   140         register int ret;
       
   141         EINTR_LOOP(ret, select(nfds, fdread, fdwrite, fdexcept, 0));
       
   142         return ret;
       
   143     }
       
   144 
       
   145     timeval start = qt_gettime();
       
   146     timeval timeout = *orig_timeout;
       
   147 
       
   148     // loop and recalculate the timeout as needed
       
   149     int ret;
       
   150     forever {
       
   151         ret = ::select(nfds, fdread, fdwrite, fdexcept, &timeout);
       
   152         if (ret != -1 || errno != EINTR)
       
   153             return ret;
       
   154 
       
   155         // recalculate the timeout
       
   156         if (!time_update(&timeout, start, *orig_timeout)) {
       
   157             // clock reset, fake timeout error
       
   158             return 0;
       
   159         }
       
   160     }
       
   161 }
       
   162 
       
   163 QT_END_NAMESPACE