mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-24 01:24:26 +01:00
timeout: make all timers one-shot.
It's closer to what we want, and simpler. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -2,54 +2,51 @@
|
||||
#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 *dstate, struct timeout *t)
|
||||
{
|
||||
timer_del(&dstate->timers, &t->timer);
|
||||
timer_add(&dstate->timers, &t->timer,
|
||||
timeabs_add(controlled_time(), t->interval));
|
||||
}
|
||||
|
||||
/* FIXME: Make all timers one-shot! */
|
||||
struct oneshot {
|
||||
struct timeout timeout;
|
||||
struct lightningd_state *dstate;
|
||||
struct timer timer;
|
||||
void (*cb)(void *);
|
||||
void *arg;
|
||||
};
|
||||
|
||||
static void remove_timer(struct oneshot *o)
|
||||
static void remove_timer(struct oneshot *t)
|
||||
{
|
||||
timer_del(&o->dstate->timers, &o->timeout.timer);
|
||||
timer_del(&t->dstate->timers, &t->timer);
|
||||
}
|
||||
|
||||
static void oneshot_done(struct oneshot *o)
|
||||
struct oneshot *new_abstimer_(struct lightningd_state *dstate,
|
||||
const tal_t *ctx,
|
||||
struct timeabs expiry,
|
||||
void (*cb)(void *), void *arg)
|
||||
{
|
||||
o->cb(o->arg);
|
||||
tal_free(o);
|
||||
struct oneshot *t = tal(ctx, struct oneshot);
|
||||
|
||||
t->cb = cb;
|
||||
t->arg = arg;
|
||||
t->dstate = dstate;
|
||||
timer_init(&t->timer);
|
||||
timer_add(&dstate->timers, &t->timer, expiry);
|
||||
tal_add_destructor(t, remove_timer);
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
struct oneshot *oneshot_timeout_(struct lightningd_state *dstate,
|
||||
const tal_t *ctx, unsigned int seconds,
|
||||
void (*cb)(void *), void *arg)
|
||||
struct oneshot *new_reltimer_(struct lightningd_state *dstate,
|
||||
const tal_t *ctx,
|
||||
struct timerel relexpiry,
|
||||
void (*cb)(void *), void *arg)
|
||||
{
|
||||
struct oneshot *o = tal(ctx, struct oneshot);
|
||||
|
||||
o->dstate = dstate;
|
||||
o->cb = cb;
|
||||
o->arg = arg;
|
||||
|
||||
init_timeout(&o->timeout, seconds, oneshot_done, o);
|
||||
refresh_timeout(dstate, &o->timeout);
|
||||
tal_add_destructor(o, remove_timer);
|
||||
|
||||
return o;
|
||||
return new_abstimer_(dstate, ctx,
|
||||
timeabs_add(controlled_time(), relexpiry),
|
||||
cb, arg);
|
||||
}
|
||||
|
||||
void timer_expired(struct lightningd_state *dstate, struct timer *timer)
|
||||
{
|
||||
struct oneshot *t = container_of(timer, struct oneshot, timer);
|
||||
tal_t *tmpctx = tal(dstate, char);
|
||||
|
||||
/* If it doesn't free itself, freeing tmpctx will do it */
|
||||
tal_steal(tmpctx, t);
|
||||
t->cb(t->arg);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user