Runtime: a command buffer's enqueue and commit wake its waits only where a thread waits
Every commit called notify_all on the command buffer's condition variable, at its enqueue and again at its commit: each a futex wake, a system call whether or not a thread waits, about 1.2 us a commit in a consumer's profile. The command buffer's state now counts the threads waiting on it, under the same lock, and both notify only where one waits. A device test has a thread wait for a command buffer not yet committed: the commit wakes it, and the wait returns once the command buffer completed.
This commit is contained in:
1 parent
6930e494d6
commit
3668435cfb
2 files changed
+51
-7
No files matched your search
@@ -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<Pair>,
|
||||
/// 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`.
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user