Type Alias SpinLock

Source
pub type SpinLock<T> = Lock<T, SpinLockBackend>;
Expand description

A spinlock.

Exposes the kernel’s spinlock_t. When multiple CPUs attempt to lock the same spinlock, only one at a time is allowed to progress, the others will block (spinning) until the spinlock is unlocked, at which point another CPU will be allowed to make progress.

Instances of SpinLock need a lock class and to be pinned. The recommended way to create such instances is with the pin_init and new_spinlock macros.

§Examples

The following example shows how to declare, allocate and initialise a struct (Example) that contains an inner struct (Inner) that is protected by a spinlock.

use kernel::sync::{new_spinlock, SpinLock};

struct Inner {
    a: u32,
    b: u32,
}

#[pin_data]
struct Example {
    c: u32,
    #[pin]
    d: SpinLock<Inner>,
}

impl Example {
    fn new() -> impl PinInit<Self> {
        pin_init!(Self {
            c: 10,
            d <- new_spinlock!(Inner { a: 20, b: 30 }),
        })
    }
}

// Allocate a boxed `Example`.
let e = KBox::pin_init(Example::new(), GFP_KERNEL)?;
assert_eq!(e.c, 10);
assert_eq!(e.d.lock().a, 20);
assert_eq!(e.d.lock().b, 30);

The following example shows how to use interior mutability to modify the contents of a struct protected by a spinlock despite only having a shared reference:

use kernel::sync::SpinLock;

struct Example {
    a: u32,
    b: u32,
}

fn example(m: &SpinLock<Example>) {
    let mut guard = m.lock();
    guard.a += 10;
    guard.b += 20;
}

Aliased Type§

struct SpinLock<T> { /* private fields */ }

Implementations

Source§

impl<B: Backend> Lock<(), B>

Source

pub unsafe fn from_raw<'a>(ptr: *mut B::State) -> &'a Self

Constructs a Lock from a raw pointer.

This can be useful for interacting with a lock which was initialised outside of Rust.

§Safety

The caller promises that ptr points to a valid initialised instance of State during the whole lifetime of 'a.

Source§

impl<T, B: Backend> Lock<T, B>

Source

pub fn new( t: T, name: &'static CStr, key: Pin<&'static LockClassKey>, ) -> impl PinInit<Self>

Constructs a new lock initialiser.

Source§

impl<T: ?Sized, B: Backend> Lock<T, B>

Source

pub fn lock(&self) -> Guard<'_, T, B>

Acquires the lock and gives the caller access to the data protected by it.

Source

pub fn try_lock(&self) -> Option<Guard<'_, T, B>>

Tries to acquire the lock.

Returns a guard that can be used to access the data protected by the lock if successful.

Trait Implementations

Source§

impl<T: ?Sized, B: Backend> HasPinData for Lock<T, B>

Source§

type PinData = __ThePinData<T, B>

Source§

unsafe fn __pin_data() -> Self::PinData

Source§

impl<T: ?Sized + Send, B: Backend> Send for Lock<T, B>

Source§

impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B>