Merged to new trunk.
[cacao.git] / src / threads / posix / thread-posix.c
index 03ac51f84839c2ac36deaddd35571ea9cf407bdb..cfa6deca7de49c11052d1257b6355caed1796132 100644 (file)
 #if !defined(__DARWIN__)
 # include <semaphore.h>
 #endif
-# if defined(__LINUX__)
-#  define GC_LINUX_THREADS
-# elif defined(__IRIX__)
-#  define GC_IRIX_THREADS
-# elif defined(__DARWIN__)
-#  define GC_DARWIN_THREADS
-# endif
-# if defined(ENABLE_GC_BOEHM)
+
+#if defined(__LINUX__)
+# define GC_LINUX_THREADS
+#elif defined(__IRIX__)
+# define GC_IRIX_THREADS
+#elif defined(__DARWIN__)
+# define GC_DARWIN_THREADS
+#endif
+
+#if defined(ENABLE_GC_BOEHM)
 /* We need to include Boehm's gc.h here because it overrides
    pthread_create and friends. */
-#  include "mm/boehm-gc/include/gc.h"
-# endif
+# include "mm/boehm-gc/include/gc.h"
+#endif
 
 #if defined(ENABLE_JVMTI)
 #include "native/jvmti/cacaodbg.h"
@@ -972,9 +974,18 @@ static void *threads_startup_thread(void *arg)
        java_handle_t      *o;
        functionptr         function;
 
+#if defined(ENABLE_GC_BOEHM)
+# if !defined(__DARWIN__)
+       struct GC_stack_base sb;
+       int result;
+# endif
+#endif
+
 #if defined(ENABLE_INTRP)
        u1 *intrp_thread_stack;
+#endif
 
+#if defined(ENABLE_INTRP)
        /* create interpreter stack */
 
        if (opt_intrp) {
@@ -1008,6 +1019,22 @@ static void *threads_startup_thread(void *arg)
 
        thread_set_current(t);
 
+#if defined(ENABLE_GC_BOEHM)
+# if defined(__DARWIN__)
+       // This is currently not implemented in Boehm-GC.  Just fail silently.
+# else
+       /* Register the thread with Boehm-GC.  This must happen before the
+          thread allocates any memory from the GC heap.*/
+
+       result = GC_get_stack_base(&sb);
+
+       if (result != 0)
+               vm_abort("threads_startup_thread: GC_get_stack_base failed: result=%d", result);
+
+       GC_register_my_thread(&sb);
+# endif
+#endif
+
        /* get the java.lang.Thread object for this thread */
 
        object = (java_lang_Thread *) thread_get_object(t);
@@ -1115,7 +1142,7 @@ static void *threads_startup_thread(void *arg)
 
        /* We ignore the return value. */
 
-       (void) threads_detach_thread(t);
+       (void) thread_detach_current_thread();
 
        /* set ThreadMXBean variables */
 
@@ -1225,14 +1252,14 @@ void threads_set_thread_priority(pthread_t tid, int priority)
 }
 
 
-/* threads_detach_thread *******************************************************
-
-   Detaches the passed thread from the VM.  Used in JNI.
-
-*******************************************************************************/
-
-bool threads_detach_thread(threadobject *t)
+/**
+ * Detaches the current thread from the VM.
+ *
+ * @return true on success, false otherwise
+ */
+bool thread_detach_current_thread(void)
 {
+       threadobject          *t;
        bool                   result;
        java_lang_Thread      *object;
        java_handle_t         *o;
@@ -1244,6 +1271,12 @@ bool threads_detach_thread(threadobject *t)
        methodinfo            *m;
 #endif
 
+       t = thread_get_current();
+
+       /* Sanity check. */
+
+       assert(t != NULL);
+
     /* If the given thread has already been detached, this operation
           is a no-op. */