Skip to content

Commit 2cb8b0c

Browse files
committed
Move check_and_update_readiness to epoll.rs and make EpollEventInterest field private
1 parent 874b0c9 commit 2cb8b0c

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

src/shims/unix/fd.rs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::rc::Weak;
1010

1111
use rustc_target::abi::Size;
1212

13-
use crate::shims::unix::linux::epoll::{EpollEventInstance, EpollReadyEvents};
13+
use crate::shims::unix::linux::epoll::EpollReadyEvents;
1414
use crate::shims::unix::*;
1515
use crate::*;
1616

@@ -270,6 +270,7 @@ impl FileDescriptionRef {
270270
&self,
271271
ecx: &mut InterpCx<'tcx, MiriMachine<'tcx>>,
272272
) -> InterpResult<'tcx, ()> {
273+
use crate::shims::unix::linux::epoll::EvalContextExt;
273274
ecx.check_and_update_readiness(self.get_id(), || self.borrow_mut().get_epoll_ready_events())
274275
}
275276
}
@@ -391,41 +392,6 @@ impl FdTable {
391392

392393
impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
393394
pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
394-
/// For a specific unique file descriptor id, get its ready events and update
395-
/// the corresponding ready list.
396-
fn check_and_update_readiness(
397-
&self,
398-
id: FdId,
399-
get_ready_events: impl FnOnce() -> InterpResult<'tcx, EpollReadyEvents>,
400-
) -> InterpResult<'tcx, ()> {
401-
let this = self.eval_context_ref();
402-
// Get a list of EpollEventInterest that is associated to a specific file description.
403-
if let Some(epoll_interests) = this.machine.epoll_interests.get_epoll_interest(id) {
404-
let epoll_ready_events = get_ready_events()?;
405-
// Get the bitmask of ready events.
406-
let ready_events = epoll_ready_events.get_event_bitmask(this);
407-
408-
for weak_epoll_interest in epoll_interests {
409-
if let Some(epoll_interest) = weak_epoll_interest.upgrade() {
410-
// This checks if any of the events specified in epoll_event_interest.events
411-
// match those in ready_events.
412-
let epoll_event_interest = epoll_interest.borrow();
413-
let flags = epoll_event_interest.events & ready_events;
414-
// If there is any event that we are interested in being specified as ready,
415-
// insert an epoll_return to the ready list.
416-
if flags != 0 {
417-
let epoll_key = (id, epoll_event_interest.file_descriptor);
418-
let ready_list = &mut epoll_event_interest.ready_list.borrow_mut();
419-
let event_instance =
420-
EpollEventInstance::new(flags, epoll_event_interest.data);
421-
ready_list.insert(epoll_key, event_instance);
422-
}
423-
}
424-
}
425-
}
426-
Ok(())
427-
}
428-
429395
fn dup(&mut self, old_fd: i32) -> InterpResult<'tcx, Scalar> {
430396
let this = self.eval_context_mut();
431397

src/shims/unix/linux/epoll.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ impl EpollEventInstance {
5050
#[derive(Clone, Debug)]
5151
pub struct EpollEventInterest {
5252
/// The file descriptor value of the file description registered.
53-
pub file_descriptor: i32,
53+
file_descriptor: i32,
5454
/// The events bitmask retrieved from `epoll_event`.
55-
pub events: u32,
55+
events: u32,
5656
/// The data retrieved from `epoll_event`.
5757
/// libc's data field in epoll_event can store integer or pointer,
5858
/// but only u64 is supported for now.
5959
/// <https://man7.org/linux/man-pages/man3/epoll_event.3type.html>
60-
pub data: u64,
60+
data: u64,
6161
/// Ready list of the epoll instance under which this EpollEventInterest is registered.
62-
pub ready_list: Rc<RefCell<BTreeMap<(FdId, i32), EpollEventInstance>>>,
62+
ready_list: Rc<RefCell<BTreeMap<(FdId, i32), EpollEventInstance>>>,
6363
}
6464

6565
/// EpollReadyEvents reflects the readiness of a file description.
@@ -437,4 +437,39 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
437437
}
438438
Ok(Scalar::from_i32(num_of_events))
439439
}
440+
441+
/// For a specific unique file descriptor id, get its ready events and update
442+
/// the corresponding ready list.
443+
fn check_and_update_readiness(
444+
&self,
445+
id: FdId,
446+
get_ready_events: impl FnOnce() -> InterpResult<'tcx, EpollReadyEvents>,
447+
) -> InterpResult<'tcx, ()> {
448+
let this = self.eval_context_ref();
449+
// Get a list of EpollEventInterest that is associated to a specific file description.
450+
if let Some(epoll_interests) = this.machine.epoll_interests.get_epoll_interest(id) {
451+
let epoll_ready_events = get_ready_events()?;
452+
// Get the bitmask of ready events.
453+
let ready_events = epoll_ready_events.get_event_bitmask(this);
454+
455+
for weak_epoll_interest in epoll_interests {
456+
if let Some(epoll_interest) = weak_epoll_interest.upgrade() {
457+
// This checks if any of the events specified in epoll_event_interest.events
458+
// match those in ready_events.
459+
let epoll_event_interest = epoll_interest.borrow();
460+
let flags = epoll_event_interest.events & ready_events;
461+
// If there is any event that we are interested in being specified as ready,
462+
// insert an epoll_return to the ready list.
463+
if flags != 0 {
464+
let epoll_key = (id, epoll_event_interest.file_descriptor);
465+
let ready_list = &mut epoll_event_interest.ready_list.borrow_mut();
466+
let event_instance =
467+
EpollEventInstance::new(flags, epoll_event_interest.data);
468+
ready_list.insert(epoll_key, event_instance);
469+
}
470+
}
471+
}
472+
}
473+
Ok(())
474+
}
440475
}

src/shims/unix/linux/eventfd.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl FileDescription for Event {
9191
self.counter = 0;
9292
// When any of the event happened, we check and update the status of all supported event
9393
// types for current file description.
94+
use crate::shims::unix::linux::epoll::EvalContextExt;
9495
ecx.check_and_update_readiness(self.id, || self.get_epoll_ready_events())?;
9596
return Ok(Ok(U64_ARRAY_SIZE));
9697
}
@@ -148,6 +149,7 @@ impl FileDescription for Event {
148149
};
149150
// When any of the event happened, we check and update the status of all supported event
150151
// types for current file description.
152+
use crate::shims::unix::linux::epoll::EvalContextExt;
151153
ecx.check_and_update_readiness(self.id, || self.get_epoll_ready_events())?;
152154
Ok(Ok(U64_ARRAY_SIZE))
153155
}

0 commit comments

Comments
 (0)