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

add lock around all API calls #1021

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion gen/bind_generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,13 @@ function _bind(__module__, __source__, sig::Expr, err::Union{String,Expr,Nothing
# avoids inserting the line number nodes for the macro --- the call site
# is instead explicitly injected into the function body via __source__.
jlfuncsig = Expr(:call, jlfuncname, args...)
jlfuncbody = Expr(:block, __source__, :($statsym = $ccallexpr))
jlfuncbody = Expr(
:block, __source__, :(lock(liblock)), :($statsym = try
$ccallexpr
finally
unlock(liblock)
end)
)
Comment on lines +244 to +250
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
jlfuncbody = Expr(
:block, __source__, :(lock(liblock)), :($statsym = try
$ccallexpr
finally
unlock(liblock)
end)
)
jlfuncbody = Expr(
:block, __source__, :(use_lock() && lock(liblock)), :($statsym = try
$ccallexpr
finally
use_lock && unlock(liblock)
end)
)

Copy link
Collaborator Author

@simonbyrne simonbyrne Nov 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds two runtime lookups at each call

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If use_lock is constant, (e.g. use_lock() = false), it gets inlined. The generated code does not actually call use_lock at runtime.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I saw that's what you did in #1022. That's an option, but I would argue that using the lock should be the default.

if errexpr !== nothing
push!(jlfuncbody.args, errexpr)
end
Expand Down
2 changes: 2 additions & 0 deletions src/api/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ else
)
end

const liblock = ReentrantLock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const liblock = ReentrantLock()
const liblock = ReentrantLock()
const _USE_LOCK = Ref(true)
use_lock() = _USE_LOCK[]


include("types.jl")
include("error.jl")
include("functions.jl") # core API ccall wrappers
Expand Down
Loading