Struct try_lock::TryLock

source ·
pub struct TryLock<T> {
    pub(crate) is_locked: AtomicBool,
    pub(crate) value: UnsafeCell<T>,
}
Expand description

A light-weight lock guarded by an atomic boolean.

Most efficient when contention is low, acquiring the lock is a single atomic swap, and releasing it just 1 more atomic swap.

It is only possible to try to acquire the lock, it is not possible to wait for the lock to become ready, like with a Mutex.

Fields§

§is_locked: AtomicBool§value: UnsafeCell<T>

Implementations§

source§

impl<T> TryLock<T>

source

pub const fn new(val: T) -> TryLock<T>

Create a TryLock around the value.

source

pub fn try_lock(&self) -> Option<Locked<'_, T>>

Try to acquire the lock of this value.

If the lock is already acquired by someone else, this returns None. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.

§Note

The default memory ordering is to use Acquire to lock, and Release to unlock. If different ordering is required, use try_lock_explicit or try_lock_explicit_unchecked.

source

pub fn try_lock_order( &self, lock_order: Ordering, unlock_order: Ordering, ) -> Option<Locked<'_, T>>

👎Deprecated since 0.2.3: This method is actually unsafe because it unsafely allows the use of weaker memory ordering. Please use try_lock_explicit instead

Try to acquire the lock of this value using the lock and unlock orderings.

If the lock is already acquired by someone else, this returns None. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.

source

pub fn try_lock_explicit( &self, lock_order: Ordering, unlock_order: Ordering, ) -> Option<Locked<'_, T>>

Try to acquire the lock of this value using the specified lock and unlock orderings.

If the lock is already acquired by someone else, this returns None. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.

§Panic

This method panics if lock_order is not any of Acquire, AcqRel, and SeqCst, or unlock_order is not any of Release and SeqCst.

source

pub unsafe fn try_lock_explicit_unchecked( &self, lock_order: Ordering, unlock_order: Ordering, ) -> Option<Locked<'_, T>>

Try to acquire the lock of this value using the specified lock and unlock orderings without checking that the specified orderings are strong enough to be safe.

If the lock is already acquired by someone else, this returns None. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.

§Safety

Unlike try_lock_explicit, this method is unsafe because it does not check that the given memory orderings are strong enough to prevent data race.

source

pub fn into_inner(self) -> T

Take the value back out of the lock when this is the sole owner.

Trait Implementations§

source§

impl<T: Debug> Debug for TryLock<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default> Default for TryLock<T>

source§

fn default() -> TryLock<T>

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

impl<T: Send> Send for TryLock<T>

source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for TryLock<T>

§

impl<T> !RefUnwindSafe for TryLock<T>

§

impl<T> Unpin for TryLock<T>
where T: Unpin,

§

impl<T> UnwindSafe for TryLock<T>
where 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.