Skip to content

Commit d4108ef

Browse files
committed
Make the call convert before entering the gc safe region. Also preserve the arguments
1 parent c335d2e commit d4108ef

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

base/threadcall.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,19 @@ macro threadcall(f, rettype, argtypes, argvals...)
3939
args = Symbol[]
4040
for (i, T) in enumerate(argtypes)
4141
arg = Symbol("arg", i)
42-
push!(body, :($arg = unsafe_load(convert(Ptr{$T}, p))))
42+
push!(body, :($arg = unsafe_convert($T, cconvert($T, unsafe_load(convert(Ptr{$T}, p))))))
4343
push!(body, :(p += Core.sizeof($T)))
4444
push!(args, arg)
4545
end
46-
push!(body, :(gc_state = ccall(:jl_gc_safe_enter, Int8, (), )))
47-
push!(body, :(ret = ccall(fptr, $rettype, ($(argtypes...),), $(args...))))
48-
push!(body, :(unsafe_store!(convert(Ptr{$rettype}, retval_ptr), ret)))
49-
push!(body, :(gc_state = ccall(:jl_gc_safe_leave, Cvoid, (Int8,), gc_state)))
50-
push!(body, :(return Int(Core.sizeof($rettype))))
46+
append!(body, (quote
47+
GC.@preserve $(args...) begin
48+
gc_state = ccall(:jl_gc_safe_enter, Int8, ())
49+
ret = ccall(fptr, $rettype, ($(argtypes...),), $(args...))
50+
ccall(:jl_gc_safe_leave, Cvoid, (Int8,), gc_state)
51+
unsafe_store!(convert(Ptr{$rettype}, retval_ptr), ret)
52+
return Int(Core.sizeof($rettype))
53+
end
54+
end).args)
5155

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

0 commit comments

Comments
 (0)