Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion crates/xcl/src/hle/d3d12x/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,14 @@ unsafe impl Send for State {}

static STATE: Mutex<Option<State>> = Mutex::new(None);

/// Put `v` in `m` and drop what it held after the guard is gone: dropping an old `State`
/// releases its resources, and `real::res_release` locks `STATE` again (`forget_buffer`), so
/// dropping it under the guard waits for itself forever (#344).
fn replace_unlocked<T>(m: &Mutex<Option<T>>, v: T) {
let old = m.lock().unwrap().replace(v);
drop(old);
}

/// (bytes of video memory in pool heaps, of those mapped) – `XCL_ACTIVITY`
pub fn pool_bytes() -> (usize, usize) {
let g = STATE.lock().unwrap();
Expand Down Expand Up @@ -546,7 +554,7 @@ pub unsafe fn init(dev: &ID3D12Device) -> Vec<(usize, usize, u64)> {
match made {
Ok(s) => {
crate::xlog_info!("D3D12.X: {} sparse window(s) for the title's own memory (tiled buffers on video memory)", out.len());
*STATE.lock().unwrap() = Some(s);
replace_unlocked(&STATE, s);
for (a, b) in std::mem::take(&mut *EARLY.lock().unwrap()) {
committed(a, b - a);
}
Expand Down Expand Up @@ -1488,6 +1496,26 @@ pub fn forget_buffer(res: usize) {
mod tests {
use super::*;

/// A value whose drop locks the mutex it was in (an old `State` whose resources reach
/// `forget_buffer`) is replaced without the thread waiting for itself (#344).
#[test]
fn replacing_drops_the_old_value_after_the_guard() {
static M: Mutex<Option<Relocks>> = Mutex::new(None);
struct Relocks;
impl Drop for Relocks {
fn drop(&mut self) {
drop(M.lock().unwrap());
}
}
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
replace_unlocked(&M, Relocks);
replace_unlocked(&M, Relocks);
let _ = tx.send(());
});
assert!(rx.recv_timeout(std::time::Duration::from_secs(10)).is_ok(), "replacing the value deadlocked");
}

#[test]
fn aligned_tile_addresses_spread_across_hash_buckets() {
use std::hash::BuildHasher;
Expand Down
Loading