Skip to content

Commit

Permalink
revert to using a vector for the queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Doris committed Jul 31, 2024
1 parent e230ce9 commit a36d7c0
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/GC/GC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module GC

using ..C: C

const QUEUE = Channel{C.PyPtr}(Inf)
const QUEUE = C.PyPtr[]
const QUEUE_LOCK = Threads.SpinLock()
const HOOK = WeakRef()

"""
Expand Down Expand Up @@ -55,22 +56,28 @@ function gc()
end

function unsafe_free_queue()
while isready(QUEUE)
ptr = take!(QUEUE)
lock(QUEUE_LOCK)
for ptr in QUEUE
if ptr != C.PyNULL
C.Py_DecRef(ptr)
end
end
empty!(QUEUE)
unlock(QUEUE_LOCK)
nothing
end

function enqueue(ptr::C.PyPtr)
if ptr != C.PyNULL && C.CTX.is_initialized
if C.PyGILState_Check() == 1
C.Py_DecRef(ptr)
unsafe_free_queue()
if !isempty(QUEUE)
unsafe_free_queue()
end
else
put!(QUEUE, ptr)
lock(QUEUE_LOCK)
push!(QUEUE, ptr)
unlock(QUEUE_LOCK)
end
end
nothing
Expand All @@ -84,11 +91,13 @@ function enqueue_all(ptrs)
C.Py_DecRef(ptr)
end
end
unsafe_free_queue()
else
for ptr in ptrs
put!(QUEUE, ptr)
if !isempty(QUEUE)
unsafe_free_queue()
end
else
lock(QUEUE_LOCK)
append!(QUEUE, ptrs)
unlock(QUEUE_LOCK)
end
end
nothing
Expand All @@ -113,7 +122,7 @@ end
function _gchook_finalizer(x)
if C.CTX.is_initialized
finalizer(_gchook_finalizer, x)
if isready(QUEUE) && C.PyGILState_Check() == 1
if !isempty(QUEUE) && C.PyGILState_Check() == 1
unsafe_free_queue()
end
end
Expand Down

0 comments on commit a36d7c0

Please sign in to comment.