| 1 | /***************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | *****************************************************************************/ |
| 16 | #include <jni.h> |
| 17 | #include <Python.h> |
| 18 | #include "jpype.h" |
| 19 | #include "jp_classloader.h" |
| 20 | #include "jp_reference_queue.h" |
| 21 | #include "jp_gc.h" |
| 22 | #include "pyjp.h" |
| 23 | |
| 24 | static jobject s_ReferenceQueue = nullptr; |
| 25 | static jmethodID s_ReferenceQueueRegisterMethod = nullptr; |
| 26 | |
| 27 | extern "C" |
| 28 | { |
| 29 | |
| 30 | static void releasePython(void* host) |
| 31 | { |
| 32 | Py_XDECREF((PyObject*) host); |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * Class: org_jpype_ref_JPypeReferenceQueue |
| 37 | * Method: init |
| 38 | * Signature: (Ljava/lang/Object;Ljava/lang/reflect/Method;)V |
| 39 | */ |
| 40 | JNIEXPORT void JNICALL Java_org_jpype_ref_JPypeReferenceNative_init |
| 41 | (JNIEnv *env, jclass clazz, jobject refqueue, jobject registerID) |
| 42 | { |
| 43 | s_ReferenceQueue = env->NewGlobalRef(refqueue); |
| 44 | s_ReferenceQueueRegisterMethod = env->FromReflectedMethod(registerID); |
| 45 | } |
| 46 | |
| 47 | JNIEXPORT void JNICALL Java_org_jpype_ref_JPypeReferenceNative_removeHostReference |
| 48 | (JNIEnv *env, jclass, jlong host, jlong cleanup) |
| 49 | { |
| 50 | // Exceptions are not allowed here |
| 51 | try |
| 52 | { |
| 53 | JPJavaFrame frame = JPJavaFrame::external(env); |
| 54 | JPPyCallAcquire callback; |
| 55 | if (cleanup != 0) |
| 56 | { |
| 57 | auto func = (JCleanupHook) cleanup; |
| 58 | (*func)((void*) host); |
| 59 | } |
| 60 | } catch (...) // GCOVR_EXCL_LINE |
| 61 | { |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** Triggered whenever the sentinel is deleted |
| 66 | */ |
| 67 | JNIEXPORT void JNICALL Java_org_jpype_ref_JPypeReferenceNative_wake |
| 68 | (JNIEnv *env, jclass clazz) |
| 69 | { |
| 70 | // Exceptions are not allowed here |
| 71 | try |
| 72 | { |
| 73 | JPContext* context = JPContext_global; |
| 74 | context->m_GC->triggered(); |
| 75 | } catch (...) // GCOVR_EXCL_LINE |
| 76 | { |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | } |
| 81 | |
| 82 | void JPReferenceQueue::registerRef(JPJavaFrame &frame, jobject obj, PyObject* hostRef) |
| 83 | { |
| 84 | // There are certain calls such as exception handling in which the |
| 85 | // Python object is null. In those cases, we don't need to bind the Java |
| 86 | // object lifespan and can just ignore it. |
| 87 | if (hostRef == nullptr) |
| 88 | return; |
| 89 | |
| 90 | // MATCH TO DECREF IN releasePython |
| 91 | Py_INCREF(hostRef); |
| 92 | registerRef(frame, obj, hostRef, &releasePython); |
| 93 | } |
| 94 | |
| 95 | void JPReferenceQueue::registerRef(JPJavaFrame &frame, jobject obj, void* host, JCleanupHook func) |
| 96 | { |
| 97 | JP_TRACE_IN("JPReferenceQueue::registerRef"); |
| 98 | |
| 99 | // create the ref ... |
| 100 | jvalue args[3]; |
| 101 | args[0].l = obj; |
| 102 | args[1].j = (jlong) host; |
| 103 | args[2].j = (jlong) func; |
| 104 | |
| 105 | if (s_ReferenceQueue == nullptr) |
| 106 | JP_RAISE(PyExc_SystemError, "Memory queue not installed"); |
| 107 | JP_TRACE("Register reference"); |
| 108 | frame.CallVoidMethodA(s_ReferenceQueue, s_ReferenceQueueRegisterMethod, args); |
| 109 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 110 | } |