Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrap UnsafeCell<MmapMut> with a mutex to fix Clippy lint on master #57

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fs4 = "0.6"
# to use direct functions on FreeBSD
rustix = { version = "0", features = [ "fs" ]}
crossbeam-channel = "0.5"
parking_lot = { version = "0.12.1" }

# Binary dependencies
docopt = "1.1"
Expand All @@ -35,4 +36,4 @@ hdrhistogram = "7.5.2"
quickcheck = "1.0.3"
regex = "1.8.1"
tempfile = "3.5.0"
chrono = "0.4.24"
chrono = "0.4.31"
2 changes: 1 addition & 1 deletion examples/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main() {
}

pub fn precise_time_ns() -> u64 {
chrono::offset::Utc::now().timestamp_nanos() as u64
chrono::offset::Utc::now().timestamp_nanos_opt().unwrap() as u64
}

fn format_duration(n: u64) -> String {
Expand Down
9 changes: 5 additions & 4 deletions src/mmap_view_sync.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use memmap2::{MmapMut, MmapOptions};
use parking_lot::Mutex;
use std::cell::UnsafeCell;
use std::fmt;
use std::fs::File;
Expand All @@ -13,7 +14,7 @@ use std::sync::Arc;
/// The view may be split into disjoint ranges, each of which will share the
/// underlying memory map.
pub struct MmapViewSync {
inner: Arc<UnsafeCell<MmapMut>>,
inner: Arc<Mutex<UnsafeCell<MmapMut>>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could actually get rid of the UnsafeCell completely now that a Mutex protects from concurrent access?

Copy link
Contributor Author

@ylgrgyq ylgrgyq Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to remove UnsafeCell completely, but in fn inner(&self) -> &MmapMut and fn inner_mut(&self) -> &mut MmapMut the compiler says cannot return value referencing temporary value. It seems we still need this UnsafeCell.

I'm a complete rookie for rust, please forgive my ignorance. 😅

offset: usize,
len: usize,
}
Expand Down Expand Up @@ -87,15 +88,15 @@ impl MmapViewSync {
///
/// The caller must ensure that memory outside the `offset`/`len` range is not accessed.
fn inner(&self) -> &MmapMut {
unsafe { &*self.inner.get() }
unsafe { &*self.inner.lock().get() }
}

/// Get a mutable reference to the inner mmap.
///
/// The caller must ensure that memory outside the `offset`/`len` range is not accessed.
#[allow(clippy::mut_from_ref)]
fn inner_mut(&self) -> &mut MmapMut {
unsafe { &mut *self.inner.get() }
unsafe { &mut *self.inner.lock().get() }
}

/// Flushes outstanding view modifications to disk.
Expand Down Expand Up @@ -147,7 +148,7 @@ impl From<MmapMut> for MmapViewSync {
fn from(mmap: MmapMut) -> MmapViewSync {
let len = mmap.len();
MmapViewSync {
inner: Arc::new(UnsafeCell::new(mmap)),
inner: Arc::new(Mutex::new(UnsafeCell::new(mmap))),
offset: 0,
len,
}
Expand Down