Skip to content

Commit

Permalink
Fix loom
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar committed Jan 5, 2024
1 parent 5ebc81e commit d8c9bff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -204,7 +212,7 @@ pub struct MappedBytesGuard<'a> {

#[cfg(feature = "loom")]
impl<'a> MappedBytesGuard<'a> {
fn new(data: Vec<u8>) -> Self {
pub fn new(data: Vec<u8>) -> Self {
Self { _phantom: std::marker::PhantomData, data }
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,12 @@ impl<B: AsRef<[u8]> + AsMut<[u8]>> std::ops::IndexMut<std::ops::Range<usize>> fo
}
}

enum LockedSlice<'a> {
FromOverlay(&'a [u8]),
FromFile(parking_lot::MappedRwLockReadGuard<'a, [u8]>),
enum LockedSlice<O: std::ops::Deref<Target = [u8]>, F: std::ops::Deref<Target = [u8]>> {
FromOverlay(O),
FromFile(F),
}

impl<'a> LockedSlice<'a> {
impl<O: std::ops::Deref<Target = [u8]>, F: std::ops::Deref<Target = [u8]>> LockedSlice<O, F> {
fn as_slice(&self) -> &[u8] {
match self {
LockedSlice::FromOverlay(slice) => &*slice,
Expand Down Expand Up @@ -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 {}",
Expand All @@ -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());

Expand Down

0 comments on commit d8c9bff

Please sign in to comment.