Skip to content

Commit 57a4dff

Browse files
committed
Document file close degree
1 parent 707414f commit 57a4dff

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/handle.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,17 @@ impl Handle {
5454
self.id
5555
}
5656

57+
/// Increment the reference count of the handle
5758
pub fn incref(&self) {
5859
if is_valid_user_id(self.id()) {
5960
h5lock!(H5Iinc_ref(self.id()));
6061
}
6162
}
6263

63-
/// An object should not be decreffed unless it has an
64-
/// associated incref
65-
pub unsafe fn decref(&self) {
64+
/// Decrease the reference count of the handle
65+
///
66+
/// This function should only be used if `incref` has been used
67+
pub fn decref(&self) {
6668
h5lock!({
6769
if self.is_valid_id() {
6870
H5Idec_ref(self.id());
@@ -80,7 +82,8 @@ impl Handle {
8082
is_valid_id(self.id())
8183
}
8284

83-
pub(crate) fn refcount(&self) -> u32 {
85+
/// Return the reference count of the object
86+
pub fn refcount(&self) -> u32 {
8487
refcount(self.id).unwrap_or(0) as u32
8588
}
8689
}

src/hl/object.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub mod tests {
8989
}
9090

9191
fn decref(&self) {
92-
unsafe { self.0.decref() }
92+
self.0.decref()
9393
}
9494
}
9595

@@ -154,11 +154,19 @@ pub mod tests {
154154
// We can now take, as we have exactly two handles
155155
let obj2 = unsafe { std::mem::ManuallyDrop::take(&mut obj2) };
156156

157-
obj.decref();
158157
h5lock!({
158+
// We must hold a lock here to prevent another thread creating an object
159+
// with the same identifier as the one we just owned. Failing to do this
160+
// might lead to the wrong object being dropped.
161+
obj.decref();
159162
obj.decref();
163+
// We here have to dangling identifiers stored in obj and obj2. As this part
164+
// is locked we know some other object is not going to created with these
165+
// identifiers
160166
assert!(!obj.is_valid());
161167
assert!(!obj2.is_valid());
168+
// By manually dropping we don't close some other unrelated objects.
169+
// Dropping/closing an invalid object is allowed
162170
drop(obj);
163171
drop(obj2);
164172
});

src/hl/plist/file_access.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,11 @@ impl FileAccessBuilder {
10271027
Ok(builder)
10281028
}
10291029

1030+
/// Sets the file close degree
1031+
///
1032+
/// If called with `FileCloseDegree::Strong`, the programmer is responsible
1033+
/// for closing all items before closing the file. Failure to do so might
1034+
/// invalidate newly created objects.
10301035
pub fn fclose_degree(&mut self, fc_degree: FileCloseDegree) -> &mut Self {
10311036
self.fclose_degree = Some(fc_degree);
10321037
self
@@ -1375,6 +1380,8 @@ impl FileAccessBuilder {
13751380
if let Some(v) = self.chunk_cache {
13761381
h5try!(H5Pset_cache(id, 0, v.nslots as _, v.nbytes as _, v.w0 as _));
13771382
}
1383+
// The default is to use CLOSE_SEMI or CLOSE_WEAK, depending on VFL driver.
1384+
// Both of these are unproblematic for our ownership
13781385
if let Some(v) = self.fclose_degree {
13791386
h5try!(H5Pset_fclose_degree(id, v.into()));
13801387
}

0 commit comments

Comments
 (0)