#include "para.h" /** \file time.c helper functions for dealing with timeouts */ /** * convert struct timeval to milliseconds * * \param tv the value to convert * * \return The number off milliseconds in \a tv */ long unsigned tv2ms(const struct timeval *tv) { return tv->tv_sec * 1000 + (tv->tv_usec + 500)/ 1000; } /** * convert milliseconds to struct timeval * * \param n the number of milliseconds * \param tv will be filled in by the function */ void ms2tv(long unsigned n, struct timeval *tv) { tv->tv_sec = n / 1000; tv->tv_usec = (n % 1000) * 1000; } /** * convert double to struct timeval * * \param x the value to convert * \param tv converted value is stored here */ void d2tv(double x, struct timeval *tv) { tv->tv_sec = x; tv->tv_usec = (x - (double)tv->tv_sec) * 1000.0 * 1000.0 + 0.5; } /** * compute the difference of two time values * * \param b minuend * \param a subtrahend * \param diff difference is stored here * * \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|. */ int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff) { int ret = 1; if ((b->tv_sec < a->tv_sec) || ((b->tv_sec == a->tv_sec) && (b->tv_usec < a->tv_usec))) { const struct timeval *tmp = a; a = b; b = tmp; ret = -1; } if (!diff) return ret; diff->tv_sec = b->tv_sec - a->tv_sec; if (b->tv_usec < a->tv_usec) { diff->tv_sec--; diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec; } else diff->tv_usec = b->tv_usec - a->tv_usec; return ret; } /** * add two structs of type timeval * * \param a first addend * \param b second addend * \param sum holds the sum upon return */ void tv_add(const struct timeval *a, const struct timeval *b, struct timeval *sum) { sum->tv_sec = a->tv_sec + b->tv_sec; if (a->tv_usec + b->tv_usec >= 1000 * 1000) { sum->tv_sec++; sum->tv_usec = a->tv_usec + b->tv_usec - 1000 * 1000; } else sum->tv_usec = a->tv_usec + b->tv_usec; } /** * compute integer multiple of given struct timeval * * \param mult the integer value to multiply with * \param tv the timevalue to multiply * \param result holds mult * tv upon return */ void tv_scale(const unsigned long mult, const struct timeval *tv, 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); } /** * compute 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 */ void tv_divide(const unsigned long divisor, const struct timeval *tv, struct timeval *result) { uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor; result->tv_sec = x / 1000 / 1000; result->tv_usec = x % (1000 * 1000); } /** * 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 * * compute x := (a * tv1 + b * tv2) / (|a| + |b|). a, b may be negative. * returns sign(x) and stores |x| in the result pointer. */ 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); tv_scale(a1, tv1, &tmp1); tv_scale(b1, tv2, &tmp2); if (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; } if (!a || !b) { if (a + b < 0) ret = -1; } else if (a < 0) ret = -ret; return ret; }