use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use crate::storage::policy::{FullySynchronized, LockingPolicy, LockingPolicyFactory};
use crate::storage::{ArcSwapConfigCell, ConfigCell};
#[derive(Debug, Default, Clone, Copy)]
pub struct FullLocking;
impl LockingPolicyFactory for FullLocking {
type Policy = FullLockingPolicy;
type IndexFlag = std::sync::atomic::AtomicBool;
type Shared<T: 'static> = std::sync::Arc<T>;
type Config<Settings: Clone + 'static> = ArcSwapConfigCell<Settings>;
fn create_policy(&self) -> Self::Policy {
FullLockingPolicy {
index: RwLock::new(()),
values: RwLock::new(()),
}
}
fn new_shared<T: 'static>(value: T) -> std::sync::Arc<T> {
std::sync::Arc::new(value)
}
fn new_config<Settings: Clone + 'static>(value: Settings) -> ArcSwapConfigCell<Settings> {
ArcSwapConfigCell::new(value)
}
}
pub struct FullLockingPolicy {
index: RwLock<()>,
values: RwLock<()>,
}
unsafe impl LockingPolicy for FullLockingPolicy {
type IndexSharedGuard<'a> = RwLockReadGuard<'a, ()>;
type IndexExclusiveGuard<'a> = RwLockWriteGuard<'a, ()>;
type ValuesSharedGuard<'a> = RwLockReadGuard<'a, ()>;
type ValuesExclusiveGuard<'a> = RwLockWriteGuard<'a, ()>;
#[inline]
fn read_index(&self) -> Self::IndexSharedGuard<'_> {
self.index.read_recursive()
}
#[inline]
fn write_index(&self) -> Self::IndexExclusiveGuard<'_> {
self.index.write()
}
#[inline]
fn read_values<Key>(&self, _key: &Key) -> Self::ValuesSharedGuard<'_> {
self.values.read_recursive()
}
#[inline]
fn write_values<Key>(&self, _key: &Key) -> Self::ValuesExclusiveGuard<'_> {
self.values.write()
}
}
unsafe impl FullySynchronized for FullLockingPolicy {}