]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add "delta" type and comparator
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sat, 18 May 2019 00:21:57 +0000 (20:21 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sat, 18 May 2019 00:21:57 +0000 (20:21 -0400)
Internally we probably want FR_TYPE_TIME_DELTA and FR_TYPE_TIME too, with FR_TYPE_TIME_DELTA replacing FR_TYPE_TIMEVAL

src/lib/util/event.c
src/lib/util/event.h
src/lib/util/time.h

index 39ff10185ef7b40fea31315a3bd34e2f8edffe10..00776426761ca20e0b84ffd94940e3f8afaf8a2e 100644 (file)
@@ -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;
 
index 7e12cc20465d034dd1ff81f2cb0114ab37b48925..c2b27e363cefdba22f4e680975f2f64f77c36f60 100644 (file)
@@ -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,
index e2c2bc3a851afeed2888d4cc536c75cdd18acd6e..a8d0d9bd84f307f1c1c06acca207525f58db7328 100644 (file)
@@ -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);