Skip to content

Commit

Permalink
📝 refine cancel logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Mar 8, 2024
1 parent ba9c255 commit 4883125
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
19 changes: 12 additions & 7 deletions src/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ pub fn trigger_cancel_panic() -> ! {
pub trait CancelIo {
type Data;
fn new() -> Self;
fn set(&self, _: Self::Data);
// set the io data
fn set(&self, io_data: Self::Data);
// clear the io data
fn clear(&self);
unsafe fn cancel(&self);
// if io was set, return Some(io::Result<()>)
unsafe fn cancel(&self) -> Option<io::Result<()>>;
}

#[cfg(not(feature = "io_cancel"))]
Expand Down Expand Up @@ -120,15 +123,19 @@ impl<T: CancelIo> CancelImpl<T> {
pub unsafe fn cancel(&self) {
self.state.fetch_or(1, Ordering::Release);

if let Some(Ok(())) = self.io.cancel() {
// successfully canceled
return;
}

if let Some(co) = self.co.take() {
if let Some(mut co) = co.take() {
self.io.clear();
// this is not safe, the kernel may still need to use the overlapped
// set the cancel result for the coroutine
set_co_para(&mut co, io::Error::new(io::ErrorKind::Other, "Canceled"));
return get_scheduler().schedule(co);
get_scheduler().schedule(co);
}
}
self.io.cancel();
}

// clear the cancel bit so that we can reuse the cancel
Expand All @@ -153,9 +160,7 @@ impl<T: CancelIo> CancelImpl<T> {
// clear the cancel io data
// should be called after io completion
pub fn clear(&self) {
// if self.co.take(Ordering::Acquire).is_none() {
self.io.clear();
// }
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/io/sys/unix/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ impl CancelIo for CancelIoImpl {
self.0.take();
}

unsafe fn cancel(&self) {
unsafe fn cancel(&self) -> Option<io::Result<()>> {
if let Some(e) = self.0.take() {
if let Some(co) = e.co.take() {
get_scheduler().schedule(co);
return Some(Ok(()));
}
}
None
}
}
4 changes: 2 additions & 2 deletions src/io/sys/windows/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl CancelIo for CancelIoImpl {
self.0.take();
}

unsafe fn cancel(&self) {
self.0.take().map(|d| d.cancel());
unsafe fn cancel(&self) -> Option<io::Result<()>> {
self.0.take().map(|d| d.cancel())
}
}

0 comments on commit 4883125

Please sign in to comment.