os/sync/
up.rs

1use core::cell::{RefCell, RefMut};
2
3/// Wrap a static data structure inside it so that we are
4/// able to access it without any `unsafe`.
5///
6/// We should only use it in uniprocessor.
7///
8/// In order to get mutable reference of inner data, call
9/// `exclusive_access`.
10pub struct UPSafeCell<T> {
11    /// inner data
12    inner: RefCell<T>,
13}
14
15unsafe impl<T> Sync for UPSafeCell<T> {}
16
17impl<T> UPSafeCell<T> {
18    /// User is responsible to guarantee that inner struct is only used in
19    /// uniprocessor.
20    pub unsafe fn new(value: T) -> Self {
21        Self {
22            inner: RefCell::new(value),
23        }
24    }
25    /// Panic if the data has been borrowed.
26    pub fn exclusive_access(&self) -> RefMut<'_, T> {
27        self.inner.borrow_mut()
28    }
29}