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

Change close_rc()'s API #414

Merged
merged 5 commits into from
Sep 28, 2021
Merged
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
24 changes: 16 additions & 8 deletions glommio/src/io/dma_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ use std::{

pub(super) type Result<T> = crate::Result<T, ()>;

/// Close result of [`DmaFile::close_rc()`]. Indicates which operation is
/// performed on close.
#[derive(Debug)]
pub enum CloseResult {
/// The file is closed.
Closed,
/// There are other references existing, only removed this one.
Unreferenced,
}

pub(crate) fn align_up(v: u64, align: u64) -> u64 {
(v + align - 1) & !(align - 1)
}
Expand Down Expand Up @@ -403,15 +413,13 @@ impl DmaFile {
self.file.path()
}

/// Convenience method that closes a DmaFile wrapped inside an Rc
pub async fn close_rc(self: Rc<DmaFile>) -> Result<()> {
/// Convenience method that closes a DmaFile wrapped inside an Rc.
///
/// Returns [CloseResult] to indicate which operation was performed.
pub async fn close_rc(self: Rc<DmaFile>) -> Result<CloseResult> {
match Rc::try_unwrap(self) {
Err(file) => Err(io::Error::new(
io::ErrorKind::Other,
format!("{} references to file still held", Rc::strong_count(&file)),
)
.into()),
Ok(file) => file.close().await,
Err(_) => Ok(CloseResult::Unreferenced),
Ok(file) => file.close().await.map(|_| CloseResult::Closed),
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions glommio/src/io/dma_file_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,7 @@ impl DmaStreamReader {
let to_cancel = handles.len();
let cancelled = stream::iter(handles).then(|f| f).count().await;
assert_eq!(to_cancel, cancelled);

// the file may be used by other processes so failing to close it is not an
// issue.
let _ = self.file.close_rc().await;
self.file.close_rc().await.unwrap();

let mut state = self.state.borrow_mut();
match state.error.take() {
Expand Down
5 changes: 2 additions & 3 deletions glommio/src/io/immutable_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,9 @@ impl ImmutableFile {
}

/// Closes this [`ImmutableFile`]
/// Note that this method returns an error if other entities hold references
/// to the underlying file, such as read streams.
pub async fn close(self) -> Result<()> {
self.stream_builder.file.close_rc().await
self.stream_builder.file.close_rc().await?;
Ok(())
}

/// Creates a [`DmaStreamReaderBuilder`] from this `ImmutableFile`.
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 @@ -163,7 +163,7 @@ pub use self::{
},
bulk_io::{IoVec, ReadManyResult},
directory::Directory,
dma_file::DmaFile,
dma_file::{CloseResult, DmaFile},
dma_file_stream::{
DmaStreamReader,
DmaStreamReaderBuilder,
Expand Down