Avoid AtomicU64 for it not being portable

This commit is contained in:
Dzmitry Malyshau 2021-09-10 10:13:40 -04:00 committed by Dzmitry Malyshau
parent cc91b38301
commit 37288a657f
3 changed files with 26 additions and 25 deletions

View File

@ -17,7 +17,7 @@ use hal::Device as _;
use parking_lot::Mutex; use parking_lot::Mutex;
use thiserror::Error; use thiserror::Error;
use std::{mem, sync::atomic::Ordering}; use std::mem;
/// A struct that keeps lists of resources that are no longer needed by the user. /// A struct that keeps lists of resources that are no longer needed by the user.
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -367,7 +367,7 @@ impl<A: HalApi> LifetimeTracker<A> {
if let Some(res) = hub.bind_groups.unregister_locked(id.0, &mut *guard) { if let Some(res) = hub.bind_groups.unregister_locked(id.0, &mut *guard) {
self.suspected_resources.add_trackers(&res.used); self.suspected_resources.add_trackers(&res.used);
let submit_index = res.life_guard.submission_index.load(Ordering::Acquire); let submit_index = res.life_guard.life_count();
self.active self.active
.iter_mut() .iter_mut()
.find(|a| a.index == submit_index) .find(|a| a.index == submit_index)
@ -393,7 +393,7 @@ impl<A: HalApi> LifetimeTracker<A> {
if let Some(res) = hub.texture_views.unregister_locked(id.0, &mut *guard) { if let Some(res) = hub.texture_views.unregister_locked(id.0, &mut *guard) {
self.suspected_resources.textures.push(res.parent_id.value); self.suspected_resources.textures.push(res.parent_id.value);
let submit_index = res.life_guard.submission_index.load(Ordering::Acquire); let submit_index = res.life_guard.life_count();
self.active self.active
.iter_mut() .iter_mut()
.find(|a| a.index == submit_index) .find(|a| a.index == submit_index)
@ -418,7 +418,7 @@ impl<A: HalApi> LifetimeTracker<A> {
} }
if let Some(res) = hub.textures.unregister_locked(id.0, &mut *guard) { if let Some(res) = hub.textures.unregister_locked(id.0, &mut *guard) {
let submit_index = res.life_guard.submission_index.load(Ordering::Acquire); let submit_index = res.life_guard.life_count();
let raw = match res.inner { let raw = match res.inner {
resource::TextureInner::Native { raw: Some(raw) } => raw, resource::TextureInner::Native { raw: Some(raw) } => raw,
_ => continue, _ => continue,
@ -446,7 +446,7 @@ impl<A: HalApi> LifetimeTracker<A> {
} }
if let Some(res) = hub.samplers.unregister_locked(id.0, &mut *guard) { if let Some(res) = hub.samplers.unregister_locked(id.0, &mut *guard) {
let submit_index = res.life_guard.submission_index.load(Ordering::Acquire); let submit_index = res.life_guard.life_count();
self.active self.active
.iter_mut() .iter_mut()
.find(|a| a.index == submit_index) .find(|a| a.index == submit_index)
@ -471,7 +471,7 @@ impl<A: HalApi> LifetimeTracker<A> {
log::debug!("Buffer {:?} is detached", id); log::debug!("Buffer {:?} is detached", id);
if let Some(res) = hub.buffers.unregister_locked(id.0, &mut *guard) { if let Some(res) = hub.buffers.unregister_locked(id.0, &mut *guard) {
let submit_index = res.life_guard.submission_index.load(Ordering::Acquire); let submit_index = res.life_guard.life_count();
if let resource::BufferMapState::Init { stage_buffer, .. } = res.map_state { if let resource::BufferMapState::Init { stage_buffer, .. } = res.map_state {
self.free_resources.buffers.push(stage_buffer); self.free_resources.buffers.push(stage_buffer);
} }
@ -498,7 +498,7 @@ impl<A: HalApi> LifetimeTracker<A> {
} }
if let Some(res) = hub.compute_pipelines.unregister_locked(id.0, &mut *guard) { if let Some(res) = hub.compute_pipelines.unregister_locked(id.0, &mut *guard) {
let submit_index = res.life_guard.submission_index.load(Ordering::Acquire); let submit_index = res.life_guard.life_count();
self.active self.active
.iter_mut() .iter_mut()
.find(|a| a.index == submit_index) .find(|a| a.index == submit_index)
@ -522,7 +522,7 @@ impl<A: HalApi> LifetimeTracker<A> {
} }
if let Some(res) = hub.render_pipelines.unregister_locked(id.0, &mut *guard) { if let Some(res) = hub.render_pipelines.unregister_locked(id.0, &mut *guard) {
let submit_index = res.life_guard.submission_index.load(Ordering::Acquire); let submit_index = res.life_guard.life_count();
self.active self.active
.iter_mut() .iter_mut()
.find(|a| a.index == submit_index) .find(|a| a.index == submit_index)
@ -588,7 +588,7 @@ impl<A: HalApi> LifetimeTracker<A> {
// #[cfg(feature = "trace")] // #[cfg(feature = "trace")]
// trace.map(|t| t.lock().add(trace::Action::DestroyComputePipeline(id.0))); // trace.map(|t| t.lock().add(trace::Action::DestroyComputePipeline(id.0)));
if let Some(res) = hub.query_sets.unregister_locked(id.0, &mut *guard) { if let Some(res) = hub.query_sets.unregister_locked(id.0, &mut *guard) {
let submit_index = res.life_guard.submission_index.load(Ordering::Acquire); let submit_index = res.life_guard.life_count();
self.active self.active
.iter_mut() .iter_mut()
.find(|a| a.index == submit_index) .find(|a| a.index == submit_index)
@ -615,7 +615,7 @@ impl<A: HalApi> LifetimeTracker<A> {
let resource_id = stored.value; let resource_id = stored.value;
let buf = &buffer_guard[resource_id]; let buf = &buffer_guard[resource_id];
let submit_index = buf.life_guard.submission_index.load(Ordering::Acquire); let submit_index = buf.life_guard.life_count();
log::trace!( log::trace!(
"Mapping of {:?} at submission {:?} gets assigned to active {:?}", "Mapping of {:?} at submission {:?} gets assigned to active {:?}",
resource_id, resource_id,

View File

@ -19,7 +19,7 @@ use smallvec::SmallVec;
use thiserror::Error; use thiserror::Error;
use wgt::{BufferAddress, TextureFormat, TextureViewDimension}; use wgt::{BufferAddress, TextureFormat, TextureViewDimension};
use std::{borrow::Cow, iter, marker::PhantomData, mem, ops::Range, ptr, sync::atomic::Ordering}; use std::{borrow::Cow, iter, marker::PhantomData, mem, ops::Range, ptr};
mod life; mod life;
pub mod queue; pub mod queue;
@ -2812,7 +2812,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let last_submission = { let last_submission = {
let (buffer_guard, _) = hub.buffers.write(&mut token); let (buffer_guard, _) = hub.buffers.write(&mut token);
match buffer_guard.get(buffer_id) { match buffer_guard.get(buffer_id) {
Ok(buffer) => buffer.life_guard.submission_index.load(Ordering::Acquire), Ok(buffer) => buffer.life_guard.life_count(),
Err(_) => return Ok(()), Err(_) => return Ok(()),
} }
}; };
@ -2964,7 +2964,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
if device.pending_writes.dst_buffers.contains(&buffer_id) { if device.pending_writes.dst_buffers.contains(&buffer_id) {
device.pending_writes.temp_resources.push(temp); device.pending_writes.temp_resources.push(temp);
} else { } else {
let last_submit_index = buffer.life_guard.submission_index.load(Ordering::Acquire); let last_submit_index = buffer.life_guard.life_count();
drop(buffer_guard); drop(buffer_guard);
device device
.lock_life(&mut token) .lock_life(&mut token)
@ -2986,8 +2986,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
match buffer_guard.get_mut(buffer_id) { match buffer_guard.get_mut(buffer_id) {
Ok(buffer) => { Ok(buffer) => {
let ref_count = buffer.life_guard.ref_count.take().unwrap(); let ref_count = buffer.life_guard.ref_count.take().unwrap();
let last_submit_index = let last_submit_index = buffer.life_guard.life_count();
buffer.life_guard.submission_index.load(Ordering::Acquire);
(ref_count, last_submit_index, buffer.device_id.value) (ref_count, last_submit_index, buffer.device_id.value)
} }
Err(InvalidId) => { Err(InvalidId) => {
@ -3170,8 +3169,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
if device.pending_writes.dst_textures.contains(&texture_id) { if device.pending_writes.dst_textures.contains(&texture_id) {
device.pending_writes.temp_resources.push(temp); device.pending_writes.temp_resources.push(temp);
} else { } else {
let last_submit_index = let last_submit_index = texture.life_guard.life_count();
texture.life_guard.submission_index.load(Ordering::Acquire);
drop(texture_guard); drop(texture_guard);
device device
.lock_life(&mut token) .lock_life(&mut token)
@ -3195,8 +3193,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
match texture_guard.get_mut(texture_id) { match texture_guard.get_mut(texture_id) {
Ok(texture) => { Ok(texture) => {
let ref_count = texture.life_guard.ref_count.take().unwrap(); let ref_count = texture.life_guard.ref_count.take().unwrap();
let last_submit_index = let last_submit_index = texture.life_guard.life_count();
texture.life_guard.submission_index.load(Ordering::Acquire);
(ref_count, last_submit_index, texture.device_id.value) (ref_count, last_submit_index, texture.device_id.value)
} }
Err(InvalidId) => { Err(InvalidId) => {
@ -3303,8 +3300,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
match texture_view_guard.get_mut(texture_view_id) { match texture_view_guard.get_mut(texture_view_id) {
Ok(view) => { Ok(view) => {
let _ref_count = view.life_guard.ref_count.take(); let _ref_count = view.life_guard.ref_count.take();
let last_submit_index = let last_submit_index = view.life_guard.life_count();
view.life_guard.submission_index.load(Ordering::Acquire);
let device_id = texture_guard[view.parent_id.value].device_id.value; let device_id = texture_guard[view.parent_id.value].device_id.value;
(last_submit_index, device_id) (last_submit_index, device_id)
} }

View File

@ -47,7 +47,7 @@ mod validation;
pub use hal::api; pub use hal::api;
use atomic::{AtomicU64, AtomicUsize, Ordering}; use atomic::{AtomicUsize, Ordering};
use std::{borrow::Cow, os::raw::c_char, ptr, sync::atomic}; use std::{borrow::Cow, os::raw::c_char, ptr, sync::atomic};
@ -147,7 +147,7 @@ impl Drop for MultiRefCount {
#[derive(Debug)] #[derive(Debug)]
pub struct LifeGuard { pub struct LifeGuard {
ref_count: Option<RefCount>, ref_count: Option<RefCount>,
submission_index: AtomicU64, submission_index: AtomicUsize,
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
pub(crate) label: String, pub(crate) label: String,
} }
@ -158,7 +158,7 @@ impl LifeGuard {
let bx = Box::new(AtomicUsize::new(1)); let bx = Box::new(AtomicUsize::new(1));
Self { Self {
ref_count: ptr::NonNull::new(Box::into_raw(bx)).map(RefCount), ref_count: ptr::NonNull::new(Box::into_raw(bx)).map(RefCount),
submission_index: AtomicU64::new(0), submission_index: AtomicUsize::new(0),
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
label: label.to_string(), label: label.to_string(),
} }
@ -170,9 +170,14 @@ impl LifeGuard {
/// Returns `true` if the resource is still needed by the user. /// Returns `true` if the resource is still needed by the user.
fn use_at(&self, submit_index: SubmissionIndex) -> bool { fn use_at(&self, submit_index: SubmissionIndex) -> bool {
self.submission_index.store(submit_index, Ordering::Release); self.submission_index
.store(submit_index as _, Ordering::Release);
self.ref_count.is_some() self.ref_count.is_some()
} }
fn life_count(&self) -> SubmissionIndex {
self.submission_index.load(Ordering::Acquire) as _
}
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]