Skip to content
Open
Show file tree
Hide file tree
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
40 changes: 39 additions & 1 deletion crates/xcl/src/hle/d3d12x/alloc_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,24 @@ pub(super) fn forget_queue(id: super::present::QueueId) {
drop(gone);
}

/// Release through a temporary reference. When the caller's was the last one, a busy allocator's
/// probe reference is handed to the pool (`retain_probe`), and an idle one's books are dropped
/// (`before_last`) before the last Release: the address is still this object's then.
pub(super) fn release_with_probe(
add_ref: impl FnOnce(),
mut release: impl FnMut() -> u32,
retain_probe: impl FnOnce() -> bool,
before_last: impl FnOnce(),
) -> (u32, bool) {
add_ref();
let after_caller = release();
if after_caller == 1 && retain_probe() { (0, true) } else { (release(), false) }
if after_caller == 1 {
if retain_probe() {
return (0, true);
}
before_last();
}
(release(), false)
}

/// Transfer the caller's temporary AddRef probe into pool ownership when releasing `raw` would
Expand Down Expand Up @@ -1624,6 +1634,7 @@ mod tests {
retained.set(retained.get() + 1);
true
},
|| panic!("a busy allocator keeps its books: the pool holds it"),
);
assert_eq!((remaining, transferred, refs.get(), retained.get()), (0, true, 1, 1));
refs.set(refs.get() - 1); // the out-of-lock sweep releases the transferred probe
Expand All @@ -1641,10 +1652,37 @@ mod tests {
retained.set(retained.get() + 1);
true
},
|| panic!("another reference is left: nothing ends"),
);
assert_eq!((remaining, transferred, refs.get(), retained.get()), (1, false, 1, 0));
}

/// The last Release of an allocator nothing uses: its books go while the object is still
/// there, so that a new allocator the driver makes at its address keeps its own. (After the
/// Release, `forget` took the new one's type and stand-ins, and marked one with a recording
/// for release by the pool.)
#[test]
fn an_idle_allocators_books_go_before_its_last_release() {
use std::cell::{Cell, RefCell};
let refs = Cell::new(1u32);
let events = RefCell::new(Vec::new());
let (remaining, transferred) = release_with_probe(
|| refs.set(refs.get() + 1),
|| {
refs.set(refs.get() - 1);
events.borrow_mut().push(if refs.get() == 0 { "freed" } else { "probe released" });
refs.get()
},
|| {
events.borrow_mut().push("asked the pool");
false
},
|| events.borrow_mut().push("books dropped"),
);
assert_eq!((remaining, transferred), (0, false));
assert_eq!(*events.borrow(), ["probe released", "asked the pool", "books dropped", "freed"]);
}

#[test]
fn release_probe_retries_a_same_address_backing_replacement() {
use std::mem::{offset_of, size_of};
Expand Down
19 changes: 15 additions & 4 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11549,15 +11549,26 @@ unsafe extern "system" fn alloc_release(this: *mut c_void) -> u32 {
return f(this);
}
let add_ref: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(orig_fn(this, 1));
// What XCL keeps by the allocator's address goes while this call still holds its last
// reference: once the driver has freed it, another thread can be given a new allocator at
// that address, and these would then be that one's books.
let ended = std::cell::Cell::new(false);
let end = || {
ended.set(true);
dispatch_chain::allocator_reset(this as usize);
super::alloc_pool::forget(this as usize);
super::list_x::on_allocator_release(this as usize);
};
let (n, _) = super::alloc_pool::release_with_probe(
|| { add_ref(this); },
|| f(this),
|| super::alloc_pool::retain_release_probe(this),
end,
);
if n == 0 {
dispatch_chain::allocator_reset(this as usize);
super::alloc_pool::forget(this as usize);
super::list_x::on_allocator_release(this as usize);
// (one the pool keeps for the GPU, or two Releases at once that both missed the last
// reference: after the Release, as before)
if n == 0 && !ended.get() {
end();
}
n
}
Expand Down
Loading