use std::sync::{Arc, Mutex, MutexGuard};
use std::sync::atomic::AtomicU64;
use std::time::Duration;
use once_cell::sync::Lazy;
pub static GLOBAL_STD_TIMEOUT: Lazy<Arc<AtomicU64>> = Lazy::new(|| Arc::new(AtomicU64::new(5)));
pub struct MutexWithTimeout<T> {
inner: Mutex<T>,
timeout: Duration,
}
impl<T> MutexWithTimeout<T> {
pub fn new(inner: T) -> Self {
Self {
inner: Mutex::new(inner),
timeout: Duration::from_secs(GLOBAL_STD_TIMEOUT.load(std::sync::atomic::Ordering::Relaxed)),
}
}
pub fn new_with_timeout(inner: T, timeout: Duration) -> Self {
Self {
inner: Mutex::new(inner),
timeout,
}
}
pub fn lock(&self) -> Option<MutexGuard<T>> {
let start = std::time::Instant::now();
loop {
if let Ok(guard) = self.inner.try_lock() {
return Some(guard);
}
if start.elapsed() > self.timeout {
break;
}
}
None
}
pub fn try_lock(&self) -> Option<std::sync::MutexGuard<T>> {
self.inner.try_lock().ok()
}
}