pub fn lock(&self) -> LockResult<MutexGuard<'_, T>> {
        let me = ExecutionState::me();

        let mut state = self.state.borrow_mut();
        trace!(holder=?state.holder, waiters=?state.waiters, "waiting to acquire mutex {:p}", self);

        // We are waiting for the lock
        state.waiters.insert(me);
        // If the lock is already held, then we are blocked
        if state.holder.is_some() {
            assert_ne!(state.holder.unwrap(), me);
            ExecutionState::with(|s| s.current_mut().block());
        }
        drop(state);

        // Acquiring a lock is a yield point
        thread::switch();

        let mut state = self.state.borrow_mut();
        // Once the scheduler has resumed this thread, we are clear to become its holder. We might
        // not actually be in the waiters, though (if the lock was uncontended).
        // TODO i think now we are guaranteed to be in the waiters?
        assert!(state.holder.is_none());
        state.holder = Some(me);
        state.waiters.remove(me);
        trace!(waiters=?state.waiters, "acquired mutex {:p}", self);
        // Block all other threads, since we won the race to take this lock
        // TODO a bit of a bummer that we have to do this (it would be cleaner if those threads
        // TODO never become unblocked), but might need to track more state to avoid this.
        for tid in state.waiters.iter() {
            ExecutionState::with(|s| s.get_mut(tid).block());
        }
        drop(state);

        match self.inner.try_lock() {
            Ok(guard) => Ok(MutexGuard {
                inner: Some(guard),
                mutex: self,
            }),
            Err(TryLockError::Poisoned(guard)) => Err(PoisonError::new(MutexGuard {
                inner: Some(guard.into_inner()),
                mutex: self,
            })),
            Err(TryLockError::WouldBlock) => panic!("mutex state out of sync"),
        }
    }
/// Acquires a mutex, blocking the current thread until it is able to do so.
Acquires a mutex, blocking the current thread until it is able to do so.
Acquires a mutex blocking the current thread until it is able to do so .
pub fn lock(&self) -> LockResult<MutexGuard<'_, T>> {
        let me = ExecutionState::me();
        let mut state = self.state.borrow_mut();
        trace!(holder=?state.holder, waiters=?state.waiters, "waiting to acquire mutex {:p}", self);
        // We are waiting for the lock
        state.waiters.insert(me);
        // If the lock is already held, then we are blocked
        if state.holder.is_some() {
            assert_ne!(state.holder.unwrap(), me);
            ExecutionState::with(|s| s.current_mut().block());
        }
        drop(state);
        // Acquiring a lock is a yield point
        thread::switch();
        let mut state = self.state.borrow_mut();
        // Once the scheduler has resumed this thread, we are clear to become its holder. We might
        // not actually be in the waiters, though (if the lock was uncontended).
        // TODO i think now we are guaranteed to be in the waiters?
        assert!(state.holder.is_none());
        state.holder = Some(me);
        state.waiters.remove(me);
        trace!(waiters=?state.waiters, "acquired mutex {:p}", self);
        // Block all other threads, since we won the race to take this lock
        // TODO a bit of a bummer that we have to do this (it would be cleaner if those threads
        // TODO never become unblocked), but might need to track more state to avoid this.
        for tid in state.waiters.iter() {
            ExecutionState::with(|s| s.get_mut(tid).block());
        }
        drop(state);
        match self.inner.try_lock() {
            Ok(guard) => Ok(MutexGuard {
                inner: Some(guard),
                mutex: self,
            }),
            Err(TryLockError::Poisoned(guard)) => Err(PoisonError::new(MutexGuard {
                inner: Some(guard.into_inner()),
                mutex: self,
            })),
            Err(TryLockError::WouldBlock) => panic!("mutex state out of sync"),
        }
    }
pub fn lock ( & self ) -> LockResult < MutexGuard < ' _ , T > > { let me = ExecutionState :: me ( ) ; let mut state = self . state . borrow_mut ( ) ; trace ! ( holder state holder waiters state waiters " " self ) ; // We are waiting for the lock state . waiters . insert ( me ) ; // If the lock is already held, then we are blocked if state . holder . is_some ( ) { assert_ne ! ( state holder unwrap ( ) me ) ; ExecutionState :: with ( | s | s . current_mut ( ) . block ( ) ) ; } drop ( state ) ; // Acquiring a lock is a yield point thread :: switch ( ) ; let mut state = self . state . borrow_mut ( ) ; // Once the scheduler has resumed this thread, we are clear to become its holder. We might // not actually be in the waiters, though (if the lock was uncontended). // TODO i think now we are guaranteed to be in the waiters? assert ! ( state holder is_none ( ) ) ; state . holder = Some ( me ) ; state . waiters . remove ( me ) ; trace ! ( waiters state waiters " " self ) ; // Block all other threads, since we won the race to take this lock // TODO a bit of a bummer that we have to do this (it would be cleaner if those threads // TODO never become unblocked), but might need to track more state to avoid this. for tid in state . waiters . iter ( ) { ExecutionState :: with ( | s | s . get_mut ( tid ) . block ( ) ) ; } drop ( state ) ; match self . inner . try_lock ( ) { Ok ( guard ) => Ok ( MutexGuard { inner : Some ( guard ) , mutex : self , } ) , Err ( TryLockError :: Poisoned ( guard ) ) => Err ( PoisonError :: new ( MutexGuard { inner : Some ( guard . into_inner ( ) ) , mutex : self , } ) ) , Err ( TryLockError :: WouldBlock ) => panic ! ( " " ) , } }
Acquires a mutex, blocking the current thread until it is able to do so.
Acquires a mutex blocking the current thread until it is able to do so .
&self
&self