This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: FYI: updated PR 13212 fix
- From: Tom Tromey <tromey at redhat dot com>
- To: Java Patch List <java-patches at gcc dot gnu dot org>
- Date: 21 Aug 2006 15:59:14 -0600
- Subject: Patch: FYI: updated PR 13212 fix
- Reply-to: tromey at redhat dot com
I'm checking this in.
This has been in the Fedora/Red Hat RPMs for quite a while; I'm
finally putting it on trunk.
This adds a GC API to register the current thread and changes libjava
to take advantage of it. This is at least partially a backport from
GC 7. Note that this backport doesn't include the Windows bits, and
in fact only works when pthread_getattr_np and friends are available,
so there's a little hack in gc_ext_config.h and in boehm.cc to account
for this.
This also fixes a regression that showed up with the previous
libgcj_bc patch. It turns out that patch relied on this one in a
subtle way.
Tom
Index: boehm-gc/ChangeLog
from Bryce McKinlay <mckinlay@redhat.com>
PR libgcj/13212:
* configure.ac: Check for pthread_getattr_np(). Remove
GC_PTHREAD_SYM_VERSION detection.
* include/gc.h (GC_register_my_thread, GC_unregister_my_thread,
GC_get_thread_stack_base): New declarations.
* pthread_support.c (GC_register_my_thread, GC_unregister_my_thread,
GC_get_thread_stack_base): New functions.
(GC_delete_thread): Don't try to free the first_thread.
* misc.c (GC_init_inner): Use GC_get_thread_stack_base() if possible.
(pthread_create_, constr): Removed.
(pthread_create): Don't rename.
* include/gc_ext_config.h.in: Rebuilt.
* include/gc_pthread_redirects.h (pthread_create): Define
unconditionally.
* include/gc_config.h.in: Rebuilt.
* configure: Rebuilt.
Index: libjava/ChangeLog
from Bryce McKinlay <mckinlay@redhat.com>
* java/lang/natThread.cc (_Jv_AttachCurrentThread): Attach thread
to GC.
(_Jv_DetachCurrentThread): Detach thread from GC.
* include/boehm-gc.h (_Jv_GCAttachThread, _Jv_GCDetachThread):
Declare.
* boehm.cc (_Jv_GCAttachThread): New function.
(_Jv_GCDetachThread): Likewise.
Index: boehm-gc/configure.ac
===================================================================
--- boehm-gc/configure.ac (revision 116140)
+++ boehm-gc/configure.ac (working copy)
@@ -1,4 +1,4 @@
-# Copyright (c) 1999, 2000, 2001, 2002, 2003 by Red Hat, Inc. All rights reserved.
+# Copyright (c) 1999, 2000, 2001, 2002, 2003, 2006 by Red Hat, Inc. All rights reserved.
# Copyright 2004 Nathanael Nerode
#
# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
@@ -329,6 +329,13 @@
;;
esac
+# Checks for pthreads functions
+#
+oldLIBS="$LIBS"
+LIBS="$LIBS $THREADLIBS"
+AC_CHECK_FUNCS([pthread_getattr_np])
+LIBS="$oldLIBS"
+
# Configuration of machine-dependent code
#
# We don't set NO_EXECUTE_PERMISSION by default because gcj (and
@@ -488,25 +495,6 @@
AC_DEFINE(USE_MMAP, 1, [use MMAP instead of sbrk to get new memory])
fi
-symver=
-case "$target" in
- *-*-linux* )
- cat > conftest.c <<EOF
-#include <pthread.h>
-void *tf (void *arg) { (void) arg; return NULL; }
-int main (void) { pthread_t th; pthread_create (&th, NULL, tf, NULL); return 0; }
-EOF
- if $CC $CFLAGS -pthread -o conftest conftest.c > /dev/null 2>&1; then
- symver=`readelf -s conftest 2> /dev/null | sed -n '/UND pthread_create@/{s/^.*@//;s/ .*$//;p;q}'`
- fi
- rm -f conftest conftest.c
- ;;
-esac
-if test -n "$symver"; then
- AC_DEFINE_UNQUOTED(GC_PTHREAD_SYM_VERSION, "$symver", [symbol version of pthread_create])
-fi
-
-
if test -n "$with_cross_host" &&
test x"$with_cross_host" != x"no"; then
toolexecdir='$(exec_prefix)/$(target_noncanonical)'
Index: boehm-gc/include/gc.h
===================================================================
--- boehm-gc/include/gc.h (revision 116140)
+++ boehm-gc/include/gc.h (working copy)
@@ -69,7 +69,6 @@
extern "C" {
# endif
-
/* Define word and signed_word to be unsigned and signed types of the */
/* size as char * or void *. There seems to be no way to do this */
/* even semi-portably. The following is probably no better/worse */
@@ -912,6 +911,25 @@
# if defined(PCR) || defined(GC_SOLARIS_THREADS) || \
defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
/* Any flavor of threads except SRC_M3. */
+
+/* Register the current thread as a new thread whose stack(s) should */
+/* be traced by the GC. */
+/* If a platform does not implicitly do so, this must be called before */
+/* a thread can allocate garbage collected memory, or assign pointers */
+/* to the garbage collected heap. Once registered, a thread will be */
+/* stopped during garbage collections. */
+GC_API void GC_register_my_thread GC_PROTO((void));
+
+/* Register the current thread, with the indicated stack base, as */
+/* a new thread whose stack(s) should be traced by the GC. If a */
+/* platform does not implicitly do so, this must be called before a */
+/* thread can allocate garbage collected memory, or assign pointers */
+/* to the garbage collected heap. Once registered, a thread will be */
+/* stopped during garbage collections. */
+GC_API void GC_unregister_my_thread GC_PROTO((void));
+
+GC_API GC_PTR GC_get_thread_stack_base GC_PROTO((void));
+
/* This returns a list of objects, linked through their first */
/* word. Its use can greatly reduce lock contention problems, since */
/* the allocation lock can be acquired and released many fewer times. */
Index: boehm-gc/include/gc_ext_config.h.in
===================================================================
--- boehm-gc/include/gc_ext_config.h.in (revision 116140)
+++ boehm-gc/include/gc_ext_config.h.in (working copy)
@@ -4,4 +4,4 @@
#undef THREAD_LOCAL_ALLOC
-#undef GC_PTHREAD_SYM_VERSION
+#undef HAVE_PTHREAD_GETATTR_NP
Index: boehm-gc/include/gc_pthread_redirects.h
===================================================================
--- boehm-gc/include/gc_pthread_redirects.h (revision 116140)
+++ boehm-gc/include/gc_pthread_redirects.h (working copy)
@@ -68,9 +68,7 @@
# undef pthread_detach
#endif
-#ifndef GC_PTHREAD_SYM_VERSION
# define pthread_create GC_pthread_create
-#endif
# define pthread_join GC_pthread_join
# define pthread_detach GC_pthread_detach
Index: boehm-gc/pthread_support.c
===================================================================
--- boehm-gc/pthread_support.c (revision 116140)
+++ boehm-gc/pthread_support.c (working copy)
@@ -602,7 +602,9 @@
} else {
prev -> next = p -> next;
}
- GC_INTERNAL_FREE(p);
+
+ if (p != &first_thread)
+ GC_INTERNAL_FREE(p);
}
/* If a thread has been joined, but we have not yet */
@@ -1124,6 +1126,107 @@
GC_bool GC_in_thread_creation = FALSE;
+GC_PTR GC_get_thread_stack_base()
+{
+# ifdef HAVE_PTHREAD_GETATTR_NP
+ pthread_t my_pthread;
+ pthread_attr_t attr;
+ ptr_t stack_addr;
+ size_t stack_size;
+
+ my_pthread = pthread_self();
+ pthread_getattr_np (my_pthread, &attr);
+ pthread_attr_getstack (&attr, (void **) &stack_addr, &stack_size);
+ pthread_attr_destroy (&attr);
+
+# ifdef DEBUG_THREADS
+ GC_printf1("attached thread stack address: 0x%x\n", stack_addr);
+# endif
+
+# ifdef STACK_GROWS_DOWN
+ return stack_addr + stack_size;
+# else
+ return stack_addr - stack_size;
+# endif
+
+# else
+# ifdef DEBUG_THREADS
+ GC_printf1("Can not determine stack base for attached thread");
+# endif
+ return 0;
+# endif
+}
+
+void GC_register_my_thread()
+{
+ GC_thread me;
+ pthread_t my_pthread;
+
+ my_pthread = pthread_self();
+# ifdef DEBUG_THREADS
+ GC_printf1("Attaching thread 0x%lx\n", my_pthread);
+ GC_printf1("pid = %ld\n", (long) getpid());
+# endif
+
+ /* Check to ensure this thread isn't attached already. */
+ LOCK();
+ me = GC_lookup_thread (my_pthread);
+ UNLOCK();
+ if (me != 0)
+ {
+# ifdef DEBUG_THREADS
+ GC_printf1("Attempt to re-attach known thread 0x%lx\n", my_pthread);
+# endif
+ return;
+ }
+
+ LOCK();
+ GC_in_thread_creation = TRUE;
+ me = GC_new_thread(my_pthread);
+ GC_in_thread_creation = FALSE;
+
+ me -> flags |= DETACHED;
+
+#ifdef GC_DARWIN_THREADS
+ me -> stop_info.mach_thread = mach_thread_self();
+#else
+ me -> stack_end = GC_get_thread_stack_base();
+ if (me -> stack_end == 0)
+ GC_abort("Can not determine stack base for attached thread");
+
+# ifdef STACK_GROWS_DOWN
+ me -> stop_info.stack_ptr = me -> stack_end - 0x10;
+# else
+ me -> stop_info.stack_ptr = me -> stack_end + 0x10;
+# endif
+#endif
+
+# ifdef IA64
+ me -> backing_store_end = (ptr_t)
+ (GC_save_regs_in_stack() & ~(GC_page_size - 1));
+ /* This is also < 100% convincing. We should also read this */
+ /* from /proc, but the hook to do so isn't there yet. */
+# endif /* IA64 */
+
+# if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
+ GC_init_thread_local(me);
+# endif
+ UNLOCK();
+}
+
+void GC_unregister_my_thread()
+{
+ pthread_t my_pthread;
+
+ my_pthread = pthread_self();
+
+# ifdef DEBUG_THREADS
+ GC_printf1("Detaching thread 0x%lx\n", my_pthread);
+# endif
+
+ GC_thread_exit_proc (0);
+}
+
void * GC_start_routine(void * arg)
{
int dummy;
@@ -1200,37 +1303,8 @@
return(result);
}
-#ifdef GC_PTHREAD_SYM_VERSION
-
-/* Force constr to execute prior to main(). */
-static void constr (void) __attribute__ ((constructor));
-
-static int
-(*pthread_create_)(pthread_t *new_thread,
- const pthread_attr_t *attr_in,
- void * (*thread_execp)(void *), void *arg);
-
-static void
-constr (void)
-{
- /* Get a pointer to the real pthread_create. */
- pthread_create_ = dlvsym (RTLD_NEXT, "pthread_create",
- GC_PTHREAD_SYM_VERSION);
-}
-
-#define GC_PTHREAD_CREATE_NAME pthread_create
-#define GC_PTHREAD_REAL_NAME (*pthread_create_)
-
-#else
-
-#define GC_PTHREAD_CREATE_NAME WRAP_FUNC(pthread_create)
-#define GC_PTHREAD_REAL_NAME REAL_FUNC(pthread_create)
-
-#endif
-
-
int
-GC_PTHREAD_CREATE_NAME(pthread_t *new_thread,
+WRAP_FUNC(pthread_create)(pthread_t *new_thread,
const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg)
{
@@ -1291,7 +1365,7 @@
pthread_self());
# endif
- result = GC_PTHREAD_REAL_NAME(new_thread, attr, GC_start_routine, si);
+ result = REAL_FUNC(pthread_create)(new_thread, attr, GC_start_routine, si);
# ifdef DEBUG_THREADS
GC_printf1("Started thread 0x%X\n", *new_thread);
Index: boehm-gc/misc.c
===================================================================
--- boehm-gc/misc.c (revision 116140)
+++ boehm-gc/misc.c (working copy)
@@ -674,7 +674,13 @@
# if !defined(THREADS) || defined(GC_PTHREADS) || defined(GC_WIN32_THREADS) \
|| defined(GC_SOLARIS_THREADS)
if (GC_stackbottom == 0) {
- GC_stackbottom = GC_get_stack_base();
+ # ifdef GC_PTHREADS
+ /* Use thread_stack_base if available, as GC could be initialized from
+ a thread that is not the "main" thread. */
+ GC_stackbottom = GC_get_thread_stack_base();
+ # endif
+ if (GC_stackbottom == 0)
+ GC_stackbottom = GC_get_stack_base();
# if (defined(LINUX) || defined(HPUX)) && defined(IA64)
GC_register_stackbottom = GC_get_register_stack_base();
# endif
Index: libjava/boehm.cc
===================================================================
--- libjava/boehm.cc (revision 116169)
+++ libjava/boehm.cc (working copy)
@@ -695,3 +695,21 @@
GC_resume_thread (_Jv_GetPlatformThreadID (thread));
#endif
}
+
+void
+_Jv_GCAttachThread ()
+{
+ // The registration interface is only defined on posixy systems and
+ // only actually works if pthread_getattr_np is defined.
+#ifdef HAVE_PTHREAD_GETATTR_NP
+ GC_register_my_thread ();
+#endif
+}
+
+void
+_Jv_GCDetachThread ()
+{
+#ifdef HAVE_PTHREAD_GETATTR_NP
+ GC_unregister_my_thread ();
+#endif
+}
Index: libjava/java/lang/natThread.cc
===================================================================
--- libjava/java/lang/natThread.cc (revision 116169)
+++ libjava/java/lang/natThread.cc (working copy)
@@ -410,7 +410,8 @@
}
// Attach the current native thread to an existing (but unstarted) Thread
-// object. Returns -1 on failure, 0 upon success.
+// object. Does not register thread with the garbage collector.
+// Returns -1 on failure, 0 upon success.
jint
_Jv_AttachCurrentThread(java::lang::Thread* thread)
{
@@ -427,6 +428,8 @@
java::lang::Thread*
_Jv_AttachCurrentThread(jstring name, java::lang::ThreadGroup* group)
{
+ // Register thread with GC before attempting any allocations.
+ _Jv_GCAttachThread ();
java::lang::Thread *thread = _Jv_ThreadCurrent ();
if (thread != NULL)
return thread;
@@ -461,6 +464,7 @@
return -1;
_Jv_ThreadUnRegister ();
+ _Jv_GCDetachThread ();
// Release the monitors.
t->finish_ ();
Index: libjava/include/boehm-gc.h
===================================================================
--- libjava/include/boehm-gc.h (revision 116169)
+++ libjava/include/boehm-gc.h (working copy)
@@ -80,6 +80,10 @@
#endif /* LIBGCJ_GC_DEBUG */
+void _Jv_GCAttachThread ();
+
+void _Jv_GCDetachThread ();
+
// _Jv_AllocBytes (jsize size) should go here, too. But clients don't
// usually include this header.