/// Lock a mutex, tolerating poison. A poisoned mutex means some thread
/// panicked while holding the lock; every mutex in this crate guards either a
/// read-through cache (a partially-populated map is still correct - the
/// missing entries are simply recomputed) or a read-only disc / archive
/// reader (byte reads are idempotent). In all those cases the guarded data is
/// still usable, so recover the guard via [`std::sync::PoisonError::into_inner`]
/// rather than cascading a second panic into an unrelated caller. Used by the
/// asset caches here and the PROT index caches in [`scene::prot_index`].
pub(crate) fn lock_poison_tolerant<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
    m.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
}

#[cfg(test)]
mod lock_poison_tolerant_tests {
    use super::lock_poison_tolerant;
    use std::sync::{Arc, Mutex};

    #[test]
    fn recovers_a_poisoned_mutex_instead_of_panicking() {
        let m = Arc::new(Mutex::new(vec![1u8, 2, 3]));
        // Poison the mutex: panic while holding the lock on another thread.
        let m2 = Arc::clone(&m);
        let _ = std::thread::spawn(move || {
            let _g = m2.lock().unwrap();
            panic!("intentional poison");
        })
        .join();
        assert!(m.lock().is_err(), "mutex should be poisoned for this test");
        // The tolerant lock recovers the guard and the data is intact.
        let g = lock_poison_tolerant(&m);
        assert_eq!(&*g, &[1u8, 2, 3]);
    }
}
