From d8c9bffe5b4d643db84fd23219c00cc93e8a9e52 Mon Sep 17 00:00:00 2001 From: arkpar Date: Fri, 5 Jan 2024 12:30:30 +0100 Subject: [PATCH] Fix loom --- src/file.rs | 12 ++++++++++-- src/table.rs | 14 +++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/file.rs b/src/file.rs index bba9af8f..6bb7a18d 100644 --- a/src/file.rs +++ b/src/file.rs @@ -8,7 +8,6 @@ use crate::{ parking_lot::RwLock, table::TableId, }; -use parking_lot::RwLockReadGuard; use std::sync::atomic::{AtomicU64, Ordering}; #[cfg(target_os = "linux")] @@ -125,6 +124,7 @@ impl TableFile { Ok(()) } + #[cfg(not(feature = "loom"))] pub fn slice_at(&self, offset: u64, len: usize) -> MappedBytesGuard { let offset = offset as usize; let map = self.map.read(); @@ -134,6 +134,14 @@ impl TableFile { }) } + #[cfg(feature = "loom")] + pub fn slice_at(&self, offset: u64, len: usize) -> MappedBytesGuard { + let offset = offset as usize; + let map = self.map.read(); + let (map, _) = map.as_ref().unwrap(); + MappedBytesGuard::new(map[offset..offset + len].to_vec()) + } + pub fn write_at(&self, buf: &[u8], offset: u64) -> Result<()> { let map = self.map.read(); let (map, _) = map.as_ref().unwrap(); @@ -204,7 +212,7 @@ pub struct MappedBytesGuard<'a> { #[cfg(feature = "loom")] impl<'a> MappedBytesGuard<'a> { - fn new(data: Vec) -> Self { + pub fn new(data: Vec) -> Self { Self { _phantom: std::marker::PhantomData, data } } } diff --git a/src/table.rs b/src/table.rs index 7d7e7433..dae909de 100644 --- a/src/table.rs +++ b/src/table.rs @@ -382,12 +382,12 @@ impl + AsMut<[u8]>> std::ops::IndexMut> fo } } -enum LockedSlice<'a> { - FromOverlay(&'a [u8]), - FromFile(parking_lot::MappedRwLockReadGuard<'a, [u8]>), +enum LockedSlice, F: std::ops::Deref> { + FromOverlay(O), + FromFile(F), } -impl<'a> LockedSlice<'a> { +impl, F: std::ops::Deref> LockedSlice { fn as_slice(&self) -> &[u8] { match self { LockedSlice::FromOverlay(slice) => &*slice, @@ -472,7 +472,7 @@ impl ValueTable { let entry_size = self.entry_size as usize; loop { let vbuf = log.value_ref(self.id, index); - let buf: LockedSlice = if let Some(buf) = vbuf.as_deref() { + let buf: LockedSlice<_, _> = if let Some(buf) = vbuf.as_deref() { log::trace!( target: "parity-db", "{}: Found in overlay {}", @@ -487,8 +487,8 @@ impl ValueTable { self.id, index, ); - let buf = self.file.slice_at(index * self.entry_size as u64, entry_size); - LockedSlice::FromFile(buf) + let vbuf = self.file.slice_at(index * self.entry_size as u64, entry_size); + LockedSlice::FromFile(vbuf) }; let mut buf = EntryRef::new(buf.as_slice());