lib: Create a helper function for io read operations

Currently when io is ready inside of the event system
the first FD received is always preferred as the ones
that are handled first.  This leads to results where
events associated with these first FD's are always handled
first.

In anticipation of a change to make this more fair
let's abstract the function handler.

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
This commit is contained in:
Donald Sharp 2023-11-02 12:02:43 -04:00
parent 448f75e56d
commit 6b19e40730

View File

@ -1589,26 +1589,16 @@ static int thread_process_io_helper(struct event_loop *m, struct event *thread,
return 1; return 1;
} }
/** static inline void thread_process_io_inner_loop(struct event_loop *m,
* Process I/O events. unsigned int num,
* struct pollfd *pfds, nfds_t *i,
* Walks through file descriptor array looking for those pollfds whose .revents uint32_t *ready)
* field has something interesting. Deletes any invalid file descriptors.
*
* @param m the thread master
* @param num the number of active file descriptors (return value of poll())
*/
static void thread_process_io(struct event_loop *m, unsigned int num)
{ {
unsigned int ready = 0;
struct pollfd *pfds = m->handler.copy;
for (nfds_t i = 0; i < m->handler.copycount && ready < num; ++i) {
/* no event for current fd? immediately continue */ /* no event for current fd? immediately continue */
if (pfds[i].revents == 0) if (pfds[*i].revents == 0)
continue; return;
ready++; *ready = *ready + 1;
/* /*
* Unless someone has called event_cancel from another * Unless someone has called event_cancel from another
@ -1624,36 +1614,52 @@ static void thread_process_io(struct event_loop *m, unsigned int num)
* this is because the read should fail and the * this is because the read should fail and the
* read function should handle it appropriately * read function should handle it appropriately
*/ */
if (pfds[i].revents & (POLLIN | POLLHUP | POLLERR)) { if (pfds[*i].revents & (POLLIN | POLLHUP | POLLERR)) {
thread_process_io_helper(m, m->read[pfds[i].fd], POLLIN, thread_process_io_helper(m, m->read[pfds[*i].fd], POLLIN,
pfds[i].revents, i); pfds[*i].revents, *i);
} }
if (pfds[i].revents & POLLOUT) if (pfds[*i].revents & POLLOUT)
thread_process_io_helper(m, m->write[pfds[i].fd], thread_process_io_helper(m, m->write[pfds[*i].fd], POLLOUT,
POLLOUT, pfds[i].revents, i); pfds[*i].revents, *i);
/* /*
* if one of our file descriptors is garbage, remove the same * if one of our file descriptors is garbage, remove the same
* from both pfds + update sizes and index * from both pfds + update sizes and index
*/ */
if (pfds[i].revents & POLLNVAL) { if (pfds[*i].revents & POLLNVAL) {
memmove(m->handler.pfds + i, m->handler.pfds + i + 1, memmove(m->handler.pfds + *i, m->handler.pfds + *i + 1,
(m->handler.pfdcount - i - 1) (m->handler.pfdcount - *i - 1) * sizeof(struct pollfd));
* sizeof(struct pollfd));
m->handler.pfdcount--; m->handler.pfdcount--;
m->handler.pfds[m->handler.pfdcount].fd = 0; m->handler.pfds[m->handler.pfdcount].fd = 0;
m->handler.pfds[m->handler.pfdcount].events = 0; m->handler.pfds[m->handler.pfdcount].events = 0;
memmove(pfds + i, pfds + i + 1, memmove(pfds + *i, pfds + *i + 1,
(m->handler.copycount - i - 1) (m->handler.copycount - *i - 1) * sizeof(struct pollfd));
* sizeof(struct pollfd));
m->handler.copycount--; m->handler.copycount--;
m->handler.copy[m->handler.copycount].fd = 0; m->handler.copy[m->handler.copycount].fd = 0;
m->handler.copy[m->handler.copycount].events = 0; m->handler.copy[m->handler.copycount].events = 0;
i--; *i = *i - 1;
} }
} }
/**
* Process I/O events.
*
* Walks through file descriptor array looking for those pollfds whose .revents
* field has something interesting. Deletes any invalid file descriptors.
*
* @param m the thread master
* @param num the number of active file descriptors (return value of poll())
*/
static void thread_process_io(struct event_loop *m, unsigned int num)
{
unsigned int ready = 0;
struct pollfd *pfds = m->handler.copy;
for (nfds_t i = 0; i < m->handler.copycount && ready < num; ++i) {
thread_process_io_inner_loop(m, num, pfds, &i, &ready);
}
} }
/* Add all timers that have popped to the ready list. */ /* Add all timers that have popped to the ready list. */