Struct gstd::sync::Mutex

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

A mutual exclusion primitive useful for protecting shared data.

This mutex will block the execution waiting for the lock to become available. The mutex can be created via a new constructor. Each mutex has a type parameter which represents the data that it is protecting. The data can only be accessed through the RAII guard MutexGuard returned from lock, which guarantees that data access only occurs when the mutex is locked.

§Examples

This example (program A), after locking the mutex, sends the PING message to another program (program B) and waits for a reply. If any other program (program C) tries to invoke program A, it will wait until program A receives the PONG reply from program B and unlocks the mutex.

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

static mut DEST: ActorId = ActorId::zero();
static MUTEX: Mutex<()> = Mutex::new(());

#[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");
    if payload == b"START" {
        let _unused = MUTEX.lock().await;

        let reply = msg::send_bytes_for_reply(unsafe { DEST }, b"PING", 0, 0)
            .expect("Unable to send bytes")
            .await
            .expect("Error in async message processing");

        if reply == b"PONG" {
            msg::reply(b"SUCCESS", 0).unwrap();
        } else {
            msg::reply(b"FAIL", 0).unwrap();
        }
    }
}
# fn main() {}

Implementations§

source§

impl<T> Mutex<T>

source

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

Create a new mutex in an unlocked state ready for use.

source

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

Acquire a mutex, protecting the subsequent code from execution by other actors until the mutex hasn’t been unlocked.

This function will block access to the section of code by other programs or users that invoke the same program. If another actor reaches the code blocked by the mutex, it goes to the wait state until the mutex unlocks. RAII guard wrapped in the future is returned to allow scoped unlock of the lock. When the guard goes out of scope, the mutex will be unlocked.

Trait Implementations§

source§

impl<T: Default> Default for Mutex<T>

source§

fn default() -> Self

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

impl<T> From<T> for Mutex<T>

source§

fn from(t: T) -> Self

Converts to this type from the input type.
source§

impl<T> Sync for Mutex<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Mutex<T>

§

impl<T> !RefUnwindSafe for Mutex<T>

§

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

§

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

§

impl<T> UnwindSafe for Mutex<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