From d453fc960b0a5012cf260175b26d8b0fe203d408 Mon Sep 17 00:00:00 2001 From: william <william@25tandclement.com> Date: Fri, 3 Jan 2014 17:40:11 -0800 Subject: [PATCH] forgot to define timeouts_open() and timeouts_close(); add timeouts_hz() --- timeout.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ timeout.h | 5 ++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/timeout.c b/timeout.c index c442e7a..938ba6a 100644 --- a/timeout.c +++ b/timeout.c @@ -26,6 +26,7 @@ #include <limits.h> /* CHAR_BIT */ #include <stddef.h> /* NULL */ +#include <stdlib.h> /* malloc(3) free(3) */ #include <stdio.h> /* FILE fprintf(3) */ #include <inttypes.h> /* UINT64_C uint64_t */ @@ -34,6 +35,8 @@ #include <string.h> /* memset(3) */ +#include <errno.h> /* errno */ + #include <sys/queue.h> /* TAILQ(3) */ #include "timeout.h" @@ -225,6 +228,55 @@ static struct timeouts *timeouts_init(struct timeouts *T, timeout_t hz) { } /* timeouts_init() */ +TIMEOUT_PUBLIC struct timeouts *timeouts_open(timeout_t hz, int *error) { + struct timeouts *T; + + if ((T = malloc(sizeof *T))) + return timeouts_init(T, hz); + + *error = errno; + + return NULL; +} /* timeouts_open() */ + + +static void timeouts_reset(struct timeouts *T) { + struct timeouts_list reset; + struct timeout *to; + + TAILQ_INIT(&reset); + + for (unsigned i = 0; i < countof(T->wheel); i++) { + for (unsigned j = 0; j < countof(T->wheel[i]); j++) { + TAILQ_CONCAT(&reset, &T->wheel[i][j]); + } + } + + TAILQ_CONCAT(&reset, &T->expired); + + TAILQ_FOREACH(to, &reset, tqe) { + to->timeouts = NULL; + to->pending = NULL; + } +} /* timeouts_reset() */ + + +TIMEOUT_PUBLIC void timeouts_close(struct timeouts *T) { + /* + * NOTE: Delete installed timeouts so timeout_pending() and + * timeout_expired() worked as expected. + */ + timeouts_reset(T); + + free(T); +} /* timeouts_close() */ + + +TIMEOUT_PUBLIC timeout_t timeouts_hz(struct timeouts *T) { + return T->hertz; +} /* timeouts_hz() */ + + TIMEOUT_PUBLIC void timeouts_del(struct timeouts *T, struct timeout *to) { if (to->pending) { if (to->pending != &T->expired && TAILQ_EMPTY(to->pending)) { diff --git a/timeout.h b/timeout.h index 885ad5f..10284ac 100644 --- a/timeout.h +++ b/timeout.h @@ -146,12 +146,15 @@ TIMEOUT_PUBLIC void timeout_del(struct timeout *); struct timeouts; -TIMEOUT_PUBLIC struct timeouts *timeouts_open(timeout_t); +TIMEOUT_PUBLIC struct timeouts *timeouts_open(timeout_t, int *); /* open a new timing wheel, setting optional HZ (for float conversions) */ TIMEOUT_PUBLIC void timeouts_close(struct timeouts *); /* destroy timing wheel */ +TIMEOUT_PUBLIC timeout_t timeouts_hz(struct timeouts *); +/* return HZ setting (for float conversions) */ + TIMEOUT_PUBLIC void timeouts_update(struct timeouts *, timeout_t); /* update timing wheel with current absolute time */ -- 2.30.9