From 6ccfcf447726a07c38a9c44c3287b3bbb21fe918 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 22 Jan 2016 06:41:48 +1030 Subject: [PATCH] daemon: timeout structure for IO. For better or worse, the ccan/timer structure is completely minimal, and designed to be wrapped inside a container structure. Signed-off-by: Rusty Russell --- daemon/Makefile | 6 ++++-- daemon/lightningd.c | 1 + daemon/lightningd.h | 4 ++++ daemon/timeout.c | 18 ++++++++++++++++++ daemon/timeout.h | 27 +++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 daemon/timeout.c create mode 100644 daemon/timeout.h diff --git a/daemon/Makefile b/daemon/Makefile index 166c22618..0b7f67edd 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -11,7 +11,8 @@ DAEMON_SRC := \ daemon/json.c \ daemon/lightningd.c \ daemon/log.c \ - daemon/pseudorand.c + daemon/pseudorand.c \ + daemon/timeout.c DAEMON_OBJS := $(DAEMON_SRC:.c=.o) DAEMON_JSMN_OBJS := daemon/jsmn.o @@ -22,7 +23,8 @@ DAEMON_HEADERS := \ daemon/json.h \ daemon/lightningd.h \ daemon/log.h \ - daemon/pseudorand.h + daemon/pseudorand.h \ + daemon/timeout.h $(DAEMON_OBJS): $(DAEMON_HEADERS) $(DAEMON_JSMN_HEADERS) $(BITCOIN_HEADERS) $(CORE_HEADERS) $(GEN_HEADERS) $(CCAN_HEADERS) $(DAEMON_JSMN_OBJS): $(DAEMON_JSMN_HEADERS) diff --git a/daemon/lightningd.c b/daemon/lightningd.c index a337aed4f..3cc1d34ee 100644 --- a/daemon/lightningd.c +++ b/daemon/lightningd.c @@ -20,6 +20,7 @@ static struct lightningd_state *lightningd_state(void) state->base_log = new_log(state, state->log_record, "lightningd(%u):", (int)getpid()); + timers_init(&state->timers, time_now()); return state; } diff --git a/daemon/lightningd.h b/daemon/lightningd.h index 48667a7a5..7b1a5db2d 100644 --- a/daemon/lightningd.h +++ b/daemon/lightningd.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_DAEMON_LIGHTNING_H #define LIGHTNING_DAEMON_LIGHTNING_H #include "config.h" +#include #include /* Here's where the global variables hide! */ @@ -13,5 +14,8 @@ struct lightningd_state { /* Our config dir, and rpc file */ char *config_dir; char *rpc_filename; + + /* Any pending timers. */ + struct timers timers; }; #endif /* LIGHTNING_DAEMON_LIGHTNING_H */ diff --git a/daemon/timeout.c b/daemon/timeout.c new file mode 100644 index 000000000..0233a426c --- /dev/null +++ b/daemon/timeout.c @@ -0,0 +1,18 @@ +#include "lightningd.h" +#include "timeout.h" + +void init_timeout_(struct timeout *t, unsigned int interval, + void (*cb)(void *), void *arg) +{ + timer_init(&t->timer); + t->interval = time_from_sec(interval); + t->cb = cb; + t->arg = arg; +} + +void refresh_timeout(struct lightningd_state *state, struct timeout *t) +{ + timer_del(&state->timers, &t->timer); + timer_add(&state->timers, &t->timer, + timeabs_add(time_now(), t->interval)); +} diff --git a/daemon/timeout.h b/daemon/timeout.h new file mode 100644 index 000000000..9cc7eb0a1 --- /dev/null +++ b/daemon/timeout.h @@ -0,0 +1,27 @@ +#ifndef LIGHTNING_DAEMON_TIMEOUT_H +#define LIGHTNING_DAEMON_TIMEOUT_H +#include "config.h" + +#include +#include +#include + +struct timeout { + struct timer timer; + struct timerel interval; + void (*cb)(void *); + void *arg; +}; + +struct lightningd_state; + +void init_timeout_(struct timeout *t, unsigned int interval, + void (*cb)(void *), void *arg); + +void refresh_timeout(struct lightningd_state *state, struct timeout *t); + +#define init_timeout(t, interval, func, arg) \ + init_timeout_((t), (interval), \ + typesafe_cb(void, void *, (func), (arg)), (arg)) + +#endif /* LIGHTNING_DAEMON_TIMEOUT_H */