-
Notifications
You must be signed in to change notification settings - Fork 795
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
Joseph Perez
committed
Oct 29, 2023
1 parent
8695c4f
commit 437aef4
Showing
10 changed files
with
300 additions
and
108 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
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,68 @@ | ||
use crate::{ffi, Py, PyObject, Python}; | ||
use futures_util::future::poll_fn; | ||
use futures_util::task::AtomicWaker; | ||
use std::ptr; | ||
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. | ||
#[derive(Debug, Default)] | ||
pub struct CoroutineCancel(Arc<Inner>); | ||
|
||
impl CoroutineCancel { | ||
/// Create a new `CoroutineCancel`. | ||
pub fn new() -> Self { | ||
Default::default() | ||
} | ||
|
||
/// Return an associated [`CancelHandle`]. | ||
pub fn handle(&self) -> CancelHandle { | ||
CancelHandle(self.0.clone()) | ||
} | ||
|
||
fn take_exception(&self) -> PyObject { | ||
let ptr = self.0.exception.swap(ptr::null_mut(), Ordering::Relaxed); | ||
Python::with_gil(|gil| unsafe { Py::from_owned_ptr(gil, ptr) }) | ||
} | ||
|
||
/// 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> { | ||
if self.is_cancelled() { | ||
return Poll::Ready(self.take_exception()); | ||
} | ||
self.0.waker.register(cx.waker()); | ||
if self.is_cancelled() { | ||
return Poll::Ready(self.take_exception()); | ||
} | ||
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 | ||
} | ||
} | ||
|
||
/// [`CoroutineCancel`] handle used in | ||
/// [`Coroutine::from_future_with_cancel`](crate::coroutine::Coroutine::from_future_with_cancel) | ||
pub struct CancelHandle(Arc<Inner>); | ||
|
||
impl CancelHandle { | ||
pub(super) fn cancel(&self, py: Python<'_>, exc: PyObject) { | ||
let ptr = self.0.exception.swap(exc.into_ptr(), Ordering::Relaxed); | ||
drop(unsafe { PyObject::from_owned_ptr_or_opt(py, ptr) }); | ||
self.0.waker.wake(); | ||
} | ||
} |
Oops, something went wrong.