diff --git a/crates/metal2vulkan-runtime/src/arena.rs b/crates/metal2vulkan-runtime/src/arena.rs index f82a80122..a6a2cab19 100644 --- a/crates/metal2vulkan-runtime/src/arena.rs +++ b/crates/metal2vulkan-runtime/src/arena.rs @@ -70,14 +70,24 @@ pub(crate) fn piece( /// the dispatch took it new: the device's last, where a command buffer still keeps it and it holds /// `len`, else a block of `len` rounded up to a power of two, within `budget` where `len` is. /// +/// The arena's lock is held only to read the device's last block and to store a new one, never +/// across the take: a take may make an allocation and run the consumer's +/// [`BlockMappings::map`](crate::BlockMappings::map) on this thread, and another thread's dispatch +/// goes on meanwhile. Two dispatches that find no block holding their slots at once each take +/// their own and keep it, held by its command buffer until it retires, even where the other's, +/// stored meanwhile, would hold their slots; the device keeps the longer of the two as its last. +/// So the arena stays within its bound: each block is its own dispatch's, at most `budget`, or +/// one workgroup's slots where those pass it, as a block taken with no race is, and a race adds +/// one such block for each dispatch that missed at once, never a longer one. +/// /// Err: what [`Core::take_memory`] returns. pub(crate) fn take( core: &Arc, len: u64, budget: u64, ) -> Result<(Arc, bool), Error> { - let mut last = lock(&core.arena); - if let Some(block) = last.upgrade().filter(|block| block.memory.len >= len) { + let last = lock(&core.arena).upgrade(); + if let Some(block) = last.filter(|block| block.memory.len >= len) { return Ok((block, false)); } let len = len @@ -86,7 +96,17 @@ pub(crate) fn take( .min(budget.max(len)); let memory = core.take_memory(MemoryClass::Private, len, Site::PrivateArena)?; let block = Arc::new(memory::Block::new(memory, Arc::clone(core))); - *last = Arc::downgrade(&block); + let mut last = lock(&core.arena); + let kept = last.upgrade(); + if kept + .as_ref() + .is_none_or(|kept| kept.memory.len < block.memory.len) + { + *last = Arc::downgrade(&block); + } + drop(last); + // Where it was the last holder of the block it read, the block goes back here, off the lock. + drop(kept); core.counters.count(Metric::PrivateArenaBlocks); Ok((block, true)) } diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index d79b33cf7..b94c708a8 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -320,10 +320,16 @@ pub trait BlockMappings: Send + Sync { /// /// It runs where the allocation is made: on the block-maker thread for the allocations made /// ahead of need, on the thread in [`Device::new`](crate::Device::new) for each class's first, - /// and inside [`Device::take_block`](crate::Device::take_block), on the consumer's thread, for - /// a take that no free range fits. Never under the pool's lock. It makes no call into the - /// runtime and drops no runtime handle. `allocation` states when the make began and how long - /// it took ([`AllocationInfo::make_start`], [`AllocationInfo::make_nanos`]). + /// and on the consumer's thread inside a call whose take no free range fits: + /// [`Device::take_block`](crate::Device::take_block), and the calls that take a block of their + /// own (a texture's place, a visible function table, an indirect command buffer, a dispatch + /// whose kernel's private memory takes a new block of the device's arena). Never under the + /// pool's or the arena's lock. It makes no call into the runtime and drops no runtime handle, + /// and it never waits for a lock that a thread inside a runtime call holds: that call may be + /// waiting for the hook (a call that waits for a make of the block-maker thread, which runs the + /// hooks between its makes, or the device's last drop), so such a wait never ends. + /// `allocation` states when the make began and how long it took + /// ([`AllocationInfo::make_start`], [`AllocationInfo::make_nanos`]). fn map(&self, allocation: &AllocationInfo) -> bool; /// Returns only once every mapping of `allocation` the consumer made is gone and none of its @@ -333,8 +339,9 @@ pub trait BlockMappings: Send + Sync { /// It runs once per allocation `map` accepted: on the block-maker thread before an idle /// allocation's free; in [`Device::quiesce`](crate::Device::quiesce), on the thread that calls /// it, for every allocation still mapped; or at teardown, on the thread that ends it, once the - /// runtime's other threads are joined. Never under the pool's lock. It makes no call into the - /// runtime and drops no runtime handle. + /// runtime's other threads are joined. Never under the pool's or the arena's lock. It makes no + /// call into the runtime and drops no runtime handle, and, as [`map`](Self::map), it never + /// waits for a lock that a thread inside a runtime call holds. fn unmap(&self, allocation: AllocationId); }