Skip to content

Commit 67160b2

Browse files
committed
Implement try_close_finalizer, do not wait for liblock, fix JuliaIO#1048
1 parent bbb6b26 commit 67160b2

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

src/HDF5.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export @read,
6262
# H5DataStore, Attribute, File, Group, Dataset, Datatype, Opaque,
6363
# Dataspace, Object, Properties, VLen, ChunkStorage, Reference
6464

65+
66+
6567
h5doc(name) = "[`$name`](https://portal.hdfgroup.org/display/HDF5/$(name))"
6668

6769
include("api/api.jl")

src/api/api.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ else
1313
)
1414
end
1515

16-
const liblock = ReentrantLock()
17-
16+
include("lock.jl")
1817
include("types.jl")
1918
include("error.jl")
2019
include("functions.jl") # core API ccall wrappers

src/api/lock.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const liblock = ReentrantLock()
2+
3+
# Try to acquire the lock (test-test-set) and close if successful
4+
# Otherwise, defer finalization
5+
# https://github.com/JuliaIO/HDF5.jl/issues/1048
6+
function try_close_finalizer(x)
7+
if !islocked(liblock) &&
8+
trylock(liblock) do
9+
close(x)
10+
true
11+
end
12+
else
13+
finalizer(try_close_finalizer, x)
14+
end
15+
end
16+

src/properties.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ macro propertyclass(name, classid)
104104
id::API.hid_t
105105
function $name(id::API.hid_t)
106106
obj = new(id)
107-
finalizer(close, obj)
107+
finalizer(API.try_close_finalizer, obj)
108108
obj
109109
end
110110
end

src/types.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Supertype of HDF5.File, HDF5.Group, JldFile, JldGroup, Matlabv5File, and MatlabHDF5File.
66
abstract type H5DataStore end
77

8+
89
# Read a list of variables, read(parent, "A", "B", "x", ...)
910
function Base.read(parent::H5DataStore, name::AbstractString...)
1011
tuple((read(parent, x) for x in name)...)
@@ -41,7 +42,7 @@ mutable struct File <: H5DataStore
4142
function File(id, filename, toclose::Bool=true)
4243
f = new(id, filename)
4344
if toclose
44-
finalizer(close, f)
45+
finalizer(API.try_close_finalizer, f)
4546
end
4647
f
4748
end
@@ -55,7 +56,7 @@ mutable struct Group <: H5DataStore
5556

5657
function Group(id, file)
5758
g = new(id, file)
58-
finalizer(close, g)
59+
finalizer(API.try_close_finalizer, g)
5960
g
6061
end
6162
end
@@ -69,7 +70,7 @@ mutable struct Dataset
6970

7071
function Dataset(id, file, xfer=DatasetTransferProperties())
7172
dset = new(id, file, xfer)
72-
finalizer(close, dset)
73+
finalizer(API.try_close_finalizer, dset)
7374
dset
7475
end
7576
end
@@ -84,14 +85,14 @@ mutable struct Datatype
8485
function Datatype(id, toclose::Bool=true)
8586
nt = new(id, toclose)
8687
if toclose
87-
finalizer(close, nt)
88+
finalizer(API.try_close_finalizer, nt)
8889
end
8990
nt
9091
end
9192
function Datatype(id, file::File, toclose::Bool=true)
9293
nt = new(id, toclose, file)
9394
if toclose
94-
finalizer(close, nt)
95+
finalizer(API.try_close_finalizer, nt)
9596
end
9697
nt
9798
end
@@ -106,7 +107,7 @@ mutable struct Dataspace
106107

107108
function Dataspace(id)
108109
dspace = new(id)
109-
finalizer(close, dspace)
110+
finalizer(API.try_close_finalizer, dspace)
110111
dspace
111112
end
112113
end
@@ -119,7 +120,7 @@ mutable struct Attribute
119120

120121
function Attribute(id, file)
121122
dset = new(id, file)
122-
finalizer(close, dset)
123+
finalizer(API.try_close_finalizer, dset)
123124
dset
124125
end
125126
end

0 commit comments

Comments
 (0)