xref: /DragonOS/kernel/src/libs/rwlock.rs (revision 90a0a49048fdaf5e31320d0c87f8bed8db1bd882)
1f1284c35SGou Ngai #![allow(dead_code)]
2f1284c35SGou Ngai use core::{
3f1284c35SGou Ngai     cell::UnsafeCell,
4f1284c35SGou Ngai     hint::spin_loop,
5f1284c35SGou Ngai     mem::{self, ManuallyDrop},
6f1284c35SGou Ngai     ops::{Deref, DerefMut},
7f1284c35SGou Ngai     sync::atomic::{AtomicU32, Ordering},
8f1284c35SGou Ngai };
9f1284c35SGou Ngai 
1040fe15e0SLoGin use crate::{
1140fe15e0SLoGin     process::preempt::{preempt_disable, preempt_enable},
1240fe15e0SLoGin     syscall::SystemError,
1340fe15e0SLoGin };
14f1284c35SGou Ngai 
15f1284c35SGou Ngai ///RwLock读写锁
16f1284c35SGou Ngai 
17f1284c35SGou Ngai /// @brief READER位占据从右往左数第三个比特位
18f1284c35SGou Ngai const READER: u32 = 1 << 2;
19f1284c35SGou Ngai 
20f1284c35SGou Ngai /// @brief UPGRADED位占据从右到左数第二个比特位
21f1284c35SGou Ngai const UPGRADED: u32 = 1 << 1;
22f1284c35SGou Ngai 
23f1284c35SGou Ngai /// @brief WRITER位占据最右边的比特位
24f1284c35SGou Ngai const WRITER: u32 = 1;
25f1284c35SGou Ngai 
26f1284c35SGou Ngai const READER_BIT: u32 = 2;
27f1284c35SGou Ngai 
28f1284c35SGou Ngai /// @brief 读写锁的基本数据结构
29f1284c35SGou Ngai /// @param lock 32位原子变量,最右边的两位从左到右分别是UPGRADED,WRITER (标志位)
30f1284c35SGou Ngai ///             剩下的bit位存储READER数量(除了MSB)
31f1284c35SGou Ngai ///             对于标志位,0代表无, 1代表有
32f1284c35SGou Ngai ///             对于剩下的比特位表征READER的数量的多少
33f1284c35SGou Ngai ///             lock的MSB必须为0,否则溢出
34f1284c35SGou Ngai #[derive(Debug)]
35f1284c35SGou Ngai pub struct RwLock<T> {
36f1284c35SGou Ngai     lock: AtomicU32,
37f1284c35SGou Ngai     data: UnsafeCell<T>,
38f1284c35SGou Ngai }
39f1284c35SGou Ngai 
40f1284c35SGou Ngai /// @brief  READER守卫的数据结构
41f1284c35SGou Ngai /// @param lock 是对RwLock的lock属性值的只读引用
42f1284c35SGou Ngai pub struct RwLockReadGuard<'a, T: 'a> {
43f1284c35SGou Ngai     data: *const T,
44f1284c35SGou Ngai     lock: &'a AtomicU32,
45f1284c35SGou Ngai }
46f1284c35SGou Ngai 
47f1284c35SGou Ngai /// @brief UPGRADED是介于READER和WRITER之间的一种锁,它可以升级为WRITER,
48f1284c35SGou Ngai ///        UPGRADED守卫的数据结构,注册UPGRADED锁只需要查看UPGRADED和WRITER的比特位
49f1284c35SGou Ngai ///        但是当UPGRADED守卫注册后,不允许有新的读者锁注册
50f1284c35SGou Ngai /// @param inner    是对RwLock数据结构的只读引用
51f1284c35SGou Ngai pub struct RwLockUpgradableGuard<'a, T: 'a> {
52f1284c35SGou Ngai     data: *const T,
53f1284c35SGou Ngai     inner: &'a RwLock<T>,
54f1284c35SGou Ngai }
55f1284c35SGou Ngai 
56f1284c35SGou Ngai /// @brief WRITER守卫的数据结构
57f1284c35SGou Ngai /// @param data     RwLock的data的可变引用
58f1284c35SGou Ngai /// @param inner    是对RwLock数据结构的只读引用
59f1284c35SGou Ngai pub struct RwLockWriteGuard<'a, T: 'a> {
60f1284c35SGou Ngai     data: *mut T,
61f1284c35SGou Ngai     inner: &'a RwLock<T>,
62f1284c35SGou Ngai }
63f1284c35SGou Ngai 
64f1284c35SGou Ngai unsafe impl<T: Send> Send for RwLock<T> {}
65f1284c35SGou Ngai unsafe impl<T: Send + Sync> Sync for RwLock<T> {}
66f1284c35SGou Ngai 
67f1284c35SGou Ngai /// @brief RwLock的API
68f1284c35SGou Ngai impl<T> RwLock<T> {
69f1284c35SGou Ngai     #[inline]
70f1284c35SGou Ngai     /// @brief  RwLock的初始化
71f1284c35SGou Ngai     pub const fn new(data: T) -> Self {
72f1284c35SGou Ngai         return RwLock {
73f1284c35SGou Ngai             lock: AtomicU32::new(0),
74f1284c35SGou Ngai             data: UnsafeCell::new(data),
75f1284c35SGou Ngai         };
76f1284c35SGou Ngai     }
77f1284c35SGou Ngai 
78f1284c35SGou Ngai     #[allow(dead_code)]
79f1284c35SGou Ngai     #[inline]
80f1284c35SGou Ngai     /// @brief 将读写锁的皮扒掉,返回内在的data,返回的是一个真身而非引用
81f1284c35SGou Ngai     pub fn into_inner(self) -> T {
82f1284c35SGou Ngai         let RwLock { data, .. } = self;
83f1284c35SGou Ngai         return data.into_inner();
84f1284c35SGou Ngai     }
85f1284c35SGou Ngai 
86f1284c35SGou Ngai     #[allow(dead_code)]
87f1284c35SGou Ngai     #[inline]
88f1284c35SGou Ngai     /// @brief 返回data的raw pointer,
89f1284c35SGou Ngai     /// unsafe
90f1284c35SGou Ngai     pub fn as_mut_ptr(&self) -> *mut T {
91f1284c35SGou Ngai         return self.data.get();
92f1284c35SGou Ngai     }
93f1284c35SGou Ngai 
94f1284c35SGou Ngai     #[allow(dead_code)]
95f1284c35SGou Ngai     #[inline]
96f1284c35SGou Ngai     /// @brief 获取实时的读者数并尝试加1,如果增加值成功则返回增加1后的读者数,否则panic
97676b8ef6SMork     fn current_reader(&self) -> Result<u32, SystemError> {
98f1284c35SGou Ngai         const MAX_READERS: u32 = core::u32::MAX >> READER_BIT >> 1; //右移3位
99f1284c35SGou Ngai 
100f1284c35SGou Ngai         let value = self.lock.fetch_add(READER, Ordering::Acquire);
101f1284c35SGou Ngai         //value二进制形式的MSB不能为1, 否则导致溢出
102f1284c35SGou Ngai 
103f1284c35SGou Ngai         if value > MAX_READERS << READER_BIT {
104f1284c35SGou Ngai             self.lock.fetch_sub(READER, Ordering::Release);
105f1284c35SGou Ngai             //panic!("Too many lock readers, cannot safely proceed");
106676b8ef6SMork             return Err(SystemError::EOVERFLOW);
107f1284c35SGou Ngai         } else {
108f1284c35SGou Ngai             return Ok(value);
109f1284c35SGou Ngai         }
110f1284c35SGou Ngai     }
111f1284c35SGou Ngai 
112f1284c35SGou Ngai     #[allow(dead_code)]
113f1284c35SGou Ngai     #[inline]
114f1284c35SGou Ngai     /// @brief 尝试获取READER守卫
115f1284c35SGou Ngai     pub fn try_read(&self) -> Option<RwLockReadGuard<T>> {
11640fe15e0SLoGin         preempt_disable();
11740fe15e0SLoGin         let r = self.inner_try_read();
11840fe15e0SLoGin         if r.is_none() {
11940fe15e0SLoGin             preempt_enable();
12040fe15e0SLoGin         }
12140fe15e0SLoGin         return r;
12240fe15e0SLoGin     }
12340fe15e0SLoGin 
12440fe15e0SLoGin     fn inner_try_read(&self) -> Option<RwLockReadGuard<T>> {
125f1284c35SGou Ngai         let reader_value = self.current_reader();
126f1284c35SGou Ngai         //得到自增后的reader_value, 包括了尝试获得READER守卫的进程
127f1284c35SGou Ngai         let value;
128f1284c35SGou Ngai 
129f1284c35SGou Ngai         if reader_value.is_err() {
130f1284c35SGou Ngai             return None; //获取失败
131f1284c35SGou Ngai         } else {
132f1284c35SGou Ngai             value = reader_value.unwrap();
133f1284c35SGou Ngai         }
134f1284c35SGou Ngai 
135f1284c35SGou Ngai         //判断有没有writer和upgrader
136f1284c35SGou Ngai         //注意, 若upgrader存在,已经存在的读者继续占有锁,但新读者不允许获得锁
137f1284c35SGou Ngai         if value & (WRITER | UPGRADED) != 0 {
138f1284c35SGou Ngai             self.lock.fetch_sub(READER, Ordering::Release);
139f1284c35SGou Ngai             return None;
140f1284c35SGou Ngai         } else {
141f1284c35SGou Ngai             return Some(RwLockReadGuard {
142f1284c35SGou Ngai                 data: unsafe { &*self.data.get() },
143f1284c35SGou Ngai                 lock: &self.lock,
144f1284c35SGou Ngai             });
145f1284c35SGou Ngai         }
146f1284c35SGou Ngai     }
147f1284c35SGou Ngai 
148f1284c35SGou Ngai     #[allow(dead_code)]
149f1284c35SGou Ngai     #[inline]
150f1284c35SGou Ngai     /// @brief 获得READER的守卫
151f1284c35SGou Ngai     pub fn read(&self) -> RwLockReadGuard<T> {
152f1284c35SGou Ngai         loop {
153f1284c35SGou Ngai             match self.try_read() {
154f1284c35SGou Ngai                 Some(guard) => return guard,
155f1284c35SGou Ngai                 None => spin_loop(),
156f1284c35SGou Ngai             }
157f1284c35SGou Ngai         } //忙等待
158f1284c35SGou Ngai     }
159f1284c35SGou Ngai 
160f1284c35SGou Ngai     #[allow(dead_code)]
161f1284c35SGou Ngai     #[inline]
162f1284c35SGou Ngai     /// @brief 获取读者+UPGRADER的数量, 不能保证能否获得同步值
163f1284c35SGou Ngai     pub fn reader_count(&self) -> u32 {
164f1284c35SGou Ngai         let state = self.lock.load(Ordering::Relaxed);
165f1284c35SGou Ngai         return state / READER + (state & UPGRADED) / UPGRADED;
166f1284c35SGou Ngai     }
167f1284c35SGou Ngai 
168f1284c35SGou Ngai     #[allow(dead_code)]
169f1284c35SGou Ngai     #[inline]
170f1284c35SGou Ngai     /// @brief 获取写者数量,不能保证能否获得同步值
171f1284c35SGou Ngai     pub fn writer_count(&self) -> u32 {
172f1284c35SGou Ngai         return (self.lock.load(Ordering::Relaxed) & WRITER) / WRITER;
173f1284c35SGou Ngai     }
174f1284c35SGou Ngai 
175f1284c35SGou Ngai     #[cfg(target_arch = "x86_64")]
176f1284c35SGou Ngai     #[allow(dead_code)]
177f1284c35SGou Ngai     #[inline]
178f1284c35SGou Ngai     /// @brief 尝试获得WRITER守卫
179f1284c35SGou Ngai     pub fn try_write(&self) -> Option<RwLockWriteGuard<T>> {
18040fe15e0SLoGin         preempt_disable();
18140fe15e0SLoGin         let r = self.inner_try_write();
18240fe15e0SLoGin         if r.is_none() {
18340fe15e0SLoGin             preempt_enable();
18440fe15e0SLoGin         }
18540fe15e0SLoGin 
18640fe15e0SLoGin         return r;
18740fe15e0SLoGin     } //当架构为arm时,有些代码需要作出调整compare_exchange=>compare_exchange_weak
18840fe15e0SLoGin 
18940fe15e0SLoGin     #[cfg(target_arch = "x86_64")]
19040fe15e0SLoGin     #[allow(dead_code)]
19140fe15e0SLoGin     fn inner_try_write(&self) -> Option<RwLockWriteGuard<T>> {
192f1284c35SGou Ngai         let res: bool = self
193f1284c35SGou Ngai             .lock
1941d489963Skong             .compare_exchange(0, WRITER, Ordering::Acquire, Ordering::Relaxed)
1951d489963Skong             .is_ok();
196f1284c35SGou Ngai         //只有lock大小为0的时候能获得写者守卫
197f1284c35SGou Ngai         if res {
198f1284c35SGou Ngai             return Some(RwLockWriteGuard {
199f1284c35SGou Ngai                 data: unsafe { &mut *self.data.get() },
200f1284c35SGou Ngai                 inner: self,
201f1284c35SGou Ngai             });
202f1284c35SGou Ngai         } else {
203f1284c35SGou Ngai             return None;
204f1284c35SGou Ngai         }
20540fe15e0SLoGin     }
206f1284c35SGou Ngai 
207f1284c35SGou Ngai     #[allow(dead_code)]
208f1284c35SGou Ngai     #[inline]
209f1284c35SGou Ngai     /// @brief 获得WRITER守卫
210f1284c35SGou Ngai     pub fn write(&self) -> RwLockWriteGuard<T> {
211f1284c35SGou Ngai         loop {
212f1284c35SGou Ngai             match self.try_write() {
213f1284c35SGou Ngai                 Some(guard) => return guard,
214f1284c35SGou Ngai                 None => spin_loop(),
215f1284c35SGou Ngai             }
216f1284c35SGou Ngai         }
217f1284c35SGou Ngai     }
218f1284c35SGou Ngai 
219f1284c35SGou Ngai     #[allow(dead_code)]
220f1284c35SGou Ngai     #[inline]
221f1284c35SGou Ngai     /// @brief 尝试获得UPGRADER守卫
222f1284c35SGou Ngai     pub fn try_upgradeable_read(&self) -> Option<RwLockUpgradableGuard<T>> {
22340fe15e0SLoGin         preempt_disable();
22440fe15e0SLoGin         let r = self.inner_try_upgradeable_read();
22540fe15e0SLoGin         if r.is_none() {
22640fe15e0SLoGin             preempt_enable();
22740fe15e0SLoGin         }
22840fe15e0SLoGin 
22940fe15e0SLoGin         return r;
23040fe15e0SLoGin     }
23140fe15e0SLoGin 
23240fe15e0SLoGin     fn inner_try_upgradeable_read(&self) -> Option<RwLockUpgradableGuard<T>> {
233f1284c35SGou Ngai         // 获得UPGRADER守卫不需要查看读者位
234f1284c35SGou Ngai         // 如果获得读者锁失败,不需要撤回fetch_or的原子操作
235f1284c35SGou Ngai         if self.lock.fetch_or(UPGRADED, Ordering::Acquire) & (WRITER | UPGRADED) == 0 {
236f1284c35SGou Ngai             return Some(RwLockUpgradableGuard {
237f1284c35SGou Ngai                 inner: self,
238f1284c35SGou Ngai                 data: unsafe { &mut *self.data.get() },
239f1284c35SGou Ngai             });
240f1284c35SGou Ngai         } else {
241f1284c35SGou Ngai             return None;
242f1284c35SGou Ngai         }
243f1284c35SGou Ngai     }
244f1284c35SGou Ngai 
245f1284c35SGou Ngai     #[allow(dead_code)]
246f1284c35SGou Ngai     #[inline]
247f1284c35SGou Ngai     /// @brief 获得UPGRADER守卫
248f1284c35SGou Ngai     pub fn upgradeable_read(&self) -> RwLockUpgradableGuard<T> {
249f1284c35SGou Ngai         loop {
250f1284c35SGou Ngai             match self.try_upgradeable_read() {
251f1284c35SGou Ngai                 Some(guard) => return guard,
252f1284c35SGou Ngai                 None => spin_loop(),
253f1284c35SGou Ngai             }
254f1284c35SGou Ngai         }
255f1284c35SGou Ngai     }
256f1284c35SGou Ngai 
257f1284c35SGou Ngai     #[allow(dead_code)]
258f1284c35SGou Ngai     #[inline]
259f1284c35SGou Ngai     //extremely unsafe behavior
260f1284c35SGou Ngai     /// @brief 强制减少READER数
261f1284c35SGou Ngai     pub unsafe fn force_read_decrement(&self) {
262f1284c35SGou Ngai         debug_assert!(self.lock.load(Ordering::Relaxed) & !WRITER > 0);
263f1284c35SGou Ngai         self.lock.fetch_sub(READER, Ordering::Release);
264f1284c35SGou Ngai     }
265f1284c35SGou Ngai 
266f1284c35SGou Ngai     #[allow(dead_code)]
267f1284c35SGou Ngai     #[inline]
268f1284c35SGou Ngai     //extremely unsafe behavior
269f1284c35SGou Ngai     /// @brief 强制给WRITER解锁
270f1284c35SGou Ngai     pub unsafe fn force_write_unlock(&self) {
271f1284c35SGou Ngai         debug_assert_eq!(self.lock.load(Ordering::Relaxed) & !(WRITER | UPGRADED), 0);
272f1284c35SGou Ngai         self.lock.fetch_and(!(WRITER | UPGRADED), Ordering::Release);
273f1284c35SGou Ngai     }
274f1284c35SGou Ngai 
275f1284c35SGou Ngai     #[allow(dead_code)]
27613776c11Slogin     pub unsafe fn get_mut(&mut self) -> &mut T {
277f1284c35SGou Ngai         unsafe { &mut *self.data.get() }
278f1284c35SGou Ngai     }
279f1284c35SGou Ngai }
280f1284c35SGou Ngai 
281f1284c35SGou Ngai impl<T: Default> Default for RwLock<T> {
282f1284c35SGou Ngai     fn default() -> Self {
283f1284c35SGou Ngai         Self::new(Default::default())
284f1284c35SGou Ngai     }
285f1284c35SGou Ngai }
286f1284c35SGou Ngai 
287f1284c35SGou Ngai /// @brief 由原有的值创建新的锁
288f1284c35SGou Ngai impl<T> From<T> for RwLock<T> {
289f1284c35SGou Ngai     fn from(data: T) -> Self {
290f1284c35SGou Ngai         return Self::new(data);
291f1284c35SGou Ngai     }
292f1284c35SGou Ngai }
293f1284c35SGou Ngai 
294f1284c35SGou Ngai impl<'rwlock, T> RwLockReadGuard<'rwlock, T> {
295*90a0a490SLoGin     /// @brief 释放守卫,获得保护的值的不可变引用
296*90a0a490SLoGin     ///
297*90a0a490SLoGin     /// ## Safety
298*90a0a490SLoGin     ///
299*90a0a490SLoGin     /// 由于这样做可能导致守卫在另一个线程中被释放,从而导致pcb的preempt count不正确,
300*90a0a490SLoGin     /// 因此必须小心的手动维护好preempt count。
301*90a0a490SLoGin     ///
302*90a0a490SLoGin     /// 并且,leak还可能导致锁的状态不正确。因此请仔细考虑是否真的需要使用这个函数。
303f1284c35SGou Ngai     #[allow(dead_code)]
304f1284c35SGou Ngai     #[inline]
305*90a0a490SLoGin     pub unsafe fn leak(this: Self) -> &'rwlock T {
306*90a0a490SLoGin         let this = ManuallyDrop::new(this);
307*90a0a490SLoGin         return unsafe { &*this.data };
308f1284c35SGou Ngai     }
309f1284c35SGou Ngai }
310f1284c35SGou Ngai 
311f1284c35SGou Ngai impl<'rwlock, T> RwLockUpgradableGuard<'rwlock, T> {
312f1284c35SGou Ngai     #[allow(dead_code)]
313f1284c35SGou Ngai     #[inline]
314f1284c35SGou Ngai     /// @brief 尝试将UPGRADER守卫升级为WRITER守卫
315f1284c35SGou Ngai     pub fn try_upgrade(self) -> Result<RwLockWriteGuard<'rwlock, T>, Self> {
316f1284c35SGou Ngai         let res = self.inner.lock.compare_exchange(
317f1284c35SGou Ngai             UPGRADED,
318f1284c35SGou Ngai             WRITER,
319f1284c35SGou Ngai             Ordering::Acquire,
320f1284c35SGou Ngai             Ordering::Relaxed,
321f1284c35SGou Ngai         );
322f1284c35SGou Ngai         //当且仅当只有UPGRADED守卫时可以升级
323f1284c35SGou Ngai 
324f1284c35SGou Ngai         if res.is_ok() {
325f1284c35SGou Ngai             let inner = self.inner;
326f1284c35SGou Ngai 
327f1284c35SGou Ngai             mem::forget(self);
328f1284c35SGou Ngai 
329f1284c35SGou Ngai             Ok(RwLockWriteGuard {
330f1284c35SGou Ngai                 data: unsafe { &mut *inner.data.get() },
331f1284c35SGou Ngai                 inner,
332f1284c35SGou Ngai             })
333f1284c35SGou Ngai         } else {
334f1284c35SGou Ngai             Err(self)
335f1284c35SGou Ngai         }
336f1284c35SGou Ngai     }
337f1284c35SGou Ngai 
338f1284c35SGou Ngai     #[allow(dead_code)]
339f1284c35SGou Ngai     #[inline]
340f1284c35SGou Ngai     /// @brief 将upgrader升级成writer
341f1284c35SGou Ngai     pub fn upgrade(mut self) -> RwLockWriteGuard<'rwlock, T> {
342f1284c35SGou Ngai         loop {
343f1284c35SGou Ngai             self = match self.try_upgrade() {
344f1284c35SGou Ngai                 Ok(writeguard) => return writeguard,
345f1284c35SGou Ngai                 Err(former) => former,
346f1284c35SGou Ngai             };
347f1284c35SGou Ngai 
348f1284c35SGou Ngai             spin_loop();
349f1284c35SGou Ngai         }
350f1284c35SGou Ngai     }
351f1284c35SGou Ngai 
352f1284c35SGou Ngai     #[allow(dead_code)]
353f1284c35SGou Ngai     #[inline]
354f1284c35SGou Ngai     /// @brief UPGRADER降级为READER
355f1284c35SGou Ngai     pub fn downgrade(self) -> RwLockReadGuard<'rwlock, T> {
356f1284c35SGou Ngai         while self.inner.current_reader().is_err() {
357f1284c35SGou Ngai             spin_loop();
358f1284c35SGou Ngai         }
359f1284c35SGou Ngai 
360f1284c35SGou Ngai         let inner: &RwLock<T> = self.inner;
361f1284c35SGou Ngai 
362f1284c35SGou Ngai         // 自动移去UPGRADED比特位
363f1284c35SGou Ngai         mem::drop(self);
364f1284c35SGou Ngai 
365f1284c35SGou Ngai         RwLockReadGuard {
366f1284c35SGou Ngai             data: unsafe { &*inner.data.get() },
367f1284c35SGou Ngai             lock: &inner.lock,
368f1284c35SGou Ngai         }
369f1284c35SGou Ngai     }
370f1284c35SGou Ngai 
371f1284c35SGou Ngai     #[allow(dead_code)]
372f1284c35SGou Ngai     #[inline]
373*90a0a490SLoGin     /// @brief 返回内部数据的引用,消除守卫
374*90a0a490SLoGin     ///
375*90a0a490SLoGin     /// ## Safety
376*90a0a490SLoGin     ///
377*90a0a490SLoGin     /// 由于这样做可能导致守卫在另一个线程中被释放,从而导致pcb的preempt count不正确,
378*90a0a490SLoGin     /// 因此必须小心的手动维护好preempt count。
379*90a0a490SLoGin     ///
380*90a0a490SLoGin     /// 并且,leak还可能导致锁的状态不正确。因此请仔细考虑是否真的需要使用这个函数。
381*90a0a490SLoGin     pub unsafe fn leak(this: Self) -> &'rwlock T {
382*90a0a490SLoGin         let this: ManuallyDrop<RwLockUpgradableGuard<'_, T>> = ManuallyDrop::new(this);
383f1284c35SGou Ngai 
384f1284c35SGou Ngai         unsafe { &*this.data }
385f1284c35SGou Ngai     }
386f1284c35SGou Ngai }
387f1284c35SGou Ngai 
388f1284c35SGou Ngai impl<'rwlock, T> RwLockWriteGuard<'rwlock, T> {
389f1284c35SGou Ngai     #[allow(dead_code)]
390f1284c35SGou Ngai     #[inline]
391*90a0a490SLoGin     /// @brief 返回内部数据的引用,消除守卫
392*90a0a490SLoGin     ///
393*90a0a490SLoGin     /// ## Safety
394*90a0a490SLoGin     ///
395*90a0a490SLoGin     /// 由于这样做可能导致守卫在另一个线程中被释放,从而导致pcb的preempt count不正确,
396*90a0a490SLoGin     /// 因此必须小心的手动维护好preempt count。
397*90a0a490SLoGin     ///
398*90a0a490SLoGin     /// 并且,leak还可能导致锁的状态不正确。因此请仔细考虑是否真的需要使用这个函数。
399*90a0a490SLoGin     pub unsafe fn leak(this: Self) -> &'rwlock T {
400f1284c35SGou Ngai         let this = ManuallyDrop::new(this);
401f1284c35SGou Ngai 
402f1284c35SGou Ngai         return unsafe { &*this.data };
403f1284c35SGou Ngai     }
404f1284c35SGou Ngai 
405f1284c35SGou Ngai     #[allow(dead_code)]
406f1284c35SGou Ngai     #[inline]
407f1284c35SGou Ngai     /// @brief 将WRITER降级为READER
408f1284c35SGou Ngai     pub fn downgrade(self) -> RwLockReadGuard<'rwlock, T> {
409f1284c35SGou Ngai         while self.inner.current_reader().is_err() {
410f1284c35SGou Ngai             spin_loop();
411f1284c35SGou Ngai         }
412f1284c35SGou Ngai         //本质上来说绝对保证没有任何读者
413f1284c35SGou Ngai 
414f1284c35SGou Ngai         let inner = self.inner;
415f1284c35SGou Ngai 
416f1284c35SGou Ngai         mem::drop(self);
417f1284c35SGou Ngai 
418f1284c35SGou Ngai         return RwLockReadGuard {
419f1284c35SGou Ngai             data: unsafe { &*inner.data.get() },
420f1284c35SGou Ngai             lock: &inner.lock,
421f1284c35SGou Ngai         };
422f1284c35SGou Ngai     }
423f1284c35SGou Ngai 
424f1284c35SGou Ngai     #[allow(dead_code)]
425f1284c35SGou Ngai     #[inline]
426f1284c35SGou Ngai     /// @brief 将WRITER降级为UPGRADER
427f1284c35SGou Ngai     pub fn downgrade_to_upgradeable(self) -> RwLockUpgradableGuard<'rwlock, T> {
428f1284c35SGou Ngai         debug_assert_eq!(
429f1284c35SGou Ngai             self.inner.lock.load(Ordering::Acquire) & (WRITER | UPGRADED),
430f1284c35SGou Ngai             WRITER
431f1284c35SGou Ngai         );
432f1284c35SGou Ngai 
433f1284c35SGou Ngai         self.inner.lock.store(UPGRADED, Ordering::Release);
434f1284c35SGou Ngai 
435f1284c35SGou Ngai         let inner = self.inner;
436f1284c35SGou Ngai 
437f1284c35SGou Ngai         mem::forget(self);
438f1284c35SGou Ngai 
439f1284c35SGou Ngai         return RwLockUpgradableGuard {
440f1284c35SGou Ngai             inner,
441f1284c35SGou Ngai             data: unsafe { &*inner.data.get() },
442f1284c35SGou Ngai         };
443f1284c35SGou Ngai     }
444f1284c35SGou Ngai }
445f1284c35SGou Ngai 
446f1284c35SGou Ngai impl<'rwlock, T> Deref for RwLockReadGuard<'rwlock, T> {
447f1284c35SGou Ngai     type Target = T;
448f1284c35SGou Ngai 
449f1284c35SGou Ngai     fn deref(&self) -> &Self::Target {
450f1284c35SGou Ngai         return unsafe { &*self.data };
451f1284c35SGou Ngai     }
452f1284c35SGou Ngai }
453f1284c35SGou Ngai 
454f1284c35SGou Ngai impl<'rwlock, T> Deref for RwLockUpgradableGuard<'rwlock, T> {
455f1284c35SGou Ngai     type Target = T;
456f1284c35SGou Ngai 
457f1284c35SGou Ngai     fn deref(&self) -> &Self::Target {
458f1284c35SGou Ngai         return unsafe { &*self.data };
459f1284c35SGou Ngai     }
460f1284c35SGou Ngai }
461f1284c35SGou Ngai 
462f1284c35SGou Ngai impl<'rwlock, T> Deref for RwLockWriteGuard<'rwlock, T> {
463f1284c35SGou Ngai     type Target = T;
464f1284c35SGou Ngai 
465f1284c35SGou Ngai     fn deref(&self) -> &Self::Target {
466f1284c35SGou Ngai         return unsafe { &*self.data };
467f1284c35SGou Ngai     }
468f1284c35SGou Ngai }
469f1284c35SGou Ngai 
470f1284c35SGou Ngai impl<'rwlock, T> DerefMut for RwLockWriteGuard<'rwlock, T> {
471f1284c35SGou Ngai     fn deref_mut(&mut self) -> &mut Self::Target {
472f1284c35SGou Ngai         return unsafe { &mut *self.data };
473f1284c35SGou Ngai     }
474f1284c35SGou Ngai }
475f1284c35SGou Ngai 
476f1284c35SGou Ngai impl<'rwlock, T> Drop for RwLockReadGuard<'rwlock, T> {
477f1284c35SGou Ngai     fn drop(&mut self) {
478f1284c35SGou Ngai         debug_assert!(self.lock.load(Ordering::Relaxed) & !(WRITER | UPGRADED) > 0);
479f1284c35SGou Ngai         self.lock.fetch_sub(READER, Ordering::Release);
48040fe15e0SLoGin         preempt_enable();
481f1284c35SGou Ngai     }
482f1284c35SGou Ngai }
483f1284c35SGou Ngai 
484f1284c35SGou Ngai impl<'rwlock, T> Drop for RwLockUpgradableGuard<'rwlock, T> {
485f1284c35SGou Ngai     fn drop(&mut self) {
486f1284c35SGou Ngai         debug_assert_eq!(
487f1284c35SGou Ngai             self.inner.lock.load(Ordering::Relaxed) & (WRITER | UPGRADED),
488f1284c35SGou Ngai             UPGRADED
489f1284c35SGou Ngai         );
490f1284c35SGou Ngai         self.inner.lock.fetch_sub(UPGRADED, Ordering::AcqRel);
49140fe15e0SLoGin         preempt_enable();
492f1284c35SGou Ngai         //这里为啥要AcqRel? Release应该就行了?
493f1284c35SGou Ngai     }
494f1284c35SGou Ngai }
495f1284c35SGou Ngai 
496f1284c35SGou Ngai impl<'rwlock, T> Drop for RwLockWriteGuard<'rwlock, T> {
497f1284c35SGou Ngai     fn drop(&mut self) {
498f1284c35SGou Ngai         debug_assert_eq!(self.inner.lock.load(Ordering::Relaxed) & WRITER, WRITER);
499f1284c35SGou Ngai         self.inner
500f1284c35SGou Ngai             .lock
501f1284c35SGou Ngai             .fetch_and(!(WRITER | UPGRADED), Ordering::Release);
50240fe15e0SLoGin 
50340fe15e0SLoGin         preempt_enable();
504f1284c35SGou Ngai     }
505f1284c35SGou Ngai }
506