2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include <common/timeout.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
struct oneshot {
|
2017-02-04 15:32:02 +01:00
|
|
|
struct timers *timers;
|
2016-05-09 22:56:09 +02:00
|
|
|
struct timer timer;
|
2016-01-21 21:15:27 +01:00
|
|
|
void (*cb)(void *);
|
|
|
|
void *arg;
|
|
|
|
};
|
|
|
|
|
2018-02-14 02:53:13 +01:00
|
|
|
static void destroy_timer(struct oneshot *t)
|
2016-01-21 21:15:27 +01:00
|
|
|
{
|
2017-02-04 15:32:02 +01:00
|
|
|
timer_del(t->timers, &t->timer);
|
2016-01-21 21:15:27 +01:00
|
|
|
}
|
|
|
|
|
2017-02-04 15:32:02 +01:00
|
|
|
struct oneshot *new_reltimer_(struct timers *timers,
|
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;
|
2017-02-04 15:32:02 +01:00
|
|
|
t->timers = timers;
|
2016-05-09 22:56:09 +02:00
|
|
|
timer_init(&t->timer);
|
2017-02-04 15:32:02 +01:00
|
|
|
timer_addrel(timers, &t->timer, relexpiry);
|
2018-02-14 02:53:13 +01:00
|
|
|
tal_add_destructor(t, destroy_timer);
|
2016-05-09 22:56:09 +02:00
|
|
|
|
|
|
|
return t;
|
2016-01-21 21:15:27 +01:00
|
|
|
}
|
|
|
|
|
2022-01-08 14:27:29 +01:00
|
|
|
struct oneshot *new_abstimer_(struct timers *timers,
|
|
|
|
const tal_t *ctx,
|
|
|
|
struct timemono expiry,
|
|
|
|
void (*cb)(void *), void *arg)
|
|
|
|
{
|
|
|
|
struct oneshot *t = tal(ctx, struct oneshot);
|
|
|
|
|
|
|
|
t->cb = cb;
|
|
|
|
t->arg = arg;
|
|
|
|
t->timers = timers;
|
|
|
|
timer_init(&t->timer);
|
|
|
|
timer_addmono(timers, &t->timer, expiry);
|
|
|
|
tal_add_destructor(t, destroy_timer);
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *oneshot_arg(struct oneshot *t)
|
2021-10-07 05:57:05 +02:00
|
|
|
{
|
|
|
|
return t->arg;
|
|
|
|
}
|
|
|
|
|
2021-12-06 15:45:50 +01:00
|
|
|
void timer_expired(struct timer *timer)
|
2016-05-09 22:56:09 +02:00
|
|
|
{
|
|
|
|
struct oneshot *t = container_of(timer, struct oneshot, timer);
|
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-01-21 21:15:27 +01:00
|
|
|
}
|