Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement try_close_finalizer, fix #1048 #1049

Merged
merged 3 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 14 additions & 0 deletions src/api/lock.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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
simonbyrne marked this conversation as resolved.
Show resolved Hide resolved
end
simonbyrne marked this conversation as resolved.
Show resolved Hide resolved
else
finalizer(try_close_finalizer, x)
end
end
mkitti marked this conversation as resolved.
Show resolved Hide resolved
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
14 changes: 7 additions & 7 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 +55,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 +69,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 +84,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 +106,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 +119,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