[−][src]Struct lock_api::RwLock
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 threads 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.
Methods
impl<R: RawRwLock, T> RwLock<R, T>
[src]
pub fn new(val: T) -> RwLock<R, T>
[src]
Creates a new instance of an RwLock<T>
which is unlocked.
pub fn into_inner(self) -> T
[src]
Consumes this RwLock
, returning the underlying data.
impl<R: RawRwLock, T: ?Sized> RwLock<R, T>
[src]
pub fn read(&self) -> RwLockReadGuard<R, T>
[src]
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.
Note that attempts to recursively acquire a read lock on a RwLock
when
the current thread already holds one may result in a deadlock.
Returns an RAII guard which will release this thread's shared access once it is dropped.
pub fn try_read(&self) -> Option<RwLockReadGuard<R, T>>
[src]
Attempts to acquire this RwLock
with shared read access.
If the access could not be granted at this time, then None
is returned.
Otherwise, an RAII guard is returned which will release the shared access
when it is dropped.
This function does not block.
pub fn write(&self) -> RwLockWriteGuard<R, T>
[src]
Locks 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.
pub fn try_write(&self) -> Option<RwLockWriteGuard<R, T>>
[src]
Attempts to lock this RwLock
with exclusive write access.
If the lock could not be acquired at this time, then None
is returned.
Otherwise, an RAII guard is returned which will release the lock when
it is dropped.
This function does not block.
pub fn get_mut(&mut self) -> &mut T
[src]
Returns a mutable reference to the underlying data.
Since this call borrows the RwLock
mutably, no actual locking needs to
take place---the mutable borrow statically guarantees no locks exist.
pub unsafe fn force_unlock_read(&self)
[src]
Forcibly unlocks a read lock.
This is useful when combined with mem::forget
to hold a lock without
the need to maintain a RwLockReadGuard
object alive, for example when
dealing with FFI.
Safety
This method must only be called if the current thread logically owns a
RwLockReadGuard
but that guard has be discarded using mem::forget
.
Behavior is undefined if a rwlock is read-unlocked when not read-locked.
pub unsafe fn force_unlock_write(&self)
[src]
Forcibly unlocks a write lock.
This is useful when combined with mem::forget
to hold a lock without
the need to maintain a RwLockWriteGuard
object alive, for example when
dealing with FFI.
Safety
This method must only be called if the current thread logically owns a
RwLockWriteGuard
but that guard has be discarded using mem::forget
.
Behavior is undefined if a rwlock is write-unlocked when not write-locked.
pub unsafe fn raw(&self) -> &R
[src]
Returns the underlying raw reader-writer lock object.
Note that you will most likely need to import the RawRwLock
trait from
lock_api
to be able to call functions on the raw
reader-writer lock.
Safety
This method is unsafe because it allows unlocking a mutex while still holding a reference to a lock guard.
impl<R: RawRwLockFair, T: ?Sized> RwLock<R, T>
[src]
pub unsafe fn force_unlock_read_fair(&self)
[src]
Forcibly unlocks a read lock using a fair unlock procotol.
This is useful when combined with mem::forget
to hold a lock without
the need to maintain a RwLockReadGuard
object alive, for example when
dealing with FFI.
Safety
This method must only be called if the current thread logically owns a
RwLockReadGuard
but that guard has be discarded using mem::forget
.
Behavior is undefined if a rwlock is read-unlocked when not read-locked.
pub unsafe fn force_unlock_write_fair(&self)
[src]
Forcibly unlocks a write lock using a fair unlock procotol.
This is useful when combined with mem::forget
to hold a lock without
the need to maintain a RwLockWriteGuard
object alive, for example when
dealing with FFI.
Safety
This method must only be called if the current thread logically owns a
RwLockWriteGuard
but that guard has be discarded using mem::forget
.
Behavior is undefined if a rwlock is write-unlocked when not write-locked.
impl<R: RawRwLockTimed, T: ?Sized> RwLock<R, T>
[src]
pub fn try_read_for(
&self,
timeout: R::Duration
) -> Option<RwLockReadGuard<R, T>>
[src]
&self,
timeout: R::Duration
) -> Option<RwLockReadGuard<R, T>>
Attempts to acquire this RwLock
with shared read access until a timeout
is reached.
If the access could not be granted before the timeout expires, then
None
is returned. Otherwise, an RAII guard is returned which will
release the shared access when it is dropped.
pub fn try_read_until(
&self,
timeout: R::Instant
) -> Option<RwLockReadGuard<R, T>>
[src]
&self,
timeout: R::Instant
) -> Option<RwLockReadGuard<R, T>>
Attempts to acquire this RwLock
with shared read access until a timeout
is reached.
If the access could not be granted before the timeout expires, then
None
is returned. Otherwise, an RAII guard is returned which will
release the shared access when it is dropped.
pub fn try_write_for(
&self,
timeout: R::Duration
) -> Option<RwLockWriteGuard<R, T>>
[src]
&self,
timeout: R::Duration
) -> Option<RwLockWriteGuard<R, T>>
Attempts to acquire this RwLock
with exclusive write access until a
timeout is reached.
If the access could not be granted before the timeout expires, then
None
is returned. Otherwise, an RAII guard is returned which will
release the exclusive access when it is dropped.
pub fn try_write_until(
&self,
timeout: R::Instant
) -> Option<RwLockWriteGuard<R, T>>
[src]
&self,
timeout: R::Instant
) -> Option<RwLockWriteGuard<R, T>>
Attempts to acquire this RwLock
with exclusive write access until a
timeout is reached.
If the access could not be granted before the timeout expires, then
None
is returned. Otherwise, an RAII guard is returned which will
release the exclusive access when it is dropped.
impl<R: RawRwLockRecursive, T: ?Sized> RwLock<R, T>
[src]
pub fn read_recursive(&self) -> RwLockReadGuard<R, T>
[src]
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.
Unlike read
, this method is guaranteed to succeed without blocking if
another read lock is held at the time of the call. This allows a thread
to recursively lock a RwLock
. However using this method can cause
writers to starve since readers no longer block if a writer is waiting
for the lock.
Returns an RAII guard which will release this thread's shared access once it is dropped.
pub fn try_read_recursive(&self) -> Option<RwLockReadGuard<R, T>>
[src]
Attempts to acquire this RwLock
with shared read access.
If the access could not be granted at this time, then None
is returned.
Otherwise, an RAII guard is returned which will release the shared access
when it is dropped.
This method is guaranteed to succeed if another read lock is held at the
time of the call. See the documentation for read_recursive
for details.
This function does not block.
impl<R: RawRwLockRecursiveTimed, T: ?Sized> RwLock<R, T>
[src]
pub fn try_read_recursive_for(
&self,
timeout: R::Duration
) -> Option<RwLockReadGuard<R, T>>
[src]
&self,
timeout: R::Duration
) -> Option<RwLockReadGuard<R, T>>
Attempts to acquire this RwLock
with shared read access until a timeout
is reached.
If the access could not be granted before the timeout expires, then
None
is returned. Otherwise, an RAII guard is returned which will
release the shared access when it is dropped.
This method is guaranteed to succeed without blocking if another read
lock is held at the time of the call. See the documentation for
read_recursive
for details.
pub fn try_read_recursive_until(
&self,
timeout: R::Instant
) -> Option<RwLockReadGuard<R, T>>
[src]
&self,
timeout: R::Instant
) -> Option<RwLockReadGuard<R, T>>
Attempts to acquire this RwLock
with shared read access until a timeout
is reached.
If the access could not be granted before the timeout expires, then
None
is returned. Otherwise, an RAII guard is returned which will
release the shared access when it is dropped.
impl<R: RawRwLockUpgrade, T: ?Sized> RwLock<R, T>
[src]
pub fn upgradable_read(&self) -> RwLockUpgradableReadGuard<R, T>
[src]
Locks this RwLock
with upgradable read access, blocking the current thread
until it can be acquired.
The calling thread will be blocked until there are no more writers or other upgradable reads which hold the lock. There may be other readers currently inside the lock when this method returns.
Returns an RAII guard which will release this thread's shared access once it is dropped.
pub fn try_upgradable_read(&self) -> Option<RwLockUpgradableReadGuard<R, T>>
[src]
Attempts to acquire this RwLock
with upgradable read access.
If the access could not be granted at this time, then None
is returned.
Otherwise, an RAII guard is returned which will release the shared access
when it is dropped.
This function does not block.
impl<R: RawRwLockUpgradeTimed, T: ?Sized> RwLock<R, T>
[src]
pub fn try_upgradable_read_for(
&self,
timeout: R::Duration
) -> Option<RwLockUpgradableReadGuard<R, T>>
[src]
&self,
timeout: R::Duration
) -> Option<RwLockUpgradableReadGuard<R, T>>
Attempts to acquire this RwLock
with upgradable read access until a timeout
is reached.
If the access could not be granted before the timeout expires, then
None
is returned. Otherwise, an RAII guard is returned which will
release the shared access when it is dropped.
pub fn try_upgradable_read_until(
&self,
timeout: R::Instant
) -> Option<RwLockUpgradableReadGuard<R, T>>
[src]
&self,
timeout: R::Instant
) -> Option<RwLockUpgradableReadGuard<R, T>>
Attempts to acquire this RwLock
with upgradable read access until a timeout
is reached.
If the access could not be granted before the timeout expires, then
None
is returned. Otherwise, an RAII guard is returned which will
release the shared access when it is dropped.
Trait Implementations
impl<R: RawRwLock, T: ?Sized + Debug> Debug for RwLock<R, T>
[src]
impl<R: RawRwLock + Send, T: ?Sized + Send> Send for RwLock<R, T>
[src]
impl<R: RawRwLock + Sync, T: ?Sized + Send + Sync> Sync for RwLock<R, T>
[src]
impl<R: RawRwLock, T> From<T> for RwLock<R, T>
[src]
impl<R: RawRwLock, T: ?Sized + Default> Default for RwLock<R, T>
[src]
Auto Trait Implementations
impl<R, T: ?Sized> Unpin for RwLock<R, T> where
R: Unpin,
T: Unpin,
R: Unpin,
T: Unpin,
impl<R, T: ?Sized> UnwindSafe for RwLock<R, T> where
R: UnwindSafe,
T: UnwindSafe,
R: UnwindSafe,
T: UnwindSafe,
impl<R, T> !RefUnwindSafe for RwLock<R, T>
Blanket Implementations
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,