Skip to content

Commit a7a3fb3

Browse files
committed
use Lockable in GC for consistency
1 parent d4a94f9 commit a7a3fb3

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/GC/GC.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ See [`gc`](@ref).
88
module GC
99

1010
using ..C: C
11+
using ..Utils: Lockable
1112

12-
const QUEUE = (; items = C.PyPtr[], lock = Threads.SpinLock())
13+
const QUEUE = Lockable(C.PyPtr[], Threads.SpinLock())
1314
const HOOK = Ref{WeakRef}()
1415

1516
"""
@@ -67,13 +68,13 @@ function gc()
6768
end
6869

6970
function unsafe_free_queue()
70-
Base.@lock QUEUE.lock begin
71-
for ptr in QUEUE.items
71+
Base.@lock QUEUE begin
72+
for ptr in QUEUE[]
7273
if ptr != C.PyNULL
7374
C.Py_DecRef(ptr)
7475
end
7576
end
76-
empty!(QUEUE.items)
77+
empty!(QUEUE[])
7778
end
7879
nothing
7980
end
@@ -87,7 +88,7 @@ function enqueue(ptr::C.PyPtr)
8788
# If the current thread holds the GIL, then we can immediately free.
8889
C.Py_DecRef(ptr)
8990
# We may as well also free any other enqueued objects.
90-
if !isempty(QUEUE.items)
91+
if !isempty(QUEUE.value)
9192
unsafe_free_queue()
9293
end
9394
else
@@ -96,7 +97,7 @@ function enqueue(ptr::C.PyPtr)
9697
# in the branch above.
9798
# (b) If the GCHook() object below is finalized in an ordinary GC.
9899
# (c) If the user calls PythonCall.GC.gc().
99-
Base.@lock QUEUE.lock push!(QUEUE.items, ptr)
100+
Base.@lock QUEUE push!(QUEUE[], ptr)
100101
end
101102
end
102103
nothing
@@ -110,11 +111,11 @@ function enqueue_all(ptrs)
110111
C.Py_DecRef(ptr)
111112
end
112113
end
113-
if !isempty(QUEUE.items)
114+
if !isempty(QUEUE.value)
114115
unsafe_free_queue()
115116
end
116117
else
117-
Base.@lock QUEUE.lock append!(QUEUE.items, ptrs)
118+
Base.@lock QUEUE append!(QUEUE[], ptrs)
118119
end
119120
end
120121
nothing
@@ -139,7 +140,7 @@ end
139140
function _gchook_finalizer(x)
140141
if C.CTX.is_initialized
141142
finalizer(_gchook_finalizer, x)
142-
if !isempty(QUEUE.items) && C.PyGILState_Check() == 1
143+
if !isempty(QUEUE.value) && C.PyGILState_Check() == 1
143144
unsafe_free_queue()
144145
end
145146
end

test/GC.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
end
88
Threads.nthreads() > 1 &&
99
VERSION >= v"1.10.0-" &&
10-
@test !isempty(PythonCall.GC.QUEUE.items)
10+
@test !isempty(PythonCall.GC.QUEUE.value)
1111
PythonCall.GC.gc()
12-
@test isempty(PythonCall.GC.QUEUE.items)
12+
@test isempty(PythonCall.GC.QUEUE.value)
1313
end
1414

1515
@testitem "GC.GCHook" begin
@@ -21,7 +21,7 @@ end
2121
end
2222
Threads.nthreads() > 1 &&
2323
VERSION >= v"1.10.0-" &&
24-
@test !isempty(PythonCall.GC.QUEUE.items)
24+
@test !isempty(PythonCall.GC.QUEUE.value)
2525
GC.gc()
26-
@test isempty(PythonCall.GC.QUEUE.items)
26+
@test isempty(PythonCall.GC.QUEUE.value)
2727
end

0 commit comments

Comments
 (0)