From: Guus Sliepen Date: Fri, 31 Oct 2014 08:10:23 +0000 (+0100) Subject: Call utcp_timeout() once every iteration of the event loop. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=ea17ede077e84f88c7f20e9053b38086583ab635;p=meshlink Call utcp_timeout() once every iteration of the event loop. --- diff --git a/src/event.c b/src/event.c index 62daea6f..5a131b64 100644 --- a/src/event.c +++ b/src/event.c @@ -177,6 +177,11 @@ void signal_del(event_loop_t *loop, signal_t *sig) { sig->cb = NULL; } +void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) { + loop->idle_cb = cb; + loop->idle_data = data; +} + bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) { loop->running = true; @@ -185,7 +190,7 @@ bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) { while(loop->running) { gettimeofday(&loop->now, NULL); - struct timeval diff, *tv = NULL; + struct timeval diff, it, *tv = NULL; while(loop->timeouts.head) { timeout_t *timeout = loop->timeouts.head->data; @@ -201,6 +206,12 @@ bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) { } } + if(loop->idle_cb) { + it = loop->idle_cb(loop, loop->idle_data); + if(it.tv_sec >= 0 && (!tv || timercmp(&it, tv, <))) + tv = ⁢ + } + memcpy(&readable, &loop->readfds, sizeof readable); memcpy(&writable, &loop->writefds, sizeof writable); diff --git a/src/event.h b/src/event.h index 4eb18251..53559f80 100644 --- a/src/event.h +++ b/src/event.h @@ -31,6 +31,7 @@ typedef struct event_loop_t event_loop_t; typedef void (*io_cb_t)(event_loop_t *loop, void *data, int flags); typedef void (*timeout_cb_t)(event_loop_t *loop, void *data); typedef void (*signal_cb_t)(event_loop_t *loop, void *data); +typedef struct timeval (*idle_cb_t)(event_loop_t *loop, void *data); typedef struct io_t { int fd; @@ -66,6 +67,9 @@ struct event_loop_t { splay_tree_t timeouts; splay_tree_t signals; + idle_cb_t idle_cb; + void *idle_data; + io_t signalio; int pipefd[2]; @@ -84,6 +88,8 @@ extern void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void * extern void signal_trigger(event_loop_t *loop, signal_t *sig); extern void signal_del(event_loop_t *loop, signal_t *sig); +extern void idle_set(event_loop_t *loop, idle_cb_t cb, void *data); + extern void event_loop_init(event_loop_t *loop); extern void event_loop_exit(event_loop_t *loop); extern bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex); diff --git a/src/meshlink.c b/src/meshlink.c index 4538548d..82666332 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -699,6 +699,20 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) { return true; } +static struct timeval idle(event_loop_t *loop, void *data) { + meshlink_handle_t *mesh = data; + int t, tmin = -1; + for splay_each(node_t, n, mesh->nodes) { + if(!n->utcp) + continue; + t = utcp_timeout(n->utcp); + if(t >= 0 && t < tmin) + tmin = t; + } + struct timeval tv = {.tv_sec = t}; + return tv; +} + static bool meshlink_setup(meshlink_handle_t *mesh) { if(mkdir(mesh->confbase, 0777) && errno != EEXIST) { logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", mesh->confbase, strerror(errno)); @@ -845,6 +859,8 @@ meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *nam return NULL; } + idle_set(&mesh->loop, idle, mesh); + logger(NULL, MESHLINK_DEBUG, "meshlink_open returning\n"); return mesh; } diff --git a/src/utcp b/src/utcp index 5d4cf9d0..52eeda5e 160000 --- a/src/utcp +++ b/src/utcp @@ -1 +1 @@ -Subproject commit 5d4cf9d0eb85c7322298a2a0526132197e3bf633 +Subproject commit 52eeda5e47e8a081cd2f94b78536c6376592a526