diff --git a/crates/metal2vulkan-runtime/src/command.rs b/crates/metal2vulkan-runtime/src/command.rs index 8698b3fa6..f5eceeb5a 100644 --- a/crates/metal2vulkan-runtime/src/command.rs +++ b/crates/metal2vulkan-runtime/src/command.rs @@ -179,6 +179,9 @@ struct CbState { encoding: bool, /// From its commit until it retires: its pair of timestamp queries, where it took one. stamps: Option, + /// The threads waiting on `changed` ([`CommandBuffer::wait`]): its enqueue and commit wake + /// them only where one waits, each wake a system call whether or not one does. + waiters: usize, } impl CbState { @@ -453,6 +456,7 @@ impl CommandBuffer { times: Times::default(), encoding: false, stamps: None, + waiters: 0, }), changed: Condvar::new(), }), @@ -646,7 +650,9 @@ impl CommandBuffer { if state.status == CommandBufferStatus::NotEnqueued { work.enqueue(&self.inner); state.status = CommandBufferStatus::Enqueued; - self.inner.changed.notify_all(); + if state.waiters > 0 { + self.inner.changed.notify_all(); + } } Ok(()) } @@ -731,7 +737,9 @@ impl CommandBuffer { timed: stamps.is_some(), }; state.status = CommandBufferStatus::Committed; - self.inner.changed.notify_all(); + if state.waiters > 0 { + self.inner.changed.notify_all(); + } let ended = state.ended; drop(state); // Where it is the last handle, the device ends below, once no lock is held. @@ -843,11 +851,18 @@ impl CommandBuffer { self.inner.queue.is(queue) } - fn wait<'a>(&self, state: MutexGuard<'a, CbState>) -> MutexGuard<'a, CbState> { - self.inner - .changed - .wait(state) - .unwrap_or_else(PoisonError::into_inner) + /// Waits for a change of `state`, counted among its waiters while it waits. + fn wait<'a>(&self, mut state: MutexGuard<'a, CbState>) -> MutexGuard<'a, CbState> { + state.waiters += 1; + let mut state = (self.inner.changed.wait(state)).unwrap_or_else(PoisonError::into_inner); + state.waiters -= 1; + state + } + + /// For the device tests: the threads waiting on its status. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) fn waiters(&self) -> usize { + self.state().waiters } /// `status`. diff --git a/crates/metal2vulkan-runtime/src/tests/command.rs b/crates/metal2vulkan-runtime/src/tests/command.rs index 0aa5251d8..6a3e17c5e 100644 --- a/crates/metal2vulkan-runtime/src/tests/command.rs +++ b/crates/metal2vulkan-runtime/src/tests/command.rs @@ -5122,4 +5122,33 @@ pub(crate) mod on_device { drop((first, second, third, one, two, arena, device)); }); } + + /// A thread that waits for a command buffer not yet enqueued or committed wakes as its status + /// changes: its enqueue and commit find the waiter and wake it, and the wait returns once the + /// command buffer completed, its write done. + #[test] + fn every_device_wakes_a_wait_begun_before_the_commit() { + each_device("command_wait_before_commit", |name, _, device| { + let mut arena = Arena::new(&device); + let written = arena.buffer(); + let queue = queue(&device); + let command_buffer = queue.command_buffer().expect("a command buffer"); + fill(&command_buffer, &written, 7); + thread::scope(|scope| { + let waiter = scope.spawn(|| command_buffer.wait_until_completed()); + within(name, || command_buffer.waiters() == 1); + command_buffer.commit().expect("a commit"); + (waiter.join().expect("the waiter")).expect("the wait"); + }); + assert_eq!( + command_buffer.status(), + CommandBufferStatus::Completed, + "{name}" + ); + assert_eq!(command_buffer.waiters(), 0, "{name}: the wait left"); + assert_eq!(word(&written), 7, "{name}: the write done"); + drop((command_buffer, queue)); + drop((written, arena, device)); + }); + } }