Merge pull request #335 from loyodea/fix/307-pipeline-name-locks

pipeline: exclude library loads by exact name
This commit is contained in:
goldfishdev
2026-12-03 12:22:53 -06:00
2 changed files with 155 additions and 10 deletions

View File

@ -56,6 +56,8 @@ use std::time::Duration;
use windows::Win32::Graphics::Direct3D12::*;
use windows::core::{HSTRING, Interface, PCWSTR};
mod name_gate;
static DIR: OnceLock<PathBuf> = OnceLock::new();
/// Keeps the pipeline library and the early list in `dir` from now on.
@ -429,16 +431,13 @@ unsafe impl Sync for Library {}
/// Held by a load of the pipeline named `name`: loads of the same name
/// wait for each other (the host's one exception to the library's thread
/// safety: such a load may change that pipeline's entry). Names share 64
/// locks, so two different names seldom wait.
fn same_name(name: &str) -> MutexGuard<'static, ()> {
static LOADING: [Mutex<()>; 64] = [const { Mutex::new(()) }; 64];
let at = name.bytes().fold(0usize, |h, b| {
h.wrapping_mul(31).wrapping_add(usize::from(b))
});
LOADING[at % LOADING.len()]
.lock()
.unwrap_or_else(|e| e.into_inner())
/// safety: such a load may change that pipeline's entry). Different names
/// share only the brief bookkeeping lock, never the driver's load call.
fn same_name(name: &str) -> name_gate::Guard<'_> {
static LOADING: OnceLock<name_gate::NameGate> = OnceLock::new();
LOADING
.get_or_init(name_gate::NameGate::default)
.enter(name)
}
fn one_call() -> Option<MutexGuard<'static, ()>> {

View File

@ -0,0 +1,146 @@
//! Excludes simultaneous library loads of one exact pipeline name.
use std::collections::HashSet;
use std::sync::{Condvar, Mutex};
#[derive(Default)]
pub(crate) struct NameGate {
active: Mutex<HashSet<String>>,
changed: Condvar,
}
impl NameGate {
pub(crate) fn enter(&self, name: &str) -> Guard<'_> {
let mut active = self.active.lock().unwrap_or_else(|e| e.into_inner());
while active.contains(name) {
active = self.changed.wait(active).unwrap_or_else(|e| e.into_inner());
}
active.insert(name.to_owned());
Guard {
gate: self,
name: name.to_owned(),
}
}
}
/// Keeps the name active without holding the bookkeeping mutex while the
/// driver works. Only active names are retained, including while unwinding.
#[must_use]
pub(crate) struct Guard<'a> {
gate: &'a NameGate,
name: String,
}
impl Drop for Guard<'_> {
fn drop(&mut self) {
self.gate
.active
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(&self.name);
// Waiters can be waiting for different names. Waking just one
// could leave the waiter for this now-free name asleep.
self.gate.changed.notify_all();
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
const FIRST: &str = "00000000000000000000000000000000";
const COLLIDING: &str = "00000000000000000000000000000022";
const DEADLINE: Duration = Duration::from_secs(5);
#[test]
fn different_names_do_not_wait_for_a_driver_load() {
// These exact names shared stripe 0 in the old 64-mutex gate.
let old_stripe = |name: &str| {
name.bytes().fold(0usize, |h, b| {
h.wrapping_mul(31).wrapping_add(usize::from(b))
}) % 64
};
assert_eq!(old_stripe(FIRST), old_stripe(COLLIDING));
let gate = NameGate::default();
let first = gate.enter(FIRST);
thread::scope(|scope| {
let gate = &gate;
let (tx, rx) = mpsc::channel();
let worker = scope.spawn(move || {
let _other = gate.enter(COLLIDING);
tx.send(()).unwrap();
});
let entered = rx.recv_timeout(DEADLINE);
// Release even on failure, so a striped-gate regression cannot
// leave the test runner waiting forever to join its worker.
drop(first);
worker.join().unwrap();
assert!(
entered.is_ok(),
"a different name waited for the first load"
);
});
}
#[test]
fn same_name_waits_until_the_first_load_finishes() {
let gate = NameGate::default();
let first = gate.enter(FIRST);
thread::scope(|scope| {
let gate = &gate;
let (ready_tx, ready_rx) = mpsc::channel();
let (entered_tx, entered_rx) = mpsc::channel();
let worker = scope.spawn(move || {
ready_tx.send(()).unwrap();
let _same = gate.enter(FIRST);
entered_tx.send(()).unwrap();
});
ready_rx.recv_timeout(DEADLINE).unwrap();
let entered_early = entered_rx.recv_timeout(Duration::from_millis(100));
drop(first);
let entered_later = entered_rx.recv_timeout(DEADLINE);
worker.join().unwrap();
assert!(entered_early.is_err(), "the same name loaded concurrently");
assert!(entered_later.is_ok(), "the waiting load was not woken");
});
}
#[test]
fn completed_names_are_not_retained() {
let gate = NameGate::default();
for n in 0..1_000 {
let guard = gate.enter(&format!("pipeline-{n}"));
assert_eq!(gate.active.lock().unwrap().len(), 1);
drop(guard);
assert!(gate.active.lock().unwrap().is_empty());
}
}
#[test]
fn unwinding_releases_the_name() {
let gate = NameGate::default();
let panicked = std::panic::catch_unwind(|| {
let _first = gate.enter(FIRST);
panic!("driver load panicked");
});
assert!(panicked.is_err());
assert!(gate.active.lock().unwrap().is_empty());
let _next = gate.enter(FIRST);
}
#[test]
fn poisoned_bookkeeping_still_allows_loads() {
let gate = NameGate::default();
let _ = std::panic::catch_unwind(|| {
let _active = gate.active.lock().unwrap();
panic!("bookkeeping panicked");
});
let first = gate.enter(FIRST);
drop(first);
assert!(gate.active.lock().unwrap_err().into_inner().is_empty());
}
}