no_threads.rs - source
Description: Source of the Rust file `library/std/src/sys/sync/mutex/no_threads.rs`.

#
std/sys/sync/mutex/
no_threads.rs

    1use crate::cell::Cell;
    2
    3pub struct Mutex {
    4    // This platform has no threads, so we can use a Cell here.
    5    locked: Cell<bool>,
    6}
    7
    8unsafe impl Send for Mutex {}
    9unsafe impl Sync for Mutex {} // no threads on this platform
    10
    11impl Mutex {
    12    #[inline]
    13    pub const fn new() -> Mutex {
    14        Mutex { locked: Cell::new(false) }
    15    }
    16
    17    #[inline]
    18    pub fn lock(&self) {
    19        assert_eq!(self.locked.replace(true), false, "cannot recursively acquire mutex");
    20    }
    21
    22    #[inline]
    23    pub unsafe fn unlock(&self) {
    24        self.locked.set(false);
    25    }
    26
    27    #[inline]
    28    pub fn try_lock(&self) -> bool {
    29        self.locked.replace(true) == false
    30    }
    31}