Commit 656c17be authored by William Ahern's avatar William Ahern

Merge branch 'optional_intervals' of git://github.com/nmathewson/timeout into...

Merge branch 'optional_intervals' of git://github.com/nmathewson/timeout into nmathewson-optional_intervals
parents 1aa993e3 6186b353
...@@ -49,6 +49,15 @@ ...@@ -49,6 +49,15 @@
#define TO_SET_TIMEOUTS(to, T) ((to)->timeouts = (T)) #define TO_SET_TIMEOUTS(to, T) ((to)->timeouts = (T))
#endif #endif
#ifndef TIMEOUT_INT
#define TIMEOUT_INT 0
#define INTERVAL_GET(to) (abort(), 1)
#define INTERVAL_SET(to, val) abort()
#else
#define INTERVAL_GET(to) ((to)->interval)
#define INTERVAL_SET(to, val) ((to)->interval = (val))
#endif
/* /*
* A N C I L L A R Y R O U T I N E S * A N C I L L A R Y R O U T I N E S
* *
...@@ -354,20 +363,21 @@ static void timeouts_sched(struct timeouts *T, struct timeout *to, timeout_t exp ...@@ -354,20 +363,21 @@ static void timeouts_sched(struct timeouts *T, struct timeout *to, timeout_t exp
static void timeouts_readd(struct timeouts *T, struct timeout *to) { static void timeouts_readd(struct timeouts *T, struct timeout *to) {
to->expires += to->interval; const timeout_t interval = INTERVAL_GET(to);
to->expires += interval;
if (to->expires <= T->curtime) { if (to->expires <= T->curtime) {
if (to->expires < T->curtime) { if (to->expires < T->curtime) {
timeout_t n = T->curtime - to->expires; timeout_t n = T->curtime - to->expires;
timeout_t q = n / to->interval; timeout_t q = n / interval;
timeout_t r = n % to->interval; timeout_t r = n % interval;
if (r) if (r)
to->expires += (to->interval * q) + (to->interval - r); to->expires += (interval * q) + (interval - r);
else else
to->expires += (to->interval * q); to->expires += (interval * q);
} else { } else {
to->expires += to->interval; to->expires += interval;
} }
} }
...@@ -377,7 +387,7 @@ static void timeouts_readd(struct timeouts *T, struct timeout *to) { ...@@ -377,7 +387,7 @@ static void timeouts_readd(struct timeouts *T, struct timeout *to) {
TIMEOUT_PUBLIC void timeouts_add(struct timeouts *T, struct timeout *to, timeout_t timeout) { TIMEOUT_PUBLIC void timeouts_add(struct timeouts *T, struct timeout *to, timeout_t timeout) {
if (to->flags & TIMEOUT_INT) if (to->flags & TIMEOUT_INT)
to->interval = MAX(1, timeout); INTERVAL_SET(to, MAX(1, timeout));
if (to->flags & TIMEOUT_ABS) if (to->flags & TIMEOUT_ABS)
timeouts_sched(T, to, timeout); timeouts_sched(T, to, timeout);
...@@ -549,7 +559,7 @@ TIMEOUT_PUBLIC struct timeout *timeouts_get(struct timeouts *T) { ...@@ -549,7 +559,7 @@ TIMEOUT_PUBLIC struct timeout *timeouts_get(struct timeouts *T) {
TAILQ_REMOVE(&T->expired, to, tqe); TAILQ_REMOVE(&T->expired, to, tqe);
to->pending = 0; to->pending = 0;
if ((to->flags & TIMEOUT_INT) && to->interval > 0) { if ((to->flags & TIMEOUT_INT) && INTERVAL_GET(to) > 0) {
timeouts_readd(T, to); timeouts_readd(T, to);
} else { } else {
TO_SET_TIMEOUTS(to, NULL); TO_SET_TIMEOUTS(to, NULL);
......
...@@ -100,7 +100,9 @@ struct timeout_cb { ...@@ -100,7 +100,9 @@ struct timeout_cb {
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef TIMEOUT_DISABLE_INTERVALS
#define TIMEOUT_INT 0x01 /* interval (repeating) timeout */ #define TIMEOUT_INT 0x01 /* interval (repeating) timeout */
#endif
#define TIMEOUT_ABS 0x02 /* treat timeout values as absolute */ #define TIMEOUT_ABS 0x02 /* treat timeout values as absolute */
#define TIMEOUT_INITIALIZER(flags) { (flags) } #define TIMEOUT_INITIALIZER(flags) { (flags) }
...@@ -113,8 +115,10 @@ struct timeout_cb { ...@@ -113,8 +115,10 @@ struct timeout_cb {
struct timeout { struct timeout {
int flags; int flags;
#ifndef TIMEOUT_DISABLE_INTERVALS
timeout_t interval; timeout_t interval;
/* timeout interval if periodic */ /* timeout interval if periodic */
#endif
timeout_t expires; timeout_t expires;
/* absolute expiration time */ /* absolute expiration time */
......
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