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

Make threadcall gc safe #55956

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions base/threadcall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ macro threadcall(f, rettype, argtypes, argvals...)
args = Symbol[]
for (i, T) in enumerate(argtypes)
arg = Symbol("arg", i)
push!(body, :($arg = unsafe_load(convert(Ptr{$T}, p))))
push!(body, :($arg = unsafe_convert($T, cconvert($T, unsafe_load(convert(Ptr{$T}, p))))))
Copy link
Member

Choose a reason for hiding this comment

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

I don't fully understand the code here (why is there an unsafe_load), but the return from cconvert must be GC preserved.

Copy link
Member Author

Choose a reason for hiding this comment

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

Threadcall passes the args in an array of ptrs if I understood the code correctly

push!(body, :(p += Core.sizeof($T)))
push!(args, arg)
end
push!(body, :(ret = ccall(fptr, $rettype, ($(argtypes...),), $(args...))))
push!(body, :(unsafe_store!(convert(Ptr{$rettype}, retval_ptr), ret)))
push!(body, :(return Int(Core.sizeof($rettype))))
append!(body, (quote
GC.@preserve $(args...) begin
Copy link
Member

@vchuravy vchuravy Oct 16, 2024

Choose a reason for hiding this comment

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

You are preserving the unsafe_convert not the cconvert here, right?

gc_state = ccall(:jl_gc_safe_enter, Int8, ())
ret = ccall(fptr, $rettype, ($(argtypes...),), $(args...))
ccall(:jl_gc_safe_leave, Cvoid, (Int8,), gc_state)
unsafe_store!(convert(Ptr{$rettype}, retval_ptr), ret)
return Int(Core.sizeof($rettype))
end
end).args)

# return code to generate wrapper function and send work request thread queue
wrapper = Expr(:var"hygienic-scope", wrapper, @__MODULE__, __source__)
Expand Down