On Tue, Dec 13, 2016 at 02:19:47PM +0100, Jan Wickbom wrote: > + > + poll(pfdset->rwfds, numfds, 1000 /* millisecs */); > + > + for (i = 0; i < numfds; ) { > pthread_mutex_lock(&pfdset->fd_mutex); > + > pfdentry = &pfdset->fd[i]; > fd = pfdentry->fd; > + pfd = &pfdset->rwfds[i]; > + > + if (fd < 0) { > + /* Removed during poll */ > + /* shrink first, last migth be deleted*/ > + > + fdset_shrink(pfdset); > + fdset_move_last(pfdset, i); > + fdset_shrink(pfdset); > + > + pthread_mutex_unlock(&pfdset->fd_mutex);
This patch looks better, but as said, I will not do shrink in the event processing loop: I would simply set a flag, something like "need_shrink" here, and do the shrink outside this for loop. > + if (remove1 || remove2) { > + pthread_mutex_lock(&pfdset->fd_mutex); > + > + /* shrink first, last migth be deleted*/ > + fdset_shrink(pfdset); > + fdset_move_last(pfdset, i); > + fdset_shrink(pfdset); > + > + pthread_mutex_unlock(&pfdset->fd_mutex); > + > + continue; Same here: just sets a flag. > + } > + > + i++; > } > + > + /* fdset_del do not shrink, pack eventual remainings of array */ > + pthread_mutex_lock(&pfdset->fd_mutex); > + > + fdset_shrink(pfdset); > + > + for ( ; i < pfdset->num; i++) { > + pfdentry = &pfdset->fd[i]; > + > + if (pfdentry->fd < 0) { > + fdset_move_last(pfdset, i); > + fdset_shrink(pfdset); > + } > + } And yes, do the shrink here (when the shrink flag is set). But I would simply call fdset_shrink() here (without the for loop), and let the fdset_shrink() to handle the details: it could either be a swap with last __valid__ entry, or simply a memmov. That said, if you prefer to choose fdset_move_last(), fine, invoke it inside fdset_shrink() then. Let fdset_shrink be able to remove an fd in the middle. > + > + pthread_mutex_unlock(&pfdset->fd_mutex); > } > } > diff --git a/lib/librte_vhost/fd_man.h b/lib/librte_vhost/fd_man.h > index bd66ed1..03e7881 100644 > --- a/lib/librte_vhost/fd_man.h > +++ b/lib/librte_vhost/fd_man.h > @@ -35,6 +35,7 @@ > #define _FD_MAN_H_ > #include <stdint.h> > #include <pthread.h> > +#include <poll.h> > > #define MAX_FDS 1024 > > @@ -49,9 +50,10 @@ struct fdentry { > }; > > struct fdset { > + struct pollfd rwfds[MAX_FDS]; > struct fdentry fd[MAX_FDS]; > pthread_mutex_t fd_mutex; > - int num; /* current fd number of this fdset */ > + int num; /* highest index occupied in fd array + 1 */ I don't see the comment change makes it more readable. --yliu