mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-17 19:03:42 +01:00
timeout: oneshot timer support.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
9f560a9494
commit
6ba5c3cc3b
@ -16,3 +16,39 @@ void refresh_timeout(struct lightningd_state *dstate, struct timeout *t)
|
||||
timer_add(&dstate->timers, &t->timer,
|
||||
timeabs_add(time_now(), t->interval));
|
||||
}
|
||||
|
||||
/* FIXME: Make all timers one-shot! */
|
||||
struct oneshot {
|
||||
struct timeout timeout;
|
||||
struct lightningd_state *dstate;
|
||||
void (*cb)(void *);
|
||||
void *arg;
|
||||
};
|
||||
|
||||
static void remove_timer(struct oneshot *o)
|
||||
{
|
||||
timer_del(&o->dstate->timers, &o->timeout.timer);
|
||||
}
|
||||
|
||||
static void oneshot_done(struct oneshot *o)
|
||||
{
|
||||
o->cb(o->arg);
|
||||
tal_free(o);
|
||||
}
|
||||
|
||||
struct oneshot *oneshot_timeout_(struct lightningd_state *dstate,
|
||||
const tal_t *ctx, unsigned int seconds,
|
||||
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;
|
||||
}
|
||||
|
@ -24,4 +24,13 @@ void refresh_timeout(struct lightningd_state *dstate, struct timeout *t);
|
||||
init_timeout_((t), (interval), \
|
||||
typesafe_cb(void, void *, (func), (arg)), (arg))
|
||||
|
||||
/* tal_free this to disable timer. */
|
||||
struct oneshot *oneshot_timeout_(struct lightningd_state *dstate,
|
||||
const tal_t *ctx, unsigned int seconds,
|
||||
void (*cb)(void *), void *arg);
|
||||
|
||||
#define oneshot_timeout(dstate, ctx, interval, func, arg) \
|
||||
oneshot_timeout_((dstate), (ctx), (interval), \
|
||||
typesafe_cb(void, void *, (func), (arg)), (arg))
|
||||
|
||||
#endif /* LIGHTNING_DAEMON_TIMEOUT_H */
|
||||
|
Loading…
Reference in New Issue
Block a user