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;
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;
}
}
+ 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);
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;
splay_tree_t timeouts;
splay_tree_t signals;
+ idle_cb_t idle_cb;
+ void *idle_data;
+
io_t signalio;
int pipefd[2];
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);
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));
return NULL;
}
+ idle_set(&mesh->loop, idle, mesh);
+
logger(NULL, MESHLINK_DEBUG, "meshlink_open returning\n");
return mesh;
}
-Subproject commit 5d4cf9d0eb85c7322298a2a0526132197e3bf633
+Subproject commit 52eeda5e47e8a081cd2f94b78536c6376592a526