Skip to content

Commit 49053fd

Browse files
committed
Document file close degree
1 parent 6c817cf commit 49053fd

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
@@ -99,7 +99,7 @@ pub mod tests {
9999
}
100100

101101
fn decref(&self) {
102-
unsafe { self.0.decref() }
102+
self.0.decref()
103103
}
104104
}
105105

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

167-
obj.decref();
168167
h5lock!({
168+
// We must hold a lock here to prevent another thread creating an object
169+
// with the same identifier as the one we just owned. Failing to do this
170+
// might lead to the wrong object being dropped.
171+
obj.decref();
169172
obj.decref();
173+
// We here have to dangling identifiers stored in obj and obj2. As this part
174+
// is locked we know some other object is not going to created with these
175+
// identifiers
170176
assert!(!obj.is_valid());
171177
assert!(!obj2.is_valid());
178+
// By manually dropping we don't close some other unrelated objects.
179+
// Dropping/closing an invalid object is allowed
172180
drop(obj);
173181
drop(obj2);
174182
});

src/hl/plist/file_access.rs

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

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

0 commit comments

Comments
 (0)