Skip to content

Commit

Permalink
Let the "strict_asserts" feature enable Token::root assertions.
Browse files Browse the repository at this point in the history
Perform run-time checks that only one root `Token` exists per thread
when the `"strict_asserts"` feature is enabled, not only in debug
builds.
  • Loading branch information
jimblandy committed Oct 18, 2023
1 parent a842019 commit 16c4137
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions wgpu-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ use crate::{
storage::{Element, Storage, StorageReport},
};

#[cfg(debug_assertions)]
use wgt::{strict_assert_eq, strict_assert_ne};

#[cfg(any(debug_assertions, feature = "strict_asserts"))]
use std::cell::Cell;
use std::{fmt::Debug, marker::PhantomData};

Expand Down Expand Up @@ -281,7 +283,7 @@ impl<A: HalApi> Access<QuerySet<A>> for RenderPipeline<A> {}
impl<A: HalApi> Access<QuerySet<A>> for ComputePipeline<A> {}
impl<A: HalApi> Access<QuerySet<A>> for Sampler<A> {}

#[cfg(debug_assertions)]
#[cfg(any(debug_assertions, feature = "strict_asserts"))]
thread_local! {
/// Per-thread state checking `Token<Root>` creation in debug builds.
///
Expand Down Expand Up @@ -320,10 +322,10 @@ impl<'a, T> Token<'a, T> {
///
/// This should only be used by `Registry` locking methods.
pub(crate) fn new() -> Self {
#[cfg(debug_assertions)]
#[cfg(any(debug_assertions, feature = "strict_asserts"))]
ACTIVE_TOKEN.with(|active| {
let old = active.get();
assert_ne!(old, 0, "Root token was dropped");
strict_assert_ne!(old, 0, "Root token was dropped");
active.set(old + 1);
});
Self { level: PhantomData }
Expand All @@ -336,9 +338,9 @@ impl Token<'static, Root> {
/// Debug builds check dynamically that each thread has at most
/// one root token at a time.
pub fn root() -> Self {
#[cfg(debug_assertions)]
#[cfg(any(debug_assertions, feature = "strict_asserts"))]
ACTIVE_TOKEN.with(|active| {
assert_eq!(0, active.replace(1), "Root token is already active");
strict_assert_eq!(0, active.replace(1), "Root token is already active");
});

Self { level: PhantomData }
Expand All @@ -347,7 +349,7 @@ impl Token<'static, Root> {

impl<'a, T> Drop for Token<'a, T> {
fn drop(&mut self) {
#[cfg(debug_assertions)]
#[cfg(any(debug_assertions, feature = "strict_asserts"))]
ACTIVE_TOKEN.with(|active| {
let old = active.get();
active.set(old - 1);
Expand Down

0 comments on commit 16c4137

Please sign in to comment.