Java directors - more generic thread name setting

Activated if a user sets SWIG_JAVA_USE_THREAD_NAME.
Implementations provided for linux/android/macos/unix.
A user's implementation will be used if SWIG_JAVA_GET_THREAD_NAME is
defined. It must implement the function:
  namespace Swig {
    SWIGINTERN int GetThreadName(char *name, size_t len);
  }
This commit is contained in:
William S Fulton 2018-10-08 21:29:11 +01:00
commit 3195c3e4da
3 changed files with 71 additions and 14 deletions

View file

@ -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;
}

View file

@ -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);
}
}