wgpu_core/lock/
vanilla.rs1use core::{fmt, ops};
7
8use crate::lock::rank::LockRank;
9
10pub struct RankData;
11
12pub struct Mutex<T>(parking_lot::Mutex<T>);
23
24pub struct MutexGuard<'a, T>(parking_lot::MutexGuard<'a, T>);
28
29impl<T> Mutex<T> {
30    pub fn new(_rank: LockRank, value: T) -> Mutex<T> {
31        Mutex(parking_lot::Mutex::new(value))
32    }
33
34    pub fn lock(&self) -> MutexGuard<T> {
35        MutexGuard(self.0.lock())
36    }
37
38    pub fn into_inner(self) -> T {
39        self.0.into_inner()
40    }
41}
42
43impl<'a, T> ops::Deref for MutexGuard<'a, T> {
44    type Target = T;
45
46    fn deref(&self) -> &Self::Target {
47        self.0.deref()
48    }
49}
50
51impl<'a, T> ops::DerefMut for MutexGuard<'a, T> {
52    fn deref_mut(&mut self) -> &mut Self::Target {
53        self.0.deref_mut()
54    }
55}
56
57impl<T: fmt::Debug> fmt::Debug for Mutex<T> {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        self.0.fmt(f)
60    }
61}
62
63pub struct RwLock<T>(parking_lot::RwLock<T>);
74
75pub struct RwLockReadGuard<'a, T>(parking_lot::RwLockReadGuard<'a, T>);
79
80pub struct RwLockWriteGuard<'a, T>(parking_lot::RwLockWriteGuard<'a, T>);
84
85impl<T> RwLock<T> {
86    pub fn new(_rank: LockRank, value: T) -> RwLock<T> {
87        RwLock(parking_lot::RwLock::new(value))
88    }
89
90    pub fn read(&self) -> RwLockReadGuard<T> {
91        RwLockReadGuard(self.0.read())
92    }
93
94    pub fn write(&self) -> RwLockWriteGuard<T> {
95        RwLockWriteGuard(self.0.write())
96    }
97
98    pub unsafe fn force_unlock_read(&self, _data: RankData) {
103        unsafe { self.0.force_unlock_read() };
104    }
105}
106
107impl<'a, T> RwLockReadGuard<'a, T> {
108    pub fn forget(this: Self) -> RankData {
113        core::mem::forget(this.0);
114
115        RankData
116    }
117}
118
119impl<'a, T> RwLockWriteGuard<'a, T> {
120    pub fn downgrade(this: Self) -> RwLockReadGuard<'a, T> {
121        RwLockReadGuard(parking_lot::RwLockWriteGuard::downgrade(this.0))
122    }
123}
124
125impl<T: fmt::Debug> fmt::Debug for RwLock<T> {
126    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127        self.0.fmt(f)
128    }
129}
130
131impl<'a, T> ops::Deref for RwLockReadGuard<'a, T> {
132    type Target = T;
133
134    fn deref(&self) -> &Self::Target {
135        self.0.deref()
136    }
137}
138
139impl<'a, T> ops::Deref for RwLockWriteGuard<'a, T> {
140    type Target = T;
141
142    fn deref(&self) -> &Self::Target {
143        self.0.deref()
144    }
145}
146
147impl<'a, T> ops::DerefMut for RwLockWriteGuard<'a, T> {
148    fn deref_mut(&mut self) -> &mut Self::Target {
149        self.0.deref_mut()
150    }
151}