Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions olp-cpp-sdk-core/src/http/android/utils/JNIThreadBinder.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 HERE Europe B.V.
* Copyright (C) 2019-2026 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@
#ifdef __ANDROID__

#include <jni.h>
#include <sys/prctl.h>

namespace olp {
namespace http {
Expand All @@ -35,7 +36,9 @@ class JNIThreadBinder final {
: attached_(false), jni_env_(nullptr), jni_vm_(vm) {
if (jni_vm_->GetEnv(reinterpret_cast<void**>(&jni_env_), JNI_VERSION_1_6) !=
JNI_OK) {
if (jni_vm_->AttachCurrentThread(&jni_env_, nullptr) == JNI_OK) {
captureThreadName();
auto attachArgs = getJvmAttachArgs();
if (jni_vm_->AttachCurrentThread(&jni_env_, &attachArgs) == JNI_OK) {
attached_ = true;
}
}
Expand All @@ -56,9 +59,23 @@ class JNIThreadBinder final {
JNIEnv* GetEnv() const { return jni_env_; }

private:
void captureThreadName() {
if (prctl(PR_GET_NAME, threadName_) != 0) {
threadName_[0] = '\0';
}
}

JavaVMAttachArgs getJvmAttachArgs() const {
return JavaVMAttachArgs{.version = JNI_VERSION_1_6,
.name = threadName_[0] ? threadName_ : nullptr,
.group = nullptr};
}

bool attached_;
JNIEnv* jni_env_;
JavaVM* jni_vm_;
constexpr static size_t kThreadNameMaxLength = 16;
char threadName_[kThreadNameMaxLength] = {0};
};

} // namespace utils
Expand Down