diff --git a/src/main/java/com/github/stephengold/joltjni/JobSystemThreadPool.java b/src/main/java/com/github/stephengold/joltjni/JobSystemThreadPool.java index d0acd335..d963089a 100644 --- a/src/main/java/com/github/stephengold/joltjni/JobSystemThreadPool.java +++ b/src/main/java/com/github/stephengold/joltjni/JobSystemThreadPool.java @@ -60,8 +60,26 @@ public JobSystemThreadPool(int maxJobs, int maxBarriers, int numThreads) { setVirtualAddress(systemVa, true); } // ************************************************************************* + // JobSystem methods + + /** + * Unassign the assigned native object, assuming there is one. Free the + * native object if the current system owns it. + */ + @Override + public void close() { + if (ownsNativeObject()) { + long systemVa = va(); + free(systemVa); + } + + unassignNativeObject(); + } + // ************************************************************************* // native private methods native private static long createJobSystem( int maxJobs, int maxBarriers, int numThreads); + + native private static void free(long systemVa); } diff --git a/src/main/native/glue/com_github_stephengold_joltjni_JobSystemThreadPool.cpp b/src/main/native/glue/com_github_stephengold_joltjni_JobSystemThreadPool.cpp index fa77a755..465a9128 100644 --- a/src/main/native/glue/com_github_stephengold_joltjni_JobSystemThreadPool.cpp +++ b/src/main/native/glue/com_github_stephengold_joltjni_JobSystemThreadPool.cpp @@ -40,3 +40,17 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_JobSystemThreadPool_ = new JobSystemThreadPool(maxJobs, maxBarriers, numThreads); return reinterpret_cast (pPool); } + +/* + * Class: com_github_stephengold_joltjni_JobSystemThreadPool + * Method: free + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_JobSystemThreadPool_free + (JNIEnv *, jclass, jlong systemVa) { +#ifndef WIN32 + // Attempting to delete a JobSystemThreadPool on Windows causes deadlock! + const JobSystem * const pSystem = reinterpret_cast (systemVa); + delete pSystem; +#endif +}