Skip to content
Open
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
18 changes: 16 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 <pthread.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) {
getThreadName();
auto attachArgs = getJvmAttachArgs();
if (jni_vm_->AttachCurrentThread(&jni_env_, &attachArgs) == JNI_OK) {
attached_ = true;
}
}
Expand All @@ -56,9 +59,20 @@ class JNIThreadBinder final {
JNIEnv* GetEnv() const { return jni_env_; }

private:
void getThreadName() {
pthread_getname_np(pthread_self(), threadName_, sizeof(threadName_));
}

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_;
char threadName_[16] = {0};
};

} // namespace utils
Expand Down