Don't abort on drop, don't close on drop
This commit is contained in:
parent
69074ee66c
commit
92fae68ad7
21
src/lib.rs
21
src/lib.rs
|
@ -22,11 +22,11 @@ const UNPARK_DURATION: Duration = Duration::from_millis(50);
|
|||
|
||||
pub struct JoinHandle<T> {
|
||||
rx: Receiver<T>,
|
||||
abort: Sender<()>,
|
||||
abort: Arc<Sender<()>>,
|
||||
}
|
||||
|
||||
pub struct AbortHandle {
|
||||
abort: Sender<()>,
|
||||
abort: Arc<Sender<()>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -53,7 +53,7 @@ impl<T> JoinHandle<T> {
|
|||
|
||||
pub fn abort_handle(&self) -> AbortHandle {
|
||||
AbortHandle {
|
||||
abort: self.abort.duplicate(),
|
||||
abort: Arc::clone(&self.abort),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ struct Task {
|
|||
|
||||
struct FutureState {
|
||||
future: BoxFuture<'static, ()>,
|
||||
abort: Receiver<()>,
|
||||
abort: Option<Receiver<()>>,
|
||||
}
|
||||
|
||||
impl Task {
|
||||
|
@ -96,8 +96,13 @@ impl Task {
|
|||
let mut context = Context::from_waker(&safe_waker);
|
||||
|
||||
self.state.lock().unwrap().as_mut().map(|state| {
|
||||
if Pin::new(&mut state.abort).poll(&mut context).is_ready() {
|
||||
return Poll::Ready(());
|
||||
if let Some(abort) = state.abort.as_mut() {
|
||||
if let Poll::Ready(res) = Pin::new(abort).poll(&mut context) {
|
||||
state.abort.take();
|
||||
if res.is_ok() {
|
||||
return Poll::Ready(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
state.future.as_mut().poll(&mut context)
|
||||
|
@ -283,7 +288,7 @@ impl ThreadState {
|
|||
future: Box::pin(async move {
|
||||
let _ = tx.send(future.await);
|
||||
}),
|
||||
abort: abort_rx,
|
||||
abort: Some(abort_rx),
|
||||
};
|
||||
*task.state.lock().unwrap() = Some(state);
|
||||
self.wake(task);
|
||||
|
@ -298,7 +303,7 @@ impl ThreadState {
|
|||
|
||||
JoinHandle {
|
||||
rx,
|
||||
abort: abort_tx,
|
||||
abort: Arc::new(abort_tx),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,12 +44,6 @@ impl<T> Sender<T> {
|
|||
self.send_borrowed(item)
|
||||
}
|
||||
|
||||
pub(crate) fn duplicate(&self) -> Self {
|
||||
Sender {
|
||||
state: Arc::clone(&self.state),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn send_borrowed(&self, item: T) -> Result<(), T> {
|
||||
let mut guard = self.state.lock().unwrap();
|
||||
|
||||
|
|
Loading…
Reference in a new issue