diff --git a/crates/media/src/exr_planes.rs b/crates/media/src/exr_planes.rs index 15591495b..52536eb7a 100644 --- a/crates/media/src/exr_planes.rs +++ b/crates/media/src/exr_planes.rs @@ -152,6 +152,15 @@ pub(crate) enum Refused { struct Flight { state: Mutex, ready: Condvar, + /// The decode began: on the decode threads, or claimed by a decode thread that asked for the + /// same planes before the queued decode began ([`Flight::claim`]). + started: std::sync::atomic::AtomicBool, + /// A decode thread claimed it: the outcome comes through `handed`, not the queued job. + claimed: std::sync::atomic::AtomicBool, + handed: Mutex, Refused>>>, + handed_ready: Condvar, + /// What the store held when the decode was asked for (the decode adds to it). + base: Option>, } struct FlightState { @@ -164,7 +173,52 @@ struct FlightState { impl Flight { fn new(index: Arc, want: Want) -> Arc { - Arc::new(Flight { state: Mutex::new(FlightState { index, want, sealed: false, outcome: None }), ready: Condvar::new() }) + Flight::with_base(index, want, None) + } + + fn with_base(index: Arc, want: Want, base: Option>) -> Arc { + Arc::new(Flight { + state: Mutex::new(FlightState { index, want, sealed: false, outcome: None }), + ready: Condvar::new(), + started: std::sync::atomic::AtomicBool::new(false), + claimed: std::sync::atomic::AtomicBool::new(false), + handed: Mutex::new(None), + handed_ready: Condvar::new(), + base, + }) + } + + /// Take over the decode while it is still queued (a decode thread asking for the planes + /// it would wait for: waiting there could starve the queue it waits on). `false` once it + /// began. + fn claim(&self) -> bool { + use std::sync::atomic::Ordering; + let won = self.started.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst).is_ok(); + if won { + self.claimed.store(true, Ordering::SeqCst); + } + won + } + + /// Hand the claimed decode's outcome to the request that queued it. + fn hand(&self, outcome: Result, Refused>) { + *self.handed.lock().unwrap_or_else(PoisonError::into_inner) = Some(outcome); + self.handed_ready.notify_all(); + } + + /// The outcome a claiming decode thread hands over (a plain block: that decode is running + /// and waits for nothing). + fn take_handed(&self) -> Result, Refused> { + let mut h = self.handed.lock().unwrap_or_else(PoisonError::into_inner); + loop { + if let Some(o) = h.as_ref() { + return o.clone(); + } + h = match self.handed_ready.wait(h) { + Ok(g) => g, + Err(p) => p.into_inner(), + }; + } } fn state(&self) -> MutexGuard<'_, FlightState> { @@ -398,6 +452,11 @@ impl PlaneStore { } } drop(st); + // A decode thread doesn't wait (see `wait`), but takes over a decode still queued: + // the planes are decoded once, here, and handed to the request that queued them. + if may_not_wait() && flight.claim() { + return Step::Done(decode_claimed(path, source, &flight, index, want)); + } return match self.wait(&flight) { Waited::Got(Ok(b)) if b.covers(index, want) => { crate::reads::note(b.file.clone()); @@ -414,7 +473,7 @@ impl PlaneStore { } // This request decodes: what it asks for, minus what the store holds. let base = e.batch.clone().filter(|b| Arc::ptr_eq(&b.index, index)); - let flight = Flight::new(index.clone(), want.clone()); + let flight = Flight::with_base(index.clone(), want.clone(), base.clone()); e.flight = Some(flight.clone()); drop(st); #[cfg(test)] @@ -522,10 +581,7 @@ impl PlaneStore { /// decode doesn't wait (see the module notes); the time limit is a safety net. #[cfg(not(target_arch = "wasm32"))] fn wait(&self, flight: &Flight) -> Waited { - // A decode thread never waits for planes (#3270: a decode doesn't depend on the requests - // waiting for it; an owner on a frame worker queues its decode on these threads). - let on_decode_thread = crate::pool::decode_threads().is_some_and(|t| t.current_thread_index().is_some()); - if on_decode_thread || DECODING.with(std::cell::Cell::get) > 0 { + if may_not_wait() { return Waited::No; } let until = std::time::Instant::now() + WAIT_LIMIT; @@ -573,12 +629,63 @@ impl PlaneStore { self.lock().entries.iter().filter(|e| e.batch.is_some() || !e.failed.is_empty()).map(|e| e.path.clone()).collect() } + #[cfg(test)] + pub(crate) fn in_flight(&self, path: &str) -> bool { + self.lock().entries.iter().any(|e| e.path == path && e.flight.is_some()) + } + #[cfg(test)] pub(crate) fn is_held(&self, path: &str) -> bool { self.lock().held.iter().any(|(p, _)| p == path) } } +/// This thread may not wait for planes: a decode thread (#3270: a decode doesn't depend on the +/// requests waiting for it; an owner on a frame worker queues its decode on these threads, and +/// a decode thread waiting for it could starve that queue), or one running a store decode. +fn may_not_wait() -> bool { + let on_decode_thread = { + #[cfg(not(target_arch = "wasm32"))] + { + crate::pool::decode_threads().is_some_and(|t| t.current_thread_index().is_some()) + } + #[cfg(target_arch = "wasm32")] + { + false + } + }; + on_decode_thread || DECODING.with(std::cell::Cell::get) > 0 +} + +/// Hands a claimed decode's outcome over when dropped, an error when the decode stopped +/// (panicked): the request that queued it never waits for an outcome that doesn't come. +struct Handing<'a> { + flight: &'a Flight, + outcome: Option, Refused>>, +} + +impl Drop for Handing<'_> { + fn drop(&mut self) { + self.flight.hand(self.outcome.take().unwrap_or(Err(Refused::Stopped))); + } +} + +/// Decode a flight claimed by this thread (see [`Flight::claim`]) here, hand the outcome to the +/// request that queued it, and answer this request with it (or a copy when it doesn't cover it). +fn decode_claimed(path: &str, source: &ExrSource, flight: &Arc, index: &Arc, want: &Want) -> Result, Refused> { + let mut handing = Handing { flight, outcome: None }; + let outcome = decode(path, source, flight, flight.base.clone(), None); + handing.outcome = Some(outcome.clone()); + drop(handing); + match outcome { + Ok(b) if b.covers(index, want) => { + crate::reads::note(b.file.clone()); + Ok(b) + } + _ => copy(path, source, index, want), + } +} + /// Run `flight`'s decode on the footage decode threads (here when this is one of them, or /// there are none) and block until it is done. fn run(path: &str, source: &ExrSource, flight: &Arc, base: Option>, opened: Option) -> Result, Refused> { @@ -587,15 +694,26 @@ fn run(path: &str, source: &ExrSource, flight: &Arc, base: Option outcome, + Err(_) if flight.claimed.load(std::sync::atomic::Ordering::SeqCst) => flight.take_handed(), + // (a decode that panicked sent nothing) + Err(_) => Err(Refused::Stopped), + }; + } + // (claimed by a decode thread before this one began: it hands the outcome over) + if flight.started.swap(true, std::sync::atomic::Ordering::SeqCst) { + return flight.take_handed(); } decode(path, source, flight, base, opened) } diff --git a/crates/media/src/exr_planes/tests.rs b/crates/media/src/exr_planes/tests.rs index 1edf62c99..d4601361c 100644 --- a/crates/media/src/exr_planes/tests.rs +++ b/crates/media/src/exr_planes/tests.rs @@ -853,3 +853,50 @@ fn decoded_planes_are_kept_on_disk_and_read_back_exactly() { assert_eq!(from_disk, [false, true, false]); let _ = std::fs::remove_dir_all(&dir); } + +/// #3271: a decode thread asking for planes whose decode is still queued on the decode threads +/// (an aux request on a frame worker queued it, then the picture's decode asks) takes it over +/// instead of decoding a copy: one decode, and both requests get its planes. +#[test] +fn a_decode_thread_takes_over_a_queued_decode() { + let bytes: Arc<[u8]> = single_part_zips().into(); + let path = "/3271-claimed.exr"; + let source = ExrSource::Bytes(bytes); + let store = Arc::new(PlaneStore::new(CACHE_BUDGET)); + let index = crate::exr_index::get(path, &source).unwrap(); + let all = Want { all: true, ids: Default::default() }; + let threads = crate::pool::decode_threads().unwrap(); + let n = threads.current_num_threads(); + // Every decode thread busy: n - 1 hold until released, one waits for the queued decode. + let (release, held) = std::sync::mpsc::channel::<()>(); + let held = Arc::new(Mutex::new(held)); + for _ in 1..n { + let held = held.clone(); + threads.spawn(move || { + let _ = held.lock().unwrap().recv_timeout(std::time::Duration::from_secs(20)); + }); + } + let (tx, rx) = std::sync::mpsc::channel(); + { + let (store, source, index, all, tx) = (store.clone(), source.clone(), index.clone(), all.clone(), tx.clone()); + threads.spawn(move || { + let until = std::time::Instant::now() + std::time::Duration::from_secs(10); + while !store.in_flight(path) && std::time::Instant::now() < until { + std::thread::sleep(std::time::Duration::from_millis(1)); + } + let _ = tx.send(store.get(path, &source, &index, &all, Use::Other).map(|b| b.ids.len())); + }); + } + // The other request, from a thread of its own: its decode is queued behind them. + let asker = { + let (store, source, index, all) = (store.clone(), source.clone(), index.clone(), all.clone()); + std::thread::spawn(move || store.get(path, &source, &index, &all, Use::Other).map(|b| b.ids.len())) + }; + let claimed = rx.recv_timeout(std::time::Duration::from_secs(20)).unwrap(); + let asked = asker.join().unwrap(); + for _ in 1..n { + let _ = release.send(()); + } + assert!(claimed.is_ok() && claimed == asked, "{claimed:?} {asked:?}"); + assert_eq!(decodes_of(path).len(), 1, "decoded once"); +}