-
Notifications
You must be signed in to change notification settings - Fork 761
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
286 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add `coroutine::CancelHandle` to catch coroutine cancellation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use crate::{ffi, Py, PyAny, PyObject}; | ||
use futures_util::future::poll_fn; | ||
use futures_util::task::AtomicWaker; | ||
use std::ptr; | ||
use std::ptr::NonNull; | ||
use std::sync::atomic::{AtomicPtr, Ordering}; | ||
use std::sync::Arc; | ||
use std::task::{Context, Poll}; | ||
|
||
#[derive(Debug, Default)] | ||
struct Inner { | ||
exception: AtomicPtr<ffi::PyObject>, | ||
waker: AtomicWaker, | ||
} | ||
|
||
/// Helper used to wait and retrieve exception thrown in [`Coroutine`](super::Coroutine). | ||
/// | ||
/// Only the last exception thrown can be retrieved. | ||
#[derive(Debug, Default)] | ||
pub struct CancelHandle(Arc<Inner>); | ||
|
||
impl CancelHandle { | ||
/// Create a new `CoroutineCancel`. | ||
pub fn new() -> Self { | ||
Default::default() | ||
} | ||
|
||
/// Returns whether the associated coroutine has been cancelled. | ||
pub fn is_cancelled(&self) -> bool { | ||
!self.0.exception.load(Ordering::Relaxed).is_null() | ||
} | ||
|
||
/// Poll to retrieve the exception thrown in the associated coroutine. | ||
pub fn poll_cancelled(&mut self, cx: &mut Context<'_>) -> Poll<PyObject> { | ||
// SAFETY: only valid owned pointer are set in `ThrowCallback::throw` | ||
let take = || unsafe { | ||
// pointer cannot be null because it is checked the line before, | ||
// and the swap is protected by `&mut self` | ||
Py::from_non_null( | ||
NonNull::new(self.0.exception.swap(ptr::null_mut(), Ordering::Relaxed)).unwrap(), | ||
) | ||
}; | ||
if self.is_cancelled() { | ||
return Poll::Ready(take()); | ||
} | ||
self.0.waker.register(cx.waker()); | ||
if self.is_cancelled() { | ||
return Poll::Ready(take()); | ||
} | ||
Poll::Pending | ||
} | ||
|
||
/// Retrieve the exception thrown in the associated coroutine. | ||
pub async fn cancelled(&mut self) -> PyObject { | ||
poll_fn(|cx| self.poll_cancelled(cx)).await | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn throw_callback(&self) -> ThrowCallback { | ||
ThrowCallback(self.0.clone()) | ||
} | ||
} | ||
|
||
#[doc(hidden)] | ||
pub struct ThrowCallback(Arc<Inner>); | ||
|
||
impl ThrowCallback { | ||
pub(super) fn throw(&self, exc: &PyAny) { | ||
let ptr = self.0.exception.swap(exc.into_ptr(), Ordering::Relaxed); | ||
// SAFETY: non-null pointers set in `self.0.exceptions` are valid owned pointers | ||
drop(unsafe { PyObject::from_owned_ptr_or_opt(exc.py(), ptr) }); | ||
self.0.waker.wake(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
use std::future::Future; | ||
|
||
use crate::coroutine::cancel::ThrowCallback; | ||
use crate::{coroutine::Coroutine, types::PyString, IntoPy, PyErr, PyObject}; | ||
|
||
pub fn new_coroutine<F, T, E>( | ||
name: &PyString, | ||
qualname_prefix: Option<&'static str>, | ||
throw_callback: Option<ThrowCallback>, | ||
future: F, | ||
) -> Coroutine | ||
where | ||
F: Future<Output = Result<T, E>> + Send + 'static, | ||
T: IntoPy<PyObject>, | ||
E: Into<PyErr>, | ||
{ | ||
Coroutine::new(Some(name.into()), qualname_prefix, future) | ||
Coroutine::new(Some(name.into()), qualname_prefix, throw_callback, future) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.