From 975a558d351ddf15d34b169b33f19d22ea0d02aa Mon Sep 17 00:00:00 2001 From: KDenKC <125705260+KDenKC@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:50:33 -0700 Subject: [PATCH] Tests: alloc_pool's outside-STATE checks wait out other tests' holds Three alloc_pool tests check that a call runs outside the allocator pool's STATE lock with one STATE.try_lock(). That fails whenever another test running beside it holds STATE at that moment, whatever the code under test does: submit_marking_calls_out_unlocked_and_revalidates_the_queue_marker failed 4 of 10 runs of 'cargo test -p xcl --lib -- submit_ --test-threads=4' on main 6bfb5c51 (RTX 5090). The check now retries for up to a second (as the transferred-owner test's release already did with 10,000 yields): held by the calling thread it never succeeds, so a call made inside STATE still fails the test. A test of the helper shows both cases. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/alloc_pool.rs | 42 ++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index bbb56bdc3f..4d9af427ea 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -1588,6 +1588,40 @@ fn commit_reset( mod tests { use super::*; + /// This thread does not hold `m` (`STATE`). Other tests running beside this one may hold it + /// for a moment, so one `try_lock` is not enough: it is retried for up to a second; held by + /// this thread, it never succeeds. + fn outside(m: &Mutex) -> bool { + let until = std::time::Instant::now() + std::time::Duration::from_secs(1); + while m.try_lock().is_err() { + if std::time::Instant::now() > until { + return false; + } + std::thread::yield_now(); + } + true + } + + #[test] + fn outside_is_false_only_while_this_thread_holds_the_lock() { + let m = Mutex::new(()); + assert!(outside(&m)); + let held = m.lock().unwrap(); + assert!(!outside(&m)); + drop(held); + // held by another thread for a moment: the retries wait it out + let m = Arc::new(m); + let (m2, (tx, rx)) = (m.clone(), std::sync::mpsc::channel()); + let t = std::thread::spawn(move || { + let _g = m2.lock().unwrap(); + tx.send(()).unwrap(); + std::thread::sleep(std::time::Duration::from_millis(5)); + }); + rx.recv().unwrap(); + assert!(outside(&*m)); + t.join().unwrap(); + } + /// When a title list goes, the segments that recorded its put-off dispatches on its own /// allocator hold neither the PC allocator it stands for now nor the ones put aside: `forget` /// then releases those the GPU is done with (they were left behind, with what was held on them). @@ -1734,7 +1768,7 @@ mod tests { refs: Cell, } unsafe extern "system" fn completed(this: *mut c_void) -> u64 { - let outside = STATE.try_lock().is_ok(); + let outside = outside(&STATE); OUTSIDE.store(outside, Ordering::Relaxed); if outside && MUTATE.swap(false, Ordering::Relaxed) { let mut g = STATE.lock().unwrap(); @@ -1859,11 +1893,11 @@ mod tests { assert!(pending.finish_with_queue_ops( first_queue, || { - assert!(STATE.try_lock().is_ok(), "fence creation must run outside STATE"); + assert!(outside(&STATE), "fence creation must run outside STATE"); Some(first_fence.clone()) }, |_, value| { - assert!(STATE.try_lock().is_ok(), "queue Signal must run outside STATE"); + assert!(outside(&STATE), "queue Signal must run outside STATE"); assert_eq!(value, 1); true }, @@ -1921,7 +1955,7 @@ mod tests { reused_queue, || Some(old_fence.clone()), |_, value| { - assert!(STATE.try_lock().is_ok(), "queue invalidation can run while Signal is outside STATE"); + assert!(outside(&STATE), "queue invalidation can run while Signal is outside STATE"); assert_eq!(value, 1); STATE.lock().unwrap().as_mut().unwrap().fences.insert( reused_queue,