From: Timo Korvola Date: Mon, 30 Jan 2017 09:13:48 +0000 (+0200) Subject: All variable setters now use GIL. X-Git-Tag: v1.31.0~6^2~2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F98%2F298%2F1;p=simantics%2Fpython.git All variable setters now use GIL. Most setters are now defined by a CPP macro. It is not pretty and may well be the ugliest way to do this except for the one it replaces. Change-Id: I27d36bfef6252f78ee801ebbc022d6fe75a3b49c --- diff --git a/org.simantics.pythonlink.win32.x86_64/jnipython.dll b/org.simantics.pythonlink.win32.x86_64/jnipython.dll index 37e4577..a605b4e 100644 Binary files a/org.simantics.pythonlink.win32.x86_64/jnipython.dll and b/org.simantics.pythonlink.win32.x86_64/jnipython.dll differ diff --git a/org.simantics.pythonlink.win32.x86_64/src/sclpy.c b/org.simantics.pythonlink.win32.x86_64/src/sclpy.c index 1cc36d8..e767192 100644 --- a/org.simantics.pythonlink.win32.x86_64/src/sclpy.c +++ b/org.simantics.pythonlink.win32.x86_64/src/sclpy.c @@ -785,97 +785,39 @@ jobject pythonDictionaryAsMap(JNIEnv *env, PyObject *dict) { return map; } -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonBooleanVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jboolean value) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - PyObject *val = getPythonBool(value); - - setPythonVariable(module, pythonName, val); -} - -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonBooleanArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jbooleanArray value) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - PyObject *val = getPythonBooleanList(env, value); - - setPythonVariable(module, pythonName, val); -} - -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonLongVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jlong value) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - PyObject *val = PyLong_FromLongLong(value); - - setPythonVariable(module, pythonName, val); -} - -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonIntegerArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jintArray value) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - PyObject *val = getPythonIntegerList(env, value); - - setPythonVariable(module, pythonName, val); -} - -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonLongArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jlongArray value) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - PyObject *val = getPythonLongList(env, value); - - setPythonVariable(module, pythonName, val); -} - -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonDoubleVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jdouble value) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - PyObject *val = PyFloat_FromDouble(value); - - setPythonVariable(module, pythonName, val); -} - -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonFloatArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jfloatArray value) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - PyObject *val = getPythonFloatList(env, value); - - setPythonVariable(module, pythonName, val); -} - -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonDoubleArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jdoubleArray value) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - PyObject *val = getPythonDoubleList(env, value); - - setPythonVariable(module, pythonName, val); -} - -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonStringVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jstring value) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - PyObject *val = getPythonString(env, value); - - setPythonVariable(module, pythonName, val); -} - -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonStringArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jobjectArray value) { - PyObject *module = (PyObject*)contextID; - - PyObject *pythonName = getPythonString(env, variableName); - PyObject *val = getPythonStringList(env, value); - - setPythonVariable(module, pythonName, val); -} +#define DEF_SETTER(typename, jtype, j2py) \ + JNIEXPORT void JNICALL \ + Java_org_simantics_pythonlink_PythonContext_setPython \ + ##typename##VariableImpl( \ + JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, \ + jtype value) { \ + PyObject *module = (PyObject*)contextID; \ + \ + PyEval_RestoreThread(main_ts); \ + setPythonVariable(module, getPythonString(env, variableName), \ + j2py(env, value)); \ + PyEval_SaveThread(); \ + } -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonNDArrayVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jobject value) { +#define getPythonBoolean(env, value) getPythonBool(value) +#define getPythonLong(env, value) PyLong_FromLongLong(value) +#define getPythonDouble(env, value) PyFloat_FromDouble(value) + +DEF_SETTER(Boolean, jboolean, getPythonBoolean) +DEF_SETTER(BooleanArray, jbooleanArray, getPythonBooleanList) +DEF_SETTER(Long, jlong, getPythonLong) +DEF_SETTER(IntegerArray, jintArray, getPythonIntegerList) +DEF_SETTER(LongArray, jlongArray, getPythonLongList) +DEF_SETTER(Double, jdouble, getPythonDouble) +DEF_SETTER(FloatArray, jfloatArray, getPythonFloatList) +DEF_SETTER(DoubleArray, jdoubleArray, getPythonDoubleList) +DEF_SETTER(String, jstring, getPythonString) +DEF_SETTER(StringArray, jobjectArray, getPythonStringList) + +JNIEXPORT void JNICALL +Java_org_simantics_pythonlink_PythonContext_setPythonNDArrayVariableImpl( + JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, + jobject value) { PyObject *module = (PyObject*)contextID; if (!hasNumpy) { @@ -883,24 +825,31 @@ JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonNDAr return; } + PyEval_RestoreThread(main_ts); { PyObject *pythonName = getPythonString(env, variableName); PyObject *val = getPythonNDArray(env, value); setPythonVariable(module, pythonName, val); } + PyEval_SaveThread(); } -JNIEXPORT void JNICALL Java_org_simantics_pythonlink_PythonContext_setPythonVariantVariableImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, jobject value, jobject binding) { +JNIEXPORT void JNICALL +Java_org_simantics_pythonlink_PythonContext_setPythonVariantVariableImpl( + JNIEnv *env, jobject thisObj, jlong contextID, jstring variableName, + jobject value, jobject binding) { PyObject *module = (PyObject*)contextID; - PyObject *pythonName = getPythonString(env, variableName); - PyObject *val = getPythonObject(env, value, binding); - - setPythonVariable(module, pythonName, val); + PyEval_RestoreThread(main_ts); + setPythonVariable(module, getPythonString(env, variableName), + getPythonObject(env, value, binding)); + PyEval_SaveThread(); } -JNIEXPORT jint JNICALL Java_org_simantics_pythonlink_PythonContext_executePythonStatementImpl(JNIEnv *env, jobject thisObj, jlong contextID, jstring statement) { +JNIEXPORT jint JNICALL +Java_org_simantics_pythonlink_PythonContext_executePythonStatementImpl( + JNIEnv *env, jobject thisObj, jlong contextID, jstring statement) { PyObject *module = (PyObject*)contextID; const char *utfchars = (*env)->GetStringUTFChars(env, statement, NULL);