From 34f7efb7d4f1cbb7ef0dfad083cc7000001f4fe8 Mon Sep 17 00:00:00 2001 From: Zac Harrold Date: Thu, 6 Aug 2026 10:38:26 +1000 Subject: [PATCH] [wgpu-core]: Switch to `wgpu-sync` for locking primitives --- Cargo.lock | 2 +- wgpu-core/Cargo.toml | 6 +++--- wgpu-core/src/device/resource.rs | 6 ++---- wgpu-core/src/lock/mod.rs | 4 ++-- wgpu-core/src/lock/observing.rs | 24 ++++++++++++------------ wgpu-core/src/lock/rank.rs | 4 ++-- wgpu-core/src/lock/ranked.rs | 24 ++++++++++++------------ wgpu-core/src/lock/vanilla.rs | 30 +++++++++++++++--------------- wgpu-core/src/storage.rs | 2 +- 9 files changed, 50 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 651f2cd23..44aa1d4b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4795,7 +4795,6 @@ dependencies = [ "naga", "naga-types", "once_cell", - "parking_lot", "portable-atomic", "profiling", "proptest", @@ -4812,6 +4811,7 @@ dependencies = [ "wgpu-core-deps-windows", "wgpu-hal", "wgpu-naga-bridge", + "wgpu-sync", "wgpu-types", ] diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index 249d41b98..76dd994ca 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -82,7 +82,7 @@ serde = [ trace = ["serde", "std", "dep:ron", "naga/serialize", "wgpu-types/trace"] ## Enable API replaying -replay = ["serde", "naga/deserialize"] +replay = ["serde", "naga/deserialize", "std"] #! ### Shading Language Support # -------------------------------------------------------------------- @@ -111,7 +111,7 @@ fragile-send-sync-non-atomic-wasm = [ ## Enable certain items to be `Send` and `Sync` when they would not otherwise be. ## Also enables backtraces in some error cases when also under cfg(debug_assertions). -std = [] +std = ["wgpu-sync/std"] #! ### External libraries # -------------------------------------------------------------------- @@ -186,6 +186,7 @@ naga.workspace = true naga-types.workspace = true wgpu-naga-bridge.workspace = true wgpu-hal.workspace = true +wgpu-sync = { workspace = true, default-features = false } wgpu-types.workspace = true arrayvec.workspace = true @@ -199,7 +200,6 @@ indexmap.workspace = true log.workspace = true macro_rules_attribute = { workspace = true, optional = true } once_cell = { workspace = true, features = ["std"] } -parking_lot.workspace = true profiling = { workspace = true, default-features = false } raw-window-handle.workspace = true ron = { workspace = true, optional = true } diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index f5b229b9e..6b48fff0f 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -205,13 +205,11 @@ impl ExternalTextureParams { /// Because all operations are push/swap (no longlived lock), /// we can have mutex without lock rank -pub(crate) struct DeferredBufferMapPendingClosures( - parking_lot::Mutex>, -); +pub(crate) struct DeferredBufferMapPendingClosures(wgpu_sync::Mutex>); impl DeferredBufferMapPendingClosures { pub(crate) fn new() -> Self { - Self(parking_lot::Mutex::new(Vec::new())) + Self(wgpu_sync::Mutex::new(Vec::new())) } pub(crate) fn push(&self, closure: BufferMapPendingClosure) { diff --git a/wgpu-core/src/lock/mod.rs b/wgpu-core/src/lock/mod.rs index 2e83f5a35..dac314f35 100644 --- a/wgpu-core/src/lock/mod.rs +++ b/wgpu-core/src/lock/mod.rs @@ -27,8 +27,8 @@ //! //! Otherwise, `wgpu-core` uses the [`vanilla`] module's locks. //! -//! [`Mutex`]: parking_lot::Mutex -//! [`RwLock`]: parking_lot::RwLock +//! [`Mutex`]: wgpu_sync::Mutex +//! [`RwLock`]: wgpu_sync::RwLock //! [`SnatchLock`]: crate::snatch::SnatchLock pub mod rank; diff --git a/wgpu-core/src/lock/observing.rs b/wgpu-core/src/lock/observing.rs index dfcb39405..29f916ef9 100644 --- a/wgpu-core/src/lock/observing.rs +++ b/wgpu-core/src/lock/observing.rs @@ -42,30 +42,30 @@ pub type RankData = Option; /// A `Mutex` instrumented for lock acquisition order observation. /// -/// This is just a wrapper around a [`parking_lot::Mutex`], along with +/// This is just a wrapper around a [`wgpu_sync::Mutex`], along with /// its rank in the `wgpu_core` lock ordering. /// /// For details, see [the module documentation][self]. pub struct Mutex { - inner: parking_lot::Mutex, + inner: wgpu_sync::Mutex, rank: LockRank, } /// A guard produced by locking [`Mutex`]. /// -/// This is just a wrapper around a [`parking_lot::MutexGuard`], along +/// This is just a wrapper around a [`wgpu_sync::MutexGuard`], along /// with the state needed to track lock acquisition. /// /// For details, see [the module documentation][self]. pub struct MutexGuard<'a, T> { - inner: parking_lot::MutexGuard<'a, T>, + inner: wgpu_sync::MutexGuard<'a, T>, _state: LockStateGuard, } impl Mutex { pub fn new(rank: LockRank, value: T) -> Mutex { Mutex { - inner: parking_lot::Mutex::new(value), + inner: wgpu_sync::Mutex::new(value), rank, } } @@ -110,41 +110,41 @@ impl core::fmt::Debug for Mutex { /// An `RwLock` instrumented for lock acquisition order observation. /// -/// This is just a wrapper around a [`parking_lot::RwLock`], along with +/// This is just a wrapper around a [`wgpu_sync::RwLock`], along with /// its rank in the `wgpu_core` lock ordering. /// /// For details, see [the module documentation][self]. pub struct RwLock { - inner: parking_lot::RwLock, + inner: wgpu_sync::RwLock, rank: LockRank, } /// A read guard produced by locking [`RwLock`] for reading. /// -/// This is just a wrapper around a [`parking_lot::RwLockReadGuard`], along with +/// This is just a wrapper around a [`wgpu_sync::RwLockReadGuard`], along with /// the state needed to track lock acquisition. /// /// For details, see [the module documentation][self]. pub struct RwLockReadGuard<'a, T> { - inner: parking_lot::RwLockReadGuard<'a, T>, + inner: wgpu_sync::RwLockReadGuard<'a, T>, _state: LockStateGuard, } /// A write guard produced by locking [`RwLock`] for writing. /// -/// This is just a wrapper around a [`parking_lot::RwLockWriteGuard`], along +/// This is just a wrapper around a [`wgpu_sync::RwLockWriteGuard`], along /// with the state needed to track lock acquisition. /// /// For details, see [the module documentation][self]. pub struct RwLockWriteGuard<'a, T> { - inner: parking_lot::RwLockWriteGuard<'a, T>, + inner: wgpu_sync::RwLockWriteGuard<'a, T>, _state: LockStateGuard, } impl RwLock { pub fn new(rank: LockRank, value: T) -> RwLock { RwLock { - inner: parking_lot::RwLock::new(value), + inner: wgpu_sync::RwLock::new(value), rank, } } diff --git a/wgpu-core/src/lock/rank.rs b/wgpu-core/src/lock/rank.rs index 30094f9d4..399f85016 100644 --- a/wgpu-core/src/lock/rank.rs +++ b/wgpu-core/src/lock/rank.rs @@ -26,8 +26,8 @@ /// TODO(): Resolve invalid /// acquisitions of DEVICE_COMMAND_INDICES followed by COMMAND_BUFFER_DATA. /// -/// [`Mutex`]: parking_lot::Mutex -/// [`RwLock`]: parking_lot::RwLock +/// [`Mutex`]: wgpu_sync::Mutex +/// [`RwLock`]: wgpu_sync::RwLock /// [`SnatchLock`]: crate::snatch::SnatchLock /// [`CommandBuffer::data`]: crate::command::CommandBuffer::data #[derive(Debug, Copy, Clone)] diff --git a/wgpu-core/src/lock/ranked.rs b/wgpu-core/src/lock/ranked.rs index cda564d44..978addcb1 100644 --- a/wgpu-core/src/lock/ranked.rs +++ b/wgpu-core/src/lock/ranked.rs @@ -63,23 +63,23 @@ pub use LockState as RankData; /// A `Mutex` instrumented for deadlock prevention. /// -/// This is just a wrapper around a [`parking_lot::Mutex`], along with +/// This is just a wrapper around a [`wgpu_sync::Mutex`], along with /// its rank in the `wgpu_core` lock ordering. /// /// For details, see [the module documentation][self]. pub struct Mutex { - inner: parking_lot::Mutex, + inner: wgpu_sync::Mutex, rank: LockRank, } /// A guard produced by locking [`Mutex`]. /// -/// This is just a wrapper around a [`parking_lot::MutexGuard`], along +/// This is just a wrapper around a [`wgpu_sync::MutexGuard`], along /// with the state needed to track lock acquisition. /// /// For details, see [the module documentation][self]. pub struct MutexGuard<'a, T> { - inner: parking_lot::MutexGuard<'a, T>, + inner: wgpu_sync::MutexGuard<'a, T>, #[cfg_attr(not(miri), expect(unused))] // but `Drop` has important side effects saved: LockStateGuard, } @@ -216,7 +216,7 @@ fn release(saved: LockState) { impl Mutex { pub fn new(rank: LockRank, value: T) -> Mutex { Mutex { - inner: parking_lot::Mutex::new(value), + inner: wgpu_sync::Mutex::new(value), rank, } } @@ -261,41 +261,41 @@ impl fmt::Debug for Mutex { /// An `RwLock` instrumented for deadlock prevention. /// -/// This is just a wrapper around a [`parking_lot::RwLock`], along with +/// This is just a wrapper around a [`wgpu_sync::RwLock`], along with /// its rank in the `wgpu_core` lock ordering. /// /// For details, see [the module documentation][self]. pub struct RwLock { - inner: parking_lot::RwLock, + inner: wgpu_sync::RwLock, rank: LockRank, } /// A read guard produced by locking [`RwLock`] for reading. /// -/// This is just a wrapper around a [`parking_lot::RwLockReadGuard`], along with +/// This is just a wrapper around a [`wgpu_sync::RwLockReadGuard`], along with /// the state needed to track lock acquisition. /// /// For details, see [the module documentation][self]. pub struct RwLockReadGuard<'a, T> { - inner: parking_lot::RwLockReadGuard<'a, T>, + inner: wgpu_sync::RwLockReadGuard<'a, T>, saved: LockStateGuard, } /// A write guard produced by locking [`RwLock`] for writing. /// -/// This is just a wrapper around a [`parking_lot::RwLockWriteGuard`], along +/// This is just a wrapper around a [`wgpu_sync::RwLockWriteGuard`], along /// with the state needed to track lock acquisition. /// /// For details, see [the module documentation][self]. pub struct RwLockWriteGuard<'a, T> { - inner: parking_lot::RwLockWriteGuard<'a, T>, + inner: wgpu_sync::RwLockWriteGuard<'a, T>, saved: LockStateGuard, } impl RwLock { pub fn new(rank: LockRank, value: T) -> RwLock { RwLock { - inner: parking_lot::RwLock::new(value), + inner: wgpu_sync::RwLock::new(value), rank, } } diff --git a/wgpu-core/src/lock/vanilla.rs b/wgpu-core/src/lock/vanilla.rs index e74783ad4..bc9f08077 100644 --- a/wgpu-core/src/lock/vanilla.rs +++ b/wgpu-core/src/lock/vanilla.rs @@ -1,4 +1,4 @@ -//! Plain, uninstrumented wrappers around [`parking_lot`] lock types. +//! Plain, uninstrumented wrappers around [`wgpu_sync`] lock types. //! //! These definitions are used when no particular lock instrumentation //! Cargo feature is selected. @@ -9,9 +9,9 @@ use crate::lock::rank::LockRank; pub struct RankData; -/// A plain wrapper around [`parking_lot::Mutex`]. +/// A plain wrapper around [`wgpu_sync::Mutex`]. /// -/// This is just like [`parking_lot::Mutex`], except that our [`new`] +/// This is just like [`wgpu_sync::Mutex`], except that our [`new`] /// method takes a rank, indicating where the new mutex should sit in /// `wgpu-core`'s lock ordering. The rank is ignored. /// @@ -19,16 +19,16 @@ pub struct RankData; /// /// [`new`]: Mutex::new /// [`lock`]: crate::lock -pub struct Mutex(parking_lot::Mutex); +pub struct Mutex(wgpu_sync::Mutex); /// A guard produced by locking [`Mutex`]. /// -/// This is just a wrapper around a [`parking_lot::MutexGuard`]. -pub struct MutexGuard<'a, T>(parking_lot::MutexGuard<'a, T>); +/// This is just a wrapper around a [`wgpu_sync::MutexGuard`]. +pub struct MutexGuard<'a, T>(wgpu_sync::MutexGuard<'a, T>); impl Mutex { pub fn new(_rank: LockRank, value: T) -> Mutex { - Mutex(parking_lot::Mutex::new(value)) + Mutex(wgpu_sync::Mutex::new(value)) } pub fn lock(&self) -> MutexGuard<'_, T> { @@ -64,9 +64,9 @@ impl fmt::Debug for Mutex { } } -/// A plain wrapper around [`parking_lot::RwLock`]. +/// A plain wrapper around [`wgpu_sync::RwLock`]. /// -/// This is just like [`parking_lot::RwLock`], except that our [`new`] +/// This is just like [`wgpu_sync::RwLock`], except that our [`new`] /// method takes a rank, indicating where the new mutex should sit in /// `wgpu-core`'s lock ordering. The rank is ignored. /// @@ -74,21 +74,21 @@ impl fmt::Debug for Mutex { /// /// [`new`]: RwLock::new /// [`lock`]: crate::lock -pub struct RwLock(parking_lot::RwLock); +pub struct RwLock(wgpu_sync::RwLock); /// A read guard produced by locking [`RwLock`] as a reader. /// -/// This is just a wrapper around a [`parking_lot::RwLockReadGuard`]. -pub struct RwLockReadGuard<'a, T>(parking_lot::RwLockReadGuard<'a, T>); +/// This is just a wrapper around a [`wgpu_sync::RwLockReadGuard`]. +pub struct RwLockReadGuard<'a, T>(wgpu_sync::RwLockReadGuard<'a, T>); /// A write guard produced by locking [`RwLock`] as a writer. /// -/// This is just a wrapper around a [`parking_lot::RwLockWriteGuard`]. -pub struct RwLockWriteGuard<'a, T>(parking_lot::RwLockWriteGuard<'a, T>); +/// This is just a wrapper around a [`wgpu_sync::RwLockWriteGuard`]. +pub struct RwLockWriteGuard<'a, T>(wgpu_sync::RwLockWriteGuard<'a, T>); impl RwLock { pub fn new(_rank: LockRank, value: T) -> RwLock { - RwLock(parking_lot::RwLock::new(value)) + RwLock(wgpu_sync::RwLock::new(value)) } pub fn read(&self) -> RwLockReadGuard<'_, T> { diff --git a/wgpu-core/src/storage.rs b/wgpu-core/src/storage.rs index 29cb4d2de..8c337a7a0 100644 --- a/wgpu-core/src/storage.rs +++ b/wgpu-core/src/storage.rs @@ -2,7 +2,7 @@ use alloc::sync::Arc; use crate::id::Marker; use crate::resource::ResourceType; -use parking_lot::Mutex; +use wgpu_sync::Mutex; /// Not a public API. For use only by `player`. #[doc(hidden)]