Merge branch 'java-director-thread-name'
* java-director-thread-name: Add changes entry for setting Java thread name Increased Java thread name size. Java directors - more generic thread name setting Use prctl method to get thread names, which is available on all NDK versions Actually, pthread_getname_np is only available in recent versions of Android (API level >= 26). Set thread name when attaching to Android JVM in SWIG Java director
This commit is contained in:
commit
d835c69d65
4 changed files with 94 additions and 4 deletions
|
|
@ -7,6 +7,17 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
|
|||
Version 4.0.0 (in progress)
|
||||
===========================
|
||||
|
||||
2018-12-20: hasinoff,wsfulton
|
||||
[Java] #1334 Set Java thread name to native thread name when using directors.
|
||||
|
||||
Default is to use name "Thread-XXX" and is still works like this by default. However,
|
||||
adding the following will turn on the thread name setting (works for more recent
|
||||
versions of Linux and MacOS):
|
||||
|
||||
%begin %{
|
||||
#define SWIG_JAVA_USE_THREAD_NAME
|
||||
%}
|
||||
|
||||
2018-12-20: chlandsi
|
||||
[Python] #1357. Fix overriding __new__ in Python 3.6.
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,11 @@
|
|||
%module(directors="1") director_thread
|
||||
#endif
|
||||
|
||||
%begin %{
|
||||
#define SWIG_JAVA_USE_THREAD_NAME
|
||||
//#define DEBUG_DIRECTOR_THREAD_NAME
|
||||
%}
|
||||
|
||||
%{
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
|
|
@ -89,10 +94,23 @@ extern "C" {
|
|||
fprintf(stderr, "pthread_create failed in run()\n");
|
||||
assert(0);
|
||||
}
|
||||
int setname = pthread_setname_np(thread, "MyThreadName");
|
||||
if (setname != 0) {
|
||||
fprintf(stderr, "pthread_setname_np failed in run()\n");
|
||||
assert(0);
|
||||
}
|
||||
%#endif
|
||||
MilliSecondSleep(500);
|
||||
}
|
||||
|
||||
|
||||
static bool namedThread() {
|
||||
%#ifdef _WIN32
|
||||
return false;
|
||||
%#else
|
||||
return true;
|
||||
%#endif
|
||||
}
|
||||
|
||||
virtual void do_foo() {
|
||||
val += 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,12 @@ class director_thread_Derived extends Foo {
|
|||
}
|
||||
|
||||
public void do_foo() {
|
||||
// Not all operating systems can name threads, so only test on those that can
|
||||
if (Foo.namedThread()) {
|
||||
String threadName = Thread.currentThread().getName();
|
||||
if (!threadName.equals("MyThreadName"))
|
||||
throw new RuntimeException("Unexpected thread name: " + threadName);
|
||||
}
|
||||
setVal(getVal() - 1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,12 +5,52 @@
|
|||
* methods can be called from C++.
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
#if defined(DEBUG_DIRECTOR_OWNED) || defined(DEBUG_DIRECTOR_EXCEPTION)
|
||||
#if defined(DEBUG_DIRECTOR_OWNED) || defined(DEBUG_DIRECTOR_EXCEPTION) || defined(DEBUG_DIRECTOR_THREAD_NAME)
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
#include <exception>
|
||||
|
||||
#if defined(SWIG_JAVA_USE_THREAD_NAME)
|
||||
|
||||
#if !defined(SWIG_JAVA_GET_THREAD_NAME)
|
||||
namespace Swig {
|
||||
SWIGINTERN int GetThreadName(char *name, size_t len);
|
||||
}
|
||||
|
||||
#if defined(__linux__)
|
||||
|
||||
#include <sys/prctl.h>
|
||||
SWIGINTERN int Swig::GetThreadName(char *name, size_t len) {
|
||||
(void)len;
|
||||
#if defined(PR_GET_NAME)
|
||||
return prctl(PR_GET_NAME, (unsigned long)name, 0, 0, 0);
|
||||
#else
|
||||
(void)name;
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
#elif defined(__unix__)
|
||||
|
||||
#include <pthread.h>
|
||||
SWIGINTERN int Swig::GetThreadName(char *name, size_t len) {
|
||||
return pthread_getname_np(pthread_self(), name, len);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
SWIGINTERN int Swig::GetThreadName(char *name, size_t len) {
|
||||
(void)len;
|
||||
(void)name;
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
namespace Swig {
|
||||
|
||||
/* Java object wrapper */
|
||||
|
|
@ -139,12 +179,27 @@ namespace Swig {
|
|||
void **jenv = (void **)&jenv_;
|
||||
#endif
|
||||
env_status = director_->swig_jvm_->GetEnv((void **)&jenv_, JNI_VERSION_1_2);
|
||||
JavaVMAttachArgs args;
|
||||
args.version = JNI_VERSION_1_2;
|
||||
args.group = NULL;
|
||||
args.name = NULL;
|
||||
#if defined(SWIG_JAVA_USE_THREAD_NAME)
|
||||
char thread_name[64]; // MAX_TASK_COMM_LEN=16 is hard-coded in the Linux kernel and MacOS has MAXTHREADNAMESIZE=64.
|
||||
if (Swig::GetThreadName(thread_name, sizeof(thread_name)) == 0) {
|
||||
args.name = thread_name;
|
||||
#if defined(DEBUG_DIRECTOR_THREAD_NAME)
|
||||
std::cout << "JNIEnvWrapper: thread name: " << thread_name << std::endl;
|
||||
} else {
|
||||
std::cout << "JNIEnvWrapper: Couldn't set Java thread name" << std::endl;
|
||||
#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.
|
||||
director_->swig_jvm_->AttachCurrentThreadAsDaemon(jenv, NULL);
|
||||
director_->swig_jvm_->AttachCurrentThreadAsDaemon(jenv, &args);
|
||||
#else
|
||||
director_->swig_jvm_->AttachCurrentThread(jenv, NULL);
|
||||
director_->swig_jvm_->AttachCurrentThread(jenv, &args);
|
||||
#endif
|
||||
}
|
||||
~JNIEnvWrapper() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue