Skip to content

Commit 0ce15b6

Browse files
committed
Add test for @gcsafe_ccall
1 parent f6a5e08 commit 0ce15b6

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/gcsafe_ccall.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ const HAS_CCALL_GCSAFE = VERSION >= v"1.13.0-DEV.70" || v"1.12-DEV.2029" <= VERS
77
"""
88
@gcsafe_ccall ...
99
10-
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
11-
useful for functions that may block, so that the GC isn't blocked from running, but may also
12-
be required to prevent deadlocks (see JuliaGPU/CUDA.jl#2261).
10+
Call a foreign function just like [`@ccall`](https://docs.julialang.org/en/v1/base/c/#Base.@ccall),
11+
but marking it safe for the GC to run. This is useful for functions that may block, so that the GC
12+
isn't blocked from running, but may also be required to prevent deadlocks (see JuliaGPU/CUDA.jl#2261).
1313
14-
Note that this is generally only safe with non-Julia C functions that do not call back into
15-
the Julia directly.
14+
Note that this is generally only safe with non-Julia C functions that do not call back into Julia
15+
directly.
1616
"""
1717
macro gcsafe_ccall end
18-
export @gcsafe_ccall
1918

2019
if HAS_CCALL_GCSAFE
2120
macro gcsafe_ccall(expr)

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[deps]
22
CodecLz4 = "5ba52731-8f18-5e0d-9241-30f10d1ec561"
3+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
34
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
45
TestsForCodecPackages = "c2e61002-3542-480d-8b3c-5f05cc4f8554"
56
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

test/gcsafe_ccall.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using CodecLz4
2+
using InteractiveUtils
3+
4+
@testset "gcsafe_ccall" begin
5+
function gc_safe_ccall()
6+
# jl_errno is marked as JL_NOTSAFEPOINT
7+
CodecLz4.@gcsafe_ccall jl_errno()::Cint
8+
end
9+
10+
let llvm = sprint(code_llvm, gc_safe_ccall, ())
11+
# check that the call works
12+
@test gc_safe_ccall() isa Cint
13+
# v1.10 is hard to test since ccall are just raw runtime pointers
14+
if VERSION >= v"1.11"
15+
if !CodecLz4.HAS_CCALL_GCSAFE
16+
# check for the gc_safe store
17+
@test occursin("jl_gc_safe_enter", llvm)
18+
@test occursin("jl_gc_safe_leave", llvm)
19+
else
20+
@test occursin("store atomic i8 2", llvm)
21+
end
22+
end
23+
end
24+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using CodecLz4
22
using Test
33

44
@testset "CodecLz4.jl" begin
5+
include("gcsafe_ccall.jl")
56
include("headers/lz4.jl")
67
include("headers/lz4frame.jl")
78
include("headers/lz4hc.jl")

0 commit comments

Comments
 (0)