From: tkorvola Date: Wed, 1 Feb 2017 17:48:01 +0000 (+0200) Subject: Mind the threads in writeSCL. X-Git-Tag: v1.31.0~4 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=1a64cbed776f5011b685b7d32bc4c217f1ae4264;p=simantics%2Fpython.git Mind the threads in writeSCL. Both JNIEnv and PyThreadState are per thread. So check that we are in the main thread before messing with JNIEnv. Writes from other threads (possibly created in Python) are just dropped; those seem difficult to do right. Also unlock GIL while calling the Java method. Change-Id: I49a447e22ba4411800445f7149f5bc257a082e4f --- diff --git a/org.simantics.pythonlink.win32.x86_64/jnipython.dll b/org.simantics.pythonlink.win32.x86_64/jnipython.dll index e533946..ab792ab 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 686221e..428a45a 100644 --- a/org.simantics.pythonlink.win32.x86_64/src/sclpy.c +++ b/org.simantics.pythonlink.win32.x86_64/src/sclpy.c @@ -53,19 +53,26 @@ writeToSCL(PyObject *self, PyObject *args) Py_UNICODE *what; Py_ssize_t length; if (!PyArg_ParseTuple(args, "u#", &what, &length)) - return Py_BuildValue(""); + Py_RETURN_NONE; { - jclass writerClass = (*env)->FindClass(env, WRITER_CLASS); - jmethodID writeMethod = (*env)->GetMethodID(env, writerClass, "write", "([CII)V"); - jcharArray chars = (*env)->NewCharArray(env, (jsize)length); - - (*env)->SetCharArrayRegion(env, chars, 0, length, what); - (*env)->CallVoidMethod(env, sclWriter, writeMethod, chars, 0, length); + PyThreadState *my_ts = PyThreadState_Get(); + if (my_ts == main_ts) { + jclass writerClass = (*env)->FindClass(env, WRITER_CLASS); + jmethodID writeMethod = (*env)->GetMethodID(env, writerClass, "write", "([CII)V"); + jcharArray chars = (*env)->NewCharArray(env, (jsize)length); + + (*env)->SetCharArrayRegion(env, chars, 0, length, what); + Py_BEGIN_ALLOW_THREADS + (*env)->CallVoidMethod(env, sclWriter, writeMethod, chars, 0, length); + Py_END_ALLOW_THREADS + } else { + //TODO + } } } - return Py_BuildValue(""); + Py_RETURN_NONE; } static PyMethodDef sclWriterMethods[] = {