21 #ifndef XAPIAN_INCLUDED_REALTIME_H 22 #define XAPIAN_INCLUDED_REALTIME_H 25 # error config.h must be included first in each C++ source file 34 # include <sys/timeb.h> 36 # ifdef HAVE_GETTIMEOFDAY 37 # include <sys/time.h> 41 # include <sys/types.h> 42 # include <sys/timeb.h> 43 extern void xapian_sleep_milliseconds(
unsigned int millisecs);
50 #if defined HAVE_CLOCK_GETTIME 61 if (
usual(clock_gettime(CLOCK_REALTIME, &ts) == 0))
62 return ts.tv_sec + (ts.tv_nsec * 1e-9);
63 return double(std::time(NULL));
64 #elif !defined __WIN32__ 65 # if defined HAVE_GETTIMEOFDAY 67 if (
usual(gettimeofday(&tv, NULL) == 0))
68 return tv.tv_sec + (tv.tv_usec * 1e-6);
69 return double(std::time(NULL));
70 # elif defined HAVE_FTIME 72 # ifdef FTIME_RETURNS_VOID 75 if (
rare(ftime(&tp) != 0))
76 return double(std::time(NULL));
78 return tp.time + (tp.millitm * 1e-3);
80 return double(std::time(NULL));
86 return tp.time + tp.millitm * 1e-3;
96 return (timeout == 0.0 ? timeout : timeout +
now());
99 #if defined HAVE_NANOSLEEP || defined HAVE_TIMER_CREATE 100 inline void to_timespec(
double t,
struct timespec *ts) {
103 ts->tv_nsec = long(std::modf(t, &secs) * 1e9);
104 ts->tv_sec = long(secs);
112 tv->tv_usec = long(std::modf(t, &secs) * 1e6);
113 tv->tv_sec = long(secs);
117 # define to_timeval(T, TV) to_timeval_((T), (TV)->tv_sec, (TV)->tv_usec) 119 inline void to_timeval_(
double t,
long & tv_sec,
long & tv_usec) {
121 tv_usec = long(std::modf(t, &secs) * 1e6);
128 #ifdef HAVE_NANOSLEEP 134 to_timespec(delta, &ts);
135 while (nanosleep(&ts, &ts) < 0 && errno == EINTR) { }
136 #elif !defined __WIN32__ 144 }
while (select(0, NULL, NULL, NULL, &tv) < 0 &&
145 (errno == EINTR || errno == EAGAIN));
150 while (
rare(delta > 4294967.0)) {
151 xapian_sleep_milliseconds(4294967000u);
154 xapian_sleep_milliseconds(
unsigned(delta * 1000.0));
160 #endif // XAPIAN_INCLUDED_REALTIME_H include <sys/select.h> with portability workarounds.
unsigned timeout
A timeout value in milliseconds.
void sleep(double t)
Sleep until the time represented by this object.
double end_time(double timeout)
Return the end time for a timeout in timeout seconds.
double now()
Return the current time.
void to_timeval(double t, struct timeval *tv)
Fill in struct timeval from number of seconds in a double.