Move mutex inside registry

This commit is contained in:
Joshua Groves 2018-09-24 12:39:49 -06:00
parent 7d35607ec1
commit 7e6765108b
4 changed files with 56 additions and 52 deletions

View File

@ -2,12 +2,20 @@ extern crate cbindgen;
use std::path::PathBuf; use std::path::PathBuf;
const HEADER: &str = "
#ifdef WGPU_REMOTE
typedef uint32_t WGPUId;
#else
typedef void *WGPUId;
#endif
";
fn main() { fn main() {
let mut crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let mut crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
crate_dir.push("../wgpu-native"); crate_dir.push("../wgpu-native");
let config = cbindgen::Config { let config = cbindgen::Config {
header: Some(String::from("#ifdef WGPU_REMOTE\n typedef uint32_t WGPUId;\n#else\n typedef void *WGPUId;\n#endif")), header: Some(String::from(HEADER.trim())),
enumeration: cbindgen::EnumConfig { enumeration: cbindgen::EnumConfig {
prefix_with_name: true, prefix_with_name: true,
..Default::default() ..Default::default()
@ -15,7 +23,7 @@ fn main() {
export: cbindgen::ExportConfig { export: cbindgen::ExportConfig {
prefix: Some(String::from("WGPU")), prefix: Some(String::from("WGPU")),
exclude: vec![ exclude: vec![
// We manually define `Id` is with an `#ifdef`, so exclude it here // We manually define `Id` is within the header, so exclude it here
String::from("Id"), String::from("Id"),
], ],
..Default::default() ..Default::default()

View File

@ -36,15 +36,11 @@ pub extern "C" fn wgpu_device_create_shader_module(
device_id: DeviceId, device_id: DeviceId,
desc: pipeline::ShaderModuleDescriptor, desc: pipeline::ShaderModuleDescriptor,
) -> ShaderModuleId { ) -> ShaderModuleId {
let device_registry = registry::DEVICE_REGISTRY.lock().unwrap(); let device = registry::DEVICE_REGISTRY.get(device_id).unwrap();
let device = device_registry.get(device_id).unwrap();
let shader = device let shader = device
.device .device
.create_shader_module(unsafe { .create_shader_module(unsafe {
::std::slice::from_raw_parts(desc.code.bytes, desc.code.length) ::std::slice::from_raw_parts(desc.code.bytes, desc.code.length)
}).unwrap(); }).unwrap();
registry::SHADER_MODULE_REGISTRY registry::SHADER_MODULE_REGISTRY.register(ShaderModule { raw: shader })
.lock()
.unwrap()
.register(ShaderModule { raw: shader })
} }

View File

@ -33,9 +33,8 @@ pub extern "C" fn wgpu_create_instance() -> InstanceId {
feature = "gfx-backend-metal" feature = "gfx-backend-metal"
))] ))]
{ {
let mut registry = registry::INSTANCE_REGISTRY.lock().unwrap();
let inst = ::back::Instance::create("wgpu", 1); let inst = ::back::Instance::create("wgpu", 1);
registry.register(inst) registry::INSTANCE_REGISTRY.register(inst)
} }
#[cfg(not(any( #[cfg(not(any(
feature = "gfx-backend-vulkan", feature = "gfx-backend-vulkan",
@ -52,8 +51,7 @@ pub extern "C" fn wgpu_instance_get_adapter(
instance_id: InstanceId, instance_id: InstanceId,
desc: AdapterDescriptor, desc: AdapterDescriptor,
) -> AdapterId { ) -> AdapterId {
let instance_registry = registry::INSTANCE_REGISTRY.lock().unwrap(); let instance = registry::INSTANCE_REGISTRY.get(instance_id).unwrap();
let instance = instance_registry.get(instance_id).unwrap();
let (mut low, mut high, mut other) = (None, None, None); let (mut low, mut high, mut other) = (None, None, None);
for adapter in instance.enumerate_adapters() { for adapter in instance.enumerate_adapters() {
match adapter.info.device_type { match adapter.info.device_type {
@ -67,10 +65,7 @@ pub extern "C" fn wgpu_instance_get_adapter(
PowerPreference::LowPower => low.or(high), PowerPreference::LowPower => low.or(high),
PowerPreference::HighPerformance | PowerPreference::Default => high.or(low), PowerPreference::HighPerformance | PowerPreference::Default => high.or(low),
}; };
registry::ADAPTER_REGISTRY registry::ADAPTER_REGISTRY.register(some.or(other).unwrap())
.lock()
.unwrap()
.register(some.or(other).unwrap())
} }
#[no_mangle] #[no_mangle]
@ -78,12 +73,8 @@ pub extern "C" fn wgpu_adapter_create_device(
adapter_id: AdapterId, adapter_id: AdapterId,
desc: DeviceDescriptor, desc: DeviceDescriptor,
) -> DeviceId { ) -> DeviceId {
let mut adapter_registry = registry::ADAPTER_REGISTRY.lock().unwrap(); let adapter = registry::ADAPTER_REGISTRY.get_mut(adapter_id).unwrap();
let adapter = adapter_registry.get_mut(adapter_id).unwrap();
let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap(); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap();
let mem_props = adapter.physical_device.memory_properties(); let mem_props = adapter.physical_device.memory_properties();
registry::DEVICE_REGISTRY registry::DEVICE_REGISTRY.register(Device::new(device, queue_group, mem_props))
.lock()
.unwrap()
.register(Device::new(device, queue_group, mem_props))
} }

View File

@ -13,9 +13,9 @@ pub(crate) type Id = u32;
pub(crate) trait Registry<T> { pub(crate) trait Registry<T> {
fn new() -> Self; fn new() -> Self;
fn register(&mut self, handle: T) -> Id; fn register(&self, handle: T) -> Id;
fn get(&self, id: Id) -> Option<&T>; fn get(&self, id: Id) -> Option<&T>;
fn get_mut(&mut self, id: Id) -> Option<&mut T>; fn get_mut(&self, id: Id) -> Option<&mut T>;
} }
#[cfg(not(feature = "remote"))] #[cfg(not(feature = "remote"))]
@ -31,7 +31,7 @@ impl<T> Registry<T> for LocalRegistry<T> {
} }
} }
fn register(&mut self, handle: T) -> Id { fn register(&self, handle: T) -> Id {
::std::boxed::Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void ::std::boxed::Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void
} }
@ -39,62 +39,71 @@ impl<T> Registry<T> for LocalRegistry<T> {
unsafe { (id as *const T).as_ref() } unsafe { (id as *const T).as_ref() }
} }
fn get_mut(&mut self, id: Id) -> Option<&mut T> { fn get_mut(&self, id: Id) -> Option<&mut T> {
unsafe { (id as *mut T).as_mut() } unsafe { (id as *mut T).as_mut() }
} }
} }
#[cfg(feature = "remote")] #[cfg(feature = "remote")]
pub(crate) struct RemoteRegistry<T> { struct Registrations<T> {
next_id: Id, next_id: Id,
tracked: FastHashMap<Id, T>, tracked: FastHashMap<Id, T>,
} }
#[cfg(feature = "remote")]
impl<T> Registrations<T> {
fn new() -> Self {
Registrations {
next_id: 0,
tracked: FastHashMap::default(),
}
}
}
#[cfg(feature = "remote")]
pub(crate) struct RemoteRegistry<T> {
registrations: Arc<Mutex<Registrations<T>>>,
}
#[cfg(feature = "remote")] #[cfg(feature = "remote")]
impl<T> Registry<T> for RemoteRegistry<T> { impl<T> Registry<T> for RemoteRegistry<T> {
fn new() -> Self { fn new() -> Self {
RemoteRegistry { RemoteRegistry {
next_id: 0, registrations: Arc::new(Mutex::new(Registrations::new())),
tracked: FastHashMap::default(),
} }
} }
fn register(&mut self, handle: T) -> Id { fn register(&self, handle: T) -> Id {
let id = self.next_id; let mut registrations = self.registrations.lock().unwrap();
self.tracked.insert(id, handle); let id = registrations.next_id;
self.next_id += 1; registrations.tracked.insert(id, handle);
registrations.next_id += 1;
id id
} }
fn get(&self, id: Id) -> Option<&T> { fn get(&self, id: Id) -> Option<&T> {
self.tracked.get(&id) let registrations = self.registrations.lock().unwrap();
registrations.tracked.get(&id)
} }
fn get_mut(&mut self, id: Id) -> Option<&mut T> { fn get_mut(&self, id: Id) -> Option<&mut T> {
self.tracked.get_mut(&id) let registrations = self.registrations.lock().unwrap();
registrations.tracked.get_mut(&id)
} }
} }
#[cfg(not(feature = "remote"))] #[cfg(not(feature = "remote"))]
lazy_static! { lazy_static! {
pub(crate) static ref ADAPTER_REGISTRY: Mutex<LocalRegistry<AdapterHandle>> = pub(crate) static ref ADAPTER_REGISTRY: LocalRegistry<AdapterHandle> = LocalRegistry::new();
Mutex::new(LocalRegistry::new()); pub(crate) static ref DEVICE_REGISTRY: LocalRegistry<DeviceHandle> = LocalRegistry::new();
pub(crate) static ref DEVICE_REGISTRY: Mutex<LocalRegistry<DeviceHandle>> = pub(crate) static ref INSTANCE_REGISTRY: LocalRegistry<InstanceHandle> = LocalRegistry::new();
Mutex::new(LocalRegistry::new()); pub(crate) static ref SHADER_MODULE_REGISTRY: LocalRegistry<ShaderModuleHandle> = LocalRegistry::new();
pub(crate) static ref INSTANCE_REGISTRY: Mutex<LocalRegistry<InstanceHandle>> =
Mutex::new(LocalRegistry::new());
pub(crate) static ref SHADER_MODULE_REGISTRY: Mutex<LocalRegistry<ShaderModuleHandle>> =
Mutex::new(LocalRegistry::new());
} }
#[cfg(feature = "remote")] #[cfg(feature = "remote")]
lazy_static! { lazy_static! {
pub(crate) static ref ADAPTER_REGISTRY: Arc<Mutex<RemoteRegistry<AdapterHandle>>> = pub(crate) static ref ADAPTER_REGISTRY: RemoteRegistry<AdapterHandle> = RemoteRegistry::new();
Arc::new(Mutex::new(RemoteRegistry::new())); pub(crate) static ref DEVICE_REGISTRY: RemoteRegistry<DeviceHandle> = RemoteRegistry::new();
pub(crate) static ref DEVICE_REGISTRY: Arc<Mutex<RemoteRegistry<DeviceHandle>>> = pub(crate) static ref INSTANCE_REGISTRY: RemoteRegistry<InstanceHandle> = RemoteRegistry::new();
Arc::new(Mutex::new(RemoteRegistry::new())); pub(crate) static ref SHADER_MODULE_REGISTRY: RemoteRegistry<ShaderModuleHandle> = RemoteRegistry::new();
pub(crate) static ref INSTANCE_REGISTRY: Arc<Mutex<RemoteRegistry<InstanceHandle>>> =
Arc::new(Mutex::new(RemoteRegistry::new()));
pub(crate) static ref SHADER_MODULE_REGISTRY: Arc<Mutex<RemoteRegistry<ShaderModuleHandle>>> =
Arc::new(Mutex::new(RemoteRegistry::new()));
} }