Skip to content

Thread-safe garbage collection v3 (ReentrantLock version) #1074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/pywrapfn.jl
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ See check_pyargsptr(nargs::Int) above
"""
function check_pyargsptr(pf::PyWrapFn{N, RT}) where {N, RT}
if unsafe_load(pf.pyargsptr).ob_refcnt > 1
pydecref_(pf.pyargsptr)
pydecref_unsafe_(pf.pyargsptr)
pf.pyargsptr =
@pycheckn ccall((@pysym :PyTuple_New), PyPtr, (Int,), nargs)
end
19 changes: 16 additions & 3 deletions src/PyCall.jl
Original file line number Diff line number Diff line change
@@ -76,11 +76,24 @@ mutable struct PyObject
o::PyPtr # the actual PyObject*
function PyObject(o::PyPtr)
po = new(o)
finalizer(pydecref, po)
finalizer(pydecref_safe_, po)
return po
end
end

const PYDECREF_PYOBJECT_LOCK = ReentrantLock()

function pydecref_safe_(po::PyObject)
# If available, we lock and decref
!islocked(PYDECREF_PYOBJECT_LOCK) &&
trylock(() -> (pydecref_unsafe_(po); true), PYDECREF_PYOBJECT_LOCK) &&
return nothing

# Add back to queue to be decref'd later
finalizer(pydecref_safe_, po)
return nothing
end

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

"""
@@ -114,13 +127,13 @@ it is equivalent to a `PyNULL()` object.
"""
ispynull(o::PyObject) = o ≛ PyPtr_NULL

function pydecref_(o::Union{PyPtr,PyObject})
function pydecref_unsafe_(o::Union{PyPtr,PyObject})
_finalized[] || ccall(@pysym(:Py_DecRef), Cvoid, (PyPtr,), o)
return o
end

function pydecref(o::PyObject)
pydecref_(o)
pydecref_unsafe_(o)
setfield!(o, :o, PyPtr_NULL)
return o
end
15 changes: 14 additions & 1 deletion src/pybuffer.jl
Original file line number Diff line number Diff line change
@@ -32,11 +32,24 @@ mutable struct PyBuffer
b = new(Py_buffer(C_NULL, PyPtr_NULL, 0, 0,
0, 0, C_NULL, C_NULL, C_NULL, C_NULL,
C_NULL, C_NULL, C_NULL))
finalizer(pydecref, b)
finalizer(pydecref_safe_, b)
return b
end
end

const PYDECREF_PYBUFFER_LOCK = ReentrantLock()

function pydecref_safe_(b::PyBuffer)
# If available, we lock and decref
!islocked(PYDECREF_PYBUFFER_LOCK) &&
trylock(() -> (pydecref(b); true), PYDECREF_PYBUFFER_LOCK) &&
return nothing

# Add back to queue to be decref'd later
finalizer(pydecref_safe_, b)
return nothing
end

"""
`pydecref(o::PyBuffer)`
Release the reference to buffer `o`
4 changes: 2 additions & 2 deletions src/pyfncall.jl
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ function _pycall!(ret::PyObject, o::Union{PyObject,PyPtr}, args, nargs::Int=leng
end
return __pycall!(ret, pyargsptr, o, kw) #::PyObject
finally
pydecref_(pyargsptr)
pydecref_unsafe_(pyargsptr)
end
end

@@ -42,7 +42,7 @@ function __pycall!(ret::PyObject, pyargsptr::PyPtr, o::Union{PyObject,PyPtr},
disable_sigint() do
retptr = @pycheckn ccall((@pysym :PyObject_Call), PyPtr, (PyPtr,PyPtr,PyPtr), o,
pyargsptr, kw)
pydecref_(ret)
pydecref_unsafe_(ret)
setfield!(ret, :o, retptr)
end
return ret #::PyObject
2 changes: 1 addition & 1 deletion src/pyinit.jl
Original file line number Diff line number Diff line change
@@ -110,7 +110,7 @@ end
#########################################################################

const _finalized = Ref(false)
# This flag is set via `Py_AtExit` to avoid calling `pydecref_` after
# This flag is set via `Py_AtExit` to avoid calling `pydecref_unsafe_` after
# Python is finalized.

function _set_finalized()