Commit 258ac288 authored by william's avatar william

move timeouts_addf to macro

parent a87ff8f7
......@@ -31,8 +31,6 @@
#include <inttypes.h> /* UINT64_C uint64_t */
#include <math.h> /* ceil(3) */
#include <string.h> /* memset(3) */
#include <errno.h> /* errno */
......@@ -370,11 +368,6 @@ TIMEOUT_PUBLIC void timeouts_add(struct timeouts *T, struct timeout *to, timeout
} /* timeouts_add() */
TIMEOUT_PUBLIC void timeouts_addf(struct timeouts *T, struct timeout *to, double timeout) {
timeouts_add(T, to, ceil(timeout * T->hertz));
} /* timeouts_addf() */
TIMEOUT_PUBLIC void timeouts_update(struct timeouts *T, abstime_t curtime) {
timeout_t elapsed = curtime - T->curtime;
struct timeout_list todo;
......
......@@ -77,6 +77,8 @@ TIMEOUT_PUBLIC int timeout_v_api(void);
typedef uint64_t timeout_t;
#define timeout_error_t int /* for documentation purposes */
/*
* C A L L B A C K I N T E R F A C E
......@@ -147,7 +149,7 @@ TIMEOUT_PUBLIC void timeout_del(struct timeout *);
struct timeouts;
TIMEOUT_PUBLIC struct timeouts *timeouts_open(timeout_t, int *);
TIMEOUT_PUBLIC struct timeouts *timeouts_open(timeout_t, timeout_error_t *);
/* open a new timing wheel, setting optional HZ (for float conversions) */
TIMEOUT_PUBLIC void timeouts_close(struct timeouts *);
......@@ -168,12 +170,12 @@ TIMEOUT_PUBLIC timeout_t timeouts_timeout(struct timeouts *);
TIMEOUT_PUBLIC void timeouts_add(struct timeouts *, struct timeout *, timeout_t);
/* add timeout to timing wheel */
TIMEOUT_PUBLIC void timeouts_addf(struct timeouts *, struct timeout *, double);
/* add timeout to timing wheel, translating floating point timeout */
TIMEOUT_PUBLIC void timeouts_del(struct timeouts *, struct timeout *);
/* remove timeout from any timing wheel or expired queue (okay if on neither) */
TIMEOUT_PUBLIC struct timeout *timeouts_get(struct timeouts *);
/* return any expired timeout (caller should loop until NULL-return) */
TIMEOUT_PUBLIC bool timeouts_pending(struct timeouts *);
/* return true if any timeouts pending on timing wheel */
......@@ -184,4 +186,26 @@ TIMEOUT_PUBLIC bool timeouts_check(struct timeouts *, FILE *);
/* return true if invariants hold. describes failures to optional file handle. */
/*
* B O N U S W H E E L I N T E R F A C E S
*
* I usually use floating point timeouts in all my code, but it's cleaner to
* separate it to keep the core algorithmic code simple.
*
* Using macros instead of static inline routines where <math.h> routines
* might be used to keep -lm linking optional.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <math.h> /* ceil(3) */
#define timeouts_f2i(T, f) \
(ceil((f) * timeouts_hz((T)))) /* prefer late expiration over early */
#define timeouts_i2f(T, i) \
((i) / timeouts_hz((T)))
#define timeouts_addf(T, to, timeout) \
timeouts_add((T), (to), timeouts_f2i((T), (timeout)))
#endif /* TIMEOUT_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment