Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cb334ca

Browse files
committedJan 8, 2024
Create ReentrantLock version of garbage collector
1 parent e54c4ee commit cb334ca

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed
 

‎src/PyCall.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,26 @@ mutable struct PyObject
7676
o::PyPtr # the actual PyObject*
7777
function PyObject(o::PyPtr)
7878
po = new(o)
79-
finalizer(pydecref, po)
79+
finalizer(_pydecref_locked, po)
8080
return po
8181
end
8282
end
8383

84+
const PYDECREF_LOCK = ReentrantLock()
85+
86+
function _pydecref_locked(po::PyObject)
87+
if !islocked(PYDECREF_LOCK)
88+
# If available, we lock and decref
89+
lock(PYDECREF_LOCK) do
90+
pydecref_(po)
91+
end
92+
else
93+
# Add back to queue to be decref'd later
94+
finalizer(_pydecref_locked, po)
95+
end
96+
return nothing
97+
end
98+
8499
PyPtr(o::PyObject) = getfield(o, :o)
85100

86101
"""

0 commit comments

Comments
 (0)