X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=time.c;h=c405351a96f978c39b18ec0466a65fad012aff26;hp=8faefc059c950c5a5889b37b14ea6e8b61072af8;hb=bd10c0f46e0b7eadfb8c6d5cd45581cec253d775;hpb=6668ac4a8c7f2a92efb9e6d405d954beff77d230 diff --git a/time.c b/time.c index 8faefc05..c405351a 100644 --- a/time.c +++ b/time.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Andre Noll + * Copyright (C) 2005-2014 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ @@ -12,7 +12,7 @@ * * \param tv The time value value to convert. * - * \return The number off milliseconds in \a tv. + * \return The number of milliseconds in \a tv. */ long unsigned tv2ms(const struct timeval *tv) { @@ -55,7 +55,8 @@ void d2tv(double x, struct timeval *tv) * * \return If \a b < \a a, this function returns -1, otherwise it returns 1. */ -int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff) +int tv_diff(const struct timeval *b, const struct timeval *a, + struct timeval *diff) { int ret = 1; @@ -82,10 +83,10 @@ int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *di * * \param a First addend. * \param b Second addend. - * \param sum Contains the sum \a + \a b on return. + * \param sum Contains the sum \a a + \a b on return. */ void tv_add(const struct timeval *a, const struct timeval *b, - struct timeval *sum) + struct timeval *sum) { sum->tv_sec = a->tv_sec + b->tv_sec; if (a->tv_usec + b->tv_usec >= 1000 * 1000) { @@ -104,7 +105,7 @@ void tv_add(const struct timeval *a, const struct timeval *b, * \param result Contains \a mult * \a tv on return. */ void tv_scale(const unsigned long mult, const struct timeval *tv, - struct timeval *result) + struct timeval *result) { uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) * mult; @@ -120,7 +121,7 @@ void tv_scale(const unsigned long mult, const struct timeval *tv, * \param result Contains (1 / mult) * tv on return. */ void tv_divide(const unsigned long divisor, const struct timeval *tv, - struct timeval *result) + struct timeval *result) { uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor; @@ -180,7 +181,7 @@ int tv_convex_combination(const long a, const struct timeval *tv1, * \param stream_start When the first chunk was sent. * \param result The time when to send chunk number \a chunk_num. * - * This function computes stream_start + chunk_num * chunk_time. + * This function computes \a stream_start + \a chunk_num * \a chunk_time. */ void compute_chunk_time(long unsigned chunk_num, struct timeval *chunk_tv, struct timeval *stream_start, @@ -191,3 +192,40 @@ void compute_chunk_time(long unsigned chunk_num, tv_scale(chunk_num, chunk_tv, &tmp); tv_add(&tmp, stream_start, result); } + +/** + * Retrieve the time of the realtime clock. + * + * \param tv Where to store the result. + * + * Gets the current value of the system-wide real-time clock (identified by id + * \p CLOCK_REALTIME). If \a tv is \p NULL, the value is stored in a static + * buffer, otherwise it is stored at the location given by \a tv. + * + * \return This function aborts on errors. On success it returns a pointer to + * memory containing the current time. + * + * \sa clock_gettime(2), gettimeofday(2). + */ +struct timeval *clock_get_realtime(struct timeval *tv) +{ + static struct timeval user_friendly; + + if (!tv) + tv = &user_friendly; +#ifdef HAVE_CLOCK_GETTIME + { + struct timespec t; + int ret; + + ret = clock_gettime(CLOCK_REALTIME, &t); + assert(ret == 0); + tv->tv_sec = t.tv_sec; + tv->tv_usec = t.tv_nsec / 1000; + } +#else + #include + gettimeofday(tv, NULL); +#endif /* HAVE_CLOCK_GETTIME */ + return tv; +}