mirror of
https://github.com/LearningOS/rust-based-os-comp2022.git
synced 2026-05-09 23:31:29 +08:00
add os[1-8]-ref for os refereces, add guide, add README
This commit is contained in:
3
os2-ref/src/sync/mod.rs
Normal file
3
os2-ref/src/sync/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
mod up;
|
||||
|
||||
pub use up::UPSafeCell;
|
||||
29
os2-ref/src/sync/up.rs
Normal file
29
os2-ref/src/sync/up.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use core::cell::{RefCell, RefMut};
|
||||
|
||||
/// 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
|
||||
/// `exclusive_access`.
|
||||
pub struct UPSafeCell<T> {
|
||||
/// inner data
|
||||
inner: RefCell<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 {
|
||||
inner: RefCell::new(value),
|
||||
}
|
||||
}
|
||||
/// Panic if the data has been borrowed.
|
||||
pub fn exclusive_access(&self) -> RefMut<'_, T> {
|
||||
self.inner.borrow_mut()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user