xref: /DragonOS/kernel/src/libs/rwlock.rs (revision da152319797436368304cbc3f85a3b9ec049134b)
1 #![allow(dead_code)]
2 use core::{
3     cell::UnsafeCell,
4     hint::spin_loop,
5     mem::{self, ManuallyDrop},
6     ops::{Deref, DerefMut},
7     sync::atomic::{AtomicU32, Ordering},
8 };
9 
10 use system_error::SystemError;
11 
12 use crate::{
13     arch::CurrentIrqArch,
14     exception::{InterruptArch, IrqFlagsGuard},
15     process::ProcessManager,
16 };
17 
18 ///RwLock读写锁
19 
20 /// @brief READER位占据从右往左数第三个比特位
21 const READER: u32 = 1 << 2;
22 
23 /// @brief UPGRADED位占据从右到左数第二个比特位
24 const UPGRADED: u32 = 1 << 1;
25 
26 /// @brief WRITER位占据最右边的比特位
27 const WRITER: u32 = 1;
28 
29 const READER_BIT: u32 = 2;
30 
31 /// @brief 读写锁的基本数据结构
32 /// @param lock 32位原子变量,最右边的两位从左到右分别是UPGRADED,WRITER (标志位)
33 ///             剩下的bit位存储READER数量(除了MSB)
34 ///             对于标志位,0代表无, 1代表有
35 ///             对于剩下的比特位表征READER的数量的多少
36 ///             lock的MSB必须为0,否则溢出
37 #[derive(Debug)]
38 pub struct RwLock<T> {
39     lock: AtomicU32,
40     data: UnsafeCell<T>,
41 }
42 
43 /// @brief  READER守卫的数据结构
44 /// @param lock 是对RwLock的lock属性值的只读引用
45 pub struct RwLockReadGuard<'a, T: 'a> {
46     data: *const T,
47     lock: &'a AtomicU32,
48     irq_guard: Option<IrqFlagsGuard>,
49 }
50 
51 /// @brief UPGRADED是介于READER和WRITER之间的一种锁,它可以升级为WRITER,
52 ///        UPGRADED守卫的数据结构,注册UPGRADED锁只需要查看UPGRADED和WRITER的比特位
53 ///        但是当UPGRADED守卫注册后,不允许有新的读者锁注册
54 /// @param inner    是对RwLock数据结构的只读引用
55 pub struct RwLockUpgradableGuard<'a, T: 'a> {
56     data: *const T,
57     inner: &'a RwLock<T>,
58     irq_guard: Option<IrqFlagsGuard>,
59 }
60 
61 /// @brief WRITER守卫的数据结构
62 /// @param data     RwLock的data的可变引用
63 /// @param inner    是对RwLock数据结构的只读引用
64 pub struct RwLockWriteGuard<'a, T: 'a> {
65     data: *mut T,
66     inner: &'a RwLock<T>,
67     irq_guard: Option<IrqFlagsGuard>,
68 }
69 
70 unsafe impl<T: Send> Send for RwLock<T> {}
71 unsafe impl<T: Send + Sync> Sync for RwLock<T> {}
72 
73 /// @brief RwLock的API
74 impl<T> RwLock<T> {
75     #[inline]
76     /// @brief  RwLock的初始化
77     pub const fn new(data: T) -> Self {
78         return RwLock {
79             lock: AtomicU32::new(0),
80             data: UnsafeCell::new(data),
81         };
82     }
83 
84     #[allow(dead_code)]
85     #[inline]
86     /// @brief 将读写锁的皮扒掉,返回内在的data,返回的是一个真身而非引用
87     pub fn into_inner(self) -> T {
88         let RwLock { data, .. } = self;
89         return data.into_inner();
90     }
91 
92     #[allow(dead_code)]
93     #[inline]
94     /// @brief 返回data的raw pointer,
95     /// unsafe
96     pub fn as_mut_ptr(&self) -> *mut T {
97         return self.data.get();
98     }
99 
100     #[allow(dead_code)]
101     #[inline]
102     /// @brief 获取实时的读者数并尝试加1,如果增加值成功则返回增加1后的读者数,否则panic
103     fn current_reader(&self) -> Result<u32, SystemError> {
104         const MAX_READERS: u32 = core::u32::MAX >> READER_BIT >> 1; //右移3位
105 
106         let value = self.lock.fetch_add(READER, Ordering::Acquire);
107         //value二进制形式的MSB不能为1, 否则导致溢出
108 
109         if value > MAX_READERS << READER_BIT {
110             self.lock.fetch_sub(READER, Ordering::Release);
111             //panic!("Too many lock readers, cannot safely proceed");
112             return Err(SystemError::EOVERFLOW);
113         } else {
114             return Ok(value);
115         }
116     }
117 
118     #[allow(dead_code)]
119     #[inline]
120     /// @brief 尝试获取READER守卫
121     pub fn try_read(&self) -> Option<RwLockReadGuard<T>> {
122         ProcessManager::preempt_disable();
123         let r = self.inner_try_read();
124         if r.is_none() {
125             ProcessManager::preempt_enable();
126         }
127         return r;
128     }
129 
130     fn inner_try_read(&self) -> Option<RwLockReadGuard<T>> {
131         let reader_value = self.current_reader();
132         //得到自增后的reader_value, 包括了尝试获得READER守卫的进程
133 
134         let value = if let Ok(rv) = reader_value {
135             rv
136         } else {
137             return None;
138         };
139 
140         //判断有没有writer和upgrader
141         //注意, 若upgrader存在,已经存在的读者继续占有锁,但新读者不允许获得锁
142         if value & (WRITER | UPGRADED) != 0 {
143             self.lock.fetch_sub(READER, Ordering::Release);
144             return None;
145         } else {
146             return Some(RwLockReadGuard {
147                 data: unsafe { &*self.data.get() },
148                 lock: &self.lock,
149                 irq_guard: None,
150             });
151         }
152     }
153 
154     #[allow(dead_code)]
155     #[inline]
156     /// @brief 获得READER的守卫
157     pub fn read(&self) -> RwLockReadGuard<T> {
158         loop {
159             match self.try_read() {
160                 Some(guard) => return guard,
161                 None => spin_loop(),
162             }
163         } //忙等待
164     }
165 
166     /// 关中断并获取读者守卫
167     pub fn read_irqsave(&self) -> RwLockReadGuard<T> {
168         loop {
169             let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
170             match self.try_read() {
171                 Some(mut guard) => {
172                     guard.irq_guard = Some(irq_guard);
173                     return guard;
174                 }
175                 None => spin_loop(),
176             }
177         }
178     }
179 
180     /// 尝试关闭中断并获取读者守卫
181     pub fn try_read_irqsave(&self) -> Option<RwLockReadGuard<T>> {
182         let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
183         if let Some(mut guard) = self.try_read() {
184             guard.irq_guard = Some(irq_guard);
185             return Some(guard);
186         } else {
187             return None;
188         }
189     }
190 
191     #[allow(dead_code)]
192     #[inline]
193     /// @brief 获取读者+UPGRADER的数量, 不能保证能否获得同步值
194     pub fn reader_count(&self) -> u32 {
195         let state = self.lock.load(Ordering::Relaxed);
196         return state / READER + (state & UPGRADED) / UPGRADED;
197     }
198 
199     #[allow(dead_code)]
200     #[inline]
201     /// @brief 获取写者数量,不能保证能否获得同步值
202     pub fn writer_count(&self) -> u32 {
203         return (self.lock.load(Ordering::Relaxed) & WRITER) / WRITER;
204     }
205 
206     #[cfg(any(target_arch = "x86_64", target_arch = "riscv64"))]
207     #[allow(dead_code)]
208     #[inline]
209     /// @brief 尝试获得WRITER守卫
210     pub fn try_write(&self) -> Option<RwLockWriteGuard<T>> {
211         ProcessManager::preempt_disable();
212         let r = self.inner_try_write();
213         if r.is_none() {
214             ProcessManager::preempt_enable();
215         }
216 
217         return r;
218     } //当架构为arm时,有些代码需要作出调整compare_exchange=>compare_exchange_weak
219 
220     #[cfg(any(target_arch = "x86_64", target_arch = "riscv64"))]
221     #[allow(dead_code)]
222     #[inline]
223     pub fn try_write_irqsave(&self) -> Option<RwLockWriteGuard<T>> {
224         ProcessManager::preempt_disable();
225         let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
226         let r = self.inner_try_write().map(|mut g| {
227             g.irq_guard = Some(irq_guard);
228             g
229         });
230         if r.is_none() {
231             ProcessManager::preempt_enable();
232         }
233 
234         return r;
235     }
236 
237     #[cfg(any(target_arch = "x86_64", target_arch = "riscv64"))]
238     #[allow(dead_code)]
239     fn inner_try_write(&self) -> Option<RwLockWriteGuard<T>> {
240         let res: bool = self
241             .lock
242             .compare_exchange(0, WRITER, Ordering::Acquire, Ordering::Relaxed)
243             .is_ok();
244         //只有lock大小为0的时候能获得写者守卫
245         if res {
246             return Some(RwLockWriteGuard {
247                 data: unsafe { &mut *self.data.get() },
248                 inner: self,
249                 irq_guard: None,
250             });
251         } else {
252             return None;
253         }
254     }
255 
256     #[allow(dead_code)]
257     #[inline]
258     /// @brief 获得WRITER守卫
259     pub fn write(&self) -> RwLockWriteGuard<T> {
260         loop {
261             match self.try_write() {
262                 Some(guard) => return guard,
263                 None => spin_loop(),
264             }
265         }
266     }
267 
268     #[allow(dead_code)]
269     #[inline]
270     /// @brief 获取WRITER守卫并关中断
271     pub fn write_irqsave(&self) -> RwLockWriteGuard<T> {
272         loop {
273             let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
274             match self.try_write() {
275                 Some(mut guard) => {
276                     guard.irq_guard = Some(irq_guard);
277                     return guard;
278                 }
279                 None => spin_loop(),
280             }
281         }
282     }
283 
284     #[allow(dead_code)]
285     #[inline]
286     /// @brief 尝试获得UPGRADER守卫
287     pub fn try_upgradeable_read(&self) -> Option<RwLockUpgradableGuard<T>> {
288         ProcessManager::preempt_disable();
289         let r = self.inner_try_upgradeable_read();
290         if r.is_none() {
291             ProcessManager::preempt_enable();
292         }
293 
294         return r;
295     }
296 
297     #[allow(dead_code)]
298     pub fn try_upgradeable_read_irqsave(&self) -> Option<RwLockUpgradableGuard<T>> {
299         let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
300         ProcessManager::preempt_disable();
301         let mut r = self.inner_try_upgradeable_read();
302         if r.is_none() {
303             ProcessManager::preempt_enable();
304         } else {
305             r.as_mut().unwrap().irq_guard = Some(irq_guard);
306         }
307 
308         return r;
309     }
310 
311     fn inner_try_upgradeable_read(&self) -> Option<RwLockUpgradableGuard<T>> {
312         // 获得UPGRADER守卫不需要查看读者位
313         // 如果获得读者锁失败,不需要撤回fetch_or的原子操作
314         if self.lock.fetch_or(UPGRADED, Ordering::Acquire) & (WRITER | UPGRADED) == 0 {
315             return Some(RwLockUpgradableGuard {
316                 inner: self,
317                 data: unsafe { &mut *self.data.get() },
318                 irq_guard: None,
319             });
320         } else {
321             return None;
322         }
323     }
324 
325     #[allow(dead_code)]
326     #[inline]
327     /// @brief 获得UPGRADER守卫
328     pub fn upgradeable_read(&self) -> RwLockUpgradableGuard<T> {
329         loop {
330             match self.try_upgradeable_read() {
331                 Some(guard) => return guard,
332                 None => spin_loop(),
333             }
334         }
335     }
336 
337     #[inline]
338     /// @brief 获得UPGRADER守卫
339     pub fn upgradeable_read_irqsave(&self) -> RwLockUpgradableGuard<T> {
340         loop {
341             let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
342             match self.try_upgradeable_read() {
343                 Some(mut guard) => {
344                     guard.irq_guard = Some(irq_guard);
345                     return guard;
346                 }
347                 None => spin_loop(),
348             }
349         }
350     }
351 
352     #[allow(dead_code)]
353     #[inline]
354     //extremely unsafe behavior
355     /// @brief 强制减少READER数
356     pub unsafe fn force_read_decrement(&self) {
357         debug_assert!(self.lock.load(Ordering::Relaxed) & !WRITER > 0);
358         self.lock.fetch_sub(READER, Ordering::Release);
359     }
360 
361     #[allow(dead_code)]
362     #[inline]
363     //extremely unsafe behavior
364     /// @brief 强制给WRITER解锁
365     pub unsafe fn force_write_unlock(&self) {
366         debug_assert_eq!(self.lock.load(Ordering::Relaxed) & !(WRITER | UPGRADED), 0);
367         self.lock.fetch_and(!(WRITER | UPGRADED), Ordering::Release);
368     }
369 
370     #[allow(dead_code)]
371     pub unsafe fn get_mut(&mut self) -> &mut T {
372         unsafe { &mut *self.data.get() }
373     }
374 }
375 
376 impl<T: Default> Default for RwLock<T> {
377     fn default() -> Self {
378         Self::new(Default::default())
379     }
380 }
381 
382 /// @brief 由原有的值创建新的锁
383 impl<T> From<T> for RwLock<T> {
384     fn from(data: T) -> Self {
385         return Self::new(data);
386     }
387 }
388 
389 impl<'rwlock, T> RwLockReadGuard<'rwlock, T> {
390     /// @brief 释放守卫,获得保护的值的不可变引用
391     ///
392     /// ## Safety
393     ///
394     /// 由于这样做可能导致守卫在另一个线程中被释放,从而导致pcb的preempt count不正确,
395     /// 因此必须小心的手动维护好preempt count。
396     ///
397     /// 并且,leak还可能导致锁的状态不正确。因此请仔细考虑是否真的需要使用这个函数。
398     #[allow(dead_code)]
399     #[inline]
400     pub unsafe fn leak(this: Self) -> &'rwlock T {
401         let this = ManuallyDrop::new(this);
402         return unsafe { &*this.data };
403     }
404 }
405 
406 impl<'rwlock, T> RwLockUpgradableGuard<'rwlock, T> {
407     #[allow(dead_code)]
408     #[inline]
409     /// @brief 尝试将UPGRADER守卫升级为WRITER守卫
410     pub fn try_upgrade(mut self) -> Result<RwLockWriteGuard<'rwlock, T>, Self> {
411         let res = self.inner.lock.compare_exchange(
412             UPGRADED,
413             WRITER,
414             Ordering::Acquire,
415             Ordering::Relaxed,
416         );
417         //当且仅当只有UPGRADED守卫时可以升级
418 
419         if res.is_ok() {
420             let inner = self.inner;
421             let irq_guard = self.irq_guard.take();
422             mem::forget(self);
423 
424             Ok(RwLockWriteGuard {
425                 data: unsafe { &mut *inner.data.get() },
426                 inner,
427                 irq_guard,
428             })
429         } else {
430             Err(self)
431         }
432     }
433 
434     #[allow(dead_code)]
435     #[inline]
436     /// @brief 将upgrader升级成writer
437     pub fn upgrade(mut self) -> RwLockWriteGuard<'rwlock, T> {
438         loop {
439             self = match self.try_upgrade() {
440                 Ok(writeguard) => return writeguard,
441                 Err(former) => former,
442             };
443 
444             spin_loop();
445         }
446     }
447 
448     #[allow(dead_code)]
449     #[inline]
450     /// @brief UPGRADER降级为READER
451     pub fn downgrade(mut self) -> RwLockReadGuard<'rwlock, T> {
452         while self.inner.current_reader().is_err() {
453             spin_loop();
454         }
455 
456         let inner: &RwLock<T> = self.inner;
457         let irq_guard = self.irq_guard.take();
458         // 自动移去UPGRADED比特位
459         mem::drop(self);
460 
461         RwLockReadGuard {
462             data: unsafe { &*inner.data.get() },
463             lock: &inner.lock,
464             irq_guard,
465         }
466     }
467 
468     #[allow(dead_code)]
469     #[inline]
470     /// @brief 返回内部数据的引用,消除守卫
471     ///
472     /// ## Safety
473     ///
474     /// 由于这样做可能导致守卫在另一个线程中被释放,从而导致pcb的preempt count不正确,
475     /// 因此必须小心的手动维护好preempt count。
476     ///
477     /// 并且,leak还可能导致锁的状态不正确。因此请仔细考虑是否真的需要使用这个函数。
478     pub unsafe fn leak(this: Self) -> &'rwlock T {
479         let this: ManuallyDrop<RwLockUpgradableGuard<'_, T>> = ManuallyDrop::new(this);
480 
481         unsafe { &*this.data }
482     }
483 }
484 
485 impl<'rwlock, T> RwLockWriteGuard<'rwlock, T> {
486     #[allow(dead_code)]
487     #[inline]
488     /// @brief 返回内部数据的引用,消除守卫
489     ///
490     /// ## Safety
491     ///
492     /// 由于这样做可能导致守卫在另一个线程中被释放,从而导致pcb的preempt count不正确,
493     /// 因此必须小心的手动维护好preempt count。
494     ///
495     /// 并且,leak还可能导致锁的状态不正确。因此请仔细考虑是否真的需要使用这个函数。
496     pub unsafe fn leak(this: Self) -> &'rwlock T {
497         let this = ManuallyDrop::new(this);
498 
499         return unsafe { &*this.data };
500     }
501 
502     #[allow(dead_code)]
503     #[inline]
504     /// @brief 将WRITER降级为READER
505     pub fn downgrade(mut self) -> RwLockReadGuard<'rwlock, T> {
506         while self.inner.current_reader().is_err() {
507             spin_loop();
508         }
509         //本质上来说绝对保证没有任何读者
510 
511         let inner = self.inner;
512         let irq_guard = self.irq_guard.take();
513         mem::drop(self);
514 
515         return RwLockReadGuard {
516             data: unsafe { &*inner.data.get() },
517             lock: &inner.lock,
518             irq_guard,
519         };
520     }
521 
522     #[allow(dead_code)]
523     #[inline]
524     /// @brief 将WRITER降级为UPGRADER
525     pub fn downgrade_to_upgradeable(mut self) -> RwLockUpgradableGuard<'rwlock, T> {
526         debug_assert_eq!(
527             self.inner.lock.load(Ordering::Acquire) & (WRITER | UPGRADED),
528             WRITER
529         );
530 
531         self.inner.lock.store(UPGRADED, Ordering::Release);
532 
533         let inner = self.inner;
534 
535         let irq_guard = self.irq_guard.take();
536         mem::forget(self);
537 
538         return RwLockUpgradableGuard {
539             inner,
540             data: unsafe { &*inner.data.get() },
541             irq_guard,
542         };
543     }
544 }
545 
546 impl<'rwlock, T> Deref for RwLockReadGuard<'rwlock, T> {
547     type Target = T;
548 
549     fn deref(&self) -> &Self::Target {
550         return unsafe { &*self.data };
551     }
552 }
553 
554 impl<'rwlock, T> Deref for RwLockUpgradableGuard<'rwlock, T> {
555     type Target = T;
556 
557     fn deref(&self) -> &Self::Target {
558         return unsafe { &*self.data };
559     }
560 }
561 
562 impl<'rwlock, T> Deref for RwLockWriteGuard<'rwlock, T> {
563     type Target = T;
564 
565     fn deref(&self) -> &Self::Target {
566         return unsafe { &*self.data };
567     }
568 }
569 
570 impl<'rwlock, T> DerefMut for RwLockWriteGuard<'rwlock, T> {
571     fn deref_mut(&mut self) -> &mut Self::Target {
572         return unsafe { &mut *self.data };
573     }
574 }
575 
576 impl<'rwlock, T> Drop for RwLockReadGuard<'rwlock, T> {
577     fn drop(&mut self) {
578         debug_assert!(self.lock.load(Ordering::Relaxed) & !(WRITER | UPGRADED) > 0);
579         self.lock.fetch_sub(READER, Ordering::Release);
580         ProcessManager::preempt_enable();
581     }
582 }
583 
584 impl<'rwlock, T> Drop for RwLockUpgradableGuard<'rwlock, T> {
585     fn drop(&mut self) {
586         debug_assert_eq!(
587             self.inner.lock.load(Ordering::Relaxed) & (WRITER | UPGRADED),
588             UPGRADED
589         );
590         self.inner.lock.fetch_sub(UPGRADED, Ordering::AcqRel);
591         ProcessManager::preempt_enable();
592         //这里为啥要AcqRel? Release应该就行了?
593     }
594 }
595 
596 impl<'rwlock, T> Drop for RwLockWriteGuard<'rwlock, T> {
597     fn drop(&mut self) {
598         debug_assert_eq!(self.inner.lock.load(Ordering::Relaxed) & WRITER, WRITER);
599         self.inner
600             .lock
601             .fetch_and(!(WRITER | UPGRADED), Ordering::Release);
602         self.irq_guard.take();
603         ProcessManager::preempt_enable();
604     }
605 }
606