diff --git a/crates/metal2vulkan-runtime/src/attributes.rs b/crates/metal2vulkan-runtime/src/attributes.rs index d449b73d7..d619cafae 100644 --- a/crates/metal2vulkan-runtime/src/attributes.rs +++ b/crates/metal2vulkan-runtime/src/attributes.rs @@ -196,8 +196,8 @@ impl Buffer { /// Metal answers where its validation stops every call there, a query included. Fails only when /// zeroing a `Managed` buffer's bytes on a device without unified memory fails. pub fn set_purgeable_state(&self, state: PurgeableState) -> Result { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); if self.heap().is_some() { return Ok(PurgeableState::NonVolatile); } diff --git a/crates/metal2vulkan-runtime/src/buffer.rs b/crates/metal2vulkan-runtime/src/buffer.rs index 4e8182072..78299f366 100644 --- a/crates/metal2vulkan-runtime/src/buffer.rs +++ b/crates/metal2vulkan-runtime/src/buffer.rs @@ -122,7 +122,7 @@ struct BufferShared { /// `next` is set: read at one relaxed load where nothing was carried ([`Buffer::s`]), `next` /// with its own ordering where it was. carried: AtomicBool, - /// The calls outside a command buffer's work under way on these parts ([`Buffer::held`]). + /// The calls outside a command buffer's work under way on these parts ([`Buffer::held_parts`]). calls: Mutex, } @@ -765,7 +765,7 @@ impl LostParts for LostBuffer { { let mut calls = lock(&buffer.calls); if calls.under_way > 0 { - // The last call under way on them lets them go (`Buffer::held`). + // The last call under way on them lets them go (`Buffer::held_parts`). calls.waiting = Some(self); return; } @@ -785,14 +785,14 @@ impl LostBuffer { } self.pages.release_lost(); // SAFETY: its parts on the generation went, no command buffer of the generation records, - // no call under way reads them (`Buffer::held`), and every other reach of the buffer goes - // to where it was carried (`Buffer::carried`). + // no call under way reads them (`Buffer::held_parts`), and every other reach of the + // buffer goes to where it was carried (`Buffer::carried`). unsafe { buffer.device.let_go() }; } } /// The calls outside a command buffer's work under way on a buffer's parts on one generation -/// ([`Buffer::held`]), and the release of those parts that waits for them to end, where the +/// ([`Buffer::held_parts`]), and the release of those parts that waits for them to end, where the /// buffer was carried from that generation and it came to rest (`device/carry.rs`). #[derive(Default)] struct LinkCalls { @@ -801,7 +801,7 @@ struct LinkCalls { } /// A call outside a command buffer's work on a buffer's parts on one generation -/// ([`Buffer::held`]), until it goes: the parts stay while it lives, and every read of the +/// ([`Buffer::held_parts`]), until it goes: the parts stay while it lives, and every read of the /// buffer's parts on this thread meanwhile answers them. pub(super) struct HeldParts<'a> { link: &'a Arc, @@ -2043,7 +2043,7 @@ impl Filled for BufferShared { impl BufferShared { /// Its parts on its lost generation were marked let go of, the buffer carried to the next one /// and that generation at rest (`LostBuffer::mark`): no call takes them from now on - /// ([`Buffer::held`]), and they go once none under way holds them. + /// ([`Buffer::held_parts`]), and they go once none under way holds them. fn released(&self) -> bool { match &self.bytes { Bytes::Own(OwnBytes { @@ -3352,7 +3352,7 @@ impl Buffer { let mut shared = &self.0; loop { // Not its parts on a lost generation that went: a call under way that holds them - // keeps them until it ends (`Buffer::held`), and no command buffer is made there + // keeps them until it ends (`Buffer::held_parts`), and no command buffer is made there // after its rest (`Carry::refuses`). if records == Some(shared.generation) && !shared.freed() { return shared; @@ -3376,7 +3376,7 @@ impl Buffer { /// the next (`RuntimeGap::MadeBeforeRecovery`), as a call that took its arguments before /// the loss (`Device::at_serving_taking`). Not for command buffers' work, whose queue slot /// holds the generation (`device/carry.rs`). - pub(super) fn held(&self) -> HeldParts<'_> { + pub(super) fn held_parts(&self) -> HeldParts<'_> { let mut link = self.s(); loop { { @@ -4585,16 +4585,19 @@ impl Buffer { } } -/// A test's hook into [`Buffer::held`], run once by the first call that holds a buffer's parts -/// after the test set it: the test parks that call there across a recovery -/// (`tests/device_recovery_carry.rs`). #[cfg(test)] -pub(crate) static HELD_HOOK: Mutex>> = Mutex::new(None); +thread_local! { + /// A test's hook into [`Buffer::held_parts`], run once by this thread's next call that holds + /// a buffer's parts after the test set it: the test parks that call there across a recovery + /// (`tests/device_recovery_carry.rs`). + pub(crate) static HELD_HOOK: std::cell::RefCell>> = + const { std::cell::RefCell::new(None) }; +} -/// Run [`HELD_HOOK`], where a test set it. +/// Run this thread's [`HELD_HOOK`], where a test set it. #[cfg(test)] fn held_hook() { - let hook = lock(&HELD_HOOK).take(); + let hook = HELD_HOOK.with(|hook| hook.borrow_mut().take()); if let Some(hook) = hook { hook(); } diff --git a/crates/metal2vulkan-runtime/src/buffer_texture.rs b/crates/metal2vulkan-runtime/src/buffer_texture.rs index a0886d10d..eea20fa68 100644 --- a/crates/metal2vulkan-runtime/src/buffer_texture.rs +++ b/crates/metal2vulkan-runtime/src/buffer_texture.rs @@ -416,8 +416,8 @@ impl Buffer { offset: u64, bytes_per_row: u64, ) -> Result { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); self.texture_over( "new_texture", descriptor, @@ -466,8 +466,8 @@ impl Buffer { bytes_per_row: u64, _bytes_per_image: u64, ) -> Result { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); self.texture_over( "new_linear_texture", descriptor, @@ -529,8 +529,8 @@ impl Buffer { offset: u64, bytes_per_row: u64, ) -> Result { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); self.texture_over( "new_tiled_texture", descriptor, @@ -696,8 +696,8 @@ impl Buffer { descriptor: &TextureDescriptor, plane: IoSurfacePlane, ) -> Result { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); plane.refuse_client_direction()?; let texture = self.texture_over( "new_texture_with_iosurface_plane", @@ -750,8 +750,8 @@ impl Buffer { offset: u64, bytes_per_row: u64, ) -> Result { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); let over = (offset, bytes_per_row); let asked = (BufferTextureCall::Texture, Whose::Program, Goal::Ask); self.placement_over("new_texture", descriptor, over, asked, None) @@ -768,8 +768,8 @@ impl Buffer { offset: u64, bytes_per_row: u64, ) -> Result { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); let over = (offset, bytes_per_row); let asked = (BufferTextureCall::Tiled, Whose::Program, Goal::Ask); self.placement_over("new_tiled_texture", descriptor, over, asked, None) @@ -785,8 +785,8 @@ impl Buffer { descriptor: &TextureDescriptor, plane: IoSurfacePlane, ) -> Result { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); plane.refuse_client_direction()?; let over = (plane.offset, plane.bytes_per_row); let form = BufferTextureCall::IoSurfacePlane { diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index 775f8a6ac..285eed072 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -3009,7 +3009,7 @@ impl NoticesFor { /// This thread's reads of a carried object's parts answer those of the generation at /// `generation` (the address of its `DeviceShared`), until the scope goes, inside whatever /// command buffer's work it does: a call outside command buffers' work that holds a buffer's - /// parts there (`Buffer::held`). + /// parts there (`Buffer::held_parts`). pub(crate) fn of_generation(generation: usize) -> NoticesFor { let outer = NOTICES_FOR.with(|current| { let outer = current.get(); diff --git a/crates/metal2vulkan-runtime/src/device/carry.rs b/crates/metal2vulkan-runtime/src/device/carry.rs index bb3745840..c76e187ed 100644 --- a/crates/metal2vulkan-runtime/src/device/carry.rs +++ b/crates/metal2vulkan-runtime/src/device/carry.rs @@ -40,7 +40,7 @@ //! that waits for or signals it after the carry refuses it, as its device is the next //! generation (`Device::foreign`). A call outside command buffers' work that reaches a buffer's //! Vulkan objects (a texture over its bytes, an export of its memory, an upload, a handle -//! handed out) holds the parts it read (`Buffer::held`): their release at the generation's +//! handed out) holds the parts it read (`Buffer::held_parts`): their release at the generation's //! rest waits for the call to end, and the call completes on the generation it began on. A //! handle handed out (`Buffer::vk_buffer`, `Event::vk_semaphore`), which the caller may use //! while the object lives, keeps its parts, and the lost `VkDevice`, until the object goes. diff --git a/crates/metal2vulkan-runtime/src/exported_memory.rs b/crates/metal2vulkan-runtime/src/exported_memory.rs index 743908619..5ee563a2e 100644 --- a/crates/metal2vulkan-runtime/src/exported_memory.rs +++ b/crates/metal2vulkan-runtime/src/exported_memory.rs @@ -439,8 +439,8 @@ impl Buffer { /// (`NotImplemented(MadeBeforeRecovery)`) or on a lost device (`DeviceLost`), and the driver's /// failure. pub fn export_memory_fd(&self) -> Result { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); self.export_memory("export_memory_fd", MemoryExport::OpaqueFd) } @@ -460,8 +460,8 @@ impl Buffer { /// (`NotImplemented(MadeBeforeRecovery)`) or on a lost device (`DeviceLost`), and the driver's /// failure. pub fn export_memory_dma_buf(&self) -> Result { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); self.export_memory("export_memory_dma_buf", MemoryExport::DmaBuf) } @@ -546,8 +546,8 @@ impl Buffer { /// before its device's recovery (`NotImplemented(MadeBeforeRecovery)`) or on a lost device /// (`DeviceLost`), and the driver's failure. pub fn external_write_done(&self) -> Result<(), Error> { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); const CALL: &str = "external_write_done"; let device = self.device(); if device.is_lost() { diff --git a/crates/metal2vulkan-runtime/src/interop.rs b/crates/metal2vulkan-runtime/src/interop.rs index f390da2cc..26982f3b6 100644 --- a/crates/metal2vulkan-runtime/src/interop.rs +++ b/crates/metal2vulkan-runtime/src/interop.rs @@ -230,8 +230,8 @@ impl Buffer { /// The `VkBuffer` stays the runtime's: the caller does not destroy it, and uses it only while /// the buffer lives, ordered against the runtime's work on it. pub unsafe fn vk_buffer(&self) -> (vk::Buffer, u64) { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); self.hand_out(); let handle = self.at_for(0, ReadApart::VkBuffer); // A failed submission gives the work back, to the device's next submission; the caller @@ -274,8 +274,8 @@ impl Buffer { /// [`SharedEvent::vk_wait`], [`SharedEvent::reserve_vk_signal`]) and against the host's /// writes to the pages, as for any use of the buffer. pub unsafe fn vk_host_memory(&self) -> Option { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); self.host_memory_at(0) } } diff --git a/crates/metal2vulkan-runtime/src/managed.rs b/crates/metal2vulkan-runtime/src/managed.rs index dba724347..35bf2467e 100644 --- a/crates/metal2vulkan-runtime/src/managed.rs +++ b/crates/metal2vulkan-runtime/src/managed.rs @@ -37,8 +37,8 @@ impl Buffer { /// buffers, in range and past it; Metal's validation stops it). Refused for a managed /// buffer's range past the buffer. pub fn did_modify_range(&self, range: Range) -> Result<(), Error> { - // Its parts held while the call reaches them, across a recovery (`Buffer::held`). - let _held = self.held(); + // Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`). + let _held = self.held_parts(); const CALL: &str = "did_modify_range"; if self.storage_mode() != StorageMode::Managed { return Ok(()); diff --git a/crates/metal2vulkan-runtime/src/tests/device_recovery_carry.rs b/crates/metal2vulkan-runtime/src/tests/device_recovery_carry.rs index c9baa07eb..bd5439ef9 100644 --- a/crates/metal2vulkan-runtime/src/tests/device_recovery_carry.rs +++ b/crates/metal2vulkan-runtime/src/tests/device_recovery_carry.rs @@ -11,9 +11,10 @@ use super::device_recovery::{ }; use crate::fresh_pages::FRESH_PAGES_PAST; use crate::{ - Buffer, CommandBufferStatus, CommandQueue, Device, Error, EventRef, PixelFormat, - ResourceOptions, RuntimeGap, SharedEvent, SharedEventListener, TextureDescriptor, TextureUsage, + Buffer, CommandBufferStatus, CommandQueue, Device, Error, EventRef, ResourceOptions, + RuntimeGap, SharedEvent, SharedEventListener, TextureDescriptor, }; +use metal2vulkan::metal::{PixelFormat, TextureUsage}; use std::sync::mpsc; use std::time::{Duration, Instant}; @@ -274,7 +275,7 @@ fn seven_recoveries_end_with_one_live_vk_device() { } /// A call outside command buffers' work that reaches a carried buffer's Vulkan objects (here a -/// texture over its bytes) holds the parts it read (`Buffer::held`): parked there while the +/// texture over its bytes) holds the parts it read (`Buffer::held_parts`): parked there while the /// recovery carries the buffer and the lost generation comes to rest, it keeps the buffer's /// parts there, which go as it ends. The buffer serves on the next generation meanwhile. #[test] @@ -288,10 +289,6 @@ fn a_call_under_way_keeps_the_parts_it_read_past_the_lost_generation_s_rest() { let made_on = buffer.device().clone(); let (parked, at_hook) = mpsc::channel(); let (go, wait) = mpsc::channel::<()>(); - *crate::device::lock(&crate::buffer::HELD_HOOK) = Some(Box::new(move || { - let _ = parked.send(()); - let _ = wait.recv(); - })); let call = { let buffer = buffer.clone(); std::thread::spawn(move || { @@ -304,6 +301,13 @@ fn a_call_under_way_keeps_the_parts_it_read_past_the_lost_generation_s_rest() { .minimum_linear_texture_alignment_for_pixel_format(PixelFormat::RGBA8Unorm) .unwrap() .max(16); + // This thread's call parks inside its hold of the buffer's parts. + crate::buffer::HELD_HOOK.with(|hook| { + *hook.borrow_mut() = Some(Box::new(move || { + let _ = parked.send(()); + let _ = wait.recv(); + })); + }); buffer .new_texture(&descriptor, 0, row) .map(|texture| texture.device().generation_serves())