As per the documentation of io_getevents(2),
On success, io_getevents() returns the number of events read.
This may be 0, or a value less than min_nr, if the timeout
expired. It may also be a nonzero value less than min_nr, if the
call was interrupted by a signal handler.
It indicates in case of error EINTR
call may returns partial events. But it not clear if we are supposed to consume the partially returned events or let the call retry unless it returns at least min_nr
event that was expected in successful case?
Are we supposed to consume the partially returned events something like as following which is similar to what happens in case of read() call?
ssize_t io_wait_for_events(io_context_t ctx, struct io_event *events, int min_nr, int max_nr) {
ssize_t num_events = 0;
do {
// Attempt to fetch events (it may return fewer than min_nr if interrupted)
num_events = io_getevents(ctx, min_nr, max_nr, events, nullptr);
if (num_events >= 0) {
// Process the successfully retrieved events
if (num_events > 0) {
process_events(events, num_events); // Handle partially retrieved events
}
// Check if we need to retry for more events
if (num_events < min_nr) {
min_nr -= num_events; // Adjust the number of remaining events we need
events += num_events; // Move the pointer forward
}
} else if (num_events == -EINTR) {
// Retry if we were interrupted by a signal (no valid events retrieved)
continue;
} else {
// Handle other potential errors from io_getevents
std::cerr << "Error in io_getevents: " << strerror(-num_events) << std::endl;
return num_events;
}
} while (num_events < min_nr); // Continue until we get at least min_nr events
return num_events;
}
It gets even more confusing when looking at the manual of io_getevents(3) that doesn’t even mention what happens in case of call is interrupted. What does one supposed to do if using libaio based wrapper APIs ?