diff options
Diffstat (limited to 'src/java/com/jogamp/gluegen/CCodeUnit.java')
-rw-r--r-- | src/java/com/jogamp/gluegen/CCodeUnit.java | 109 |
1 files changed, 108 insertions, 1 deletions
diff --git a/src/java/com/jogamp/gluegen/CCodeUnit.java b/src/java/com/jogamp/gluegen/CCodeUnit.java index df5c6e7..5c0db27 100644 --- a/src/java/com/jogamp/gluegen/CCodeUnit.java +++ b/src/java/com/jogamp/gluegen/CCodeUnit.java @@ -50,13 +50,16 @@ public class CCodeUnit extends CodeUnit { CodeGenUtils.emitAutogeneratedWarning(output, generator, "C-Unit: "+cUnitName+", "+filename); } - public void emitHeader(final String packageName, final String className, final List<String> customCode) { + public void emitHeader(final String libraryBasename, 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); + } emitln("static jobject JVMUtil_NewDirectByteBufferCopy(JNIEnv *env, jclass clazzBuffers, void * source_address, size_t capacity); /* forward decl. */"); emitln(); @@ -71,6 +74,16 @@ public class CCodeUnit extends CodeUnit { } } + /** Emits {@link #getJNIOnLoadJNIEnvDecl(String)}. */ + public void emitJNIOnLoadJNIEnvDecl(final String libraryBasename) { + emitln( getJNIOnLoadJNIEnvDecl(libraryBasename) ); + } + + /** Emits {@link #getJNIOnLoadJNIEnvCode(String)}. */ + public void emitJNIOnLoadJNIEnvCode(final String libraryBasename) { + emitln( getJNIOnLoadJNIEnvCode(libraryBasename) ); + } + @Override public String toString() { return "CCodeUnit[unit "+cUnitName+", file "+filename+"]"; } @@ -94,4 +107,98 @@ public class CCodeUnit extends CodeUnit { " }\n"+ " return jbyteBuffer;\n"+ "}\n"; + + /** + * Returns native JNI declarations for `JavaVM* {libraryBasename}_jvmHandle` + * and `JVMUtil_GetJNIEnv(..)`. + * <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"; + } + /** + * 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`. + * <p> + * The `JNI_OnLoad*(..)` methods set the `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 - + * 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. + * @return the code + * @see #getJNIOnLoadJNIEnvDecl(String) + */ + 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("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"); + 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"); + 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("}\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("}\n"); + sb.append("\n"); + sb.append("JNIEnv* JVMUtil_GetJNIEnv() {\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==").append(jvmHandleName).append(") {\n"); + sb.append(" fprintf(stderr, \"JVMUtil_GetJNIEnv(").append(libraryBasename).append("): No JavaVM handle registered.\");\n"); + sb.append(" return NULL;\n"); + sb.append(" }\n"); + sb.append("\n"); + sb.append(" // retrieve this thread's JNIEnv curEnv - or detect it's detached\n"); + 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( 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"); + sb.append(" }\n"); + sb.append(" curEnv = newEnv;\n"); + sb.append(" } else if( JNI_OK != envRes ) {\n"); + sb.append(" // oops ..\n"); + sb.append(" fprintf(stderr, \"JVMUtil_GetJNIEnv(").append(libraryBasename).append("): Can't GetEnv: %d\\n\", envRes);\n"); + sb.append(" return NULL;\n"); + sb.append(" }\n"); + sb.append(" if (curEnv==NULL) {\n"); + sb.append(" fprintf(stderr, \"JVMUtil_GetJNIEnv(").append(libraryBasename).append("): env is NULL\\n\");\n"); + sb.append(" return NULL;\n"); + sb.append(" }\n"); + sb.append(" return curEnv;\n"); + sb.append("}\n"); + sb.append("\n"); + return sb.toString(); + } } + |