]> git.meshlink.io Git - meshlink/blobdiff - src/event.c
Merge branch 'mesh_topology_output' into roles
[meshlink] / src / event.c
index aa451801ea64742b5494785a1f378cee989a73e6..62daea6f13cf3a6138116e67773c1c60502bd22a 100644 (file)
@@ -81,6 +81,8 @@ void io_del(event_loop_t *loop, io_t *io) {
        if(!io->cb)
                return;
 
+       loop->deletion = true;
+
        io_set(loop, io, 0);
 
        splay_unlink_node(&loop->ios, &io->node);
@@ -88,6 +90,9 @@ void io_del(event_loop_t *loop, io_t *io) {
 }
 
 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
+       if(!timeout->cb)
+               timeout->tv = (struct timeval){0, 0};
+
        timeout->cb = cb;
        timeout->data = data;
        timeout->node.data = timeout;
@@ -112,6 +117,8 @@ void timeout_del(event_loop_t *loop, timeout_t *timeout) {
        if(!timeout->cb)
                return;
 
+       loop->deletion = true;
+
        splay_unlink_node(&loop->timeouts, &timeout->node);
        timeout->cb = 0;
        timeout->tv = (struct timeval){0, 0};
@@ -164,11 +171,13 @@ void signal_del(event_loop_t *loop, signal_t *sig) {
        if(!sig->cb)
                return;
 
+       loop->deletion = true;
+
        splay_unlink_node(&loop->signals, &sig->node);
        sig->cb = NULL;
 }
 
-bool event_loop_run(event_loop_t *loop) {
+bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
        loop->running = true;
 
        fd_set readable;
@@ -202,7 +211,12 @@ bool event_loop_run(event_loop_t *loop) {
                        fds = last->fd + 1;
                }
 
+               // release mesh mutex during select
+               if(mutex)
+                       pthread_mutex_unlock(mutex);
                int n = select(fds, &readable, &writable, NULL, tv);
+               if(mutex)
+                       pthread_mutex_lock(mutex);
 
                if(n < 0) {
                        if(sockwouldblock(errno))
@@ -214,11 +228,21 @@ bool event_loop_run(event_loop_t *loop) {
                if(!n)
                        continue;
 
+               // Normally, splay_each allows the current node to be deleted. However,
+               // it can be that one io callback triggers the deletion of another io,
+               // so we have to detect this and break the loop.
+
+               loop->deletion = false;
+
                for splay_each(io_t, io, &loop->ios) {
-                       if(FD_ISSET(io->fd, &writable))
+                       if(FD_ISSET(io->fd, &writable) && io->cb)
                                io->cb(loop, io->data, IO_WRITE);
-                       else if(FD_ISSET(io->fd, &readable))
+                       if(loop->deletion)
+                               break;
+                       if(FD_ISSET(io->fd, &readable) && io->cb)
                                io->cb(loop, io->data, IO_READ);
+                       if(loop->deletion)
+                               break;
                }
        }