1//! Plain, uninstrumented wrappers around [`parking_lot`] lock types.
2//!
3//! These definitions are used when no particular lock instrumentation
4//! Cargo feature is selected.
56use core::{fmt, ops};
78use crate::lock::rank::LockRank;
910pub struct RankData;
1112/// A plain wrapper around [`parking_lot::Mutex`].
13///
14/// This is just like [`parking_lot::Mutex`], except that our [`new`]
15/// method takes a rank, indicating where the new mutex should sit in
16/// `wgpu-core`'s lock ordering. The rank is ignored.
17///
18/// See the [`lock`] module documentation for other wrappers.
19///
20/// [`new`]: Mutex::new
21/// [`lock`]: crate::lock
22pub struct Mutex<T>(parking_lot::Mutex<T>);
2324/// A guard produced by locking [`Mutex`].
25///
26/// This is just a wrapper around a [`parking_lot::MutexGuard`].
27pub struct MutexGuard<'a, T>(parking_lot::MutexGuard<'a, T>);
2829impl<T> Mutex<T> {
30pub fn new(_rank: LockRank, value: T) -> Mutex<T> {
31 Mutex(parking_lot::Mutex::new(value))
32 }
3334pub fn lock(&self) -> MutexGuard<'_, T> {
35 MutexGuard(self.0.lock())
36 }
3738pub fn into_inner(self) -> T {
39self.0.into_inner()
40 }
41}
4243impl<'a, T> ops::Deref for MutexGuard<'a, T> {
44type Target = T;
4546fn deref(&self) -> &Self::Target {
47self.0.deref()
48 }
49}
5051impl<'a, T> ops::DerefMut for MutexGuard<'a, T> {
52fn deref_mut(&mut self) -> &mut Self::Target {
53self.0.deref_mut()
54 }
55}
5657impl<T: fmt::Debug> fmt::Debug for Mutex<T> {
58fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59self.0.fmt(f)
60 }
61}
6263/// A plain wrapper around [`parking_lot::RwLock`].
64///
65/// This is just like [`parking_lot::RwLock`], except that our [`new`]
66/// method takes a rank, indicating where the new mutex should sit in
67/// `wgpu-core`'s lock ordering. The rank is ignored.
68///
69/// See the [`lock`] module documentation for other wrappers.
70///
71/// [`new`]: RwLock::new
72/// [`lock`]: crate::lock
73pub struct RwLock<T>(parking_lot::RwLock<T>);
7475/// A read guard produced by locking [`RwLock`] as a reader.
76///
77/// This is just a wrapper around a [`parking_lot::RwLockReadGuard`].
78pub struct RwLockReadGuard<'a, T>(parking_lot::RwLockReadGuard<'a, T>);
7980/// A write guard produced by locking [`RwLock`] as a writer.
81///
82/// This is just a wrapper around a [`parking_lot::RwLockWriteGuard`].
83pub struct RwLockWriteGuard<'a, T>(parking_lot::RwLockWriteGuard<'a, T>);
8485impl<T> RwLock<T> {
86pub fn new(_rank: LockRank, value: T) -> RwLock<T> {
87 RwLock(parking_lot::RwLock::new(value))
88 }
8990pub fn read(&self) -> RwLockReadGuard<'_, T> {
91 RwLockReadGuard(self.0.read())
92 }
9394pub fn write(&self) -> RwLockWriteGuard<'_, T> {
95 RwLockWriteGuard(self.0.write())
96 }
9798/// Force an read-unlock operation on this lock.
99 ///
100 /// Safety:
101 /// - A read lock must be held which is not held by a guard.
102pub unsafe fn force_unlock_read(&self, _data: RankData) {
103unsafe { self.0.force_unlock_read() };
104 }
105}
106107impl<'a, T> RwLockReadGuard<'a, T> {
108// Forget the read guard, leaving the lock in a locked state with no guard.
109 //
110 // Equivalent to std::mem::forget, but preserves the information about the lock
111 // rank.
112pub fn forget(this: Self) -> RankData {
113 core::mem::forget(this.0);
114115 RankData
116 }
117}
118119impl<'a, T> RwLockWriteGuard<'a, T> {
120pub fn downgrade(this: Self) -> RwLockReadGuard<'a, T> {
121 RwLockReadGuard(parking_lot::RwLockWriteGuard::downgrade(this.0))
122 }
123}
124125impl<T: fmt::Debug> fmt::Debug for RwLock<T> {
126fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127self.0.fmt(f)
128 }
129}
130131impl<'a, T> ops::Deref for RwLockReadGuard<'a, T> {
132type Target = T;
133134fn deref(&self) -> &Self::Target {
135self.0.deref()
136 }
137}
138139impl<'a, T> ops::Deref for RwLockWriteGuard<'a, T> {
140type Target = T;
141142fn deref(&self) -> &Self::Target {
143self.0.deref()
144 }
145}
146147impl<'a, T> ops::DerefMut for RwLockWriteGuard<'a, T> {
148fn deref_mut(&mut self) -> &mut Self::Target {
149self.0.deref_mut()
150 }
151}