X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=time.c;h=c405351a96f978c39b18ec0466a65fad012aff26;hp=83231c895020245d72cea40cd34cae03e9951a29;hb=bd10c0f46e0b7eadfb8c6d5cd45581cec253d775;hpb=6e2e2739acdbacb5f0d4b4272b0e04f74184c66a;ds=sidebyside diff --git a/time.c b/time.c index 83231c89..c405351a 100644 --- a/time.c +++ b/time.c @@ -1,12 +1,18 @@ +/* + * Copyright (C) 2005-2014 Andre Noll + * + * Licensed under the GPL v2. For licencing details see COPYING. + */ + #include "para.h" -/** \file time.c helper functions for dealing with timeouts */ +/** \file time.c Helper functions for dealing with time values. */ /** - * convert struct timeval to milliseconds + * Convert struct timeval to milliseconds. * - * \param tv the value to convert + * \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) { @@ -14,10 +20,10 @@ long unsigned tv2ms(const struct timeval *tv) } /** - * convert milliseconds to struct timeval + * Convert milliseconds to a struct timeval. * - * \param n the number of milliseconds - * \param tv will be filled in by the function + * \param n The number of milliseconds. + * \param tv Result pointer. */ void ms2tv(long unsigned n, struct timeval *tv) { @@ -26,10 +32,10 @@ void ms2tv(long unsigned n, struct timeval *tv) } /** - * convert double to struct timeval + * Convert a double to a struct timeval. * - * \param x the value to convert - * \param tv converted value is stored here + * \param x The value to convert. + * \param tv Result pointer. */ void d2tv(double x, struct timeval *tv) { @@ -38,17 +44,19 @@ void d2tv(double x, struct timeval *tv) } /** - * compute the difference of two time values + * Compute the difference of two time values. * - * \param b minuend - * \param a subtrahend - * \param diff difference is stored here + * \param b Minuend. + * \param a Subtrahend. + * \param diff Result pointer. * - * \return If b > a, compute b - a and return 1. if b < a - * compute a - b and return -1. If \a diff is not \p NULL, \a diff gets - * filled in with the absolute value |b - a|. + * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on + * return. + * + * \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; @@ -71,14 +79,14 @@ int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *di } /** - * add two structs of type timeval + * Add two time values. * - * \param a first addend - * \param b second addend - * \param sum holds the sum upon return + * \param a First addend. + * \param b Second addend. + * \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) { @@ -89,84 +97,135 @@ void tv_add(const struct timeval *a, const struct timeval *b, } /** - * compute integer multiple of given struct timeval + * Compute integer multiple of given struct timeval. + * + * \param mult The integer value to multiply with. + * \param tv The timevalue to multiply. * - * \param mult the integer value to multiply with - * \param tv the timevalue to multiply - * \param result holds mult * tv upon return + * \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) { - result->tv_sec = mult * tv->tv_sec; - result->tv_sec += tv->tv_usec * mult / 1000 / 1000; - result->tv_usec = tv->tv_usec * mult % (1000 * 1000); + uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) * mult; + + result->tv_sec = x / 1000 / 1000; + result->tv_usec = x % (1000 * 1000); } /** - * compute fraction of given struct timeval + * Compute a fraction of given struct timeval. * - * \param divisor the integer value to divide by - * \param tv the timevalue to divide - * \param result holds (1 / mult) * tv upon return + * \param divisor The integer value to divide by. + * \param tv The timevalue to divide. + * \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) { - long unsigned q; + uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor; - if (!divisor) { - PARA_EMERG_LOG("%s\n", "division by zero"); - exit(EXIT_FAILURE); - } - q = tv->tv_usec / divisor; - result->tv_sec = tv->tv_sec / divisor; - result->tv_usec = (tv->tv_sec - result->tv_sec * divisor) - * 1000 * 1000 / divisor; - if (result->tv_usec + q >= 1000 * 1000) { - result->tv_sec++; - result->tv_usec = 1000 * 1000 - result->tv_usec - q; - } else - result->tv_usec += q; + result->tv_sec = x / 1000 / 1000; + result->tv_usec = x % (1000 * 1000); } /** - * compute a convex combination of two time values + * Compute a convex combination of two time values. * - * \param a the first coefiicent - * \param tv1 the first time value - * \param b the second coefiicent - * \param tv2 the second time value - * \param result holds the convex combination upon return + * \param a The first coefficient. + * \param tv1 The first time value. + * \param b The second coefficient. + * \param tv2 The second time value. + * \param result Contains the convex combination upon return. * - * compute x := (a * tv1 + b * tv2) / (|a| + |b|). a, b may be negative. - * returns sign(x) and stores |x| in the result pointer. + * Compute x := (a * tv1 + b * tv2) / (|a| + |b|) and store |x| in \a result. + * Both \a a and \a b may be negative. + * + * \return Zero, 1 or -1, if \a x is zero, positive or negative, respectively. */ int tv_convex_combination(const long a, const struct timeval *tv1, const long b, const struct timeval *tv2, struct timeval *result) { struct timeval tmp1, tmp2, tmp3; - int ret = 1, subtract = ((a > 0 && b < 0) || (a < 0 && b > 0)); - unsigned long a1 = PARA_ABS(a), b1 = PARA_ABS(b); + int ret = 1; + unsigned long a1, b1; + if (a == 0 && b == 0) { + result->tv_sec = 0; + result->tv_usec = 0; + return 0; + } + a1 = PARA_ABS(a); + b1 = PARA_ABS(b); tv_scale(a1, tv1, &tmp1); tv_scale(b1, tv2, &tmp2); - if (subtract) + if ((a > 0 && b < 0) || (a < 0 && b > 0)) /* subtract */ ret = tv_diff(&tmp1, &tmp2, &tmp3); else tv_add(&tmp1, &tmp2, &tmp3); - if (a1 + b1) - tv_divide(a1 + b1, &tmp3, result); - else { - result->tv_sec = 0; - result->tv_usec = 0; - } + tv_divide(a1 + b1, &tmp3, result); if (!a || !b) { if (a + b < 0) ret = -1; - } else - if (a < 0) - ret = -ret; + } else if (a < 0) + ret = -ret; return ret; } + +/** + * Compute when to send a chunk of an audio file. + * + * \param chunk_num The number of the chunk. + * \param chunk_tv The duration of one chunk. + * \param stream_start When the first chunk was sent. + * \param result The time when to send chunk number \a chunk_num. + * + * 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, + struct timeval *result) +{ + struct timeval tmp; + + 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; +}