pub struct MapleGuard<'tree, T: ForeignOwnable>(/* private fields */);
Expand description
Implementations§
Source§impl<'tree, T: ForeignOwnable> MapleGuard<'tree, T>
impl<'tree, T: ForeignOwnable> MapleGuard<'tree, T>
Sourcepub fn ma_state(&mut self, first: usize, end: usize) -> MaState<'_, T>
pub fn ma_state(&mut self, first: usize, end: usize) -> MaState<'_, T>
Create a MaState
protected by this lock guard.
Sourcepub fn load(&mut self, index: usize) -> Option<T::BorrowedMut<'_>>
pub fn load(&mut self, index: usize) -> Option<T::BorrowedMut<'_>>
Load the value at the given index.
§Examples
Read the value while holding the spinlock.
use kernel::maple_tree::MapleTree;
let tree = KBox::pin_init(MapleTree::<KBox<i32>>::new(), GFP_KERNEL)?;
let ten = KBox::new(10, GFP_KERNEL)?;
let twenty = KBox::new(20, GFP_KERNEL)?;
tree.insert(100, ten, GFP_KERNEL)?;
tree.insert(200, twenty, GFP_KERNEL)?;
let mut lock = tree.lock();
assert_eq!(lock.load(100).map(|v| *v), Some(10));
assert_eq!(lock.load(200).map(|v| *v), Some(20));
assert_eq!(lock.load(300).map(|v| *v), None);
Increment refcount under the lock, to keep value alive afterwards.
use kernel::maple_tree::MapleTree;
use kernel::sync::Arc;
let tree = KBox::pin_init(MapleTree::<Arc<i32>>::new(), GFP_KERNEL)?;
let ten = Arc::new(10, GFP_KERNEL)?;
let twenty = Arc::new(20, GFP_KERNEL)?;
tree.insert(100, ten, GFP_KERNEL)?;
tree.insert(200, twenty, GFP_KERNEL)?;
// Briefly take the lock to increment the refcount.
let value = tree.lock().load(100).map(Arc::from);
// At this point, another thread might remove the value.
tree.erase(100);
// But we can still access it because we took a refcount.
assert_eq!(value.map(|v| *v), Some(10));
Trait Implementations§
Source§impl<'tree, T: ForeignOwnable> Drop for MapleGuard<'tree, T>
impl<'tree, T: ForeignOwnable> Drop for MapleGuard<'tree, T>
Auto Trait Implementations§
impl<'tree, T> Freeze for MapleGuard<'tree, T>
impl<'tree, T> !RefUnwindSafe for MapleGuard<'tree, T>
impl<'tree, T> !Send for MapleGuard<'tree, T>
impl<'tree, T> !Sync for MapleGuard<'tree, T>
impl<'tree, T> Unpin for MapleGuard<'tree, T>
impl<'tree, T> !UnwindSafe for MapleGuard<'tree, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> PinInit<T> for T
impl<T> PinInit<T> for T
Source§unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>
unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>
Initializes
slot
. Read more