Skip to content

Commit

Permalink
WeakDmaFile fixups (#661)
Browse files Browse the repository at this point in the history
Missing from exports. Also missing were default and new constructor
methods as well as strong_count.
  • Loading branch information
vlovich authored May 1, 2024
1 parent f2bdaa3 commit 83d3023
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
77 changes: 77 additions & 0 deletions glommio/src/io/dma_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,17 @@ impl DmaFile {
}
}

/// How many strong references are holding this file open. The returned count will always be at least 1.
///
/// # Safety
///
/// This method by itself is safe, but using it correctly requires extra care. Another thread can change the count
/// at any time, including potentially between calling this method and acting on the result. You probably want to
/// use [Self::downgrade] or [DmaFile::try_take_last_clone] to manage the ownership.
pub fn strong_count(&self) -> usize {
self.file.strong_count()
}

/// Convenience method that closes a DmaFile wrapped inside an Rc.
///
/// Returns [CloseResult] to indicate which operation was performed.
Expand Down Expand Up @@ -832,6 +843,17 @@ impl OwnedDmaFile {
pollable: self.pollable,
}
}

/// How many strong references are holding this file open. The returned count will always be at least 1.
///
/// # Safety
///
/// This method by itself is safe, but using it correctly requires extra care. Another thread can change the count
/// at any time, including potentially between calling this method and acting on the result. You probably want to
/// use [Self::downgrade] or convert to a [DmaFile] and use [DmaFile::try_take_last_clone].
pub fn strong_count(&self) -> usize {
self.file.strong_count()
}
}

impl From<DmaFile> for OwnedDmaFile {
Expand Down Expand Up @@ -880,7 +902,31 @@ pub struct WeakDmaFile {
pollable: PollableStatus,
}

impl Default for WeakDmaFile {
fn default() -> Self {
Self::new()
}
}

impl WeakDmaFile {
/// Creates an empty reference that will never upgrade.
pub fn new() -> WeakDmaFile {
Self {
file: WeakGlommioFile::new(),
o_direct_alignment: 0,
max_sectors_size: 0,
max_segment_size: 0,
pollable: PollableStatus::Pollable,
}
}

/// The number of strong references that still remain on the file.
/// If created via [Self::new], this will return 0.
pub fn strong_count(&self) -> usize {
self.file.strong_count()
}

/// Returns an `Option` containing ownership over the file if the file hasn't been closed yet, otherwise None.
pub fn upgrade(&self) -> Option<OwnedDmaFile> {
self.file.upgrade().map(|file| OwnedDmaFile {
file,
Expand Down Expand Up @@ -2131,4 +2177,35 @@ pub(crate) mod test {
})
.await;
});

#[test]
fn new_weak_file_strong_count() {
assert_eq!(WeakDmaFile::new().strong_count(), 0);
}

dma_file_test!(weak_file_strong_count, path, _k, {
let file = OpenOptions::new()
.create_new(true)
.read(true)
.write(true)
.tmpfile(true)
.dma_open(&path)
.await
.unwrap();

let weak = file.downgrade();
assert_eq!(weak.strong_count(), 1);

let cloned = file.clone();

assert_eq!(weak.strong_count(), 2);

std::mem::drop(file);

assert_eq!(weak.strong_count(), 1);

std::mem::drop(cloned);

assert_eq!(weak.strong_count(), 0);
});
}
20 changes: 18 additions & 2 deletions glommio/src/io/glommio_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ impl GlommioFile {
dev_minor: self.dev_minor,
}
}

pub(crate) fn strong_count(&self) -> usize {
self.file.as_ref().map_or(0, Arc::strong_count)
}
}

/// This lets you open a DmaFile on one thread and then send it safely to another thread for processing.
Expand Down Expand Up @@ -384,7 +388,11 @@ impl OwnedGlommioFile {
})
}

pub fn downgrade(&self) -> WeakGlommioFile {
pub(crate) fn strong_count(&self) -> usize {
self.fd.as_ref().map_or(0, Arc::strong_count)
}

pub(crate) fn downgrade(&self) -> WeakGlommioFile {
WeakGlommioFile {
fd: self.fd.as_ref().map_or(AWeak::new(), Arc::downgrade),
path: self.path.clone(),
Expand Down Expand Up @@ -437,7 +445,7 @@ impl From<GlommioFile> for OwnedGlommioFile {
}
}

#[derive(Debug, Clone)]
#[derive(Default, Debug, Clone)]
pub(crate) struct WeakGlommioFile {
pub(crate) fd: AWeak<RawFd>,
pub(crate) path: Option<PathBuf>,
Expand All @@ -447,6 +455,14 @@ pub(crate) struct WeakGlommioFile {
}

impl WeakGlommioFile {
pub(crate) fn new() -> Self {
Self::default()
}

pub(crate) fn strong_count(&self) -> usize {
self.fd.strong_count()
}

pub(crate) fn upgrade(&self) -> Option<OwnedGlommioFile> {
self.fd.upgrade().map(|fd| OwnedGlommioFile {
fd: Some(fd),
Expand Down
2 changes: 1 addition & 1 deletion glommio/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub use self::{
},
bulk_io::{IoVec, MergedBufferLimit, ReadAmplificationLimit, ReadManyResult},
directory::Directory,
dma_file::{CloseResult, DmaFile, OwnedDmaFile},
dma_file::{CloseResult, DmaFile, OwnedDmaFile, WeakDmaFile},
dma_file_stream::{
DmaStreamReader, DmaStreamReaderBuilder, DmaStreamWriter, DmaStreamWriterBuilder,
},
Expand Down

0 comments on commit 83d3023

Please sign in to comment.