Skip to content

Commit a36d7c0

Browse files
author
Christopher Doris
committed
revert to using a vector for the queue
1 parent e230ce9 commit a36d7c0

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/GC/GC.jl

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ module GC
99

1010
using ..C: C
1111

12-
const QUEUE = Channel{C.PyPtr}(Inf)
12+
const QUEUE = C.PyPtr[]
13+
const QUEUE_LOCK = Threads.SpinLock()
1314
const HOOK = WeakRef()
1415

1516
"""
@@ -55,22 +56,28 @@ function gc()
5556
end
5657

5758
function unsafe_free_queue()
58-
while isready(QUEUE)
59-
ptr = take!(QUEUE)
59+
lock(QUEUE_LOCK)
60+
for ptr in QUEUE
6061
if ptr != C.PyNULL
6162
C.Py_DecRef(ptr)
6263
end
6364
end
65+
empty!(QUEUE)
66+
unlock(QUEUE_LOCK)
6467
nothing
6568
end
6669

6770
function enqueue(ptr::C.PyPtr)
6871
if ptr != C.PyNULL && C.CTX.is_initialized
6972
if C.PyGILState_Check() == 1
7073
C.Py_DecRef(ptr)
71-
unsafe_free_queue()
74+
if !isempty(QUEUE)
75+
unsafe_free_queue()
76+
end
7277
else
73-
put!(QUEUE, ptr)
78+
lock(QUEUE_LOCK)
79+
push!(QUEUE, ptr)
80+
unlock(QUEUE_LOCK)
7481
end
7582
end
7683
nothing
@@ -84,11 +91,13 @@ function enqueue_all(ptrs)
8491
C.Py_DecRef(ptr)
8592
end
8693
end
87-
unsafe_free_queue()
88-
else
89-
for ptr in ptrs
90-
put!(QUEUE, ptr)
94+
if !isempty(QUEUE)
95+
unsafe_free_queue()
9196
end
97+
else
98+
lock(QUEUE_LOCK)
99+
append!(QUEUE, ptrs)
100+
unlock(QUEUE_LOCK)
92101
end
93102
end
94103
nothing
@@ -113,7 +122,7 @@ end
113122
function _gchook_finalizer(x)
114123
if C.CTX.is_initialized
115124
finalizer(_gchook_finalizer, x)
116-
if isready(QUEUE) && C.PyGILState_Check() == 1
125+
if !isempty(QUEUE) && C.PyGILState_Check() == 1
117126
unsafe_free_queue()
118127
end
119128
end

0 commit comments

Comments
 (0)