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>
impl<T> RwLock<T>
sourcepub const READERS_LIMIT: u8 = 32u8
pub const READERS_LIMIT: u8 = 32u8
Limit of readers for RwLock
sourcepub fn read(&self) -> RwLockReadFuture<'_, T> ⓘ
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.
sourcepub fn write(&self) -> RwLockWriteFuture<'_, T> ⓘ
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.