aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <sgothel@jausoft.com>2023-07-05 10:04:16 +0200
committerSven Gothel <sgothel@jausoft.com>2023-07-05 10:04:16 +0200
commitae4c2c3e59ed92caa6f0e18360b7236e50899bf6 (patch)
tree4f19c0991b4dd54debb36a5c1c57f71bca848575
parent0c4067379e5e12617b9a4530e607ca34762b54e3 (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!
-rw-r--r--doc/GlueGen_Mapping.html71
-rw-r--r--doc/GlueGen_Mapping.md29
-rw-r--r--src/java/com/jogamp/gluegen/CCodeUnit.java91
-rw-r--r--src/java/com/jogamp/gluegen/JavaCallbackEmitter.java5
-rw-r--r--src/java/com/jogamp/gluegen/JavaEmitter.java4
5 files changed, 132 insertions, 68 deletions
diff --git a/doc/GlueGen_Mapping.html b/doc/GlueGen_Mapping.html
index 674ef45..7bec555 100644
--- a/doc/GlueGen_Mapping.html
+++ b/doc/GlueGen_Mapping.html
@@ -2099,13 +2099,14 @@ key parameter of <code>SetCallbackFunction</code> as defined via
</ul>
<h4 id="required-libraryonload">Required <em>LibraryOnLoad</em></h4>
<p>Note that <a
-href="#libraryonload-librarybasename-for-jni_onload-"><code>LibraryOnLoad Bindingtest2</code></a>
-must be specified in exactly one native code-unit. It provides code to
-allow the generated native callback-function to attach the current
-thread to the <code>JavaVM*</code> generating a new
-<code>JNIEnv*</code>in daemon mode - or just to retrieve the thread's
-<code>JNIEnv*</code>, if already attached to the
-<code>JavaVM*</code>.</p>
+href="#libraryonload-librarybasename-for-jni_onload-"><code>LibraryOnLoad &lt;LibraryBasename&gt;</code></a>
+must be specified in exactly one native code-unit within one native
+library.</p>
+<p>It provides code to allow the generated native callback-function to
+attach the current thread to the <code>JavaVM*</code>, retrieving a
+valid <code>JNIEnv*</code>, see <a
+href="#libraryonload-librarybasename-for-jni_onload-"><code>LibraryOnLoad &lt;LibraryBasename&gt;</code></a>
+for details.</p>
<h3
id="javacallback-generated-interfaces-classes-and-methods"><em>JavaCallback</em>
Generated Interfaces, Classes and Methods</h3>
@@ -2188,7 +2189,7 @@ argument but with the same <a
href="#javacallback-key-definition"><em>key arguments</em> (see
<code>JavaCallbackKey</code>)</a> as previously called to set the
callback.</li>
-<li>Exactly one native code-unit for the library must specify <a
+<li>Exactly one native code-unit within the library must specify <a
href="#libraryonload-librarybasename-for-jni_onload-"><code>LibraryOnLoad libraryBasename</code></a></li>
<li><code>SetCallbackFunction</code>, all <em>maintenance</em> methods
and the native callback dispatcher <strong>are thread-safe</strong></li>
@@ -2232,9 +2233,9 @@ ArgumentIsString InjectMessageCallback01 1
JavaCallbackDef MessageCallback01 T2_CallbackFunc01 2</code></pre>
<p>Note that <a
href="#libraryonload-librarybasename-for-jni_onload-"><code>LibraryOnLoad Bindingtest2</code></a>
-must be specified in exactly one native code-unit. It provides code to
-allow the generated native callback-function to attach the current
-thread to the <code>JavaVM*</code> generating a new
+must be specified in exactly one native code-unit within the library. It
+provides code to allow the generated native callback-function to attach
+the current thread to the <code>JavaVM*</code> generating a new
<code>JNIEnv*</code>in daemon mode - or just to retrieve the thread's
<code>JNIEnv*</code>, if already attached to the
<code>JavaVM*</code>.</p>
@@ -2540,22 +2541,40 @@ users' <code>void*</code> pointer with the
<h3
id="libraryonload-librarybasename-for-jni_onload-"><code>LibraryOnLoad &lt;LibraryBasename&gt;</code>
for <code>JNI_OnLoad*(..)</code> ...</h3>
+<p><code>LibraryOnLoad &lt;LibraryBasename&gt;</code> <em>can</em> be
+specified in one native code-unit within one native library maximum,
+otherwise multiple function definitions would occur.</p>
+<p>In case <a href="#java-callback">Java™ callback methods are used</a>,
+it is required to have
+<code>LibraryOnLoad &lt;LibraryBasename&gt;</code> specified in exactly
+one native code-unit within one native library.</p>
<p><code>LibraryOnLoad &lt;LibraryBasename&gt;</code> generates native
-JNI code <code>JNI_OnLoad(..)</code> used for dynamic libraries,
-<code>JNI_OnLoad_&lt;LibraryBasename&gt;(..)</code> used for static
-libraries, <code>JVMUtil_GetJNIEnv(..)</code> and the instance of
-<code>JavaVM* &lt;LibraryBasename&gt;_jvmHandle</code>.</p>
-<p>The <code>JNI_OnLoad*(..)</code> methods set the
-<code>JavaVM* &lt;LibraryBasename&gt;_jvmHandle</code>, which in turn is
-utilized by <code>JVMUtil_GetJNIEnv(..)</code> to attach a new thread to
-the <code>JavaVM*</code> generating a new <code>JNIEnv*</code>in daemon
-mode - or just to retrieve the thread's <code>JNIEnv*</code>, if already
-attached to the <code>JavaVM*</code>.</p>
-<p>The <code>LibraryBasename</code> parameter is used to generate the
-<code>JNI_OnLoad_&lt;LibraryBasename&gt;(..)</code> variant for
-statically linked libraries. <code>JNI_OnLoad(..)</code>,
-<code>JNI_OnLoad_&lt;LibraryBasename&gt;(..)</code> and
-<code>JVMUtil_GetJNIEnv(..)</code></p>
+JNI code to handle the <code>JavaVM*</code> instance</p>
+<ul>
+<li><code>JavaVM* JVMUtil_GetJavaVM()</code> returning the static
+<code>JavaVM*</code> instance for <code>LibraryBasename</code> set by
+<code>JNI_OnLoad*()</code></li>
+<li><code>JNI_OnLoad(..)</code> setting the static <code>JavaVM*</code>
+instance for <code>LibraryBasename</code>, used for dynamic
+libraries,</li>
+<li><code>JNI_OnLoad_&lt;LibraryBasename&gt;(..)</code> setting the
+static <code>JavaVM*</code> instance for <code>LibraryBasename</code>,
+used for static libraries,</li>
+</ul>
+<p>Further the following functions are produced to attach and detach the
+current thread to and from the JVM, getting and releasing the
+<code>JNIEnv*</code></p>
+<ul>
+<li><code>JNIEnv* JVMUtil_GetJNIEnv(int asDaemon, int* jvmAttached)</code>
+returns the <code>JNIEnv*</code> with current thread being newly
+attached to the <code>JavaVM*</code> <strong>if</strong> result
+<code>*jvmAttached == true</code>, otherwise the current thread was
+already attached to the <code>JavaVM*</code></li>
+<li><code>void JVMUtil_ReleaseJNIEnv(JNIEnv* env, int detachJVM)</code>
+releases the <code>JNIEnv*</code>, i.e. detaching the current thread
+from the <code>JavaVM*</code> <strong>if</strong>
+<code>detachJVM == true</code>, otherwise funtion does nothing.</li>
+</ul>
<h2 id="platform-header-files">Platform Header Files</h2>
<p>GlueGen provides convenient platform headers,<br />
which can be included in your C header files for native compilation and
diff --git a/doc/GlueGen_Mapping.md b/doc/GlueGen_Mapping.md
index 68617a2..7a72733 100644
--- a/doc/GlueGen_Mapping.md
+++ b/doc/GlueGen_Mapping.md
@@ -862,9 +862,9 @@ The `SetCallback-KeyClass` shall implement the following hash-map-key standard m
- `SetCallback-KeyClassName(...)` constructor receiving all key parameter of `SetCallbackFunction` as defined via `JavaCallbackKey`, see above.
#### Required *LibraryOnLoad*
-Note that [`LibraryOnLoad Bindingtest2`](#libraryonload-librarybasename-for-jni_onload-) must be specified in exactly one native code-unit.
-It provides code to allow the generated native callback-function to attach the current 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*`.
+Note that [`LibraryOnLoad <LibraryBasename>`](#libraryonload-librarybasename-for-jni_onload-) must be specified in exactly one native code-unit within one native library.
+
+It provides code to allow the generated native callback-function to attach the current thread to the `JavaVM*`, retrieving a valid `JNIEnv*`, see [`LibraryOnLoad <LibraryBasename>`](#libraryonload-librarybasename-for-jni_onload-) for details.
### *JavaCallback* Generated Interfaces, Classes and Methods
@@ -903,7 +903,7 @@ Please consider the following *currently enabled* constraints using JavaCallback
using the same conversion function `CMethodBindingEmitter.emitBodyMapCToJNIType(..)`.
- To remove a JavaCallback the `SetCallbackFunction` must be called with `null` for the `CallbackFunction` argument
but with the same [*key arguments* (see `JavaCallbackKey`)](#javacallback-key-definition) as previously called to set the callback.
-- Exactly one native code-unit for the library must specify [`LibraryOnLoad libraryBasename`](#libraryonload-librarybasename-for-jni_onload-)
+- Exactly one native code-unit within the library must specify [`LibraryOnLoad libraryBasename`](#libraryonload-librarybasename-for-jni_onload-)
- `SetCallbackFunction`, all *maintenance* methods and the native callback dispatcher **are thread-safe**
- ...
@@ -946,7 +946,7 @@ ArgumentIsString InjectMessageCallback01 1
JavaCallbackDef MessageCallback01 T2_CallbackFunc01 2
```
-Note that [`LibraryOnLoad Bindingtest2`](#libraryonload-librarybasename-for-jni_onload-) must be specified in exactly one native code-unit.
+Note that [`LibraryOnLoad Bindingtest2`](#libraryonload-librarybasename-for-jni_onload-) must be specified in exactly one native code-unit within the library.
It provides code to allow the generated native callback-function to attach the current 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*`.
@@ -1267,16 +1267,19 @@ leading to the following interface
### `LibraryOnLoad <LibraryBasename>` for `JNI_OnLoad*(..)` ...
-`LibraryOnLoad <LibraryBasename>` generates 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`.
+`LibraryOnLoad <LibraryBasename>` *can* be specified in one native code-unit within one native library maximum, otherwise multiple function definitions would occur.
-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*`.
+In case [Java™ callback methods are used](#java-callback), it is required to have `LibraryOnLoad <LibraryBasename>` specified in exactly one native code-unit within one native library.
+
+`LibraryOnLoad <LibraryBasename>` generates native JNI code to handle the `JavaVM*` instance
+- `JavaVM* JVMUtil_GetJavaVM()` returning the static `JavaVM*` instance for `LibraryBasename` set by `JNI_OnLoad*()`
+- `JNI_OnLoad(..)` setting the static `JavaVM*` instance for `LibraryBasename`, used for dynamic libraries,
+- `JNI_OnLoad_<LibraryBasename>(..)` setting the static `JavaVM*` instance for `LibraryBasename`, used for static libraries,
+
+Further the following functions are produced to attach and detach the current thread to and from the JVM, getting and releasing the `JNIEnv*`
+- `JNIEnv* JVMUtil_GetJNIEnv(int asDaemon, int* jvmAttached)` returns the `JNIEnv*` with current thread being newly attached to the `JavaVM*` **if** result `*jvmAttached == true`, otherwise the current thread was already attached to the `JavaVM*`
+- `void JVMUtil_ReleaseJNIEnv(JNIEnv* env, int detachJVM)` releases the `JNIEnv*`, i.e. detaching the current thread from the `JavaVM*` **if** `detachJVM == true`, otherwise funtion does nothing.
-The `LibraryBasename` parameter is used to generate the `JNI_OnLoad_<LibraryBasename>(..)` variant for statically linked libraries.
-`JNI_OnLoad(..)`, `JNI_OnLoad_<LibraryBasename>(..)` and `JVMUtil_GetJNIEnv(..)`
## Platform Header Files
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(