2016-01-21 21:11:48 +01:00
|
|
|
#include "lightningd.h"
|
|
|
|
#include "timeout.h"
|
2016-11-01 12:04:27 +01:00
|
|
|
#include "utils.h"
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
struct oneshot {
|
|
|
|
struct lightningd_state *dstate;
|
2016-05-09 22:56:09 +02:00
|
|
|
struct timer timer;
|
2016-01-21 21:15:27 +01:00
|
|
|
void (*cb)(void *);
|
|
|
|
void *arg;
|
|
|
|
};
|
|
|
|
|
2016-05-09 22:56:09 +02:00
|
|
|
static void remove_timer(struct oneshot *t)
|
2016-01-21 21:15:27 +01:00
|
|
|
{
|
2016-05-09 22:56:09 +02:00
|
|
|
timer_del(&t->dstate->timers, &t->timer);
|
2016-01-21 21:15:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 09:23:15 +01:00
|
|
|
struct oneshot *new_reltimer_(struct lightningd_state *dstate,
|
2016-05-09 22:56:09 +02:00
|
|
|
const tal_t *ctx,
|
2016-11-09 09:23:15 +01:00
|
|
|
struct timerel relexpiry,
|
2016-05-09 22:56:09 +02:00
|
|
|
void (*cb)(void *), void *arg)
|
2016-01-21 21:15:27 +01:00
|
|
|
{
|
2016-05-09 22:56:09 +02:00
|
|
|
struct oneshot *t = tal(ctx, struct oneshot);
|
|
|
|
|
|
|
|
t->cb = cb;
|
|
|
|
t->arg = arg;
|
|
|
|
t->dstate = dstate;
|
|
|
|
timer_init(&t->timer);
|
2016-11-09 09:26:15 +01:00
|
|
|
timer_addrel(&dstate->timers, &t->timer, relexpiry);
|
2016-05-09 22:56:09 +02:00
|
|
|
tal_add_destructor(t, remove_timer);
|
|
|
|
|
|
|
|
return t;
|
2016-01-21 21:15:27 +01:00
|
|
|
}
|
|
|
|
|
2016-05-09 22:56:09 +02:00
|
|
|
void timer_expired(struct lightningd_state *dstate, struct timer *timer)
|
|
|
|
{
|
|
|
|
struct oneshot *t = container_of(timer, struct oneshot, timer);
|
2016-11-01 12:04:27 +01:00
|
|
|
const tal_t *tmpctx = tal_tmpctx(dstate);
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-05-09 22:56:09 +02:00
|
|
|
/* If it doesn't free itself, freeing tmpctx will do it */
|
|
|
|
tal_steal(tmpctx, t);
|
|
|
|
t->cb(t->arg);
|
2016-11-01 12:04:27 +01:00
|
|
|
tal_free(tmpctx);
|
2016-01-21 21:15:27 +01:00
|
|
|
}
|