core-lightning/common/timeout.c
Rusty Russell 55d962046b Rename (almost) all destructors to destroy_<type>.
We usually did this, but sometimes they were named after what they did,
rather than what they cleaned up.

There are still a few exceptions:
1. I didn't bother creating destroy_xxx wrappers for htable routines
   which already existed.
2. Sometimes destructors really are used for side-effects (eg. to simply
   mark that something was freed): these are clearer with boutique names.
3. Generally destructors are static, but they don't need to be: in some
   cases we attach a destructor then remove it later, or only attach
   to *some* cases.  These are best with qualifiers in the destroy_<type>
   name.

Suggested-by: @ZmnSCPxj
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-02-14 11:31:58 +01:00

43 lines
888 B
C

#include "timeout.h"
#include <common/utils.h>
struct oneshot {
struct timers *timers;
struct timer timer;
void (*cb)(void *);
void *arg;
};
static void destroy_timer(struct oneshot *t)
{
timer_del(t->timers, &t->timer);
}
struct oneshot *new_reltimer_(struct timers *timers,
const tal_t *ctx,
struct timerel relexpiry,
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_addrel(timers, &t->timer, relexpiry);
tal_add_destructor(t, destroy_timer);
return t;
}
void timer_expired(tal_t *ctx, struct timer *timer)
{
struct oneshot *t = container_of(timer, struct oneshot, timer);
const tal_t *tmpctx = tal_tmpctx(ctx);
/* If it doesn't free itself, freeing tmpctx will do it */
tal_steal(tmpctx, t);
t->cb(t->arg);
tal_free(tmpctx);
}