1 // Copyright 2024 The percore Authors. 2 // This project is dual-licensed under Apache 2.0 and MIT terms. 3 // See LICENSE-APACHE and LICENSE-MIT for details. 4 5 use crate::ExceptionFree; 6 use core::cell::{RefCell, RefMut}; 7 8 /// Allows access to the given value only while exceptions are masked, allowing it to be shared 9 /// between exception contexts on a given core. 10 pub struct ExceptionLock<T> { 11 value: T, 12 } 13 14 impl<T> ExceptionLock<T> { 15 /// Creates a new `ExceptionLock` containing the given value. new(value: T) -> Self16 pub const fn new(value: T) -> Self { 17 Self { value } 18 } 19 20 /// Gets a unique reference to the contents of the cell, given a token proving that exceptions 21 /// are currently masked. borrow<'cs>(&'cs self, _: ExceptionFree<'cs>) -> &'cs T22 pub fn borrow<'cs>(&'cs self, _: ExceptionFree<'cs>) -> &'cs T { 23 &self.value 24 } 25 } 26 27 impl<T> ExceptionLock<RefCell<T>> { 28 /// Gets a unique reference to the contents of the `RefCell`, given a token proving that 29 /// exceptions are currently masked. borrow_mut<'cs>(&'cs self, token: ExceptionFree<'cs>) -> RefMut<'cs, T>30 pub fn borrow_mut<'cs>(&'cs self, token: ExceptionFree<'cs>) -> RefMut<'cs, T> { 31 self.borrow(token).borrow_mut() 32 } 33 34 /// Returns a raw pointer to the contents of the cell. 35 /// 36 /// This must not be dereferenced while any `RefMut` to the same cell exists. as_ptr(&self) -> *mut T37 pub fn as_ptr(&self) -> *mut T { 38 self.value.as_ptr() 39 } 40 } 41