Skip to content

Add @gcsafe ccall #835

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

Closed
wants to merge 7 commits into from
Closed
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
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.16.0"
version = "4.17.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand All @@ -13,14 +13,15 @@ julia = "1.6"

[extras]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[extensions]
CompatLinearAlgebraExt = "LinearAlgebra"

[targets]
test = ["Dates", "LinearAlgebra", "Test"]
test = ["Dates", "InteractiveUtils", "LinearAlgebra", "Test"]

[weakdeps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ changes in `julia`.

## Supported features

* `@gcsafe_ccall` allows GC to run while a Julia thread is in the `ccall` ([#49933]) (since Compat 4.17.0)

* `insertdims(D; dims)` is the opposite of `dropdims` ([#45793]) (since Compat 4.16.0)

* `Compat.Fix{N}` which fixes an argument at the `N`th position ([#54653]) (since Compat 4.16.0)
Expand Down Expand Up @@ -196,5 +198,6 @@ Note that you should specify the correct minimum version for `Compat` in the
[#47354]: https://github.com/JuliaLang/julia/issues/47354
[#47679]: https://github.com/JuliaLang/julia/pull/47679
[#48038]: https://github.com/JuliaLang/julia/issues/48038
[#49933]: https://github.com/JuliaLang/julia/pull/49933
[#50105]: https://github.com/JuliaLang/julia/issues/50105
[#54653]: https://github.com/JuliaLang/julia/issues/54653
71 changes: 71 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,77 @@ else
using Base: Fix, Fix1, Fix2
end

## version of ccall that calls jl_gc_safe_enter|leave around the inner ccall
# note that this is generally only safe with functions that do not call back into Julia.
# when callbacks occur, the code should ensure the GC is not running by wrapping the code
# in the `@gcunsafe` macro

const HAS_CCALL_GCSAFE = VERSION >= v"1.13.0-DEV.70" || v"1.12-DEV.2029" <= VERSION < v"1.13-"

"""
@gcsafe_ccall ...

Call a foreign function just like [`@ccall`](https://docs.julialang.org/en/v1/base/c/#Base.@ccall), but marking it safe for the GC to run. This is
useful for functions that may block, so that the GC isn't blocked from running, but may also
be required to prevent deadlocks (see JuliaGPU/CUDA.jl#2261).

Note that this is generally only safe with non-Julia C functions that do not call back into
the Julia directly.
"""
macro gcsafe_ccall end
export @gcsafe_ccall

if HAS_CCALL_GCSAFE
macro gcsafe_ccall(expr)
exprs = Any[:(gc_safe = true), expr]
return Base.ccall_macro_lower((:ccall), Base.ccall_macro_parse(exprs)...)
end
else
function ccall_macro_lower(func, rettype, types, args, nreq)
# instead of re-using ccall or Expr(:foreigncall) to perform argument conversion,
# we need to do so ourselves in order to insert a jl_gc_safe_enter|leave
# just around the inner ccall

cconvert_exprs = []
cconvert_args = []
for (typ, arg) in zip(types, args)
var = gensym("$(func)_cconvert")
push!(cconvert_args, var)
push!(cconvert_exprs, :($var = Base.cconvert($(esc(typ)), $(esc(arg)))))
end

unsafe_convert_exprs = []
unsafe_convert_args = []
for (typ, arg) in zip(types, cconvert_args)
var = gensym("$(func)_unsafe_convert")
push!(unsafe_convert_args, var)
push!(unsafe_convert_exprs, :($var = Base.unsafe_convert($(esc(typ)), $arg)))
end

call = quote
$(unsafe_convert_exprs...)

gc_state = @ccall(jl_gc_safe_enter()::Int8)
ret = ccall(
$(esc(func)), $(esc(rettype)), $(Expr(:tuple, map(esc, types)...)),
$(unsafe_convert_args...)
)
@ccall(jl_gc_safe_leave(gc_state::Int8)::Cvoid)
ret
end

return quote
@inline
$(cconvert_exprs...)
GC.@preserve $(cconvert_args...) $(call)
end
end

macro gcsafe_ccall(expr)
return ccall_macro_lower(Base.ccall_macro_parse(expr)...)
end
end # HAS_CCALL_GCSAFE

include("deprecated.jl")

end # module Compat
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Compat
using Dates
using InteractiveUtils
using TOML
using Test

Expand Down Expand Up @@ -1071,3 +1072,25 @@ end
end
end
end

@testset "gcsafe_ccall" begin
function gc_safe_ccall()
# jl_errno is marked as JL_NOTSAFEPOINT
@gcsafe_ccall jl_errno()::Cint
end

let llvm = sprint(code_llvm, gc_safe_ccall, ())
# check that the call works
@test gc_safe_ccall() isa Cint
# v1.10 is hard to test since ccall are just raw runtime pointers
if VERSION >= v"1.11"
if !Compat.HAS_CCALL_GCSAFE
# check for the gc_safe store
@test occursin("jl_gc_safe_enter", llvm)
@test occursin("jl_gc_safe_leave", llvm)
else
@test occursin("store atomic i8 2", llvm)
end
end
end
end
Loading