agent_tui/
sync_utils.rs

1use std::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
2
3pub fn rwlock_read_or_recover<T>(lock: &RwLock<T>) -> RwLockReadGuard<'_, T> {
4    lock.read().unwrap_or_else(|poisoned| {
5        eprintln!("Warning: recovering from poisoned rwlock (read)");
6        poisoned.into_inner()
7    })
8}
9
10pub fn rwlock_write_or_recover<T>(lock: &RwLock<T>) -> RwLockWriteGuard<'_, T> {
11    lock.write().unwrap_or_else(|poisoned| {
12        eprintln!("Warning: recovering from poisoned rwlock (write)");
13        poisoned.into_inner()
14    })
15}
16
17pub fn mutex_lock_or_recover<T>(lock: &Mutex<T>) -> MutexGuard<'_, T> {
18    lock.lock().unwrap_or_else(|poisoned| {
19        eprintln!("Warning: recovering from poisoned mutex");
20        poisoned.into_inner()
21    })
22}