pub struct Lock<T> { /* private fields */ }Expand description
A value that one thread at a time can reach.
lock waits for it and hands back a Held, which derefs
to the value and releases the lock when it is dropped. A caller holding the
lock by exclusive reference skips all of that through
get_mut, which is how single threaded code and setup code
reach the value for free.
Implementations§
Source§impl<T> Lock<T>
impl<T> Lock<T>
Sourcepub fn lock(&self) -> Held<'_, T>
pub fn lock(&self) -> Held<'_, T>
Wait for the lock and take it.
§Panics
In a debug build, if the calling thread already holds this lock. In a release build that case spins forever instead, which is the usual bargain for a check that costs something on the hot path.
Sourcepub fn try_lock(&self) -> Option<Held<'_, T>>
pub fn try_lock(&self) -> Option<Held<'_, T>>
Take the lock if it is free, and give up rather than wait if it is not.
Returns None if another thread holds it, and also if the calling
thread does, since a lock cannot be taken twice by anyone.
Sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Reach the value without locking, because the caller owns the lock.
An exclusive reference to the lock is already proof that no other thread can be holding it, so this costs nothing at all. Setup, teardown and everything a single threaded server does can go through here.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Take the value back out and drop the lock around it.