# HG changeset patch
# User Markus Mützel <markus.muetzel@gmx.de>
# Date 1593189875 -7200
#      Fri Jun 26 18:44:35 2020 +0200
# Node ID fa6b7c3113896b8cda817309a814a7597d70ecd2
# Parent  f480103d8333f1f2dfa584c7ff89b90bddfe6328
ov-java.cc: Set reaper thread to use default stack size (bug #58641).

* m4/acinclude.m4 (OCTAVE_CHECK_BROKEN_PTHREAD_STACKSIZE): New configure test.

* configure.ac: Run new configure test.

* ov-java.cc (initialize_jvm): Set jdk.lang.processReaperUseDefaultStackSize to
true if the new configure test failed (work around a glibc bug).

diff -r f480103d8333 -r fa6b7c311389 configure.ac
--- a/configure.ac	Thu Jun 25 18:20:03 2020 -0700
+++ b/configure.ac	Fri Jun 26 18:44:35 2020 +0200
@@ -549,6 +549,12 @@
 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
 CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"
 
+dnl Check if glibc uses wrong stack size
+OCTAVE_CHECK_BROKEN_PTHREAD_STACKSIZE
+AM_CONDITIONAL([OCTAVE_CHECK_BROKEN_PTHREAD_STACKSIZE],
+  [test $octave_cv_broken_pthread_stacksize = yes])
+
+
 ### Test whether the compiler supports OpenMP.
 dnl This is enabled by default to allow the option of using OpenMP in
 dnl loadable modules.
diff -r f480103d8333 -r fa6b7c311389 libinterp/octave-value/ov-java.cc
--- a/libinterp/octave-value/ov-java.cc	Thu Jun 25 18:20:03 2020 -0700
+++ b/libinterp/octave-value/ov-java.cc	Fri Jun 26 18:44:35 2020 +0200
@@ -221,7 +221,7 @@
 
 bool Vjava_matrix_autoconversion = false;
 bool Vjava_unsigned_autoconversion = true;
-bool Vdebug_java = false;
+bool Vdebug_java = true;
 
 namespace octave
 {
@@ -763,6 +763,9 @@
 
       // Hard-coded options for the jvm.
       vm_args.add ("-Djava.class.path=" + initial_class_path ());
+#if defined (HAVE_BROKEN_PTHREAD_STACKSIZE)
+      vm_args.add ("-Djdk.lang.processReaperUseDefaultStackSize=true");
+#endif
       vm_args.add ("-Xrs");
 
       // Additional options given by file java.opts.
diff -r f480103d8333 -r fa6b7c311389 m4/acinclude.m4
--- a/m4/acinclude.m4	Thu Jun 25 18:20:03 2020 -0700
+++ b/m4/acinclude.m4	Fri Jun 26 18:44:35 2020 +0200
@@ -187,6 +187,74 @@
   fi
 ])
 dnl
+dnl Check if pthread stack size accounts for thread-local storage.
+dnl
+dnl This program should succeed if the pthread library allocates memory
+dnl for thread-local (__thread) variables independently of the
+dnl requested thread stack size.
+dnl
+dnl It will fail if (as in the current version of glibc) the storage
+dnl for thread-local variables is subtracted from the memory allocated
+dnl for the thread stack.  (This can cause problems for Java and for
+dnl other libraries.)
+dnl
+dnl This bug is tracked in glibc at:
+dnl https://sourceware.org/bugzilla/show_bug.cgi?id=11787
+dnl
+AC_DEFUN([OCTAVE_CHECK_BROKEN_PTHREAD_STACKSIZE], [
+  AC_CACHE_CHECK([whether pthread stack size does not account for thread-local storage],
+    [octave_cv_broken_pthread_stacksize],
+    [AC_LANG_PUSH(C)
+    AC_RUN_IFELSE([AC_LANG_PROGRAM([[
+#include <stdio.h>
+#include <string.h>
+#include <pthread.h>
+
+static char __thread data[100 * 1024];
+
+static void * threadfunc(void *arg)
+{
+    return data;
+}
+      ]], [[
+  pthread_attr_t attr;
+  pthread_t thread;
+  int errnum;
+
+  pthread_attr_init (&attr);
+  errnum = pthread_attr_setstacksize (&attr, 64 * 1024);
+  if (errnum != 0)
+  {
+    fprintf (stderr, "pthread_attr_setstacksize: %s\n", strerror(errnum));
+    return 1;
+  }
+  errnum = pthread_create (&thread, &attr, &threadfunc, NULL);
+  if (errnum != 0)
+  {
+    fprintf (stderr, "pthread_create: %s\n", strerror(errnum));
+    return 1;
+  }
+  errnum = pthread_join (thread, NULL);
+  if (errnum != 0)
+  {
+    fprintf (stderr, "pthread_join: %s\n", strerror(errnum));
+    return 1;
+  }
+
+  pthread_attr_destroy (&attr);
+  return 0;
+    ]])],
+    octave_cv_broken_pthread_stacksize=no,
+    octave_cv_broken_pthread_stacksize=yes,
+    octave_cv_broken_pthread_stacksize=no)
+    AC_LANG_POP(C)
+  ])
+  if test $octave_cv_broken_pthread_stacksize = yes; then
+    AC_DEFINE(HAVE_BROKEN_PTHREAD_STACKSIZE, 1,
+      [Define to 1 if pthread stack size does not account for thread-local storage.])
+  fi
+])
+dnl
 dnl Check whether CXSparse is version 2.2 or later
 dnl FIXME: This test uses a version number.  It potentially could
 dnl        be re-written to actually call a function, but is it worth it?
