Skip to content

Commit

Permalink
refactor(driver): remove one Result usage (#369)
Browse files Browse the repository at this point in the history
* refactor(driver): remove one Result usage

* fix(driver): make push_blocking return bool
  • Loading branch information
Berrysoft authored Dec 19, 2024
1 parent 6f7e828 commit 415c8db
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 33 deletions.
8 changes: 4 additions & 4 deletions compio-driver/src/fusion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ impl Driver {
}
}

pub fn handle(&self) -> io::Result<NotifyHandle> {
pub fn handle(&self) -> NotifyHandle {
let fuse = match &self.fuse {
FuseDriver::Poll(driver) => FuseNotifyHandle::Poll(driver.handle()?),
FuseDriver::IoUring(driver) => FuseNotifyHandle::IoUring(driver.handle()?),
FuseDriver::Poll(driver) => FuseNotifyHandle::Poll(driver.handle()),
FuseDriver::IoUring(driver) => FuseNotifyHandle::IoUring(driver.handle()),
};
Ok(NotifyHandle::from_fuse(fuse))
NotifyHandle::from_fuse(fuse)
}
}

Expand Down
16 changes: 6 additions & 10 deletions compio-driver/src/iocp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl Driver {
match op_pin.op_type() {
OpType::Overlapped => unsafe { op_pin.operate(optr.cast()) },
OpType::Blocking => loop {
if self.push_blocking(user_data)? {
if self.push_blocking(user_data) {
break Poll::Pending;
} else {
// It's OK to wait forever, because any blocking task will notify the IOCP after
Expand All @@ -267,17 +267,16 @@ impl Driver {
}
}

fn push_blocking(&mut self, user_data: usize) -> io::Result<bool> {
fn push_blocking(&mut self, user_data: usize) -> bool {
let port = self.port.handle();
Ok(self
.pool
self.pool
.dispatch(move || {
let mut op = unsafe { Key::<dyn OpCode>::new_unchecked(user_data) };
let optr = op.as_mut_ptr();
let res = op.operate_blocking();
port.post(res, optr).ok();
})
.is_ok())
.is_ok()
}

fn create_entry(
Expand Down Expand Up @@ -322,11 +321,8 @@ impl Driver {
Ok(())
}

pub fn handle(&self) -> io::Result<NotifyHandle> {
Ok(NotifyHandle::new(
self.port.handle(),
self.notify_overlapped.clone(),
))
pub fn handle(&self) -> NotifyHandle {
NotifyHandle::new(self.port.handle(), self.notify_overlapped.clone())
}
}

Expand Down
18 changes: 8 additions & 10 deletions compio-driver/src/iour/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl Driver {
Poll::Pending
}
OpEntry::Blocking => loop {
if self.push_blocking(user_data)? {
if self.push_blocking(user_data) {
break Poll::Pending;
} else {
self.poll_blocking();
Expand All @@ -261,20 +261,18 @@ impl Driver {
}
}

fn push_blocking(&mut self, user_data: usize) -> io::Result<bool> {
let handle = self.handle()?;
fn push_blocking(&mut self, user_data: usize) -> bool {
let handle = self.handle();
let completed = self.pool_completed.clone();
let is_ok = self
.pool
self.pool
.dispatch(move || {
let mut op = unsafe { Key::<dyn crate::sys::OpCode>::new_unchecked(user_data) };
let op_pin = op.as_op_pin();
let res = op_pin.call_blocking();
completed.push(Entry::new(user_data, res));
handle.notify().ok();
})
.is_ok();
Ok(is_ok)
.is_ok()
}

pub unsafe fn poll(&mut self, timeout: Option<Duration>) -> io::Result<()> {
Expand All @@ -290,7 +288,7 @@ impl Driver {
Ok(())
}

pub fn handle(&self) -> io::Result<NotifyHandle> {
pub fn handle(&self) -> NotifyHandle {
self.notifier.handle()
}
}
Expand Down Expand Up @@ -360,8 +358,8 @@ impl Notifier {
}
}

pub fn handle(&self) -> io::Result<NotifyHandle> {
Ok(NotifyHandle::new(self.fd.clone()))
pub fn handle(&self) -> NotifyHandle {
NotifyHandle::new(self.fd.clone())
}
}

Expand Down
2 changes: 1 addition & 1 deletion compio-driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ impl Proactor {
}

/// Create a notify handle to interrupt the inner driver.
pub fn handle(&self) -> io::Result<NotifyHandle> {
pub fn handle(&self) -> NotifyHandle {
self.driver.handle()
}
}
Expand Down
4 changes: 2 additions & 2 deletions compio-driver/src/poll/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ impl Driver {
Ok(())
}

pub fn handle(&self) -> io::Result<NotifyHandle> {
Ok(NotifyHandle::new(self.poll.clone()))
pub fn handle(&self) -> NotifyHandle {
NotifyHandle::new(self.poll.clone())
}
}

Expand Down
2 changes: 1 addition & 1 deletion compio-driver/tests/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn register_multiple() {
fn notify() {
let mut driver = Proactor::new().unwrap();

let handle = driver.handle().unwrap();
let handle = driver.handle();

let thread = std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(1));
Expand Down
6 changes: 1 addition & 5 deletions compio-runtime/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,7 @@ impl Runtime {
/// The caller should ensure the captured lifetime long enough.
pub unsafe fn spawn_unchecked<F: Future>(&self, future: F) -> Task<F::Output> {
let runnables = self.runnables.clone();
let handle = self
.driver
.borrow()
.handle()
.expect("cannot create notify handle of the proactor");
let handle = self.driver.borrow().handle();
let schedule = move |runnable| {
runnables.schedule(runnable, &handle);
};
Expand Down

0 comments on commit 415c8db

Please sign in to comment.