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,