summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qjnienvironment.cpp
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2023-10-25 17:47:02 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2023-10-26 20:04:51 +0200
commit84e70976f3d3df8ac921877d32c1dd884fd64267 (patch)
treefa2e3afa4785a6c67ba8ca8a6d2715212464696d /src/corelib/kernel/qjnienvironment.cpp
parentf419a8d67f2487aa07496539a49c0aca7eab1c27 (diff)
JNI: Tolerate threading mismatches and improve diagnostics
Amend 944200b5a9705a7617f82cdaf5caf8932380aba4. We have code paths in Qt that result in a global QJniObject being called from multiple threads (QtActivity instance, possibly the QAndroidSystemLocale), so we need to tolerate that until we understand better under which circumstances this should be allowed, and what tools we can use to control this better. Don't instantiate QJniEnvironment to check the JNIEnv, but use raw JNI calls so that we don't implicitly attach a thread to the JVM when checking. Use categorized logging to emit log output if we have an environment/threading mismatch, including the name of the class we are trying to call, and fall-back to the thread-local JNI environment pointer instead of the one stored in the QJniObject's private. Also, give the threads that we attach to the JVM the name of the QThread instances if we can, resulting in better logcat output on the device. This still results in less overhead than the code had prior to 944200b5a9705a7617f82cdaf5caf8932380aba4, as we don't instantiate multiple temporary QJniEnvironment objects on the stack for each call. Change-Id: I98b92edd29162dccfc5c34e6261d4de219b59531 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Diffstat (limited to 'src/corelib/kernel/qjnienvironment.cpp')
-rw-r--r--src/corelib/kernel/qjnienvironment.cpp8
1 files changed, 5 insertions, 3 deletions
diff --git a/src/corelib/kernel/qjnienvironment.cpp b/src/corelib/kernel/qjnienvironment.cpp
index 97c5584ea2..8d4e386a68 100644
--- a/src/corelib/kernel/qjnienvironment.cpp
+++ b/src/corelib/kernel/qjnienvironment.cpp
@@ -29,8 +29,6 @@ QT_BEGIN_NAMESPACE
It has not been tested for other platforms.
*/
-static const char qJniThreadName[] = "QtThread";
-
class QJniEnvironmentPrivate
{
public:
@@ -61,7 +59,11 @@ QJniEnvironment::QJniEnvironment()
return;
if (ret == JNI_EDETACHED) { // We need to (re-)attach
- JavaVMAttachArgs args = { JNI_VERSION_1_6, qJniThreadName, nullptr };
+ const QByteArray threadName = QThread::currentThread()->objectName().toUtf8();
+ JavaVMAttachArgs args = { JNI_VERSION_1_6,
+ threadName.isEmpty() ? "QtThread" : threadName.constData(),
+ nullptr
+ };
if (vm->AttachCurrentThread(&d->jniEnv, &args) != JNI_OK)
return;