Skip to main content

Lock

Struct Lock 

Source
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>

Source

pub const fn new(value: T) -> Self

Put value behind a lock that nobody holds yet.

Source

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.

Source

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.

Source

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.

Source

pub fn into_inner(self) -> T

Take the value back out and drop the lock around it.

Source

pub fn is_held(&self) -> bool

Whether somebody holds it, which is only ever a hint.

True can be stale by the time the caller reads it and so can false. It is here for statistics and for tests, not for deciding anything.

Trait Implementations§

Source§

impl<T: Default> Default for Lock<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Send> Send for Lock<T>

Source§

impl<T: Send> Sync for Lock<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Lock<T>

§

impl<T> !RefUnwindSafe for Lock<T>

§

impl<T> Unpin for Lock<T>
where UnsafeCell<T>: Unpin,

§

impl<T> UnsafeUnpin for Lock<T>
where UnsafeCell<T>: UnsafeUnpin,

§

impl<T> UnwindSafe for Lock<T>
where UnsafeCell<T>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.