diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index bc824f3cf..27cadece8 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -258,9 +258,9 @@ type DeviceToken<'a> = Token<'a, Device>; impl PendingResources { fn triage_referenced(&mut self, trackers: &mut TrackerSet, mut token: &mut DeviceToken) { // Before destruction, a resource is expected to have the following strong refs: - // 1. in resource itself - // 2. in the device tracker - // 3. in this list + // - in resource itself + // - in the device tracker + // - in this list const MIN_REFS: usize = 4; if self.referenced.iter().all(|r| r.1.load() >= MIN_REFS) { diff --git a/wgpu-native/src/hub.rs b/wgpu-native/src/hub.rs index e59f2bbaa..eea249490 100644 --- a/wgpu-native/src/hub.rs +++ b/wgpu-native/src/hub.rs @@ -136,15 +136,22 @@ impl Storage { /// Type system for enforcing the lock order on shared HUB structures. -/// The main property we are trying to establish is that each type -/// of resources have only a single path from Root{} to reach by, -/// when locking multiple resources types. +/// If type A implements `Access`, that means we are allowed to proceed +/// with locking resource `B` after we lock `A`. +/// +/// The implenentations basically describe the edges in a directed graph +/// of lock transitions. As long as it doesn't have loops, we can have +/// multiple concurrent paths on this graph (from multiple threads) without +/// deadlocks, i.e. there is always a path whose next resource is not locked +/// by some other path, at any time. pub trait Access {} pub enum Root {} //TODO: establish an order instead of declaring all the pairs. +#[cfg(not(feature = "gfx-backend-gl"))] impl Access for Root {} impl Access for Root {} +#[cfg(not(feature = "gfx-backend-gl"))] impl Access for InstanceHandle {} impl Access for Root {} impl Access for SurfaceHandle {} @@ -195,6 +202,11 @@ thread_local! { static ACTIVE_TOKEN: Cell = Cell::new(false); } +/// A permission token to lock resource `T` or anything after it, +/// as defined by the `Access` implementations. +/// +/// Note: there can only be one non-borrowed `Token` alive on a thread +/// at a time, which is enforced by `ACTIVE_TOKEN`. pub struct Token<'a, T: 'a> { level: PhantomData<&'a T>, is_root: bool, diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 6c3a219de..91f1e17d0 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -207,15 +207,14 @@ pub fn instance_get_adapter( desc: &AdapterDescriptor, token: &mut Token, ) -> AdapterHandle { - let (instance_guard, mut token) = HUB.instances.read(token); #[cfg(not(feature = "gfx-backend-gl"))] let adapters = { - let _ = &mut token; + let (instance_guard, _) = HUB.instances.read(token); instance_guard[instance_id].enumerate_adapters() }; #[cfg(feature = "gfx-backend-gl")] let adapters = { - let (surface_guard, _) = HUB.surfaces.read(&mut token); + let (surface_guard, _) = HUB.surfaces.read(token); surface_guard[instance_id].raw.enumerate_adapters() }; diff --git a/wgpu-remote/src/server.rs b/wgpu-remote/src/server.rs index ca92f70d6..b0c8673c9 100644 --- a/wgpu-remote/src/server.rs +++ b/wgpu-remote/src/server.rs @@ -28,11 +28,11 @@ fn process(message: GlobalMessage) -> ControlFlow { wgn::HUB.instances.register(instance_id, instance, &mut token); } InstanceMessage::InstanceGetAdapter(instance_id, ref desc, id) => { - let adapter = wgn::instance_get_adapter(instance_id, desc); + let adapter = wgn::instance_get_adapter(instance_id, desc, &mut token); wgn::HUB.adapters.register(id, adapter, &mut token); } InstanceMessage::AdapterCreateDevice(adapter_id, ref desc, id) => { - let device = wgn::adapter_create_device(adapter_id, desc); + let device = wgn::adapter_create_device(adapter_id, desc, &mut token); wgn::HUB.devices.register(id, device, &mut token); } InstanceMessage::Destroy(instance_id) => {