pub struct RwLockIrqSafe<T: ?Sized> { /* private fields */ }
Expand description

A simple wrapper around a RwLock whose guards disable interrupts properly

Implementations§

source§

impl<T> RwLockIrqSafe<T>

source

pub const fn new(data: T) -> RwLockIrqSafe<T>

Creates a new spinlock wrapping the supplied data.

May be used statically:

static RW_LOCK_IRQ_SAFE: RwLockIrqSafe<()> = RwLockIrqSafe::new(());

fn demo() {
    let lock = RW_LOCK_IRQ_SAFE.read();
    // do something with lock
    drop(lock);
}
source

pub fn into_inner(self) -> T

Consumes this RwLockIrqSafe, returning the underlying data.

source§

impl<T: ?Sized> RwLockIrqSafe<T>

source

pub fn read<'a>(&'a self) -> RwLockIrqSafeReadGuard<'a, T>

Locks this RwLockIrqSafe with shared read access, blocking the current thread until it can be acquired.

The calling thread will be blocked until there are no more writers which hold the lock. There may be other readers currently inside the lock when this method returns. This method does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.

Returns an RAII guard which will release this thread’s shared access once it is dropped, along with restoring interrupts.

let mylock = irq_safety::RwLockIrqSafe::new(0);
{
    let mut data = mylock.read();
    // The lock is now locked, interrupts are disabled, and the data can be read
    println!("{}", *data);
    // The lock is dropped and interrupts are restored to their prior state
}
source

pub fn try_read(&self) -> Option<RwLockIrqSafeReadGuard<'_, T>>

Attempt to acquire this lock with shared read access.

This function will never block and will return immediately if read would otherwise succeed. Returns Some of an RAII guard which will release the shared access of this thread when dropped, or None if the access could not be granted. This method does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.

let mylock = irq_safety::RwLockIrqSafe::new(0);
{
    match mylock.try_read() {
        Some(data) => {
            // The lock is now locked and the data can be read
            println!("{}", *data);
            // The lock is dropped
        },
        None => (), // no cigar
    };
}
source

pub fn reader_count(&self) -> usize

Return the number of readers that currently hold the lock (including upgradable readers).

Safety

This function provides no synchronization guarantees and so its result should be considered ‘out of date’ the instant it is called. Do not use it for synchronization purposes. However, it may be useful as a heuristic.

source

pub fn writer_count(&self) -> usize

Return the number of writers that currently hold the lock.

Because RwLockIrqSafe guarantees exclusive mutable access, this function may only return either 0 or 1.

Safety

This function provides no synchronization guarantees and so its result should be considered ‘out of date’ the instant it is called. Do not use it for synchronization purposes. However, it may be useful as a heuristic.

source

pub unsafe fn force_read_decrement(&self)

Force decrement the reader count.

This is extremely unsafe if there are outstanding RwLockReadGuards live, or if called more times than read has been called, but can be useful in FFI contexts where the caller doesn’t know how to deal with RAII.

source

pub unsafe fn force_write_unlock(&self)

Force unlock exclusive write access.

This is extremely unsafe if there are outstanding RwLockWriteGuards live, or if called when there are current readers, but can be useful in FFI contexts where the caller doesn’t know how to deal with RAII.

source

pub fn write<'a>(&'a self) -> RwLockIrqSafeWriteGuard<'a, T>

Lock this rwlock with exclusive write access, blocking the current thread until it can be acquired.

This function will not return while other writers or other readers currently have access to the lock.

Returns an RAII guard which will drop the write access of this rwlock when dropped.

let mylock = irq_safety::RwLockIrqSafe::new(0);
{
    let mut data = mylock.write();
    // The lock is now locked and the data can be written
    *data += 1;
    // The lock is dropped
}
source

pub fn try_write(&self) -> Option<RwLockIrqSafeWriteGuard<'_, T>>

Attempt to lock this rwlock with exclusive write access.

This function does not ever block, and it will return None if a call to write would otherwise block. If successful, an RAII guard is returned.

let mylock = irq_safety::RwLockIrqSafe::new(0);
{
    match mylock.try_write() {
        Some(mut data) => {
            // The lock is now locked and the data can be written
            *data += 1;
            // The lock is implicitly dropped
        },
        None => (), // no cigar
    };
}
source

pub fn get_mut(&mut self) -> &mut T

Returns a mutable reference to the underlying data.

Since this call borrows the RwLockIrqSafe mutably, and a mutable reference is guaranteed to be exclusive in Rust, no actual locking needs to take place – the mutable borrow statically guarantees no locks exist. As such, this is a ‘zero-cost’ operation.

Example
let mut lock = irq_safety::RwLockIrqSafe::new(0);
*lock.get_mut() = 10;
assert_eq!(*lock.lock(), 10);

Trait Implementations§

source§

impl<T: ?Sized + Debug> Debug for RwLockIrqSafe<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: ?Sized + Default> Default for RwLockIrqSafe<T>

source§

fn default() -> RwLockIrqSafe<T>

Returns the “default value” for a type. Read more
source§

impl<T: ?Sized + Send> Send for RwLockIrqSafe<T>

source§

impl<T: ?Sized + Send + Sync> Sync for RwLockIrqSafe<T>

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for RwLockIrqSafe<T>

§

impl<T: ?Sized> Unpin for RwLockIrqSafe<T>where T: Unpin,

§

impl<T: ?Sized> UnwindSafe for RwLockIrqSafe<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.