diff --git a/src/main/java/org/jpy/PyObject.java b/src/main/java/org/jpy/PyObject.java index e0715ec..44f0740 100644 --- a/src/main/java/org/jpy/PyObject.java +++ b/src/main/java/org/jpy/PyObject.java @@ -58,7 +58,7 @@ private static void startCleanupThread() { } public static int cleanup() { - return REFERENCES.asProxy().cleanupOnlyUseFromGIL(); + return REFERENCES.asProxy().threadSafeCleanup(); } private final PyObjectState state; @@ -71,8 +71,8 @@ public static int cleanup() { PyObject(long pointer, boolean fromJNI) { state = new PyObjectState(pointer); if (fromJNI) { - if (CLEANUP_ON_INIT && PyLib.hasGil()) { - REFERENCES.cleanupOnlyUseFromGIL(); // only performs *one* cleanup + if (CLEANUP_ON_INIT) { + REFERENCES.threadSafeCleanup(); // only performs *one* cleanup } if (CLEANUP_ON_THREAD) { // ensures that we've only started after python has been started, and we know there is something to cleanup diff --git a/src/main/java/org/jpy/PyObjectCleanup.java b/src/main/java/org/jpy/PyObjectCleanup.java index cda8455..7285e3a 100644 --- a/src/main/java/org/jpy/PyObjectCleanup.java +++ b/src/main/java/org/jpy/PyObjectCleanup.java @@ -1,5 +1,5 @@ package org.jpy; interface PyObjectCleanup { - int cleanupOnlyUseFromGIL(); + int threadSafeCleanup(); } diff --git a/src/main/java/org/jpy/PyObjectReferences.java b/src/main/java/org/jpy/PyObjectReferences.java index 9eec190..4afea8a 100644 --- a/src/main/java/org/jpy/PyObjectReferences.java +++ b/src/main/java/org/jpy/PyObjectReferences.java @@ -76,11 +76,11 @@ private Reference asRef(PyObject pyObject) { /** * This should *only* be invoked through the proxy, or when we *know* we have the GIL. */ - public int cleanupOnlyUseFromGIL() { - return cleanupOnlyUseFromGIL(buffer); + public int threadSafeCleanup() { + return threadSafeCleanup(buffer); } - private int cleanupOnlyUseFromGIL(long[] buffer) { + private int threadSafeCleanup(long[] buffer) { return PyLib.ensureGil(() -> { int index = 0; while (index < buffer.length) { @@ -164,7 +164,7 @@ def cleanup(references): // any fairness guarantees. As such, we need to be mindful of other python users/code, // and ensure we don't overly acquire the GIL causing starvation issues, especially when // there is no cleanup work to do. - final int size = proxy.cleanupOnlyUseFromGIL(); + final int size = proxy.threadSafeCleanup(); // Although, it *does* make sense to potentially take the GIL in a tight loop when there