Fix `held_by_thread` in `no-std` to return instead of panicing
authorMatt Corallo <git@bluematt.me>
Tue, 30 May 2023 18:11:47 +0000 (18:11 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 30 May 2023 18:15:32 +0000 (18:15 +0000)
Our `no-std` locks simply panic if a lock cannot be taken as there
should be no lock contention in a single-threaded environment.
However, the `held_by_thread` debug methods were delegating to the
lock methods which resulted in a panic when asserting that a lock
*is* held by the current thread.

Instead, they are updated here to call the relevant `RefCell`
testing methods.

lightning/src/sync/nostd_sync.rs

index 08d54a939be66ecccb83d30c0569d25f31a523e1..27cfb9b8f782c4bafabecd5180a5d0d2256105c3 100644 (file)
@@ -49,7 +49,7 @@ impl<T> Mutex<T> {
 impl<'a, T: 'a> LockTestExt<'a> for Mutex<T> {
        #[inline]
        fn held_by_thread(&self) -> LockHeldState {
-               if self.lock().is_err() { return LockHeldState::HeldByThread; }
+               if self.inner.try_borrow_mut().is_err() { return LockHeldState::HeldByThread; }
                else { return LockHeldState::NotHeldByThread; }
        }
        type ExclLock = MutexGuard<'a, T>;
@@ -115,7 +115,7 @@ impl<T> RwLock<T> {
 impl<'a, T: 'a> LockTestExt<'a> for RwLock<T> {
        #[inline]
        fn held_by_thread(&self) -> LockHeldState {
-               if self.write().is_err() { return LockHeldState::HeldByThread; }
+               if self.inner.try_borrow_mut().is_err() { return LockHeldState::HeldByThread; }
                else { return LockHeldState::NotHeldByThread; }
        }
        type ExclLock = RwLockWriteGuard<'a, T>;