Use prctl method to get thread names, which is available on all NDK versions

This commit is contained in:
Sam Hasinoff 2018-10-04 17:59:38 -07:00 committed by William S Fulton
commit 989f4289c1

View file

@ -5,13 +5,16 @@
* methods can be called from C++.
* ----------------------------------------------------------------------------- */
#if defined(__ANDROID__)
#include <sys/prctl.h>
#endif
#if defined(DEBUG_DIRECTOR_OWNED) || defined(DEBUG_DIRECTOR_EXCEPTION)
#if defined(__ANDROID__)
#include <errno.h>
#include <string.h>
#include <iostream>
#endif
#if defined(__ANDROID__)
#include <pthread.h>
#include <iostream>
#endif
#include <exception>
@ -149,17 +152,15 @@ namespace Swig {
args.group = NULL;
args.name = NULL;
#if defined(__ANDROID__)
#if defined(__ANDROID_API__) && __ANDROID_API__ >= 26
char thread_name[16]; // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.
if (pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name)) == 0) {
if (prctl(PR_GET_NAME, (unsigned long)thread_name, 0, 0, 0) == 0) {
args.name = thread_name;
} else {
#if defined(DEBUG_DIRECTOR_OWNED)
std::cout << "JNIEnvWrapper: Couldn't get thread name: " << strerror(errno) << std::endl;
#if defined(DEBUG_DIRECTOR_OWNED) || defined(DEBUG_DIRECTOR_EXCEPTION)
std::cerr << "JNIEnvWrapper: Couldn't set thread name: " << strerror(errno) << std::endl;
#endif
}
#endif
#endif
#if defined(SWIG_JAVA_ATTACH_CURRENT_THREAD_AS_DAEMON)
// Attach a daemon thread to the JVM. Useful when the JVM should not wait for
// the thread to exit upon shutdown. Only for jdk-1.4 and later.