ariel_os::storage

Function lock

pub async fn lock() -> MutexGuard<'static, CriticalSectionRawMutex, Storage<Flash>>
Available on crate feature storage only.
Expand description

Gets a [MutexGuard] of the global Storage object.

This can be used to implement atomic RMW (like counters). It is not needed for using the global get(), insert() and remove() functions.

Note: don’t forget to drop the mutex guard returned by this.

Example:

{
    // By getting the Storage mutex directly, changing e.g., a counter,
    // can be done atomically w.r.t. concurrent access from the same firmware:
    let mut s = storage::lock().await;
    let value: Option<u32> = s.get("counter").await.unwrap();
    let value = value.unwrap_or_default();
    s.insert("counter", value + 1).await.unwrap();
}