"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "common/realtime.h" between
xapian-omega-1.4.18.tar.xz and xapian-omega-1.4.19.tar.xz

About: Xapian Omega is an application built on Xapian, consisting of indexers and a CGI search frontend.

realtime.h  (xapian-omega-1.4.18.tar.xz):realtime.h  (xapian-omega-1.4.19.tar.xz)
/** @file realtime.h /** @file
* @brief Functions for handling a time or time interval in a double. * @brief Functions for handling a time or time interval in a double.
*/ */
/* Copyright (C) 2010,2011,2013,2014,2015 Olly Betts /* Copyright (C) 2010,2011,2013,2014,2015,2020 Olly Betts
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
skipping to change at line 50 skipping to change at line 50
#else #else
# include <sys/types.h> # include <sys/types.h>
# include <sys/timeb.h> # include <sys/timeb.h>
extern void xapian_sleep_milliseconds(unsigned int millisecs); extern void xapian_sleep_milliseconds(unsigned int millisecs);
#endif #endif
namespace RealTime { namespace RealTime {
/// Return the current time. /// Return the current time.
inline double now() { inline double now() {
#ifndef __WIN32__ #if defined HAVE_CLOCK_GETTIME
// We prefer clock_gettime() over gettimeofday() over ftime(). This order // We prefer functions which can return the time with a higher precision.
// favours functions which can return the time with a higher precision. // On POSIX platforms that means we prefer clock_gettime() over
// gettimeofday() over ftime().
//
// (Also, POSIX.1-2008 stopped specifying ftime(), and marked gettimeofday()
// as obsolete, recommending clock_gettime() instead.)
// //
// Also, POSIX.1-2008 stopped specifying ftime(), and marked gettimeofday() // Modern mingw provides clock_gettime(). For older mingw and MSVC we fall
// as obsolete, recommending clock_gettime() instead. // back to _ftime64().
# if defined HAVE_CLOCK_GETTIME
struct timespec ts; struct timespec ts;
if (usual(clock_gettime(CLOCK_REALTIME, &ts) == 0)) if (usual(clock_gettime(CLOCK_REALTIME, &ts) == 0))
return ts.tv_sec + (ts.tv_nsec * 1e-9); return ts.tv_sec + (ts.tv_nsec * 1e-9);
return double(std::time(NULL)); return double(std::time(NULL));
# elif defined HAVE_GETTIMEOFDAY #elif !defined __WIN32__
# if defined HAVE_GETTIMEOFDAY
struct timeval tv; struct timeval tv;
if (usual(gettimeofday(&tv, NULL) == 0)) if (usual(gettimeofday(&tv, NULL) == 0))
return tv.tv_sec + (tv.tv_usec * 1e-6); return tv.tv_sec + (tv.tv_usec * 1e-6);
return double(std::time(NULL)); return double(std::time(NULL));
# elif defined HAVE_FTIME # elif defined HAVE_FTIME
struct timeb tp; struct timeb tp;
# ifdef FTIME_RETURNS_VOID # ifdef FTIME_RETURNS_VOID
ftime(&tp); ftime(&tp);
# else # else
if (rare(ftime(&tp) != 0)) if (rare(ftime(&tp) != 0))
return double(std::time(NULL)); return double(std::time(NULL));
# endif # endif
return tp.time + (tp.millitm * 1e-3); return tp.time + (tp.millitm * 1e-3);
# else # else
return double(std::time(NULL)); return double(std::time(NULL));
# endif # endif
#else #else
// For __WIN32__.
struct __timeb64 tp; struct __timeb64 tp;
_ftime64(&tp); _ftime64(&tp);
return tp.time + tp.millitm * 1e-3; return tp.time + tp.millitm * 1e-3;
#endif #endif
} }
/** Return the end time for a timeout in @a timeout seconds. /** Return the end time for a timeout in @a timeout seconds.
* *
* If @a timeout is 0, that means "no timeout", so 0 is returned. Otherwise * If @a timeout is 0, that means "no timeout", so 0 is returned. Otherwise
* the current time plus @a timeout seconds is returned. * the current time plus @a timeout seconds is returned.
*/ */
inline double end_time(double timeout) { inline double end_time(double timeout) {
return (timeout == 0.0 ? timeout : timeout + now()); return (timeout == 0.0 ? timeout : timeout + now());
} }
#ifndef __WIN32__ #if defined HAVE_NANOSLEEP || defined HAVE_TIMER_CREATE
# if defined HAVE_NANOSLEEP || defined HAVE_TIMER_CREATE
/// Fill in struct timespec from number of seconds in a double. /// Fill in struct timespec from number of seconds in a double.
inline void to_timespec(double t, struct timespec *ts) { inline void to_timespec(double t, struct timespec *ts) {
double secs; double secs;
ts->tv_nsec = long(std::modf(t, &secs) * 1e9); ts->tv_nsec = long(std::modf(t, &secs) * 1e9);
ts->tv_sec = long(secs); ts->tv_sec = long(secs);
} }
# endif #endif
/// Fill in struct timeval from number of seconds in a double. /// Fill in struct timeval from number of seconds in a double.
#ifndef __WIN32__
inline void to_timeval(double t, struct timeval *tv) { inline void to_timeval(double t, struct timeval *tv) {
double secs; double secs;
tv->tv_usec = long(std::modf(t, &secs) * 1e6); tv->tv_usec = long(std::modf(t, &secs) * 1e6);
tv->tv_sec = long(secs); tv->tv_sec = long(secs);
} }
#else #else
// Use a macro to avoid having to pull in winsock2.h just for struct timeval. // Use a macro to avoid having to pull in winsock2.h just for struct timeval.
#define to_timeval(T, TV) to_timeval_((T), (TV)->tv_sec, (TV)->tv_usec) # define to_timeval(T, TV) to_timeval_((T), (TV)->tv_sec, (TV)->tv_usec)
inline void to_timeval_(double t, long & tv_sec, long & tv_usec) { inline void to_timeval_(double t, long & tv_sec, long & tv_usec) {
double secs; double secs;
tv_usec = long(std::modf(t, &secs) * 1e6); tv_usec = long(std::modf(t, &secs) * 1e6);
tv_sec = long(secs); tv_sec = long(secs);
} }
#endif #endif
/// Sleep until the time represented by this object. /// Sleep until the time represented by this object.
inline void sleep(double t) { inline void sleep(double t) {
#ifndef __WIN32__ #ifdef HAVE_NANOSLEEP
# ifdef HAVE_NANOSLEEP // Available on modern POSIX systems and under modern mingw.
double delta = t - RealTime::now(); double delta = t - RealTime::now();
if (delta <= 0.0) if (delta <= 0.0)
return; return;
struct timespec ts; struct timespec ts;
to_timespec(delta, &ts); to_timespec(delta, &ts);
while (nanosleep(&ts, &ts) < 0 && errno == EINTR) { } while (nanosleep(&ts, &ts) < 0 && errno == EINTR) { }
# else #elif !defined __WIN32__
double delta; double delta;
struct timeval tv; struct timeval tv;
do { do {
delta = t - RealTime::now(); delta = t - RealTime::now();
if (delta <= 0.0) if (delta <= 0.0)
return; return;
to_timeval(delta, &tv); to_timeval(delta, &tv);
} while (select(0, NULL, NULL, NULL, &tv) < 0 && } while (select(0, NULL, NULL, NULL, &tv) < 0 &&
(errno == EINTR || errno == EAGAIN)); (errno == EINTR || errno == EAGAIN));
# endif
#else #else
double delta = t - RealTime::now(); double delta = t - RealTime::now();
if (delta <= 0.0) if (delta <= 0.0)
return; return;
while (rare(delta > 4294967.0)) { while (rare(delta > 4294967.0)) {
xapian_sleep_milliseconds(4294967000u); xapian_sleep_milliseconds(4294967000u);
delta -= 4294967.0; delta -= 4294967.0;
} }
xapian_sleep_milliseconds(unsigned(delta * 1000.0)); xapian_sleep_milliseconds(unsigned(delta * 1000.0));
#endif #endif
 End of changes. 13 change blocks. 
17 lines changed or deleted 22 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)