Struct gstd::sync::RwLock

source ·
pub struct RwLock<T> { /* private fields */ }
Expand description

A reader-writer lock.

This type of lock allows a number of readers or at most one writer at any point in time. The write portion of this lock typically allows modification of the underlying data (exclusive access) and the read portion of this lock typically allows for read-only access (shared access).

In comparison, a Mutex does not distinguish between readers or writers that acquire the lock, therefore blocking any actors waiting for the lock to become available. An RwLock will allow any number of readers to acquire the lock as long as a writer is not holding the lock.

The type parameter T represents the data that this lock protects. The RAII guards returned from the locking methods implement Deref (and DerefMut for the write methods) to allow access to the content of the lock.

§Examples

The following program processes several messages. It locks the RwLock for reading when processing one of the get commands and for writing in the case of the inc command.

use gstd::{msg, sync::RwLock, ActorId};

static mut DEST: ActorId = ActorId::zero();
static RWLOCK: RwLock<u32> = RwLock::new(0);

#[no_mangle]
extern "C" fn init() {
    // `some_address` can be obtained from the init payload
    # let some_address = ActorId::zero();
    unsafe { DEST = some_address };
}

#[gstd::async_main]
async fn main() {
    let payload = msg::load_bytes().expect("Unable to load payload bytes");

    match payload.as_slice() {
        b"get" => {
            msg::reply(*RWLOCK.read().await, 0).unwrap();
        }
        b"inc" => {
            let mut val = RWLOCK.write().await;
            *val += 1;
        }
        b"ping&get" => {
            let _ = msg::send_bytes_for_reply(unsafe { DEST }, b"PING", 0, 0)
                .expect("Unable to send bytes")
                .await
                .expect("Error in async message processing");
            msg::reply(*RWLOCK.read().await, 0).unwrap();
        }
        b"inc&ping" => {
            let mut val = RWLOCK.write().await;
            *val += 1;
            let _ = msg::send_bytes_for_reply(unsafe { DEST }, b"PING", 0, 0)
                .expect("Unable to send bytes")
                .await
                .expect("Error in async message processing");
        }
        b"get&ping" => {
            let val = RWLOCK.read().await;
            let _ = msg::send_bytes_for_reply(unsafe { DEST }, b"PING", 0, 0)
                .expect("Unable to send bytes")
                .await
                .expect("Error in async message processing");
            msg::reply(*val, 0).unwrap();
        }
        _ => {
            let _write = RWLOCK.write().await;
            RWLOCK.read().await;
        }
    }
}
# fn main() {}

Implementations§

source§

impl<T> RwLock<T>

source

pub const READERS_LIMIT: u8 = 32u8

Limit of readers for RwLock

source

pub const fn new(t: T) -> RwLock<T>

Create a new instance of an RwLock<T> which is unlocked.

source

pub fn read(&self) -> RwLockReadFuture<'_, T>

Locks this rwlock with shared read access, protecting the subsequent code from executing by other actors until it can be acquired.

The underlying code section will be blocked until there are no more writers who 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.

source

pub fn write(&self) -> RwLockWriteFuture<'_, T>

Locks this rwlock with exclusive write access, blocking the underlying code section 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.

Trait Implementations§

source§

impl<T: Default> Default for RwLock<T>

source§

fn default() -> Self

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

impl<T> From<T> for RwLock<T>

source§

fn from(t: T) -> Self

Converts to this type from the input type.
source§

impl<T> Sync for RwLock<T>

Auto Trait Implementations§

§

impl<T> !Freeze for RwLock<T>

§

impl<T> !RefUnwindSafe for RwLock<T>

§

impl<T> Send for RwLock<T>
where T: Send,

§

impl<T> Unpin for RwLock<T>
where T: Unpin,

§

impl<T> UnwindSafe for RwLock<T>
where T: UnwindSafe,

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<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.
§

impl<T> JsonSchemaMaybe for T