Skip to content

Commit

Permalink
Create ReentrantLock version of garbage collector
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Jan 8, 2024
1 parent e54c4ee commit cb334ca
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/PyCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,26 @@ mutable struct PyObject
o::PyPtr # the actual PyObject*
function PyObject(o::PyPtr)
po = new(o)
finalizer(pydecref, po)
finalizer(_pydecref_locked, po)
return po
end
end

const PYDECREF_LOCK = ReentrantLock()

function _pydecref_locked(po::PyObject)
if !islocked(PYDECREF_LOCK)
# If available, we lock and decref
lock(PYDECREF_LOCK) do
pydecref_(po)
end
else
# Add back to queue to be decref'd later
finalizer(_pydecref_locked, po)
end
return nothing
end

PyPtr(o::PyObject) = getfield(o, :o)

"""
Expand Down

0 comments on commit cb334ca

Please sign in to comment.