DragonOS/kernel/src/libs/lock_free_flags.rs
LoGin b5b571e026
修复内核的clippy检查报错 (#637)
修复内核的clippy检查报错
---------

Co-authored-by: Samuel Dai <947309196@qq.com>
Co-authored-by: Donkey Kane <109840258+xiaolin2004@users.noreply.github.com>
Co-authored-by: themildwind <107623059+themildwind@users.noreply.github.com>
Co-authored-by: GnoCiYeH <heyicong@dragonos.org>
Co-authored-by: MemoryShore <105195940+MemoryShore@users.noreply.github.com>
Co-authored-by: 曾俊 <110876916+ZZJJWarth@users.noreply.github.com>
Co-authored-by: sun5etop <146408999+sun5etop@users.noreply.github.com>
Co-authored-by: hmt <114841534+1037827920@users.noreply.github.com>
Co-authored-by: laokengwt <143977175+laokengwt@users.noreply.github.com>
Co-authored-by: TTaq <103996388+TTaq@users.noreply.github.com>
Co-authored-by: Jomo <2512364506@qq.com>
Co-authored-by: Samuel Dai <samuka007@qq.com>
Co-authored-by: sspphh <112558065+sspphh@users.noreply.github.com>
2024-03-22 23:26:39 +08:00

52 lines
1.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use core::{cell::UnsafeCell, fmt::Debug};
/// 一个无锁的标志位
///
/// 可与bitflags配合使用以实现无锁的标志位
///
/// ## Safety
///
/// 由于标识位的修改是无锁,且不保证原子性,因此需要使用者自行在别的机制中,确保
/// 哪怕标识位的值是老的,执行动作也不会有问题(或者有状态恢复机制)。
pub struct LockFreeFlags<T> {
inner: UnsafeCell<T>,
}
impl<T> LockFreeFlags<T> {
pub unsafe fn new(inner: T) -> Self {
Self {
inner: UnsafeCell::new(inner),
}
}
#[allow(clippy::mut_from_ref)]
pub fn get_mut(&self) -> &mut T {
unsafe {
(self.inner.get().as_ref().unwrap() as *const T as *mut T)
.as_mut()
.unwrap()
}
}
pub fn get(&self) -> &T {
unsafe { self.inner.get().as_ref().unwrap() }
}
}
unsafe impl<T: Sync> Sync for LockFreeFlags<T> {}
unsafe impl<T: Send> Send for LockFreeFlags<T> {}
impl<T: Clone> Clone for LockFreeFlags<T> {
fn clone(&self) -> Self {
unsafe { Self::new(self.get().clone()) }
}
}
impl<T: Debug> Debug for LockFreeFlags<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("LockFreeFlags")
.field("inner", self.get())
.finish()
}
}