From 5d25fa3d222c06fed54e0aba81e44663df1ef78d Mon Sep 17 00:00:00 2001 From: Terra854 Date: Sun, 22 Nov 2026 01:56:29 +0800 Subject: [PATCH] Share scoped media request cancellation (#2861) --- crates/media-source/src/cancel.rs | 111 ++++++++++++++++++++++++++++++ crates/media-source/src/lib.rs | 2 + 2 files changed, 113 insertions(+) create mode 100644 crates/media-source/src/cancel.rs diff --git a/crates/media-source/src/cancel.rs b/crates/media-source/src/cancel.rs new file mode 100644 index 000000000..41654d51c --- /dev/null +++ b/crates/media-source/src/cancel.rs @@ -0,0 +1,111 @@ +//! Cooperative cancellation of media requests. +//! +//! A request worker installs a flag with [`with_cancel`]. Readers poll [`cancelled`] at +//! their existing interruption points and can return [`crate::MediaError::Cancelled`]. +//! Use [`Context`] to carry the same flag to another worker. + +use std::cell::RefCell; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; + +thread_local! { + static CURRENT: RefCell>> = const { RefCell::new(None) }; +} + +/// Run `f` with this cancellation flag, restoring the preceding state when the scope ends. +pub fn with_cancel(flag: &Arc, f: impl FnOnce() -> R) -> R { + Context(Some(Arc::clone(flag))).run(f) +} + +/// Whether the flag installed on this thread has been set. An empty context is never cancelled. +pub fn cancelled() -> bool { + CURRENT.with_borrow(|flag| flag.as_ref().is_some_and(|flag| flag.load(Ordering::Relaxed))) +} + +/// A captured cancellation scope that can be carried to another thread. +/// +/// It holds the flag itself, so cancellation after capture is visible to the worker. +/// A default context has no flag. +#[derive(Clone, Default)] +pub struct Context(Option>); + +impl Context { + /// Capture this thread's current cancellation scope. + pub fn current() -> Self { + Self(CURRENT.with_borrow(Clone::clone)) + } + + /// Run `f` with this context, even when empty. Restore the previous context on return or unwind. + pub fn run(self, f: impl FnOnce() -> R) -> R { + struct Restore(Option>); + + impl Drop for Restore { + fn drop(&mut self) { + CURRENT.with_borrow_mut(|flag| *flag = self.0.take()); + } + } + + let _restore = Restore(CURRENT.with_borrow_mut(|flag| std::mem::replace(flag, self.0))); + f() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn live_flag_is_scoped_and_nested_contexts_can_mask_it() { + let outer = Arc::new(AtomicBool::new(false)); + let inner = Arc::new(AtomicBool::new(false)); + assert!(!cancelled()); + with_cancel(&outer, || { + assert!(!cancelled()); + outer.store(true, Ordering::Relaxed); + assert!(cancelled()); + with_cancel(&inner, || assert!(!cancelled())); + assert!(cancelled()); + Context::default().run(|| assert!(!cancelled())); + assert!(cancelled()); + }); + assert!(!cancelled()); + } + + #[test] + fn unwind_restores_the_enclosing_flag() { + let outer = Arc::new(AtomicBool::new(false)); + let inner = Arc::new(AtomicBool::new(true)); + with_cancel(&outer, || { + let result = std::panic::catch_unwind(|| with_cancel(&inner, || panic!("request failed"))); + assert!(result.is_err()); + assert!(!cancelled()); + outer.store(true, Ordering::Relaxed); + assert!(cancelled()); + }); + assert!(!cancelled()); + } + + #[test] + fn captured_flag_is_live_on_the_worker_and_restores_its_context() { + let request = Arc::new(AtomicBool::new(false)); + let context = with_cancel(&request, Context::current); + request.store(true, Ordering::Relaxed); + + std::thread::spawn(move || { + let worker = Arc::new(AtomicBool::new(false)); + with_cancel(&worker, || { + assert!(context.clone().run(cancelled)); + assert!(!cancelled()); + let result = std::panic::catch_unwind(|| context.run(|| panic!("delegate failed"))); + assert!(result.is_err()); + assert!(!cancelled()); + worker.store(true, Ordering::Relaxed); + assert!(cancelled()); + }); + assert!(!cancelled()); + }) + .join() + .unwrap(); + assert!(!cancelled()); + } +} diff --git a/crates/media-source/src/lib.rs b/crates/media-source/src/lib.rs index c27aea688..65b42e498 100644 --- a/crates/media-source/src/lib.rs +++ b/crates/media-source/src/lib.rs @@ -26,6 +26,8 @@ use craft_media::ByteSource; use craft_time::{FrameRate, Tick}; use serde::{Deserialize, Serialize}; +pub mod cancel; + /// Errors from opening or reading a media source. #[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)] pub enum MediaError {