Skip to content

Commit bf2c3eb

Browse files
committed
Use test-and-test-set pattern
1 parent cb334ca commit bf2c3eb

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/PyCall.jl

+7-9
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,13 @@ end
8484
const PYDECREF_LOCK = ReentrantLock()
8585

8686
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
87+
# If available, we lock and decref
88+
if !islocked(PYDECREF_LOCK) &&
89+
trylock(() -> pydecref_(po), PYDECREF_LOCK) &&
90+
return nothing
91+
92+
# Add back to queue to be decref'd later
93+
finalizer(_pydecref_locked, po)
9694
return nothing
9795
end
9896

0 commit comments

Comments
 (0)