Struct MapleGuard

Source
pub struct MapleGuard<'tree, T: ForeignOwnable>(/* private fields */);
Expand description

A reference to a MapleTree that owns the inner lock.

§Invariants

This guard owns the inner spinlock.

Implementations§

Source§

impl<'tree, T: ForeignOwnable> MapleGuard<'tree, T>

Source

pub fn ma_state(&mut self, first: usize, end: usize) -> MaState<'_, T>

Create a MaState protected by this lock guard.

Source

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>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Init<T> for T

Source§

unsafe fn __init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PinInit<T> for T

Source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.