From c310c82764f81eac61087cfba0ec9a60d30d6608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Sun, 15 Dec 2024 23:35:46 +0900 Subject: [PATCH 1/2] refactor(driver): remove one Result usage --- compio-driver/src/fusion/mod.rs | 8 ++++---- compio-driver/src/iocp/mod.rs | 7 ++----- compio-driver/src/iour/mod.rs | 6 +++--- compio-driver/src/lib.rs | 2 +- compio-driver/src/poll/mod.rs | 4 ++-- compio-driver/tests/file.rs | 2 +- compio-runtime/src/runtime/mod.rs | 6 +----- 7 files changed, 14 insertions(+), 21 deletions(-) diff --git a/compio-driver/src/fusion/mod.rs b/compio-driver/src/fusion/mod.rs index 964d03de..2829851b 100644 --- a/compio-driver/src/fusion/mod.rs +++ b/compio-driver/src/fusion/mod.rs @@ -84,12 +84,12 @@ impl Driver { } } - pub fn handle(&self) -> io::Result { + 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) } } diff --git a/compio-driver/src/iocp/mod.rs b/compio-driver/src/iocp/mod.rs index 6c910b22..d20680ab 100644 --- a/compio-driver/src/iocp/mod.rs +++ b/compio-driver/src/iocp/mod.rs @@ -322,11 +322,8 @@ impl Driver { Ok(()) } - pub fn handle(&self) -> io::Result { - Ok(NotifyHandle::new( - self.port.handle(), - self.notify_overlapped.clone(), - )) + pub fn handle(&self) -> NotifyHandle { + NotifyHandle::new(self.port.handle(), self.notify_overlapped.clone()) } } diff --git a/compio-driver/src/iour/mod.rs b/compio-driver/src/iour/mod.rs index 692ecef2..24792d6f 100644 --- a/compio-driver/src/iour/mod.rs +++ b/compio-driver/src/iour/mod.rs @@ -290,7 +290,7 @@ impl Driver { Ok(()) } - pub fn handle(&self) -> io::Result { + pub fn handle(&self) -> NotifyHandle { self.notifier.handle() } } @@ -360,8 +360,8 @@ impl Notifier { } } - pub fn handle(&self) -> io::Result { - Ok(NotifyHandle::new(self.fd.clone())) + pub fn handle(&self) -> NotifyHandle { + NotifyHandle::new(self.fd.clone()) } } diff --git a/compio-driver/src/lib.rs b/compio-driver/src/lib.rs index cacf9276..bb0911bd 100644 --- a/compio-driver/src/lib.rs +++ b/compio-driver/src/lib.rs @@ -311,7 +311,7 @@ impl Proactor { } /// Create a notify handle to interrupt the inner driver. - pub fn handle(&self) -> io::Result { + pub fn handle(&self) -> NotifyHandle { self.driver.handle() } } diff --git a/compio-driver/src/poll/mod.rs b/compio-driver/src/poll/mod.rs index b90d51f9..7ef64701 100644 --- a/compio-driver/src/poll/mod.rs +++ b/compio-driver/src/poll/mod.rs @@ -438,8 +438,8 @@ impl Driver { Ok(()) } - pub fn handle(&self) -> io::Result { - Ok(NotifyHandle::new(self.poll.clone())) + pub fn handle(&self) -> NotifyHandle { + NotifyHandle::new(self.poll.clone()) } } diff --git a/compio-driver/tests/file.rs b/compio-driver/tests/file.rs index 745d91dd..c64a6a21 100644 --- a/compio-driver/tests/file.rs +++ b/compio-driver/tests/file.rs @@ -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)); diff --git a/compio-runtime/src/runtime/mod.rs b/compio-runtime/src/runtime/mod.rs index b164c13e..d30d2391 100644 --- a/compio-runtime/src/runtime/mod.rs +++ b/compio-runtime/src/runtime/mod.rs @@ -159,11 +159,7 @@ impl Runtime { /// The caller should ensure the captured lifetime long enough. pub unsafe fn spawn_unchecked(&self, future: F) -> Task { 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); }; From b5c36c24d80f79fd0e1b305dcc71d7dd1e08e09a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Sun, 15 Dec 2024 23:40:43 +0900 Subject: [PATCH 2/2] fix(driver): make push_blocking return bool --- compio-driver/src/iocp/mod.rs | 9 ++++----- compio-driver/src/iour/mod.rs | 12 +++++------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/compio-driver/src/iocp/mod.rs b/compio-driver/src/iocp/mod.rs index d20680ab..ae5a0bd3 100644 --- a/compio-driver/src/iocp/mod.rs +++ b/compio-driver/src/iocp/mod.rs @@ -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 @@ -267,17 +267,16 @@ impl Driver { } } - fn push_blocking(&mut self, user_data: usize) -> io::Result { + 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::::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( diff --git a/compio-driver/src/iour/mod.rs b/compio-driver/src/iour/mod.rs index 24792d6f..331d6d16 100644 --- a/compio-driver/src/iour/mod.rs +++ b/compio-driver/src/iour/mod.rs @@ -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(); @@ -261,11 +261,10 @@ impl Driver { } } - fn push_blocking(&mut self, user_data: usize) -> io::Result { - 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::::new_unchecked(user_data) }; let op_pin = op.as_op_pin(); @@ -273,8 +272,7 @@ impl Driver { 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) -> io::Result<()> {