Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lock token lifetimes #403

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ impl<F> Global<F> {
) {
let hub = B::hub(self);
let mut token = Token::root();
let (mut pass_guard, mut token) = hub.compute_passes.write(&mut token);
let (buffer_guard, _) = hub.buffers.read(&mut token);
let (mut pass_guard, _) = hub.compute_passes.write(&mut token);

let pass = &mut pass_guard[pass_id];

let (src_buffer, src_pending) = pass.trackers.buffers.use_replace(
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/command/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl<F: IdentityFilter<RenderPassId>> Global<F> {
) {
let hub = B::hub(self);
let mut token = Token::root();
let (mut pass_guard, _) = hub.render_passes.write(&mut token);
let (mut pass_guard, mut token) = hub.render_passes.write(&mut token);
let (buffer_guard, _) = hub.buffers.read(&mut token);
let pass = &mut pass_guard[pass_id];
pass.is_ready().unwrap();
Expand Down Expand Up @@ -447,7 +447,7 @@ impl<F: IdentityFilter<RenderPassId>> Global<F> {
) {
let hub = B::hub(self);
let mut token = Token::root();
let (mut pass_guard, _) = hub.render_passes.write(&mut token);
let (mut pass_guard, mut token) = hub.render_passes.write(&mut token);
let (buffer_guard, _) = hub.buffers.read(&mut token);
let pass = &mut pass_guard[pass_id];
pass.is_ready().unwrap();
Expand Down
14 changes: 8 additions & 6 deletions wgpu-core/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ impl<F: IdentityFilter<BufferId>> Global<F> {
let hub = B::hub(self);
let mut token = Token::root();

let (device_guard, _) = hub.devices.read(&mut token);
let (device_guard, mut token) = hub.devices.read(&mut token);
let device = &device_guard[device_id];
let buffer = device.create_buffer(device_id, desc);
let ref_count = buffer.life_guard.ref_count.clone();
Expand Down Expand Up @@ -839,7 +839,7 @@ impl<F: IdentityFilter<BufferId>> Global<F> {
let mut desc = desc.clone();
desc.usage |= resource::BufferUsage::MAP_WRITE;

let (device_guard, _) = hub.devices.read(&mut token);
let (device_guard, mut token) = hub.devices.read(&mut token);
let device = &device_guard[device_id];
let mut buffer = device.create_buffer(device_id, &desc);
let ref_count = buffer.life_guard.ref_count.clone();
Expand Down Expand Up @@ -960,7 +960,7 @@ impl<F: IdentityFilter<TextureId>> Global<F> {
let hub = B::hub(self);
let mut token = Token::root();

let (device_guard, _) = hub.devices.read(&mut token);
let (device_guard, mut token) = hub.devices.read(&mut token);
let device = &device_guard[device_id];
let texture = device.create_texture(device_id, desc);
let range = texture.full_range.clone();
Expand Down Expand Up @@ -1267,7 +1267,7 @@ impl<F: IdentityFilter<BindGroupId>> Global<F> {

let (device_guard, mut token) = hub.devices.read(&mut token);
let device = &device_guard[device_id];
let (bind_group_layout_guard, _) = hub.bind_group_layouts.read(&mut token);
let (bind_group_layout_guard, mut token) = hub.bind_group_layouts.read(&mut token);
let bind_group_layout = &bind_group_layout_guard[desc.layout];
let bindings =
unsafe { slice::from_raw_parts(desc.bindings, desc.bindings_length as usize) };
Expand Down Expand Up @@ -2074,8 +2074,9 @@ impl<F: IdentityFilter<SwapChainId>> Global<F> {
impl<F: AllIdentityFilter> Global<F> {
pub fn device_poll<B: GfxBackend>(&self, device_id: DeviceId, force_wait: bool) {
let hub = B::hub(self);
let mut token = Token::root();
let callbacks = {
let (device_guard, mut token) = hub.devices.read(&mut Token::root());
let (device_guard, mut token) = hub.devices.read(&mut token);
device_guard[device_id].maintain(self, force_wait, &mut token)
};
Device::<B>::fire_map_callbacks(callbacks);
Expand All @@ -2085,7 +2086,8 @@ impl<F: AllIdentityFilter> Global<F> {
impl<F: AllIdentityFilter + IdentityFilter<DeviceId>> Global<F> {
pub fn device_destroy<B: GfxBackend>(&self, device_id: DeviceId) {
let hub = B::hub(self);
let (device, mut token) = hub.devices.unregister(device_id, &mut Token::root());
let mut token = Token::root();
let (device, mut token) = hub.devices.unregister(device_id, &mut token);
device.maintain(self, true, &mut token);
device.com_allocator.destroy(&device.raw);
}
Expand Down
23 changes: 14 additions & 9 deletions wgpu-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl<B: hal::Backend> Access<BindGroupLayout<B>> for Root {}
impl<B: hal::Backend> Access<BindGroupLayout<B>> for Device<B> {}
impl<B: hal::Backend> Access<BindGroup<B>> for Root {}
impl<B: hal::Backend> Access<BindGroup<B>> for Device<B> {}
impl<B: hal::Backend> Access<BindGroup<B>> for BindGroupLayout<B> {}
impl<B: hal::Backend> Access<BindGroup<B>> for PipelineLayout<B> {}
impl<B: hal::Backend> Access<BindGroup<B>> for CommandBuffer<B> {}
impl<B: hal::Backend> Access<CommandBuffer<B>> for Root {}
Expand Down Expand Up @@ -319,17 +320,17 @@ impl<T, I: TypedId + Copy, F> Registry<T, I, F> {
assert!(old.is_none());
}

pub fn read<A: Access<T>>(
&self,
_token: &mut Token<A>,
) -> (RwLockReadGuard<Storage<T, I>>, Token<T>) {
pub fn read<'a, A: Access<T>>(
&'a self,
_token: &'a mut Token<A>,
) -> (RwLockReadGuard<'a, Storage<T, I>>, Token<'a, T>) {
(self.data.read(), Token::new())
}

pub fn write<A: Access<T>>(
&self,
_token: &mut Token<A>,
) -> (RwLockWriteGuard<Storage<T, I>>, Token<T>) {
pub fn write<'a, A: Access<T>>(
&'a self,
_token: &'a mut Token<A>,
) -> (RwLockWriteGuard<'a, Storage<T, I>>, Token<'a, T>) {
(self.data.write(), Token::new())
}
}
Expand All @@ -346,7 +347,11 @@ impl<T, I: TypedId + Copy, F: IdentityFilter<I>> Registry<T, I, F> {
id
}

pub fn unregister<A: Access<T>>(&self, id: I, _token: &mut Token<A>) -> (T, Token<T>) {
pub fn unregister<'a, A: Access<T>>(
&self,
id: I,
_token: &'a mut Token<A>,
) -> (T, Token<'a, T>) {
let value = self.data.write().remove(id).unwrap();
//Note: careful about the order here!
self.identity.free(id);
Expand Down