Improve locking safety for Registry

This commit is contained in:
Dzmitry Malyshau 2018-11-26 17:25:14 -05:00
parent c440791ab2
commit d4c415f666
15 changed files with 116 additions and 155 deletions

6
Cargo.lock generated
View File

@ -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)" = "<none>"
"checksum cbindgen 0.6.7 (git+https://github.com/eqrion/cbindgen)" = "<none>"
"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"

View File

@ -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" }

View File

@ -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;

View File

@ -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 }

View File

@ -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]);

View File

@ -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<B> {
I: Iterator<Item = (BufferId, Range<BufferUsageFlags>)>,
J: Iterator<Item = (TextureId, Range<TextureUsageFlags>)>,
{
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<TextureViewId>,
) -> 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))
}

View File

@ -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() {

View File

@ -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<B: hal::Backend> DestroyedResources<B> {
self.referenced.push((resource_id, life_guard.ref_count.clone()));
}
fn triage_referenced(
&mut self,
buffer_guard: &mut ItemsGuard<resource::Buffer<B>>,
texture_guard: &mut ItemsGuard<resource::Texture<B>>,
) {
fn triage_referenced<Gb, Gt>(
&mut self, buffer_guard: &mut Gb, texture_guard: &mut Gt,
) where
Gb: Items<resource::Buffer<B>>,
Gt: Items<resource::Texture<B>>,
{
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 })
}

View File

@ -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))
}

View File

@ -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<T> Sync for Stored<T> {}
//#[cfg(not(feature = "remote"))]
//unsafe impl<T> Send for Stored<T> {}
type SubmissionIndex = usize;
#[derive(Debug)]

View File

@ -3,22 +3,29 @@ use std::os::raw::c_void;
pub type Id = *mut c_void;
pub type ItemsGuard<'a, T> = Items<T>;
pub struct Items<T> {
marker: PhantomData<T>,
}
impl<T> Default for Items<T> {
fn default() -> Self {
Items {
marker: PhantomData,
}
}
}
impl<T> super::Items<T> for Items<T> {
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<T> {
fn get(&self, id: Id) -> &T {
unsafe { (id as *mut T).as_ref() }.unwrap()
}
fn get_mut(&mut self, id: Id) -> super::ItemMut<T> {
fn get_mut(&mut self, id: Id) -> &mut T {
unsafe { (id as *mut T).as_mut() }.unwrap()
}
@ -26,23 +33,3 @@ impl<T> super::Items<T> for Items<T> {
unsafe { *Box::from_raw(id as *mut T) }
}
}
pub struct Registry<T> {
marker: PhantomData<T>,
}
impl<T> Default for Registry<T> {
fn default() -> Self {
Registry {
marker: PhantomData,
}
}
}
impl<T> super::Registry<T> for Registry<T> {
fn lock(&self) -> ItemsGuard<T> {
Items {
marker: PhantomData,
}
}
}

View File

@ -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<T>: Default {
fn lock(&self) -> ItemsGuard<T>;
}
#[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<T> {
pub trait Items<T>: Default {
fn register(&mut self, handle: T) -> Id;
fn get(&self, id: Id) -> Item<T>;
fn get_mut(&mut self, id: Id) -> ItemMut<T>;
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<T> = Arc<RwLock<ConcreteItems<T>>>;
#[derive(Default)]
pub struct Hub {
pub(crate) instances: ConcreteRegistry<InstanceHandle>,

View File

@ -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<T>>;
pub struct Items<T> {
next_id: Id,
next_id: u32,
tracked: FastHashMap<Id, T>,
free: Vec<Id>,
}
impl<T> Items<T> {
fn new() -> Self {
impl<T> Default for Items<T> {
fn default() -> Self {
Items {
next_id: 0,
tracked: FastHashMap::default(),
@ -35,11 +32,11 @@ impl<T> super::Items<T> for Items<T> {
id
}
fn get(&self, id: Id) -> super::Item<T> {
fn get(&self, id: Id) -> & T {
self.tracked.get(&id).unwrap()
}
fn get_mut(&mut self, id: Id) -> super::ItemMut<T> {
fn get_mut(&mut self, id: Id) -> &mut T {
self.tracked.get_mut(&id).unwrap()
}
@ -48,21 +45,3 @@ impl<T> super::Items<T> for Items<T> {
self.tracked.remove(&id).unwrap()
}
}
pub struct Registry<T> {
items: Arc<Mutex<Items<T>>>,
}
impl<T> Default for Registry<T> {
fn default() -> Self {
Registry {
items: Arc::new(Mutex::new(Items::new())),
}
}
}
impl<T> super::Registry<T> for Registry<T> {
fn lock(&self) -> ItemsGuard<T> {
self.items.lock()
}
}

View File

@ -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;

View File

@ -8,6 +8,7 @@ use std::ops::{BitOr, Range};
#[derive(Clone, Debug, PartialEq)]
#[allow(unused)]
pub enum Tracktion<T> {
Init,
Keep,