Fix the ordering of id freeing and deleting (#5554)

This commit is contained in:
Daniel McNab authored and GitHub committed 2024-04-19 12:59:09 -04:00
1 parent 54740d5982
commit 54d22a89d3
1 file changed
+55 -1
+55 -1
View File
@@ -38,6 +38,7 @@ impl RegistryReport {
///
#[derive(Debug)]
pub(crate) struct Registry<T: Resource> {
// Must only contain an id which has either never been used or has been released from `storage`
identity: Arc<IdentityManager<T::Marker>>,
storage: RwLock<Storage<T>>,
backend: Backend,
@@ -162,8 +163,11 @@ impl<T: Resource> Registry<T> {
storage.insert_error(id, label);
}
pub(crate) fn unregister(&self, id: Id<T::Marker>) -> Option<Arc<T>> {
self.identity.free(id);
let value = self.storage.write().remove(id);
// This needs to happen *after* removing it from the storage, to maintain the
// invariant that `self.identity` only contains ids which are actually available
// See https://github.com/gfx-rs/wgpu/issues/5372
self.identity.free(id);
//Returning None is legal if it's an error ID
value
}
@@ -206,3 +210,53 @@ impl<T: Resource> Registry<T> {
report
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use crate::{
id::Marker,
resource::{Resource, ResourceInfo, ResourceType},
};
use super::Registry;
struct TestData {
info: ResourceInfo<TestData>,
}
struct TestDataId;
impl Marker for TestDataId {}
impl Resource for TestData {
type Marker = TestDataId;
const TYPE: ResourceType = "Test data";
fn as_info(&self) -> &ResourceInfo<Self> {
&self.info
}
fn as_info_mut(&mut self) -> &mut ResourceInfo<Self> {
&mut self.info
}
}
#[test]
fn simultaneous_registration() {
let registry = Registry::without_backend();
std::thread::scope(|s| {
for _ in 0..5 {
s.spawn(|| {
for _ in 0..1000 {
let value = Arc::new(TestData {
info: ResourceInfo::new("Test data", None),
});
let new_id = registry.prepare(None);
let (id, _) = new_id.assign(value);
registry.unregister(id);
}
});
}
})
}
}