if(!io->cb)
return;
+ loop->deletion = true;
+
io_set(loop, io, 0);
splay_unlink_node(&loop->ios, &io->node);
if(!timeout->cb)
return;
+ loop->deletion = true;
+
splay_unlink_node(&loop->timeouts, &timeout->node);
timeout->cb = 0;
timeout->tv = (struct timeval){0, 0};
if(!sig->cb)
return;
+ loop->deletion = true;
+
splay_unlink_node(&loop->signals, &sig->node);
sig->cb = NULL;
}
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;
}
}