From: Arran Cudbard-Bell Date: Sat, 18 May 2019 00:21:57 +0000 (-0400) Subject: Add "delta" type and comparator X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=158e720fc7b71097590db9c023c2bce08a7eb6bb;p=thirdparty%2Ffreeradius-server.git Add "delta" type and comparator Internally we probably want FR_TYPE_TIME_DELTA and FR_TYPE_TIME too, with FR_TYPE_TIME_DELTA replacing FR_TYPE_TIMEVAL --- diff --git a/src/lib/util/event.c b/src/lib/util/event.c index 39ff10185ef..00776426761 100644 --- a/src/lib/util/event.c +++ b/src/lib/util/event.c @@ -1124,7 +1124,7 @@ int fr_event_timer_at(TALLOC_CTX *ctx, fr_event_list_t *el, fr_event_timer_t con * - -1 on failure. */ int fr_event_timer_in(TALLOC_CTX *ctx, fr_event_list_t *el, fr_event_timer_t const **ev_p, - fr_time_t delta, fr_event_cb_t callback, void const *uctx) + fr_time_delta_t delta, fr_event_cb_t callback, void const *uctx) { fr_time_t now; diff --git a/src/lib/util/event.h b/src/lib/util/event.h index 7e12cc20465..c2b27e363ce 100644 --- a/src/lib/util/event.h +++ b/src/lib/util/event.h @@ -217,7 +217,7 @@ int fr_event_pid_wait(TALLOC_CTX *ctx, fr_event_list_t *el, fr_event_pid_t cons int fr_event_timer_at(TALLOC_CTX *ctx, fr_event_list_t *el, fr_event_timer_t const **ev, fr_time_t when, fr_event_cb_t callback, void const *uctx); int fr_event_timer_in(TALLOC_CTX *ctx, fr_event_list_t *el, fr_event_timer_t const **ev, - fr_time_t delta, fr_event_cb_t callback, void const *uctx); + fr_time_delta_t delta, fr_event_cb_t callback, void const *uctx); int fr_event_timer_delete(fr_event_list_t *el, fr_event_timer_t const **ev); int fr_event_timer_run(fr_event_list_t *el, fr_time_t *when); int fr_event_timer_insert(TALLOC_CTX *ctx, fr_event_list_t *el, fr_event_timer_t const **ev_p, diff --git a/src/lib/util/time.h b/src/lib/util/time.h index e2c2bc3a851..a8d0d9bd84f 100644 --- a/src/lib/util/time.h +++ b/src/lib/util/time.h @@ -41,23 +41,28 @@ RCSIDH(time_h, "$Id$") extern "C" { #endif -/** - * A typedef for "server local" time. This is the time in - * nanoseconds since the application started. +/** "server local" time. This is the time in nanoseconds since the application started. + * */ typedef int64_t fr_time_t; -/** - * A structure to track the time spent processing a request. +/** A time delta, a difference in time measured in nanoseconds. + * + * This is easier to distinguish where server epoch time is being + * used, and where relative time is being used. + */ +typedef int64_t fr_time_delta_t; + +/** A structure to track the time spent processing a request. * - * The same structure is used by threads to track when they are - * running / waiting. The functions modifying fr_time_tracking_t all - * take an explicit "when" parameter. This parameter allows the - * thread to update a requests tracking structure, and then use that - * same fr_time_t to update the threads tracking structure. + * The same structure is used by threads to track when they are + * running / waiting. The functions modifying fr_time_tracking_t all + * take an explicit "when" parameter. This parameter allows the + * thread to update a requests tracking structure, and then use that + * same fr_time_t to update the threads tracking structure. * - * While fr_time() is fast, it is also called very often. We should - * therefore be careful to call it only when necessary. + * While fr_time() is fast, it is also called very often. We should + * therefore be careful to call it only when necessary. */ typedef struct { fr_time_t when; //!< last time we changed a field @@ -82,66 +87,80 @@ typedef struct { int fr_time_start(void); fr_time_t fr_time(void); -static inline fr_time_t fr_time_delta_from_usec(uint64_t usec) +static inline fr_time_delta_t fr_time_delta_from_usec(uint64_t usec) { return (usec * 1000); } -static inline fr_time_t fr_time_delta_from_msec(uint64_t msec) +static inline fr_time_delta_t fr_time_delta_from_msec(uint64_t msec) { return (msec * 1000000); } -static inline fr_time_t fr_time_delta_from_sec(uint64_t sec) +static inline fr_time_delta_t fr_time_delta_from_sec(uint64_t sec) { return (sec * NSEC); } -static inline fr_time_t fr_time_delta_from_timeval(struct timeval const *tv) +static inline fr_time_delta_t fr_time_delta_from_timeval(struct timeval const *tv) { return (tv->tv_sec * NSEC) + (tv->tv_usec * 1000); } -static inline fr_time_t fr_time_delta_from_timespec(struct timespec const *ts) +static inline fr_time_delta_t fr_time_delta_from_timespec(struct timespec const *ts) { return (ts->tv_sec * NSEC) + ts->tv_nsec; } -static inline fr_time_t fr_time_delta_to_usec(uint64_t usec) +static inline int64_t fr_time_delta_to_usec(fr_time_delta_t delta) { - return (usec / 1000); + return (delta / 1000); } -static inline fr_time_t fr_time_delta_to_msec(uint64_t msec) +static inline int64_t fr_time_delta_to_msec(fr_time_delta_t delta) { - return (msec / 1000000); + return (delta / 1000000); } -static inline fr_time_t fr_time_delta_to_sec(uint64_t sec) +static inline int64_t fr_time_delta_to_sec(fr_time_delta_t delta) { - return (sec / NSEC); + return (delta / NSEC); } -static inline void fr_time_delta_to_timeval(struct timeval *tv, fr_time_t delta) +static inline void fr_time_delta_to_timeval(struct timeval *tv, fr_time_delta_t delta) { tv->tv_sec = delta / NSEC; tv->tv_usec = (delta % NSEC) / 1000; } -static inline void fr_time_delta_to_timespec(struct timespec *ts, fr_time_t delta) +static inline void fr_time_delta_to_timespec(struct timespec *ts, fr_time_delta_t delta) { ts->tv_sec = delta / NSEC; ts->tv_nsec = delta % NSEC; } -void fr_time_to_timeval(struct timeval *tv, fr_time_t when) CC_HINT(nonnull); -void fr_time_to_timespec(struct timespec *ts, fr_time_t when) CC_HINT(nonnull); -int64_t fr_time_to_usec(fr_time_t when); -int64_t fr_time_to_msec(fr_time_t when); -int64_t fr_time_to_sec(fr_time_t when); +/** Compare two fr_time_t values + * + * @param[in] a The first value to compare. + * @param[in] b The second value to compare. + * @return + * - +1 if a > b + * - 0 if a == b + * - -1 if a < b + */ +static inline int fr_time_cmp(fr_time_t a, fr_time_t b) +{ + return (a > b) - (a < b); +} + +void fr_time_to_timeval(struct timeval *tv, fr_time_t when) CC_HINT(nonnull); +void fr_time_to_timespec(struct timespec *ts, fr_time_t when) CC_HINT(nonnull); +int64_t fr_time_to_usec(fr_time_t when); +int64_t fr_time_to_msec(fr_time_t when); +int64_t fr_time_to_sec(fr_time_t when); -fr_time_t fr_time_from_timeval(struct timeval const *when_tv) CC_HINT(nonnull); -fr_time_t fr_time_from_timespec(struct timespec const *when_tv) CC_HINT(nonnull); +fr_time_t fr_time_from_timeval(struct timeval const *when_tv) CC_HINT(nonnull); +fr_time_t fr_time_from_timespec(struct timespec const *when_tv) CC_HINT(nonnull); void fr_time_tracking_start(fr_time_tracking_t *tt, fr_time_t when, fr_time_tracking_t *worker) CC_HINT(nonnull); void fr_time_tracking_end(fr_time_tracking_t *tt, fr_time_t when, fr_time_tracking_t *worker) CC_HINT(nonnull);