RefCell->UPSafeCell

This commit is contained in:
Yifan Wu 2021-07-09 21:18:51 +08:00
parent 5a40784918
commit f1aabb5e0e
12 changed files with 65 additions and 22 deletions

3
os/src/sync/mod.rs Normal file
View file

@ -0,0 +1,3 @@
mod up;
pub use up::UPSafeCell;

27
os/src/sync/up.rs Normal file
View file

@ -0,0 +1,27 @@
/// Wrap a static data structure inside it so that we are
/// able to access it without any `unsafe`.
///
/// We should only use it in uniprocessor.
///
/// In order to get mutable reference of inner data, call
/// `upsafe_access`.
pub struct UPSafeCell<T> {
/// inner data
data: T,
}
unsafe impl<T> Sync for UPSafeCell<T> {}
impl<T> UPSafeCell<T> {
/// User is responsible to guarantee that inner struct is only used in
/// uniprocessor.
pub unsafe fn new(value: T) -> Self {
Self { data: value, }
}
/// Mention that user should hold exactly one &mut T at a time.
pub fn upsafe_access(&self) -> &mut T {
unsafe {
&mut *(&self.data as *const _ as usize as *mut T)
}
}
}