use crate::{error::ReportedError, prelude::*, time::TimeoutErr};
use std::time::Duration;
use tokio::sync::MutexGuard;
pub struct Mutex<T: ?Sized> {
pub dur: std::time::Duration,
pub mutex: tokio::sync::Mutex<T>,
}
unsafe impl<T> Send for Mutex<T> where T: ?Sized + Send {}
unsafe impl<T> Sync for Mutex<T> where T: ?Sized + Send {}
impl<T: ?Sized> Mutex<T> {
const DEFAULT: Duration = Duration::from_secs(1);
pub fn new(t: T) -> Self
where
T: Sized,
{
Mutex {
dur: Self::DEFAULT,
mutex: tokio::sync::Mutex::new(t),
}
}
pub async fn lock(&self) -> Result<MutexGuard<'_, T>, TimeoutErr> {
self.mutex
.lock()
.timeout(self.dur)
.await
.wrap_reported_err("ystd::sync::Mutex::lock timed out")
}
}