diff options
author | Sven Gothel <[email protected]> | 2023-07-05 10:04:16 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-07-05 10:04:16 +0200 |
commit | ae4c2c3e59ed92caa6f0e18360b7236e50899bf6 (patch) | |
tree | 4f19c0991b4dd54debb36a5c1c57f71bca848575 /src/java/com/jogamp | |
parent | 0c4067379e5e12617b9a4530e607ca34762b54e3 (diff) |
GlueGen JavaCallback/LibraryOnLoad: Always include the `libraryBasename` agnostic 'emitJNIEnvDecl()' (declaration) in JNI code; Detach the thread from the JVM if newly attach in callback!
Diffstat (limited to 'src/java/com/jogamp')
-rw-r--r-- | src/java/com/jogamp/gluegen/CCodeUnit.java | 91 | ||||
-rw-r--r-- | src/java/com/jogamp/gluegen/JavaCallbackEmitter.java | 5 | ||||
-rw-r--r-- | src/java/com/jogamp/gluegen/JavaEmitter.java | 4 |
3 files changed, 71 insertions, 29 deletions
diff --git a/src/java/com/jogamp/gluegen/CCodeUnit.java b/src/java/com/jogamp/gluegen/CCodeUnit.java index 5c0db27..6659305 100644 --- a/src/java/com/jogamp/gluegen/CCodeUnit.java +++ b/src/java/com/jogamp/gluegen/CCodeUnit.java @@ -50,16 +50,15 @@ public class CCodeUnit extends CodeUnit { CodeGenUtils.emitAutogeneratedWarning(output, generator, "C-Unit: "+cUnitName+", "+filename); } - public void emitHeader(final String libraryBasename, final String packageName, final String className, final List<String> customCode) { + public void emitHeader(final String packageName, final String className, final List<String> customCode) { emitln("#include <jni.h>"); emitln("#include <stdlib.h>"); emitln("#include <string.h>"); emitln("#include <assert.h>"); emitln("#include <stddef.h>"); emitln(); - if( null != libraryBasename && libraryBasename.length() > 0 ) { - emitJNIOnLoadJNIEnvDecl(libraryBasename); - } + emitJNIEnvDecl(); + emitln(); emitln("static jobject JVMUtil_NewDirectByteBufferCopy(JNIEnv *env, jclass clazzBuffers, void * source_address, size_t capacity); /* forward decl. */"); emitln(); @@ -74,9 +73,9 @@ public class CCodeUnit extends CodeUnit { } } - /** Emits {@link #getJNIOnLoadJNIEnvDecl(String)}. */ - public void emitJNIOnLoadJNIEnvDecl(final String libraryBasename) { - emitln( getJNIOnLoadJNIEnvDecl(libraryBasename) ); + /** Emits {@link #getJNIEnvDecl()}. */ + public void emitJNIEnvDecl() { + emitln( getJNIEnvDecl() ); } /** Emits {@link #getJNIOnLoadJNIEnvCode(String)}. */ @@ -109,28 +108,26 @@ public class CCodeUnit extends CodeUnit { "}\n"; /** - * Returns native JNI declarations for `JavaVM* {libraryBasename}_jvmHandle` - * and `JVMUtil_GetJNIEnv(..)`. + * Returns native JNI declarations for `JVMUtil_GetJavaVM()`, `JVMUtil_GetJNIEnv(..)` and `JVMUtil_ReleaseJNIEnv(..)`. * <p> * See {@link #getJNIOnLoadJNIEnvCode(String)} for details. * </p> - * @param libraryBasename library basename to generate the `JNI_OnLoad_{libraryBasename}(..)` variant for statically linked libraries. * @return the code * @see #getJNIOnLoadJNIEnvCode(String) */ - public static final String getJNIOnLoadJNIEnvDecl(final String libraryBasename) { - return "extern JavaVM *"+libraryBasename+"_jvmHandle;\n"+ - "JNIEnv* JVMUtil_GetJNIEnv();\n"+ - "\n"; + public static final String getJNIEnvDecl() { + return "JavaVM* JVMUtil_GetJavaVM();\n"+ + "JNIEnv* JVMUtil_GetJNIEnv(int asDaemon, int* jvmAttached);\n"+ + "void JVMUtil_ReleaseJNIEnv(JNIEnv* env, int detachJVM);\n"; } /** * Returns native JNI code `JNI_OnLoad(..)` used for dynamic libraries, * `JNI_OnLoad_{libraryBasename}(..)` used for static libraries, - * `JVMUtil_GetJNIEnv(..)` and the instance of `JavaVM* {libraryBasename}_jvmHandle`. + * `JVMUtil_GetJNIEnv(..)` etc. * <p> - * The `JNI_OnLoad*(..)` methods set the `JavaVM* {libraryBasename}_jvmHandle`, + * The `JNI_OnLoad*(..)` methods set a `static JavaVM* {libraryBasename}_jvmHandle`, * which in turn is utilized by `JVMUtil_GetJNIEnv(..)` - * to attach a new thread to the `JavaVM*` generating a new `JNIEnv*`in daemon mode - + * to attach a new thread to the `JavaVM*` generating a new `JNIEnv*`- * or just to retrieve the thread's `JNIEnv*`, if already attached to the `JavaVM*`. * </p> * @param libraryBasename library basename to generate the `JNI_OnLoad_{libraryBasename}(..)` variant for statically linked libraries. @@ -140,39 +137,56 @@ public class CCodeUnit extends CodeUnit { public static final String getJNIOnLoadJNIEnvCode(final String libraryBasename) { final String jvmHandleName = libraryBasename+"_jvmHandle"; final StringBuilder sb = new StringBuilder(); - sb.append("JavaVM *").append(jvmHandleName).append(" = NULL;\n"); + sb.append("static JavaVM *").append(jvmHandleName).append(" = NULL;\n"); + sb.append("\n"); + sb.append("JavaVM* JVMUtil_GetJavaVM() {\n"); + sb.append(" return ").append(jvmHandleName).append(";\n"); + sb.append("}\n"); sb.append("JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *initVM, void *reserved) {\n"); sb.append(" (void)reserved;\n"); sb.append(" ").append(jvmHandleName).append(" = initVM;\n"); - sb.append(" fprintf(stderr, \"ON_LOAD_0\\n\");\n"); + if( GlueGen.debug() ) { + sb.append(" fprintf(stderr, \"ON_LOAD_0 lib '").append(libraryBasename).append("'\\n\");\n"); + } sb.append(" return JNI_VERSION_1_8;\n"); sb.append("}\n"); sb.append("JNIEXPORT jint JNICALL JNI_OnLoad_").append(libraryBasename).append("(JavaVM *initVM, void *reserved) {\n"); sb.append(" (void)reserved;\n"); sb.append(" ").append(jvmHandleName).append(" = initVM;\n"); - sb.append(" fprintf(stderr, \"ON_LOAD_1\\n\");\n"); + if( GlueGen.debug() ) { + sb.append(" fprintf(stderr, \"ON_LOAD_1 lib '").append(libraryBasename).append("'\\n\");\n"); + } sb.append(" return JNI_VERSION_1_8;\n"); sb.append("}\n"); sb.append("\n"); sb.append("JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {\n"); sb.append(" (void)vm;\n"); sb.append(" (void)reserved;\n"); - sb.append(" fprintf(stderr, \"ON_UNLOAD_0\\n\");\n"); + sb.append(" ").append(jvmHandleName).append(" = NULL;\n"); + if( GlueGen.debug() ) { + sb.append(" fprintf(stderr, \"ON_UNLOAD_0 lib '").append(libraryBasename).append("'\\n\");\n"); + } sb.append("}\n"); sb.append("\n"); sb.append("JNIEXPORT void JNICALL JNI_OnUnload_").append(libraryBasename).append("(JavaVM *vm, void *reserved) {\n"); sb.append(" (void)vm;\n"); sb.append(" (void)reserved;\n"); - sb.append(" fprintf(stderr, \"ON_UNLOAD_1\\n\");\n"); + sb.append(" ").append(jvmHandleName).append(" = NULL;\n"); + if( GlueGen.debug() ) { + sb.append(" fprintf(stderr, \"ON_UNLOAD_1 lib '").append(libraryBasename).append("'\\n\");\n"); + } sb.append("}\n"); sb.append("\n"); - sb.append("JNIEnv* JVMUtil_GetJNIEnv() {\n"); + sb.append("JNIEnv* JVMUtil_GetJNIEnv(int asDaemon, int* jvmAttached) {\n"); sb.append(" JNIEnv* curEnv = NULL;\n"); sb.append(" JNIEnv* newEnv = NULL;\n"); sb.append(" int envRes;\n"); sb.append("\n"); + sb.append(" if( NULL != jvmAttached ) {\n"); + sb.append(" *jvmAttached = 0;\n"); + sb.append(" }\n"); sb.append(" if(NULL==").append(jvmHandleName).append(") {\n"); - sb.append(" fprintf(stderr, \"JVMUtil_GetJNIEnv(").append(libraryBasename).append("): No JavaVM handle registered.\");\n"); + sb.append(" fprintf(stderr, \"JVMUtil_GetJNIEnv(").append(libraryBasename).append("): No JavaVM handle registered.\\n\");\n"); sb.append(" return NULL;\n"); sb.append(" }\n"); sb.append("\n"); @@ -180,7 +194,11 @@ public class CCodeUnit extends CodeUnit { sb.append(" envRes = (*").append(jvmHandleName).append(")->GetEnv(").append(jvmHandleName).append(", (void **) &curEnv, JNI_VERSION_1_8) ;\n"); sb.append(" if( JNI_EDETACHED == envRes ) {\n"); sb.append(" // detached thread - attach to JVM as daemon, w/o need to be detached!\n"); - sb.append(" envRes = (*").append(jvmHandleName).append(")->AttachCurrentThreadAsDaemon(").append(jvmHandleName).append(", (void**) &newEnv, NULL);\n"); + sb.append(" if( asDaemon ) {\n"); + sb.append(" envRes = (*").append(jvmHandleName).append(")->AttachCurrentThreadAsDaemon(").append(jvmHandleName).append(", (void**) &newEnv, NULL);\n"); + sb.append(" } else {\n"); + sb.append(" envRes = (*").append(jvmHandleName).append(")->AttachCurrentThread(").append(jvmHandleName).append(", (void**) &newEnv, NULL);\n"); + sb.append(" }\n"); sb.append(" if( JNI_OK != envRes ) {\n"); sb.append(" fprintf(stderr, \"JVMUtil_GetJNIEnv(").append(libraryBasename).append("): Can't attach thread: %d\\n\", envRes);\n"); sb.append(" return NULL;\n"); @@ -195,9 +213,32 @@ public class CCodeUnit extends CodeUnit { sb.append(" fprintf(stderr, \"JVMUtil_GetJNIEnv(").append(libraryBasename).append("): env is NULL\\n\");\n"); sb.append(" return NULL;\n"); sb.append(" }\n"); + sb.append(" if( NULL != jvmAttached ) {\n"); + sb.append(" *jvmAttached = NULL != newEnv;\n"); + sb.append(" }\n"); + if( GlueGen.debug() ) { + sb.append(" fprintf(stderr, \"JVMUtil_GetJNIEnv(").append(libraryBasename).append(", asDaemon %d): jvmAttached %d -> env %p\\n.\", asDaemon, (NULL != newEnv), curEnv);\n"); + } sb.append(" return curEnv;\n"); sb.append("}\n"); sb.append("\n"); + sb.append("void JVMUtil_ReleaseJNIEnv(JNIEnv* env, int detachJVM) {\n"); + sb.append(" if(NULL==").append(jvmHandleName).append(") {\n"); + sb.append(" fprintf(stderr, \"JVMUtil_ReleaseJNIEnv(").append(libraryBasename).append("): No JavaVM handle registered.\\n\");\n"); + sb.append(" return;\n"); + sb.append(" }\n"); + sb.append(" if( detachJVM ) {\n"); + sb.append(" jint res = (*").append(jvmHandleName).append(")->DetachCurrentThread(").append(jvmHandleName).append(") ;\n"); + sb.append(" if( 0 != res ) {\n"); + sb.append(" fprintf(stderr, \"JVMUtil_ReleaseJNIEnv(").append(libraryBasename).append(", env %p): Failed with res %d\\n.\", env, res);\n"); + sb.append(" return;\n"); + sb.append(" }\n"); + if( GlueGen.debug() ) { + sb.append(" fprintf(stderr, \"JVMUtil_ReleaseJNIEnv(").append(libraryBasename).append(", env %p) -> res %d\\n.\", env, res);\n"); + } + sb.append(" }\n"); + sb.append("}\n"); + sb.append("\n"); return sb.toString(); } } diff --git a/src/java/com/jogamp/gluegen/JavaCallbackEmitter.java b/src/java/com/jogamp/gluegen/JavaCallbackEmitter.java index 96e1e77..b152801 100644 --- a/src/java/com/jogamp/gluegen/JavaCallbackEmitter.java +++ b/src/java/com/jogamp/gluegen/JavaCallbackEmitter.java @@ -578,7 +578,8 @@ public final class JavaCallbackEmitter { unit.emitln(") {"); // javaCallback.cbFuncCEmitter.emitBody(); { - unit.emitln(" JNIEnv* env = JVMUtil_GetJNIEnv();"); + unit.emitln(" int detachJVM = 0;"); + unit.emitln(" JNIEnv* env = JVMUtil_GetJNIEnv(1 /* daemon */, &detachJVM);"); unit.emitln(" jclass cbClazz = "+staticBindingClazzVarName+";"); unit.emitln(" jmethodID cbMethod = "+staticBindingMethodIDVarName+";"); unit.emitln(" if( NULL == env || NULL == cbClazz || NULL == cbMethod ) {"); @@ -623,7 +624,7 @@ public final class JavaCallbackEmitter { // javaCallback.cbFuncCEmitter.emitBodyUserVariableAssignments(); // javaCallback.cbFuncCEmitter.emitBodyVariablePostCallCleanup(); // javaCallback.cbFuncCEmitter.emitBodyMapCToJNIType(-1 /* return value */, true /* addLocalVar */) - + unit.emitln(" JVMUtil_ReleaseJNIEnv(env, detachJVM);"); unit.emitln(" "+returnStatement); } unit.emitln("}"); diff --git a/src/java/com/jogamp/gluegen/JavaEmitter.java b/src/java/com/jogamp/gluegen/JavaEmitter.java index 4a81a01..96202bf 100644 --- a/src/java/com/jogamp/gluegen/JavaEmitter.java +++ b/src/java/com/jogamp/gluegen/JavaEmitter.java @@ -910,7 +910,7 @@ public class JavaEmitter implements GlueEmitter { final String fname = nRoot + File.separator + cUnitName; jniUnit = openCUnit(fname, cUnitName); // jniUnit.emitHeader(structClassPkgName, containingJTypeName, Collections.emptyList()); - jniUnit.emitHeader(null, structClassPkgName, containingJTypeName, cfg.customCCode()); + jniUnit.emitHeader(structClassPkgName, containingJTypeName, cfg.customCCode()); } else { jniUnit = null; } @@ -3018,7 +3018,7 @@ public class JavaEmitter implements GlueEmitter { if( !cfg.getJavaCallbackList().isEmpty() && null == cfg.libraryOnLoadName() ) { LOG.log(WARNING, "JavaCallback used, but no 'LibraryOnLoad' basename specified for JNI_OnLoad(..). Exactly one native code-unit for the library must specify 'LibraryOnLoad' basename"); } - cUnit().emitHeader(cfg.libraryOnLoadName(), getImplPackageName(), cfg.implClassName(), cfg.customCCode()); + cUnit().emitHeader(getImplPackageName(), cfg.implClassName(), cfg.customCCode()); } } catch (final Exception e) { throw new RuntimeException( |