libplugin: allow freeing in timer callback, clarify docs, allow nested timers.

1. Allow timers to be freed in their callback.
2. Clarify in header that we have to terminate our timer with timer_finished()
   eventually.
3. We don't currently have plugins with more than one outstanding timer, but
   it certainly would be possible, so fix in_timer to be a counter.

Suggested-by: @ZmnSCPxj
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2019-05-21 17:02:31 +09:30
committed by Christian Decker
parent 9b61c19a20
commit 6a8cd9a016
2 changed files with 16 additions and 11 deletions

View File

@@ -29,7 +29,7 @@ static STRMAP(const char *) usagemap;
/* Timers */
static struct timers timers;
static bool in_timer;
static size_t in_timer;
bool deprecated_apis;
@@ -244,8 +244,8 @@ command_done_raw(struct command *cmd,
struct command_result *timer_complete(void)
{
assert(in_timer);
in_timer = false;
assert(in_timer > 0);
in_timer--;
return &complete;
}
@@ -603,9 +603,10 @@ static void call_plugin_timer(struct plugin_conn *rpc, struct timer *timer)
{
struct plugin_timer *t = container_of(timer, struct plugin_timer, timer);
in_timer = true;
in_timer++;
/* Free this if they don't. */
tal_steal(tmpctx, t);
t->cb();
tal_free(t);
}
static void destroy_plugin_timer(struct plugin_timer *timer)