From b4def7c7a3596f71eac51aa0a5baac9d72f89405 Mon Sep 17 00:00:00 2001 From: RandomGithub12309 <71151833+RandomGithub12309@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:25:28 -0500 Subject: [PATCH] D3D12.X: serialize real::init against concurrent device creation real::init previously checked device_raw().is_null() without a lock, allowing concurrent threads (such as parallel GPU tests) to enter device creation and open_uma/sparse::init simultaneously before DEVICE was published. Add a double-checked static Mutex guard around initialization in real::init() and a 4-thread concurrent_init_returns_consistent_adapter test. Assisted-by: Antigravity (Gemini 4 Neon) --- crates/xcl/src/hle/d3d12x/real.rs | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index be3a38a40..852c9da36 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -454,6 +454,11 @@ fn graphics_wave32(device: *mut c_void) -> bool { /// by default the high-performance GPU → WARP); once – later calls /// return the existing device's adapter. pub fn init() -> Result { + if !device_raw().is_null() { + return Ok(ADAPTER.lock().map(|a| a.clone()).unwrap_or_default()); + } + static INIT: Mutex<()> = Mutex::new(()); + let _init = INIT.lock().unwrap(); if !device_raw().is_null() { return Ok(ADAPTER.lock().map(|a| a.clone()).unwrap_or_default()); } @@ -5531,3 +5536,31 @@ mod console_format_query_tests { } } } + +#[cfg(test)] +mod init_tests { + use super::*; + + #[test] + fn concurrent_init_returns_consistent_adapter() { + let barrier = std::sync::Arc::new(std::sync::Barrier::new(4)); + let handles: Vec<_> = (0..4) + .map(|_| { + let b = std::sync::Arc::clone(&barrier); + std::thread::spawn(move || { + b.wait(); + init() + }) + }) + .collect(); + let results: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect(); + if results.iter().any(|r| r.is_err()) { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + } + let first = results[0].as_ref().unwrap(); + for res in &results { + assert_eq!(res.as_ref().unwrap(), first); + } + } +}