pub struct RwLock<T: AsRaw> { /* private fields */ }
Expand description
Advisory reader-writer lock for files.
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).
Implementations§
source§impl<T: AsRaw> RwLock<T>
impl<T: AsRaw> RwLock<T>
sourcepub fn new(inner: T) -> Self
pub fn new(inner: T) -> Self
Create a new instance.
Examples
use fd_lock::RwLock;
use std::fs::File;
fn main() -> std::io::Result<()> {
let mut f = RwLock::new(File::open("foo.txt")?);
Ok(())
}
sourcepub fn read(&self) -> Result<RwLockReadGuard<'_, T>>
pub fn read(&self) -> Result<RwLockReadGuard<'_, T>>
Locks this lock 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.
Errors
On Unix this may return an ErrorKind::Interrupted
if the operation was
interrupted by a signal handler.
sourcepub fn try_read(&self) -> Result<RwLockReadGuard<'_, T>>
pub fn try_read(&self) -> Result<RwLockReadGuard<'_, T>>
Attempts to acquire this lock with shared read access.
If the access could not be granted at this time, then Err
is returned.
Otherwise, an RAII guard is returned which will release the shared access
when it is dropped.
This function does not block.
This function does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.
Errors
If the lock is already held and ErrorKind::WouldBlock
error is returned.
On Unix this may return an ErrorKind::Interrupted
if the operation was
interrupted by a signal handler.
sourcepub fn write(&mut self) -> Result<RwLockWriteGuard<'_, T>>
pub fn write(&mut self) -> Result<RwLockWriteGuard<'_, T>>
Locks this lock 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.
Errors
On Unix this may return an ErrorKind::Interrupted
if the operation was
interrupted by a signal handler.
sourcepub fn try_write(&mut self) -> Result<RwLockWriteGuard<'_, T>>
pub fn try_write(&mut self) -> Result<RwLockWriteGuard<'_, T>>
Attempts to lock this lock with exclusive write access.
If the lock could not be acquired at this time, then Err
is returned.
Otherwise, an RAII guard is returned which will release the lock when
it is dropped.
Errors
If the lock is already held and ErrorKind::WouldBlock
error is returned.
On Unix this may return an ErrorKind::Interrupted
if the operation was
interrupted by a signal handler.
sourcepub fn into_inner(self) -> Twhere
T: Sized,
pub fn into_inner(self) -> Twhere T: Sized,
Consumes this RwLock
, returning the underlying data.