Skip to content

Commit

Permalink
change close_rc()'s API (#414)
Browse files Browse the repository at this point in the history
* change close_rc() to return Result<bool>

* update doc

* add enum CloseResult

Co-authored-by: Hippolyte Barraud <[email protected]>
  • Loading branch information
waynexia and HippoBaro authored Sep 28, 2021
1 parent fc0af8c commit e99328d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
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

0 comments on commit e99328d

Please sign in to comment.