Skip to content

Commit f021072

Browse files
author
Christopher Doris
committed
combine queue into a single item
1 parent a5a2c96 commit f021072

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/GC/GC.jl

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

1010
using ..C: C
1111

12-
const QUEUE = C.PyPtr[]
13-
const QUEUE_LOCK = Threads.SpinLock()
12+
const QUEUE = (; items = C.PyPtr[], lock = Threads.SpinLock())
1413
const HOOK = WeakRef()
1514

1615
"""
@@ -56,28 +55,28 @@ function gc()
5655
end
5756

5857
function unsafe_free_queue()
59-
lock(QUEUE_LOCK)
60-
for ptr in QUEUE
58+
lock(QUEUE.lock)
59+
for ptr in QUEUE.items
6160
if ptr != C.PyNULL
6261
C.Py_DecRef(ptr)
6362
end
6463
end
65-
empty!(QUEUE)
66-
unlock(QUEUE_LOCK)
64+
empty!(QUEUE.items)
65+
unlock(QUEUE.lock)
6766
nothing
6867
end
6968

7069
function enqueue(ptr::C.PyPtr)
7170
if ptr != C.PyNULL && C.CTX.is_initialized
7271
if C.PyGILState_Check() == 1
7372
C.Py_DecRef(ptr)
74-
if !isempty(QUEUE)
73+
if !isempty(QUEUE.items)
7574
unsafe_free_queue()
7675
end
7776
else
78-
lock(QUEUE_LOCK)
79-
push!(QUEUE, ptr)
80-
unlock(QUEUE_LOCK)
77+
lock(QUEUE.lock)
78+
push!(QUEUE.items, ptr)
79+
unlock(QUEUE.lock)
8180
end
8281
end
8382
nothing
@@ -91,13 +90,13 @@ function enqueue_all(ptrs)
9190
C.Py_DecRef(ptr)
9291
end
9392
end
94-
if !isempty(QUEUE)
93+
if !isempty(QUEUE.items)
9594
unsafe_free_queue()
9695
end
9796
else
98-
lock(QUEUE_LOCK)
99-
append!(QUEUE, ptrs)
100-
unlock(QUEUE_LOCK)
97+
lock(QUEUE.lock)
98+
append!(QUEUE.items, ptrs)
99+
unlock(QUEUE.lock)
101100
end
102101
end
103102
nothing
@@ -122,7 +121,7 @@ end
122121
function _gchook_finalizer(x)
123122
if C.CTX.is_initialized
124123
finalizer(_gchook_finalizer, x)
125-
if !isempty(QUEUE) && C.PyGILState_Check() == 1
124+
if !isempty(QUEUE.items) && C.PyGILState_Check() == 1
126125
unsafe_free_queue()
127126
end
128127
end

0 commit comments

Comments
 (0)