From: Yang Tse Date: Thu, 10 Jul 2008 07:16:45 +0000 (+0000) Subject: fallback to gettimeofday when monotonic clock is unavailable at run-time X-Git-Tag: curl-7_19_0~391 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d8f109176c909c3cca72c6b23d557b9a30f0c666;p=thirdparty%2Fcurl.git fallback to gettimeofday when monotonic clock is unavailable at run-time --- diff --git a/src/curlutil.c b/src/curlutil.c index 56cfa53c14..1712ce3c62 100644 --- a/src/curlutil.c +++ b/src/curlutil.c @@ -54,9 +54,24 @@ struct timeval cutil_tvnow(void) */ struct timeval now; struct timespec tsnow; - (void)clock_gettime(CLOCK_MONOTONIC, &tsnow); - now.tv_sec = tsnow.tv_sec; - now.tv_usec = tsnow.tv_nsec / 1000; + if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) { + now.tv_sec = tsnow.tv_sec; + now.tv_usec = tsnow.tv_nsec / 1000; + } + /* + ** Even when the configure process has truly detected monotonic clock + ** availability, it might happen that it is not actually available at + ** run-time. When this occurs simply fallback to other time source. + */ +#ifdef HAVE_GETTIMEOFDAY + else + (void)gettimeofday(&now, NULL); +#else + else { + now.tv_sec = (long)time(NULL); + now.tv_usec = 0; + } +#endif return now; } diff --git a/tests/libtest/testutil.c b/tests/libtest/testutil.c index 435b30e095..573e6faed2 100644 --- a/tests/libtest/testutil.c +++ b/tests/libtest/testutil.c @@ -54,9 +54,24 @@ struct timeval tutil_tvnow(void) */ struct timeval now; struct timespec tsnow; - (void)clock_gettime(CLOCK_MONOTONIC, &tsnow); - now.tv_sec = tsnow.tv_sec; - now.tv_usec = tsnow.tv_nsec / 1000; + if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) { + now.tv_sec = tsnow.tv_sec; + now.tv_usec = tsnow.tv_nsec / 1000; + } + /* + ** Even when the configure process has truly detected monotonic clock + ** availability, it might happen that it is not actually available at + ** run-time. When this occurs simply fallback to other time source. + */ +#ifdef HAVE_GETTIMEOFDAY + else + (void)gettimeofday(&now, NULL); +#else + else { + now.tv_sec = (long)time(NULL); + now.tv_usec = 0; + } +#endif return now; }