新增rust版本的lockref (#135)

* new:Rust封装cpu_relax(),通过pause指令,让cpu休息一会儿。降低空转功耗

* new: Rust版本的lockref

* Rust的RawSpinlock新增is_locked()和set_value()方法。

* lockref文档
This commit is contained in:
login
2023-01-03 23:09:25 +08:00
committed by GitHub
parent 2726f101b4
commit 61de2cdc3f
10 changed files with 557 additions and 7 deletions

View File

@ -0,0 +1,7 @@
#include <arch/x86_64/include/asm/cmpxchg.h>
bool __try_cmpxchg_q(uint64_t *ptr, uint64_t *old_ptr, uint64_t *new_ptr)
{
bool success = __raw_try_cmpxchg(ptr, old_ptr, *new_ptr, 8);
return success;
}

View File

@ -0,0 +1,12 @@
// 该函数在cmpxchg.c中实现
extern "C" {
fn __try_cmpxchg_q(ptr: *mut u64, old_ptr: *mut u64, new_ptr: *mut u64) -> bool;
}
/// @brief 封装lock cmpxchg指令
/// 由于Rust实现这部分的内联汇编比较麻烦实在想不出办法因此使用C的实现。
#[inline]
pub unsafe fn try_cmpxchg_q(ptr: *mut u64, old_ptr: *mut u64, new_ptr: *mut u64) -> bool {
let retval = __try_cmpxchg_q(ptr, old_ptr, new_ptr);
return retval;
}

View File

@ -2,4 +2,5 @@ pub mod irqflags;
#[macro_use]
pub mod current;
pub mod ptrace;
pub mod bitops;
pub mod bitops;
pub mod cmpxchg;

View File

@ -14,3 +14,10 @@ pub fn arch_current_apic_id() -> u8 {
}
return (cpuid_res >> 24) as u8;
}
/// @brief 通过pause指令让cpu休息一会儿。降低空转功耗
pub fn cpu_relax() {
unsafe {
asm!("pause");
}
}

View File

@ -79,3 +79,5 @@ extern void __cmpxchg_wrong_size(void) __compiletime_error("Bad argument size fo
#define arch_try_cmpxchg(ptr, old_ptr, new_ptr) \
__raw_try_cmpxchg((ptr), (old_ptr), (new_ptr), sizeof(*ptr))
bool __try_cmpxchg_q(uint64_t *ptr, uint64_t *old_ptr, uint64_t *new_ptr);