mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-19 00:46:31 +00:00
新增rust版本的lockref (#135)
* new:Rust封装cpu_relax(),通过pause指令,让cpu休息一会儿。降低空转功耗 * new: Rust版本的lockref * Rust的RawSpinlock新增is_locked()和set_value()方法。 * lockref文档
This commit is contained in:
7
kernel/src/arch/x86_64/asm/cmpxchg.c
Normal file
7
kernel/src/arch/x86_64/asm/cmpxchg.c
Normal 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;
|
||||
}
|
12
kernel/src/arch/x86_64/asm/cmpxchg.rs
Normal file
12
kernel/src/arch/x86_64/asm/cmpxchg.rs
Normal 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;
|
||||
}
|
@ -2,4 +2,5 @@ pub mod irqflags;
|
||||
#[macro_use]
|
||||
pub mod current;
|
||||
pub mod ptrace;
|
||||
pub mod bitops;
|
||||
pub mod bitops;
|
||||
pub mod cmpxchg;
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
Reference in New Issue
Block a user