Skip to content

Commit

Permalink
Implement try_close_finalizer, do not wait for liblock, fix JuliaIO#1048
Browse files Browse the repository at this point in the history
  • Loading branch information
mkitti committed Feb 3, 2023
1 parent bbb6b26 commit 67160b2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export @read,
# H5DataStore, Attribute, File, Group, Dataset, Datatype, Opaque,
# Dataspace, Object, Properties, VLen, ChunkStorage, Reference



h5doc(name) = "[`$name`](https://portal.hdfgroup.org/display/HDF5/$(name))"

include("api/api.jl")
Expand Down
3 changes: 1 addition & 2 deletions src/api/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ else
)
end

const liblock = ReentrantLock()

include("lock.jl")
include("types.jl")
include("error.jl")
include("functions.jl") # core API ccall wrappers
Expand Down
16 changes: 16 additions & 0 deletions src/api/lock.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const liblock = ReentrantLock()

# Try to acquire the lock (test-test-set) and close if successful
# Otherwise, defer finalization
# https://github.com/JuliaIO/HDF5.jl/issues/1048
function try_close_finalizer(x)
if !islocked(liblock) &&
trylock(liblock) do
close(x)
true
end
else
finalizer(try_close_finalizer, x)
end
end

2 changes: 1 addition & 1 deletion src/properties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ macro propertyclass(name, classid)
id::API.hid_t
function $name(id::API.hid_t)
obj = new(id)
finalizer(close, obj)
finalizer(API.try_close_finalizer, obj)
obj
end
end
Expand Down
15 changes: 8 additions & 7 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Supertype of HDF5.File, HDF5.Group, JldFile, JldGroup, Matlabv5File, and MatlabHDF5File.
abstract type H5DataStore end


# Read a list of variables, read(parent, "A", "B", "x", ...)
function Base.read(parent::H5DataStore, name::AbstractString...)
tuple((read(parent, x) for x in name)...)
Expand Down Expand Up @@ -41,7 +42,7 @@ mutable struct File <: H5DataStore
function File(id, filename, toclose::Bool=true)
f = new(id, filename)
if toclose
finalizer(close, f)
finalizer(API.try_close_finalizer, f)
end
f
end
Expand All @@ -55,7 +56,7 @@ mutable struct Group <: H5DataStore

function Group(id, file)
g = new(id, file)
finalizer(close, g)
finalizer(API.try_close_finalizer, g)
g
end
end
Expand All @@ -69,7 +70,7 @@ mutable struct Dataset

function Dataset(id, file, xfer=DatasetTransferProperties())
dset = new(id, file, xfer)
finalizer(close, dset)
finalizer(API.try_close_finalizer, dset)
dset
end
end
Expand All @@ -84,14 +85,14 @@ mutable struct Datatype
function Datatype(id, toclose::Bool=true)
nt = new(id, toclose)
if toclose
finalizer(close, nt)
finalizer(API.try_close_finalizer, nt)
end
nt
end
function Datatype(id, file::File, toclose::Bool=true)
nt = new(id, toclose, file)
if toclose
finalizer(close, nt)
finalizer(API.try_close_finalizer, nt)
end
nt
end
Expand All @@ -106,7 +107,7 @@ mutable struct Dataspace

function Dataspace(id)
dspace = new(id)
finalizer(close, dspace)
finalizer(API.try_close_finalizer, dspace)
dspace
end
end
Expand All @@ -119,7 +120,7 @@ mutable struct Attribute

function Attribute(id, file)
dset = new(id, file)
finalizer(close, dset)
finalizer(API.try_close_finalizer, dset)
dset
end
end
Expand Down

0 comments on commit 67160b2

Please sign in to comment.