Skip to content

Commit 6431733

Browse files
committed
Refactor the logic
1 parent a9eb9d9 commit 6431733

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

src/shims/unix/linux/epoll.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -431,35 +431,42 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
431431
let mut num_of_events: i32 = 0;
432432
let mut array_iter = this.project_array_fields(&event)?;
433433

434-
while let Some(des) = array_iter.next(this)?
435-
&& !ready_list.is_empty()
436-
{
437-
let mut entry_written = false;
438-
// Fetch an event from the ready list.
439-
while !entry_written {
440-
if let Some((epoll_key, epoll_return)) = ready_list.pop_first() {
441-
// If the file description is fully close, the entry for corresponding FdID in the
442-
// global epoll event interest table would be empty.
443-
if this.machine.epoll_interests.get_epoll_interest(epoll_key.0).is_some() {
444-
// Return notification to the caller if the file description is not fully closed.
445-
this.write_int_fields_named(
446-
&[
447-
("events", epoll_return.events.into()),
448-
("u64", epoll_return.data.into()),
449-
],
450-
&des.1,
451-
)?;
452-
num_of_events = num_of_events.checked_add(1).unwrap();
453-
entry_written = true;
454-
}
455-
} else {
456-
break;
457-
}
434+
while let Some(des) = array_iter.next(this)? {
435+
if let Some(epoll_event_instance) = this.ready_list_next(&mut ready_list) {
436+
this.write_int_fields_named(
437+
&[
438+
("events", epoll_event_instance.events.into()),
439+
("u64", epoll_event_instance.data.into()),
440+
],
441+
&des.1,
442+
)?;
443+
num_of_events = num_of_events.checked_add(1).unwrap();
444+
} else {
445+
break;
458446
}
459447
}
460448
Ok(Scalar::from_i32(num_of_events))
461449
}
462450

451+
/// This function takes in ready list and returns EpollEventInstance with file descriptions
452+
/// that are not closed.
453+
fn ready_list_next(
454+
&self,
455+
ready_list: &mut BTreeMap<(FdId, i32), EpollEventInstance>,
456+
) -> Option<EpollEventInstance> {
457+
let this = self.eval_context_ref();
458+
while let Some((epoll_key, epoll_event_instance)) = ready_list.pop_first() {
459+
// This ensures that we only return events that we are interested. The FD might have been closed since
460+
// the event was generated, in which case we are not interested anymore.
461+
if this.machine.epoll_interests.get_epoll_interest(epoll_key.0).is_some() {
462+
// If the file description is fully close, the entry for corresponding FdID in the
463+
// global epoll event interest table would be empty.
464+
return Some(epoll_event_instance);
465+
}
466+
}
467+
return None;
468+
}
469+
463470
/// For a specific file description, get its ready events and update the corresponding ready
464471
/// list. This function should be called whenever an event causes more bytes or an EOF to become
465472
/// newly readable from an FD, and whenever more bytes can be written to an FD or no more future

0 commit comments

Comments
 (0)