Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Firefox/third_party/rust/tokio/src/util/   (Firefox Browser Version 136.0.1©)  Datei vom 10.2.2025 mit Größe 1 kB image not shown  

Quelle  try_lock.rs

  Sprache: Rust
 

use crate::loom::sync::atomic::AtomicBool;

use std::cell::UnsafeCell;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::sync::atomic::Ordering::SeqCst;

pub(cratestruct TryLock<T> {
    locked: AtomicBool,
    data: UnsafeCell<T>,
}

pub(cratestruct LockGuard<'a, T> {
    lock: &'a TryLock<T>,
    _p: PhantomData<std::rc::Rc<()>>,
}

unsafe impl<T: Send> Send for TryLock<T> {}
unsafe impl<T: Send> Sync for TryLock<T> {}

unsafe impl<T: Sync> Sync for LockGuard<'_, T> {}

macro_rules! new {
    ($data:ident) => {
        TryLock {
            locked: AtomicBool::new(false),
            data: UnsafeCell::new($data),
        }
    };
}

impl<T> TryLock<T> {
    #[cfg(not(loom))]
    /// Create a new `TryLock`
    pub(crateconst fn new(data: T) -> TryLock<T> {
        new!(data)
    }

    #[cfg(loom)]
    /// Create a new `TryLock`
    pub(cratefn new(data: T) -> TryLock<T> {
        new!(data)
    }

    /// Attempt to acquire lock
    pub(cratefn try_lock(&self) -> Option<LockGuard<'_, T>> {
        if self
            .locked
            .compare_exchange(falsetrue, SeqCst, SeqCst)
            .is_err()
        {
            return None;
        }

        Some(LockGuard {
            lock: self,
            _p: PhantomData,
        })
    }
}

impl<T> Deref for LockGuard<'_, T> {
    type Target = T;

    fn deref(&self) -> &T {
        unsafe { &*self.lock.data.get() }
    }
}

impl<T> DerefMut for LockGuard<'_, T> {
    fn deref_mut(&mut self) -> &mut T {
        unsafe { &mut *self.lock.data.get() }
    }
}

impl<T> Drop for LockGuard<'_, T> {
    fn drop(&mut self) {
        self.lock.locked.store(false, SeqCst);
    }
}

Messung V0.5 in Prozent
C=86 H=96 G=90

¤ Dauer der Verarbeitung: 0.15 Sekunden  (vorverarbeitet am  2026-06-18) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.