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