From 415c8dba3c58a2ebd586e4068decba6b525b7c41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=87=E9=80=B8?= Date: Fri, 20 Dec 2024 08:32:17 +0900 Subject: [PATCH] refactor(driver): remove one Result usage (#369) * refactor(driver): remove one Result usage * fix(driver): make push_blocking return bool --- compio-driver/src/fusion/mod.rs | 8 ++++---- compio-driver/src/iocp/mod.rs | 16 ++++++---------- compio-driver/src/iour/mod.rs | 18 ++++++++---------- 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, 23 insertions(+), 33 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..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( @@ -322,11 +321,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..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<()> { @@ -290,7 +288,7 @@ impl Driver { Ok(()) } - pub fn handle(&self) -> io::Result { + pub fn handle(&self) -> NotifyHandle { self.notifier.handle() } } @@ -360,8 +358,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 67dc2052..71a8720c 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); };