mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 14:42:40 +01:00
daemon: timeout structure for IO.
For better or worse, the ccan/timer structure is completely minimal, and designed to be wrapped inside a container structure. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
aed857a30c
commit
6ccfcf4477
5 changed files with 54 additions and 2 deletions
|
@ -11,7 +11,8 @@ DAEMON_SRC := \
|
|||
daemon/json.c \
|
||||
daemon/lightningd.c \
|
||||
daemon/log.c \
|
||||
daemon/pseudorand.c
|
||||
daemon/pseudorand.c \
|
||||
daemon/timeout.c
|
||||
DAEMON_OBJS := $(DAEMON_SRC:.c=.o)
|
||||
|
||||
DAEMON_JSMN_OBJS := daemon/jsmn.o
|
||||
|
@ -22,7 +23,8 @@ DAEMON_HEADERS := \
|
|||
daemon/json.h \
|
||||
daemon/lightningd.h \
|
||||
daemon/log.h \
|
||||
daemon/pseudorand.h
|
||||
daemon/pseudorand.h \
|
||||
daemon/timeout.h
|
||||
|
||||
$(DAEMON_OBJS): $(DAEMON_HEADERS) $(DAEMON_JSMN_HEADERS) $(BITCOIN_HEADERS) $(CORE_HEADERS) $(GEN_HEADERS) $(CCAN_HEADERS)
|
||||
$(DAEMON_JSMN_OBJS): $(DAEMON_JSMN_HEADERS)
|
||||
|
|
|
@ -20,6 +20,7 @@ static struct lightningd_state *lightningd_state(void)
|
|||
state->base_log = new_log(state, state->log_record,
|
||||
"lightningd(%u):", (int)getpid());
|
||||
|
||||
timers_init(&state->timers, time_now());
|
||||
return state;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef LIGHTNING_DAEMON_LIGHTNING_H
|
||||
#define LIGHTNING_DAEMON_LIGHTNING_H
|
||||
#include "config.h"
|
||||
#include <ccan/timer/timer.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* Here's where the global variables hide! */
|
||||
|
@ -13,5 +14,8 @@ struct lightningd_state {
|
|||
/* Our config dir, and rpc file */
|
||||
char *config_dir;
|
||||
char *rpc_filename;
|
||||
|
||||
/* Any pending timers. */
|
||||
struct timers timers;
|
||||
};
|
||||
#endif /* LIGHTNING_DAEMON_LIGHTNING_H */
|
||||
|
|
18
daemon/timeout.c
Normal file
18
daemon/timeout.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
#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 *state, struct timeout *t)
|
||||
{
|
||||
timer_del(&state->timers, &t->timer);
|
||||
timer_add(&state->timers, &t->timer,
|
||||
timeabs_add(time_now(), t->interval));
|
||||
}
|
27
daemon/timeout.h
Normal file
27
daemon/timeout.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#ifndef LIGHTNING_DAEMON_TIMEOUT_H
|
||||
#define LIGHTNING_DAEMON_TIMEOUT_H
|
||||
#include "config.h"
|
||||
|
||||
#include <ccan/time/time.h>
|
||||
#include <ccan/timer/timer.h>
|
||||
#include <ccan/typesafe_cb/typesafe_cb.h>
|
||||
|
||||
struct timeout {
|
||||
struct timer timer;
|
||||
struct timerel interval;
|
||||
void (*cb)(void *);
|
||||
void *arg;
|
||||
};
|
||||
|
||||
struct lightningd_state;
|
||||
|
||||
void init_timeout_(struct timeout *t, unsigned int interval,
|
||||
void (*cb)(void *), void *arg);
|
||||
|
||||
void refresh_timeout(struct lightningd_state *state, struct timeout *t);
|
||||
|
||||
#define init_timeout(t, interval, func, arg) \
|
||||
init_timeout_((t), (interval), \
|
||||
typesafe_cb(void, void *, (func), (arg)), (arg))
|
||||
|
||||
#endif /* LIGHTNING_DAEMON_TIMEOUT_H */
|
Loading…
Add table
Reference in a new issue