From 7c842a3b483f8b41f08ba21584d9e8502ea26e4e Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Wed, 1 May 2024 17:39:45 -0700 Subject: [PATCH] [core] Implement `downgrade` for the `lock` module's `RwLock`s. Implement `RwLockWriteGuard::downgrade` for `lock::vanilla` and `lock::ranked`, to downgrade a write lock to a read lock. --- wgpu-core/src/lock/ranked.rs | 9 +++++++++ wgpu-core/src/lock/vanilla.rs | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/wgpu-core/src/lock/ranked.rs b/wgpu-core/src/lock/ranked.rs index 4b5871d26f..4237116c2c 100644 --- a/wgpu-core/src/lock/ranked.rs +++ b/wgpu-core/src/lock/ranked.rs @@ -281,6 +281,15 @@ impl RwLock { } } +impl<'a, T> RwLockWriteGuard<'a, T> { + pub fn downgrade(this: Self) -> RwLockReadGuard<'a, T> { + RwLockReadGuard { + inner: parking_lot::RwLockWriteGuard::downgrade(this.inner), + saved: this.saved, + } + } +} + impl std::fmt::Debug for RwLock { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.inner.fmt(f) diff --git a/wgpu-core/src/lock/vanilla.rs b/wgpu-core/src/lock/vanilla.rs index 4fc419f12e..9a35b6d9d8 100644 --- a/wgpu-core/src/lock/vanilla.rs +++ b/wgpu-core/src/lock/vanilla.rs @@ -86,6 +86,12 @@ impl RwLock { } } +impl<'a, T> RwLockWriteGuard<'a, T> { + pub fn downgrade(this: Self) -> RwLockReadGuard<'a, T> { + RwLockReadGuard(parking_lot::RwLockWriteGuard::downgrade(this.0)) + } +} + impl std::fmt::Debug for RwLock { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f)