From d4c415f666f7cbf2a2edec20bf2ae9ec64301d94 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Mon, 26 Nov 2018 17:25:14 -0500 Subject: [PATCH] Improve locking safety for Registry --- Cargo.lock | 6 +- wgpu-bindings/Cargo.toml | 3 +- wgpu-bindings/wgpu.h | 4 +- wgpu-native/Cargo.toml | 4 +- wgpu-native/src/command/compute.rs | 16 +++--- wgpu-native/src/command/mod.rs | 22 ++++---- wgpu-native/src/command/render.rs | 6 +- wgpu-native/src/device.rs | 91 +++++++++++++++--------------- wgpu-native/src/instance.rs | 12 ++-- wgpu-native/src/lib.rs | 8 +-- wgpu-native/src/registry/local.rs | 33 ++++------- wgpu-native/src/registry/mod.rs | 32 +++++------ wgpu-native/src/registry/remote.rs | 31 ++-------- wgpu-native/src/resource.rs | 2 - wgpu-native/src/track.rs | 1 + 15 files changed, 116 insertions(+), 155 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cc710f64b..6fb175342 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -78,7 +78,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "cbindgen" version = "0.6.7" -source = "git+https://github.com/eqrion/cbindgen.git#3e07fe7ffe5c04c1f80446a655afa9319d796080" +source = "git+https://github.com/eqrion/cbindgen#9861755b05269495ecacd9bf513bdda948f302af" dependencies = [ "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -899,7 +899,7 @@ dependencies = [ name = "wgpu-bindings" version = "0.1.0" dependencies = [ - "cbindgen 0.6.7 (git+https://github.com/eqrion/cbindgen.git)", + "cbindgen 0.6.7 (git+https://github.com/eqrion/cbindgen)", ] [[package]] @@ -1004,7 +1004,7 @@ dependencies = [ "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" "checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d" -"checksum cbindgen 0.6.7 (git+https://github.com/eqrion/cbindgen.git)" = "" +"checksum cbindgen 0.6.7 (git+https://github.com/eqrion/cbindgen)" = "" "checksum cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "f159dfd43363c4d08055a07703eb7a3406b0dac4d0584d96965a3262db3c9d16" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" "checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" diff --git a/wgpu-bindings/Cargo.toml b/wgpu-bindings/Cargo.toml index 34760331e..ea21732df 100644 --- a/wgpu-bindings/Cargo.toml +++ b/wgpu-bindings/Cargo.toml @@ -10,4 +10,5 @@ authors = [ default = [] [dependencies] -cbindgen = { git = "https://github.com/eqrion/cbindgen.git" } +#cbindgen = "0.6.7" +cbindgen = { git = "https://github.com/eqrion/cbindgen" } diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index d38a1774c..154021958 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -105,6 +105,8 @@ typedef enum { typedef struct WGPURenderPassDescriptor_TextureViewId WGPURenderPassDescriptor_TextureViewId; +typedef struct WGPUTextureUsageFlags WGPUTextureUsageFlags; + typedef WGPUId WGPUDeviceId; typedef WGPUId WGPUAdapterId; @@ -235,8 +237,6 @@ typedef struct { uint32_t depth; } WGPUExtent3d; -typedef uint32_t WGPUTextureUsageFlags; - typedef struct { WGPUExtent3d size; uint32_t array_size; diff --git a/wgpu-native/Cargo.toml b/wgpu-native/Cargo.toml index 2ae9757fe..17352c0b0 100644 --- a/wgpu-native/Cargo.toml +++ b/wgpu-native/Cargo.toml @@ -11,13 +11,13 @@ crate-type = ["lib", "cdylib", "staticlib"] [features] default = [] -remote = ["parking_lot"] +remote = [] [dependencies] bitflags = "1.0" lazy_static = "1.1.0" log = "0.4" -parking_lot = { version = "0.6", optional = true } +parking_lot = { version = "0.6" } gfx-hal = { git = "https://github.com/gfx-rs/gfx" } # required by gfx-memory gfx-backend-empty = { git = "https://github.com/gfx-rs/gfx" } gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", optional = true } diff --git a/wgpu-native/src/command/compute.rs b/wgpu-native/src/command/compute.rs index 7c8eeffde..54d8cbf01 100644 --- a/wgpu-native/src/command/compute.rs +++ b/wgpu-native/src/command/compute.rs @@ -1,4 +1,4 @@ -use registry::{HUB, Items, Registry}; +use registry::{HUB, Items}; use { Stored, BindGroupId, CommandBufferId, ComputePassId, ComputePipelineId, @@ -29,11 +29,11 @@ pub extern "C" fn wgpu_compute_pass_end_pass( pass_id: ComputePassId, ) -> CommandBufferId { let pass = HUB.compute_passes - .lock() + .write() .take(pass_id); HUB.command_buffers - .lock() + .write() .get_mut(pass.cmb_id.value) .raw .push(pass.raw); @@ -44,13 +44,13 @@ pub extern "C" fn wgpu_compute_pass_end_pass( pub extern "C" fn wgpu_compute_pass_set_bind_group( pass_id: ComputePassId, index: u32, bind_group_id: BindGroupId, ) { - let bind_group_guard = HUB.bind_groups.lock(); + let bind_group_guard = HUB.bind_groups.read(); let set = &bind_group_guard.get(bind_group_id).raw; let layout = unimplemented!(); // see https://github.com/gpuweb/gpuweb/pull/93 HUB.compute_passes - .lock() + .write() .get_mut(pass_id) .raw .bind_compute_descriptor_sets(layout, index as usize, iter::once(set), &[]); @@ -60,11 +60,11 @@ pub extern "C" fn wgpu_compute_pass_set_bind_group( pub extern "C" fn wgpu_compute_pass_set_pipeline( pass_id: ComputePassId, pipeline_id: ComputePipelineId, ) { - let pipeline_guard = HUB.compute_pipelines.lock(); + let pipeline_guard = HUB.compute_pipelines.read(); let pipeline = &pipeline_guard.get(pipeline_id).raw; HUB.compute_passes - .lock() + .write() .get_mut(pass_id) .raw .bind_compute_pipeline(pipeline); @@ -75,7 +75,7 @@ pub extern "C" fn wgpu_compute_pass_dispatch( pass_id: ComputePassId, x: u32, y: u32, z: u32, ) { HUB.compute_passes - .lock() + .write() .get_mut(pass_id) .raw .dispatch([x, y, z]); diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index 4fb17f790..0c32fe8a2 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -15,7 +15,7 @@ use { }; use conv; use device::{FramebufferKey, RenderPassKey}; -use registry::{HUB, Items, Registry}; +use registry::{HUB, Items}; use track::{BufferTracker, TextureTracker}; use std::collections::hash_map::Entry; @@ -97,8 +97,8 @@ impl CommandBuffer { I: Iterator)>, J: Iterator)>, { - let buffer_guard = HUB.buffers.lock(); - let texture_guard = HUB.textures.lock(); + let buffer_guard = HUB.buffers.read(); + let texture_guard = HUB.textures.read(); let buffer_barriers = buffer_iter.map(|(id, transit)| { let b = buffer_guard.get(id); @@ -137,11 +137,11 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( command_buffer_id: CommandBufferId, desc: RenderPassDescriptor, ) -> RenderPassId { - let mut cmb_guard = HUB.command_buffers.lock(); + let mut cmb_guard = HUB.command_buffers.write(); let cmb = cmb_guard.get_mut(command_buffer_id); - let device_guard = HUB.devices.lock(); + let device_guard = HUB.devices.read(); let device = device_guard.get(cmb.device_id.value); - let view_guard = HUB.texture_views.lock(); + let view_guard = HUB.texture_views.read(); let mut current_comb = device.com_allocator.extend(cmb); current_comb.begin( @@ -202,7 +202,7 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( } }; - let mut render_pass_cache = device.render_passes.lock().unwrap(); + let mut render_pass_cache = device.render_passes.lock(); let render_pass = match render_pass_cache.entry(rp_key) { Entry::Occupied(e) => e.into_mut(), Entry::Vacant(e) => { @@ -231,7 +231,7 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( } }; - let mut framebuffer_cache = device.framebuffers.lock().unwrap(); + let mut framebuffer_cache = device.framebuffers.lock(); let fb_key = FramebufferKey { attachments: desc.color_attachments .iter() @@ -287,7 +287,7 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( ); HUB.render_passes - .lock() + .write() .register(RenderPass::new( current_comb, Stored { @@ -301,7 +301,7 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( pub extern "C" fn wgpu_command_buffer_begin_compute_pass( command_buffer_id: CommandBufferId, ) -> ComputePassId { - let mut cmb_guard = HUB.command_buffers.lock(); + let mut cmb_guard = HUB.command_buffers.write(); let cmb = cmb_guard.get_mut(command_buffer_id); let raw = cmb.raw.pop().unwrap(); @@ -311,6 +311,6 @@ pub extern "C" fn wgpu_command_buffer_begin_compute_pass( }; HUB.compute_passes - .lock() + .write() .register(ComputePass::new(raw, stored)) } diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index 9a3840159..79b68dc6c 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -1,4 +1,4 @@ -use registry::{HUB, Items, Registry}; +use registry::{HUB, Items}; use track::{BufferTracker, TextureTracker}; use { CommandBuffer, Stored, @@ -35,11 +35,11 @@ pub extern "C" fn wgpu_render_pass_end_pass( pass_id: RenderPassId, ) -> CommandBufferId { let mut pass = HUB.render_passes - .lock() + .write() .take(pass_id); pass.raw.end_render_pass(); - let mut cmb_guard = HUB.command_buffers.lock(); + let mut cmb_guard = HUB.command_buffers.write(); let cmb = cmb_guard.get_mut(pass.cmb_id.value); if let Some(ref mut last) = cmb.raw.last_mut() { diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 411cb5dcd..eb8a215a6 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1,5 +1,5 @@ use {back, binding_model, command, conv, pipeline, resource}; -use registry::{HUB, Items, ItemsGuard, Registry}; +use registry::{HUB, Items}; use track::{BufferTracker, TextureTracker}; use { CommandBuffer, LifeGuard, RefCount, Stored, SubmissionIndex, WeaklyStored, @@ -16,7 +16,8 @@ use hal::{self, Device as _Device}; use std::{ffi, slice}; use std::collections::hash_map::{Entry, HashMap}; -use std::sync::Mutex; + +use parking_lot::Mutex; use std::sync::atomic::Ordering; @@ -68,11 +69,12 @@ impl DestroyedResources { self.referenced.push((resource_id, life_guard.ref_count.clone())); } - fn triage_referenced( - &mut self, - buffer_guard: &mut ItemsGuard>, - texture_guard: &mut ItemsGuard>, - ) { + fn triage_referenced( + &mut self, buffer_guard: &mut Gb, texture_guard: &mut Gt, + ) where + Gb: Items>, + Gt: Items>, + { for i in (0 .. self.referenced.len()).rev() { // one in resource itself, and one here in this list let num_refs = self.referenced[i].1.load(); @@ -201,7 +203,7 @@ pub extern "C" fn wgpu_device_create_texture( let format = conv::map_texture_format(desc.format); let aspects = format.surface_desc().aspects; let usage = conv::map_texture_usage(desc.usage, aspects); - let device_guard = HUB.devices.lock(); + let device_guard = HUB.devices.read(); let device = &device_guard.get(device_id); let image_unbound = device .raw @@ -242,7 +244,7 @@ pub extern "C" fn wgpu_device_create_texture( let life_guard = LifeGuard::new(); let ref_count = life_guard.ref_count.clone(); let id = HUB.textures - .lock() + .write() .register(resource::Texture { raw: bound_image, device_id: Stored { @@ -256,7 +258,6 @@ pub extern "C" fn wgpu_device_create_texture( }); let query = device.texture_tracker .lock() - .unwrap() .query( &Stored { value: id, ref_count }, TextureUsageFlags::WRITE_ALL, @@ -271,11 +272,11 @@ pub extern "C" fn wgpu_texture_create_texture_view( texture_id: TextureId, desc: &resource::TextureViewDescriptor, ) -> TextureViewId { - let texture_guard = HUB.textures.lock(); + let texture_guard = HUB.textures.read(); let texture = texture_guard.get(texture_id); let raw = HUB.devices - .lock() + .read() .get(texture.device_id.value) .raw .create_image_view( @@ -292,7 +293,7 @@ pub extern "C" fn wgpu_texture_create_texture_view( .unwrap(); HUB.texture_views - .lock() + .write() .register(resource::TextureView { raw, texture_id: Stored { @@ -310,7 +311,7 @@ pub extern "C" fn wgpu_texture_create_texture_view( pub extern "C" fn wgpu_texture_create_default_texture_view( texture_id: TextureId, ) -> TextureViewId { - let texture_guard = HUB.textures.lock(); + let texture_guard = HUB.textures.read(); let texture = texture_guard.get(texture_id); let view_kind = match texture.kind { @@ -320,7 +321,7 @@ pub extern "C" fn wgpu_texture_create_default_texture_view( }; let raw = HUB.devices - .lock() + .read() .get(texture.device_id.value) .raw .create_image_view( @@ -333,7 +334,7 @@ pub extern "C" fn wgpu_texture_create_default_texture_view( .unwrap(); HUB.texture_views - .lock() + .write() .register(resource::TextureView { raw, texture_id: Stored { @@ -351,14 +352,13 @@ pub extern "C" fn wgpu_texture_create_default_texture_view( pub extern "C" fn wgpu_texture_destroy( texture_id: TextureId, ) { - let texture_guard = HUB.textures.lock(); + let texture_guard = HUB.textures.read(); let texture = texture_guard.get(texture_id); - let device_guard = HUB.devices.lock(); + let device_guard = HUB.devices.write(); device_guard .get(texture.device_id.value) .destroyed .lock() - .unwrap() .add(ResourceId::Texture(texture_id), &texture.life_guard); } @@ -377,7 +377,7 @@ pub extern "C" fn wgpu_device_create_bind_group_layout( let bindings = unsafe { slice::from_raw_parts(desc.bindings, desc.bindings_length) }; let descriptor_set_layout = HUB.devices - .lock() + .read() .get(device_id) .raw .create_descriptor_set_layout( @@ -395,7 +395,7 @@ pub extern "C" fn wgpu_device_create_bind_group_layout( .unwrap(); HUB.bind_group_layouts - .lock() + .write() .register(binding_model::BindGroupLayout { raw: descriptor_set_layout, }) @@ -409,21 +409,21 @@ pub extern "C" fn wgpu_device_create_pipeline_layout( let bind_group_layouts = unsafe { slice::from_raw_parts(desc.bind_group_layouts, desc.bind_group_layouts_length) }; - let bind_group_layout_guard = HUB.bind_group_layouts.lock(); + let bind_group_layout_guard = HUB.bind_group_layouts.read(); let descriptor_set_layouts = bind_group_layouts .iter() .map(|&id| &bind_group_layout_guard.get(id).raw); // TODO: push constants let pipeline_layout = HUB.devices - .lock() + .read() .get(device_id) .raw .create_pipeline_layout(descriptor_set_layouts, &[]) .unwrap(); HUB.pipeline_layouts - .lock() + .write() .register(binding_model::PipelineLayout { raw: pipeline_layout, }) @@ -435,7 +435,7 @@ pub extern "C" fn wgpu_device_create_blend_state( desc: &pipeline::BlendStateDescriptor, ) -> BlendStateId { HUB.blend_states - .lock() + .write() .register(pipeline::BlendState { raw: conv::map_blend_state_descriptor(desc), }) @@ -447,7 +447,7 @@ pub extern "C" fn wgpu_device_create_depth_stencil_state( desc: &pipeline::DepthStencilStateDescriptor, ) -> DepthStencilStateId { HUB.depth_stencil_states - .lock() + .write() .register(pipeline::DepthStencilState { raw: conv::map_depth_stencil_state(desc), }) @@ -462,14 +462,14 @@ pub extern "C" fn wgpu_device_create_shader_module( slice::from_raw_parts(desc.code.bytes, desc.code.length) }; let shader = HUB.devices - .lock() + .read() .get(device_id) .raw .create_shader_module(spv) .unwrap(); HUB.shader_modules - .lock() + .write() .register(ShaderModule { raw: shader }) } @@ -478,7 +478,7 @@ pub extern "C" fn wgpu_device_create_command_buffer( device_id: DeviceId, _desc: &command::CommandBufferDescriptor, ) -> CommandBufferId { - let device_guard = HUB.devices.lock(); + let device_guard = HUB.devices.read(); let device = device_guard.get(device_id); let dev_stored = Stored { @@ -490,7 +490,7 @@ pub extern "C" fn wgpu_device_create_command_buffer( hal::command::CommandBufferFlags::ONE_TIME_SUBMIT, hal::command::CommandBufferInheritanceInfo::default(), ); - HUB.command_buffers.lock().register(cmd_buf) + HUB.command_buffers.write().register(cmd_buf) } #[no_mangle] @@ -504,18 +504,18 @@ pub extern "C" fn wgpu_queue_submit( command_buffer_ptr: *const CommandBufferId, command_buffer_count: usize, ) { - let mut device_guard = HUB.devices.lock(); + let mut device_guard = HUB.devices.write(); let device = device_guard.get_mut(queue_id); - let mut buffer_tracker = device.buffer_tracker.lock().unwrap(); - let mut texture_tracker = device.texture_tracker.lock().unwrap(); + let mut buffer_tracker = device.buffer_tracker.lock(); + let mut texture_tracker = device.texture_tracker.lock(); - let mut command_buffer_guard = HUB.command_buffers.lock(); + let mut command_buffer_guard = HUB.command_buffers.write(); let command_buffer_ids = unsafe { slice::from_raw_parts(command_buffer_ptr, command_buffer_count) }; - let mut buffer_guard = HUB.buffers.lock(); - let mut texture_guard = HUB.textures.lock(); + let mut buffer_guard = HUB.buffers.write(); + let mut texture_guard = HUB.textures.write(); let old_submit_index = device.life_guard.submission_index.fetch_add(1, Ordering::Relaxed); //TODO: if multiple command buffers are submitted, we can re-use the last @@ -572,8 +572,9 @@ pub extern "C" fn wgpu_queue_submit( } } - if let Ok(mut destroyed) = device.destroyed.lock() { - destroyed.triage_referenced(&mut buffer_guard, &mut texture_guard); + { + let mut destroyed = device.destroyed.lock(); + destroyed.triage_referenced(&mut *buffer_guard, &mut *texture_guard); destroyed.cleanup(&device.raw); destroyed.active.push(ActiveFrame { @@ -601,12 +602,12 @@ pub extern "C" fn wgpu_device_create_render_pipeline( height: 100, }; - let device_guard = HUB.devices.lock(); + let device_guard = HUB.devices.read(); let device = device_guard.get(device_id); - let pipeline_layout_guard = HUB.pipeline_layouts.lock(); + let pipeline_layout_guard = HUB.pipeline_layouts.read(); let layout = &pipeline_layout_guard.get(desc.layout).raw; let pipeline_stages = unsafe { slice::from_raw_parts(desc.stages, desc.stages_length) }; - let shader_module_guard = HUB.shader_modules.lock(); + let shader_module_guard = HUB.shader_modules.read(); let rp_key = { let op_keep = hal::pass::AttachmentOps { @@ -641,7 +642,7 @@ pub extern "C" fn wgpu_device_create_render_pipeline( } }; - let mut render_pass_cache = device.render_passes.lock().unwrap(); + let mut render_pass_cache = device.render_passes.lock(); let main_pass = match render_pass_cache.entry(rp_key) { Entry::Occupied(e) => e.into_mut(), Entry::Vacant(e) => { @@ -731,7 +732,7 @@ pub extern "C" fn wgpu_device_create_render_pipeline( primitive_restart: hal::pso::PrimitiveRestart::Disabled, // TODO }; - let blend_state_guard = HUB.blend_states.lock(); + let blend_state_guard = HUB.blend_states.read(); let blend_states = unsafe { slice::from_raw_parts(desc.blend_states, desc.blend_states_length) } .iter() .map(|id| blend_state_guard.get(id.clone()).raw) @@ -742,7 +743,7 @@ pub extern "C" fn wgpu_device_create_render_pipeline( targets: blend_states, }; - let depth_stencil_state_guard = HUB.depth_stencil_states.lock(); + let depth_stencil_state_guard = HUB.depth_stencil_states.read(); let depth_stencil = depth_stencil_state_guard.get(desc.depth_stencil_state).raw; // TODO @@ -801,6 +802,6 @@ pub extern "C" fn wgpu_device_create_render_pipeline( .unwrap(); HUB.render_pipelines - .lock() + .write() .register(pipeline::RenderPipeline { raw: pipeline }) } diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 2c2ad3e14..922ff500e 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -1,6 +1,6 @@ use hal::{self, Instance as _Instance, PhysicalDevice as _PhysicalDevice}; -use registry::{HUB, Items, Registry}; +use registry::{HUB, Items}; use {AdapterId, Device, DeviceId, InstanceId}; #[repr(C)] @@ -35,7 +35,7 @@ pub extern "C" fn wgpu_create_instance() -> InstanceId { ))] { let inst = ::back::Instance::create("wgpu", 1); - HUB.instances.lock().register(inst) + HUB.instances.write().register(inst) } #[cfg(not(any( feature = "gfx-backend-vulkan", @@ -52,7 +52,7 @@ pub extern "C" fn wgpu_instance_get_adapter( instance_id: InstanceId, desc: &AdapterDescriptor, ) -> AdapterId { - let instance_guard = HUB.instances.lock(); + let instance_guard = HUB.instances.read(); let instance = instance_guard.get(instance_id); let (mut low, mut high, mut other) = (None, None, None); for adapter in instance.enumerate_adapters() { @@ -68,7 +68,7 @@ pub extern "C" fn wgpu_instance_get_adapter( PowerPreference::HighPerformance | PowerPreference::Default => high.or(low), }; HUB.adapters - .lock() + .write() .register(some.or(other).unwrap()) } @@ -77,11 +77,11 @@ pub extern "C" fn wgpu_adapter_create_device( adapter_id: AdapterId, _desc: &DeviceDescriptor, ) -> DeviceId { - let mut adapter_guard = HUB.adapters.lock(); + let mut adapter_guard = HUB.adapters.write(); let adapter = adapter_guard.get_mut(adapter_id); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap(); let mem_props = adapter.physical_device.memory_properties(); HUB.devices - .lock() + .write() .register(Device::new(device, queue_group, mem_props)) } diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 812c95263..f90186e54 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -4,7 +4,6 @@ extern crate bitflags; extern crate lazy_static; #[macro_use] extern crate log; -#[cfg(feature = "remote")] extern crate parking_lot; #[cfg(feature = "gfx-backend-dx12")] @@ -41,17 +40,12 @@ pub use self::pipeline::*; pub use self::resource::*; use back::Backend as B; -use registry::Id; +pub use registry::Id; use std::ptr; use std::sync::atomic::{AtomicUsize, Ordering}; -//#[cfg(not(feature = "remote"))] -//unsafe impl Sync for Stored {} -//#[cfg(not(feature = "remote"))] -//unsafe impl Send for Stored {} - type SubmissionIndex = usize; #[derive(Debug)] diff --git a/wgpu-native/src/registry/local.rs b/wgpu-native/src/registry/local.rs index e4e4bae6d..6f7b26395 100644 --- a/wgpu-native/src/registry/local.rs +++ b/wgpu-native/src/registry/local.rs @@ -3,22 +3,29 @@ use std::os::raw::c_void; pub type Id = *mut c_void; -pub type ItemsGuard<'a, T> = Items; pub struct Items { marker: PhantomData, } +impl Default for Items { + fn default() -> Self { + Items { + marker: PhantomData, + } + } +} + impl super::Items for Items { fn register(&mut self, handle: T) -> Id { Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void } - fn get(&self, id: Id) -> super::Item { + fn get(&self, id: Id) -> &T { unsafe { (id as *mut T).as_ref() }.unwrap() } - fn get_mut(&mut self, id: Id) -> super::ItemMut { + fn get_mut(&mut self, id: Id) -> &mut T { unsafe { (id as *mut T).as_mut() }.unwrap() } @@ -26,23 +33,3 @@ impl super::Items for Items { unsafe { *Box::from_raw(id as *mut T) } } } - -pub struct Registry { - marker: PhantomData, -} - -impl Default for Registry { - fn default() -> Self { - Registry { - marker: PhantomData, - } - } -} - -impl super::Registry for Registry { - fn lock(&self) -> ItemsGuard { - Items { - marker: PhantomData, - } - } -} diff --git a/wgpu-native/src/registry/mod.rs b/wgpu-native/src/registry/mod.rs index 1ddd7f791..caa9801f5 100644 --- a/wgpu-native/src/registry/mod.rs +++ b/wgpu-native/src/registry/mod.rs @@ -1,12 +1,6 @@ -#[cfg(not(feature = "remote"))] -mod local; -#[cfg(feature = "remote")] -mod remote; +use std::sync::Arc; -#[cfg(not(feature = "remote"))] -pub use self::local::{Id, ItemsGuard, Registry as ConcreteRegistry}; -#[cfg(feature = "remote")] -pub use self::remote::{Id, ItemsGuard, Registry as ConcreteRegistry}; +use parking_lot::RwLock; use { AdapterHandle, BindGroupLayoutHandle, BindGroupHandle, @@ -17,20 +11,26 @@ use { }; -type Item<'a, T> = &'a T; -type ItemMut<'a, T> = &'a mut T; +#[cfg(not(feature = "remote"))] +mod local; +#[cfg(feature = "remote")] +mod remote; -pub trait Registry: Default { - fn lock(&self) -> ItemsGuard; -} +#[cfg(not(feature = "remote"))] +pub use self::local::{Id, Items as ConcreteItems}; +#[cfg(feature = "remote")] +pub use self::remote::{Id, Items as ConcreteItems}; -pub trait Items { + +pub trait Items: Default { fn register(&mut self, handle: T) -> Id; - fn get(&self, id: Id) -> Item; - fn get_mut(&mut self, id: Id) -> ItemMut; + fn get(&self, id: Id) -> &T; + fn get_mut(&mut self, id: Id) -> &mut T; fn take(&mut self, id: Id) -> T; } +pub type ConcreteRegistry = Arc>>; + #[derive(Default)] pub struct Hub { pub(crate) instances: ConcreteRegistry, diff --git a/wgpu-native/src/registry/remote.rs b/wgpu-native/src/registry/remote.rs index 1253ef05e..7e4cf2e9a 100644 --- a/wgpu-native/src/registry/remote.rs +++ b/wgpu-native/src/registry/remote.rs @@ -1,19 +1,16 @@ use hal::backend::FastHashMap; -use parking_lot::{Mutex, MutexGuard}; -use std::sync::Arc; pub type Id = u32; -pub type ItemsGuard<'a, T> = MutexGuard<'a, Items>; pub struct Items { - next_id: Id, + next_id: u32, tracked: FastHashMap, free: Vec, } -impl Items { - fn new() -> Self { +impl Default for Items { + fn default() -> Self { Items { next_id: 0, tracked: FastHashMap::default(), @@ -35,11 +32,11 @@ impl super::Items for Items { id } - fn get(&self, id: Id) -> super::Item { + fn get(&self, id: Id) -> & T { self.tracked.get(&id).unwrap() } - fn get_mut(&mut self, id: Id) -> super::ItemMut { + fn get_mut(&mut self, id: Id) -> &mut T { self.tracked.get_mut(&id).unwrap() } @@ -48,21 +45,3 @@ impl super::Items for Items { self.tracked.remove(&id).unwrap() } } - -pub struct Registry { - items: Arc>>, -} - -impl Default for Registry { - fn default() -> Self { - Registry { - items: Arc::new(Mutex::new(Items::new())), - } - } -} - -impl super::Registry for Registry { - fn lock(&self) -> ItemsGuard { - self.items.lock() - } -} diff --git a/wgpu-native/src/resource.rs b/wgpu-native/src/resource.rs index bfe0903e6..1c6c30fe6 100644 --- a/wgpu-native/src/resource.rs +++ b/wgpu-native/src/resource.rs @@ -7,7 +7,6 @@ use hal; bitflags! { - #[repr(transparent)] pub struct BufferUsageFlags: u32 { const MAP_READ = 1; const MAP_WRITE = 2; @@ -55,7 +54,6 @@ pub enum TextureFormat { } bitflags! { - #[repr(transparent)] pub struct TextureUsageFlags: u32 { const TRANSFER_SRC = 1; const TRANSFER_DST = 2; diff --git a/wgpu-native/src/track.rs b/wgpu-native/src/track.rs index bfa9a80a8..afa585749 100644 --- a/wgpu-native/src/track.rs +++ b/wgpu-native/src/track.rs @@ -8,6 +8,7 @@ use std::ops::{BitOr, Range}; #[derive(Clone, Debug, PartialEq)] +#[allow(unused)] pub enum Tracktion { Init, Keep,