xref: /DragonOS/kernel/src/libs/spinlock.rs (revision 6cb769c423b09e88fea1763210200a716477be0a)
1 #![allow(dead_code)]
2 use core::ptr::read_volatile;
3 
4 use crate::arch::x86_64::asm::irqflags::{local_irq_restore, local_irq_save};
5 use crate::include::bindings::bindings::{spin_lock, spin_unlock, spinlock_t};
6 
7 /// @brief 保存中断状态到flags中,关闭中断,并对自旋锁加锁
8 pub fn spin_lock_irqsave(lock: *mut spinlock_t, flags: &mut u64) {
9     local_irq_save(flags);
10     unsafe {
11         spin_lock(lock);
12     }
13 }
14 
15 /// @brief 恢复rflags以及中断状态并解锁自旋锁
16 #[no_mangle]
17 pub fn spin_unlock_irqrestore(lock: *mut spinlock_t, flags: &u64) {
18     unsafe {
19         spin_unlock(lock);
20     }
21     // kdebug!("123");
22     local_irq_restore(flags);
23 }
24 
25 /// 判断一个自旋锁是否已经被加锁
26 pub fn spin_is_locked(lock: &spinlock_t) -> bool {
27     let val = unsafe { read_volatile(&lock.lock as *const i8) };
28 
29     return if val == 0 { true } else { false };
30 }
31 
32 impl Default for spinlock_t {
33     fn default() -> Self {
34         Self { lock: 1 }
35     }
36 }