diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2013-07-04 12:51:57 +0300 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2013-07-04 12:51:57 +0300 |
commit | 736903c64bef394c06685d79908e397bcb08b88f (patch) | |
tree | 43fe081b093ef6ec278ae67099af4a25356a3008 /src/xz/mytime.c | |
parent | Update THANKS. (diff) | |
download | xz-736903c64bef394c06685d79908e397bcb08b88f.tar.xz |
xz: Move some of the timing code into mytime.[hc].
This switches units from microseconds to milliseconds.
New clock_gettime(CLOCK_MONOTONIC) will be used if available.
There is still a fallback to gettimeofday().
Diffstat (limited to '')
-rw-r--r-- | src/xz/mytime.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/xz/mytime.c b/src/xz/mytime.c new file mode 100644 index 00000000..4be184fd --- /dev/null +++ b/src/xz/mytime.c @@ -0,0 +1,89 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file mytime.c +/// \brief Time handling functions +// +// Author: Lasse Collin +// +// This file has been put into the public domain. +// You can do whatever you want with this file. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "private.h" + +#if !(defined(HAVE_CLOCK_GETTIME) && HAVE_DECL_CLOCK_MONOTONIC) +# include <sys/time.h> +#endif + +uint64_t opt_flush_timeout = 0; +bool flush_needed; + +static uint64_t start_time; +static uint64_t next_flush; + + +/// \brief Get the current time as milliseconds +/// +/// It's relative to some point but not necessarily to the UNIX Epoch. +static uint64_t +mytime_now(void) +{ + // NOTE: HAVE_DECL_CLOCK_MONOTONIC is always defined to 0 or 1. +#if defined(HAVE_CLOCK_GETTIME) && HAVE_DECL_CLOCK_MONOTONIC + // If CLOCK_MONOTONIC was available at compile time but for some + // reason isn't at runtime, fallback to CLOCK_REALTIME which + // according to POSIX is mandatory for all implementations. + static clockid_t clk_id = CLOCK_MONOTONIC; + struct timespec tv; + while (clock_gettime(clk_id, &tv)) + clk_id = CLOCK_REALTIME; + + return (uint64_t)(tv.tv_sec) * UINT64_C(1000) + tv.tv_nsec / 1000000; +#else + struct timeval tv; + gettimeofday(&tv, NULL); + return (uint64_t)(tv.tv_sec) * UINT64_C(1000) + tv.tv_usec / 1000; +#endif +} + + +extern void +mytime_set_start_time(void) +{ + start_time = mytime_now(); + next_flush = start_time + opt_flush_timeout; + flush_needed = false; + return; +} + + +extern uint64_t +mytime_get_elapsed(void) +{ + return mytime_now() - start_time; +} + + +extern void +mytime_set_flush_time(void) +{ + next_flush = mytime_now() + opt_flush_timeout; + flush_needed = false; + return; +} + + +extern int +mytime_get_flush_timeout(void) +{ + if (opt_flush_timeout == 0 || opt_mode != MODE_COMPRESS) + return -1; + + const uint64_t now = mytime_now(); + if (now >= next_flush) + return 0; + + const uint64_t remaining = next_flush - now; + return remaining > INT_MAX ? INT_MAX : (int)remaining; +} |