diff options
author | Yang Tse <yangsita@gmail.com> | 2008-05-09 16:30:24 +0000 |
---|---|---|
committer | Yang Tse <yangsita@gmail.com> | 2008-05-09 16:30:24 +0000 |
commit | 0848b4fdaa130054c981194afa2ff0cd136d2590 (patch) | |
tree | 7c155964a353e2a872427b3aa1402e3f9be02f7a /ares__timeval.c | |
parent | fd5fdecf303826473ccb50a8762a331473b3c481 (diff) | |
download | c-ares-0848b4fdaa130054c981194afa2ff0cd136d2590.tar.gz c-ares-0848b4fdaa130054c981194afa2ff0cd136d2590.tar.bz2 c-ares-0848b4fdaa130054c981194afa2ff0cd136d2590.zip |
Use monotonic time source if available.
Diffstat (limited to 'ares__timeval.c')
-rw-r--r-- | ares__timeval.c | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/ares__timeval.c b/ares__timeval.c new file mode 100644 index 0000000..0d8bbfa --- /dev/null +++ b/ares__timeval.c @@ -0,0 +1,95 @@ +/* $Id$ */ + +/* Copyright (C) 2008 by Daniel Stenberg et al + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose and without fee is hereby granted, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of M.I.T. not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. M.I.T. makes no representations about the + * suitability of this software for any purpose. It is provided "as is" + * without express or implied warranty. + */ + +#include "setup.h" +#include "ares.h" +#include "ares_private.h" + +#if defined(WIN32) && !defined(MSDOS) + +struct timeval ares__tvnow(void) +{ + /* + ** GetTickCount() is available on _all_ Windows versions from W95 up + ** to nowadays. Returns milliseconds elapsed since last system boot, + ** increases monotonically and wraps once 49.7 days have elapsed. + */ + struct timeval now; + DWORD milliseconds = GetTickCount(); + now.tv_sec = milliseconds / 1000; + now.tv_usec = (milliseconds % 1000) * 1000; + return now; +} + +#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) + +struct timeval ares__tvnow(void) +{ + /* + ** clock_gettime() is granted to be increased monotonically when the + ** monotonic clock is queried. Time starting point is unspecified, it + ** could be the system start-up time, the Epoch, or something else, + ** in any case the time starting point does not change once that the + ** system has started up. + */ + 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; + return now; +} + +#elif defined(HAVE_GETTIMEOFDAY) + +struct timeval ares__tvnow(void) +{ + /* + ** gettimeofday() is not granted to be increased monotonically, due to + ** clock drifting and external source time synchronization it can jump + ** forward or backward in time. + */ + struct timeval now; + (void)gettimeofday(&now, NULL); + return now; +} + +#else + +struct timeval ares__tvnow(void) +{ + /* + ** time() returns the value of time in seconds since the Epoch. + */ + struct timeval now; + now.tv_sec = (long)time(NULL); + now.tv_usec = 0; + return now; +} + +#endif + +/* + * Make sure that the first argument is the more recent time, as otherwise + * we'll get a weird negative time-diff back... + * + * Returns: the time difference in number of milliseconds. + */ +long ares__tvdiff(struct timeval newer, struct timeval older) +{ + return (newer.tv_sec-older.tv_sec)*1000+ + (newer.tv_usec-older.tv_usec)/1000; +} + |