use std::sync::Arc;
use noxu_sync::{Condvar, Mutex};
use crate::LockType;
pub type WaiterNotify = Arc<(Mutex<bool>, Condvar)>;
#[derive(Debug, Clone)]
pub struct LockInfo {
pub locker_id: i64,
pub lock_type: LockType,
pub notify: Option<WaiterNotify>,
}
impl LockInfo {
pub fn new(locker_id: i64, lock_type: LockType) -> Self {
Self { locker_id, lock_type, notify: None }
}
}
impl PartialEq for LockInfo {
fn eq(&self, other: &Self) -> bool {
self.locker_id == other.locker_id && self.lock_type == other.lock_type
}
}
impl Eq for LockInfo {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let info = LockInfo::new(42, LockType::Write);
assert_eq!(info.locker_id, 42);
assert_eq!(info.lock_type, LockType::Write);
assert!(info.notify.is_none());
}
#[test]
fn test_clone() {
let info1 = LockInfo::new(100, LockType::Read);
let info2 = info1.clone();
assert_eq!(info1, info2);
}
#[test]
fn test_equality() {
let info1 = LockInfo::new(1, LockType::Read);
let info2 = LockInfo::new(1, LockType::Read);
let info3 = LockInfo::new(2, LockType::Read);
let info4 = LockInfo::new(1, LockType::Write);
assert_eq!(info1, info2);
assert_ne!(info1, info3);
assert_ne!(info1, info4);
}
#[test]
fn test_notify_ignored_in_equality() {
let mut info1 = LockInfo::new(1, LockType::Read);
let mut info2 = LockInfo::new(1, LockType::Read);
info1.notify = Some(Arc::new((Mutex::new(false), Condvar::new())));
info2.notify = Some(Arc::new((Mutex::new(false), Condvar::new())));
assert_eq!(info1, info2);
}
}