if(!io->cb)
return;
+ loop->deletion = true;
+
io_set(loop, io, 0);
splay_unlink_node(&loop->ios, &io->node);
}
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;
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;
}
-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;
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))
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;
}
}