Skip to content

Commit

Permalink
📝 remove some unsafe code and use the safe version (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Dec 24, 2024
1 parent 2dae05f commit 2c570ff
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/io/sys/unix/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl Selector {
events: &mut [SysEvent],
_timeout: Option<u64>,
) -> io::Result<Option<u64>> {
assert!(id < self.vec.len());
#[cfg(feature = "io_timeout")]
let timeout_ms = _timeout
.map(|to| EpollTimeout::try_from(to.div_ceil(1_000_000)).unwrap())
Expand All @@ -86,15 +87,15 @@ impl Selector {
let timeout_ms = EpollTimeout::NONE;
// info!("select; timeout={:?}", timeout_ms);

let single_selector = unsafe { self.vec.get_unchecked(id) };
let single_selector = &self.vec[id];
let epoll = &single_selector.epoll;

// Wait for epoll events for at most timeout_ms milliseconds
let n = epoll.wait(events, timeout_ms)?;
// println!("epoll_wait = {}", n);

// collect coroutines
for event in unsafe { events.get_unchecked(..n) } {
for event in &events[..n] {
if event.data() == 0 {
// this is just a wakeup event, ignore it
let mut buf = [0u8; 8];
Expand Down Expand Up @@ -152,7 +153,7 @@ impl Selector {
#[inline]
pub fn wakeup(&self, id: usize) {
let buf = 1u64.to_le_bytes();
let ret = write(&unsafe { self.vec.get_unchecked(id) }.evfd, &buf);
let ret = write(&self.vec[id].evfd, &buf);
trace!("wakeup id={:?}, ret={:?}", id, ret);
}

Expand All @@ -169,7 +170,7 @@ impl Selector {

let fd = io_data.fd;
let id = fd as usize % self.vec.len();
let single_selector = unsafe { self.vec.get_unchecked(id) };
let single_selector = &self.vec[id];
let epoll = &single_selector.epoll;
info!("add fd to epoll select, fd={:?}", fd);
epoll
Expand All @@ -194,7 +195,7 @@ impl Selector {

let fd = io_data.fd;
let id = fd as usize % self.vec.len();
let single_selector = unsafe { self.vec.get_unchecked(id) };
let single_selector = &self.vec[id];
let epoll = &single_selector.epoll;
info!("mod fd to epoll select, fd={:?}, is_read={}", fd, is_read);
epoll
Expand All @@ -217,7 +218,7 @@ impl Selector {

let fd = io_data.fd;
let id = fd as usize % self.vec.len();
let single_selector = unsafe { self.vec.get_unchecked(id) };
let single_selector = &self.vec[id];
let epoll = &single_selector.epoll;
info!("del fd from epoll select, fd={:?}", fd);
epoll.delete(unsafe { BorrowedFd::borrow_raw(fd) }).ok();
Expand All @@ -230,7 +231,7 @@ impl Selector {
// must free them before the next epoll_wait
#[inline]
fn free_unused_event_data(&self, id: usize) {
let free_ev = &unsafe { self.vec.get_unchecked(id) }.free_ev;
let free_ev = &self.vec[id].free_ev;
while !free_ev.bulk_pop().is_empty() {}
}

Expand All @@ -240,9 +241,7 @@ impl Selector {
pub fn add_io_timer(&self, io: &IoData, timeout: Duration) {
let id = io.fd as usize % self.vec.len();
// info!("io timeout = {:?}", dur);
let (h, b_new) = unsafe { self.vec.get_unchecked(id) }
.timer_list
.add_timer(timeout, io.timer_data());
let (h, b_new) = self.vec[id].timer_list.add_timer(timeout, io.timer_data());
if b_new {
// wake up the event loop thread to recall the next wait timeout
self.wakeup(id);
Expand Down

0 comments on commit 2c570ff

Please sign in to comment.