From 1c4c874d3fa571e6211027409d2230cf7d25ca2e Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 4 Feb 2017 15:32:02 +0100 Subject: [PATCH] refactor: Making timers independent of the lightningd_state The `dstate` reference was only an indirection to the `timers` sub-structure anyway, so removing this indirection allows us to reuse the timers in the subdaemon arch. --- daemon/chaintopology.c | 2 +- daemon/irc_announce.c | 7 ++++--- daemon/p2p_announce.c | 8 ++++---- daemon/peer.c | 4 ++-- daemon/timeout.c | 14 +++++++------- daemon/timeout.h | 8 ++++---- 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/daemon/chaintopology.c b/daemon/chaintopology.c index 0024a57d8..b07791698 100644 --- a/daemon/chaintopology.c +++ b/daemon/chaintopology.c @@ -80,7 +80,7 @@ static void next_topology_timer(struct lightningd_state *dstate) dstate->topology->startup = false; io_break(dstate); } - new_reltimer(dstate, dstate, dstate->config.poll_time, + new_reltimer(&dstate->timers, dstate, dstate->config.poll_time, start_poll_chaintip, dstate); } diff --git a/daemon/irc_announce.c b/daemon/irc_announce.c index ae2731d69..9c75a4f23 100644 --- a/daemon/irc_announce.c +++ b/daemon/irc_announce.c @@ -95,7 +95,7 @@ static void announce(struct ircstate *state) /* By default we announce every 6 hours, otherwise when someone joins */ log_debug(state->log, "Setting long announce time: 6 hours"); - state->dstate->announce = new_reltimer(state->dstate, state, + state->dstate->announce = new_reltimer(&state->dstate->timers, state, time_from_sec(3600 * 6), announce, state); } @@ -105,7 +105,8 @@ static void handle_irc_disconnect(struct ircstate *state) { /* Stop announcing. */ state->dstate->announce = tal_free(state->dstate->announce); - new_reltimer(state->dstate, state, state->reconnect_timeout, irc_connect, state); + new_reltimer(&state->dstate->timers, state, state->reconnect_timeout, + irc_connect, state); } /* Verify a signed privmsg */ @@ -243,7 +244,7 @@ static void handle_irc_command(struct ircstate *istate, const struct irccommand delay = pseudorand(60000000); log_debug(istate->log, "Setting new announce time %u sec", delay / 1000000); - dstate->announce = new_reltimer(dstate, istate, + dstate->announce = new_reltimer(&dstate->timers, istate, time_from_usec(delay), announce, istate); } diff --git a/daemon/p2p_announce.c b/daemon/p2p_announce.c index a97d6050a..d77d57f02 100644 --- a/daemon/p2p_announce.c +++ b/daemon/p2p_announce.c @@ -176,7 +176,7 @@ static void announce(struct lightningd_state *dstate) struct peer *p; int nchan = 0; - new_reltimer(dstate, dstate, time_from_sec(5*60*60), announce, dstate); + new_reltimer(&dstate->timers, dstate, time_from_sec(5*60*60), announce, dstate); list_for_each(&dstate->peers, p, list) { if (state_is_normal(p->state)) { @@ -203,7 +203,7 @@ static void process_broadcast_queue(struct lightningd_state *dstate) { struct peer *p; struct queued_message *msg; - new_reltimer(dstate, dstate, time_from_sec(30), process_broadcast_queue, dstate); + new_reltimer(&dstate->timers, dstate, time_from_sec(30), process_broadcast_queue, dstate); list_for_each(&dstate->peers, p, list) { if (!state_is_normal(p->state)) continue; @@ -219,6 +219,6 @@ static void process_broadcast_queue(struct lightningd_state *dstate) void setup_p2p_announce(struct lightningd_state *dstate) { - new_reltimer(dstate, dstate, time_from_sec(5*60*60), announce, dstate); - new_reltimer(dstate, dstate, time_from_sec(30), process_broadcast_queue, dstate); + new_reltimer(&dstate->timers, dstate, time_from_sec(5*60*60), announce, dstate); + new_reltimer(&dstate->timers, dstate, time_from_sec(30), process_broadcast_queue, dstate); } diff --git a/daemon/peer.c b/daemon/peer.c index 94b00a93c..c448e8d52 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -259,7 +259,7 @@ static void remote_changes_pending(struct peer *peer) { if (!peer->commit_timer) { log_debug(peer->log, "remote_changes_pending: adding timer"); - peer->commit_timer = new_reltimer(peer->dstate, peer, + peer->commit_timer = new_reltimer(&peer->dstate->timers, peer, peer->dstate->config.commit_time, try_commit, peer); } else @@ -4415,7 +4415,7 @@ static void reconnect_failed(struct io_conn *conn, struct peer *peer) } log_debug(peer->log, "Setting timer to re-connect"); - new_reltimer(peer->dstate, peer, time_from_sec(15), try_reconnect, peer); + new_reltimer(&peer->dstate->timers, peer, time_from_sec(15), try_reconnect, peer); } static struct io_plan *init_conn(struct io_conn *conn, struct peer *peer) diff --git a/daemon/timeout.c b/daemon/timeout.c index d04ab8cae..0ea87a713 100644 --- a/daemon/timeout.c +++ b/daemon/timeout.c @@ -3,7 +3,7 @@ #include "utils.h" struct oneshot { - struct lightningd_state *dstate; + struct timers *timers; struct timer timer; void (*cb)(void *); void *arg; @@ -11,10 +11,10 @@ struct oneshot { static void remove_timer(struct oneshot *t) { - timer_del(&t->dstate->timers, &t->timer); + timer_del(t->timers, &t->timer); } -struct oneshot *new_reltimer_(struct lightningd_state *dstate, +struct oneshot *new_reltimer_(struct timers *timers, const tal_t *ctx, struct timerel relexpiry, void (*cb)(void *), void *arg) @@ -23,18 +23,18 @@ struct oneshot *new_reltimer_(struct lightningd_state *dstate, t->cb = cb; t->arg = arg; - t->dstate = dstate; + t->timers = timers; timer_init(&t->timer); - timer_addrel(&dstate->timers, &t->timer, relexpiry); + timer_addrel(timers, &t->timer, relexpiry); tal_add_destructor(t, remove_timer); return t; } -void timer_expired(struct lightningd_state *dstate, struct timer *timer) +void timer_expired(tal_t *ctx, struct timer *timer) { struct oneshot *t = container_of(timer, struct oneshot, timer); - const tal_t *tmpctx = tal_tmpctx(dstate); + const tal_t *tmpctx = tal_tmpctx(ctx); /* If it doesn't free itself, freeing tmpctx will do it */ tal_steal(tmpctx, t); diff --git a/daemon/timeout.h b/daemon/timeout.h index 0264d0d08..5ef96d68a 100644 --- a/daemon/timeout.h +++ b/daemon/timeout.h @@ -8,15 +8,15 @@ #include /* tal_free this to disable timer. */ -struct oneshot *new_reltimer_(struct lightningd_state *dstate, +struct oneshot *new_reltimer_(struct timers *timers, const tal_t *ctx, struct timerel expire, void (*cb)(void *), void *arg); -#define new_reltimer(dstate, ctx, relexpire, func, arg) \ - new_reltimer_((dstate), (ctx), (relexpire), \ +#define new_reltimer(timers, ctx, relexpire, func, arg) \ + new_reltimer_((timers), (ctx), (relexpire), \ typesafe_cb(void, void *, (func), (arg)), (arg)) -void timer_expired(struct lightningd_state *dstate, struct timer *timer); +void timer_expired(tal_t *ctx, struct timer *timer); #endif /* LIGHTNING_DAEMON_TIMEOUT_H */