Qt 4.8
Functions
qcore_unix.cpp File Reference
#include "qcore_unix_p.h"
#include "qelapsedtimer.h"
#include <sys/select.h>
#include <sys/time.h>
#include <stdlib.h>
#include <mach/mach_time.h>

Go to the source code of this file.

Functions

int qt_safe_select (int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept, const struct timeval *orig_timeout)
 
static bool time_update (struct timeval *tv, const struct timeval &start, const struct timeval &timeout)
 

Function Documentation

◆ qt_safe_select()

int qt_safe_select ( int  nfds,
fd_set *  fdread,
fd_set *  fdwrite,
fd_set *  fdexcept,
const struct timeval *  orig_timeout 
)

Definition at line 73 of file qcore_unix.cpp.

Referenced by QNativeSocketEnginePrivate::nativeSelect(), SelectWorker::run(), QEventDispatcherUNIX::select(), and select_msecs().

75 {
76  if (!orig_timeout) {
77  // no timeout -> block forever
78  register int ret;
79  EINTR_LOOP(ret, select(nfds, fdread, fdwrite, fdexcept, 0));
80  return ret;
81  }
82 
83  timeval start = qt_gettime();
84  timeval timeout = *orig_timeout;
85 
86  // loop and recalculate the timeout as needed
87  int ret;
88  forever {
89  ret = ::select(nfds, fdread, fdwrite, fdexcept, &timeout);
90  if (ret != -1 || errno != EINTR)
91  return ret;
92 
93  // recalculate the timeout
94  if (!time_update(&timeout, start, *orig_timeout)) {
95  // timeout during update
96  // or clock reset, fake timeout error
97  return 0;
98  }
99  }
100 }
#define EINTR_LOOP(var, cmd)
Definition: qcore_unix_p.h:96
int select(int, fd_set *, fd_set *, fd_set *, struct timeval *)
timeval qt_gettime()
int errno
static bool time_update(struct timeval *tv, const struct timeval &start, const struct timeval &timeout)
Definition: qcore_unix.cpp:63
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452

◆ time_update()

static bool time_update ( struct timeval *  tv,
const struct timeval &  start,
const struct timeval &  timeout 
)
inlinestatic

Definition at line 63 of file qcore_unix.cpp.

Referenced by qt_safe_select().

65 {
66  // clock source is (hopefully) monotonic, so we can recalculate how much timeout is left;
67  // if it isn't monotonic, we'll simply hope that it hasn't jumped, because we have no alternative
68  struct timeval now = qt_gettime();
69  *tv = timeout + start - now;
70  return tv->tv_sec >= 0;
71 }
timeval qt_gettime()