Struct spin::RwLock
[−]
[src]
pub struct RwLock<T> { // some fields omitted }
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).
The type parameter T
represents the data that this lock protects. It is
required that T
satisfies Send
to be shared across tasks and Sync
to
allow concurrent access through readers. The RAII guards returned from the
locking methods implement Deref
(and DerefMut
for the write
methods)
to allow access to the contained of the lock.
Based on https://jfdube.wordpress.com/2014/01/03/implementing-a-recursive-read-write-spinlock/
Methods
impl<T> RwLock<T>
[src]
const fn new(user_data: T) -> RwLock<T>
Creates a new spinlock wrapping the supplied data.
May be used statically:
#![feature(const_fn)] use spin; static RW_LOCK: spin::RwLock<()> = spin::RwLock::new(()); fn demo() { let lock = RW_LOCK.read(); // do something with lock drop(lock); }
fn read<'a>(&'a self) -> RwLockReadGuard<'a, T>
Locks this rwlock 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.
let mylock = spin::RwLock::new(0); { let mut data = mylock.read(); // The lock is now locked and the data can be read println!("{}", *data); // The lock is dropped }
fn try_read(&self) -> Option<RwLockReadGuard<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 = spin::RwLock::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 }; }
fn write<'a>(&'a self) -> RwLockWriteGuard<'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 = spin::RwLock::new(0); { let mut data = mylock.write(); // The lock is now locked and the data can be written *data += 1; // The lock is dropped }
fn try_write(&self) -> Option<RwLockWriteGuard<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 = spin::RwLock::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 }; }