* src/threads/native/threads.c (threads_init): Use variable t instead
[cacao.git] / src / threads / threads-common.c
index a6abe93a1fe01acd74fda76db3ae1a439ac88625..84f89daac5b013aea7318ee8e88f1128ff091369 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: signal.c 7246 2007-01-29 18:49:05Z twisti $
+   $Id: threads-common.c 7805 2007-04-25 11:47:15Z twisti $
 
 */
 
 
 #include "config.h"
-#include "vm/types.h"
 
+#include <assert.h>
+
+#include "vm/types.h"
 
 #include "native/jni.h"
+
 #include "native/include/java_lang_Object.h"
+#include "native/include/java_lang_String.h"
 #include "native/include/java_lang_Thread.h"
 
 #if defined(WITH_CLASSPATH_GNU)
 #include "vm/jit/stacktrace.h"
 
 #include "vmcore/class.h"
+
+#if defined(ENABLE_STATISTICS)
+# include "vmcore/options.h"
+# include "vmcore/statistics.h"
+#endif
+
 #include "vmcore/utf8.h"
 
 
@@ -69,7 +79,11 @@ threadobject *threads_create_thread(utf *name)
 
        /* create the vm internal thread object */
 
+#if defined(ENABLE_GC_BOEHM)
+       thread = GCNEW_UNCOLLECTABLE(threadobject, 1);
+#else
        thread = NEW(threadobject);
+#endif
 
        if (thread == NULL)
                return NULL;
@@ -100,7 +114,7 @@ threadobject *threads_create_thread(utf *name)
 
        /* set java.lang.Thread fields */
 
-       t->name     = javastring_new(name);
+       t->name     = (java_lang_String *) javastring_new(name);
 #if defined(ENABLE_JAVASE)
        t->daemon   = true;
 #endif
@@ -112,6 +126,59 @@ threadobject *threads_create_thread(utf *name)
 }
 
 
+/* threads_start_javathread ***************************************************
+
+   Start a thread in the JVM. Only the java thread object exists so far.
+
+   IN:
+      object.....the java thread object java.lang.Thread
+
+******************************************************************************/
+
+void threads_start_javathread(java_lang_Thread *object)
+{
+       threadobject *thread;
+
+       /* create the vm internal threadobject */
+
+#if defined(ENABLE_GC_BOEHM)
+       thread = GCNEW_UNCOLLECTABLE(threadobject, 1);
+#else
+       thread = NEW(threadobject);
+#endif
+
+#if defined(ENABLE_STATISTICS)
+       if (opt_stat)
+               size_threadobject += sizeof(threadobject);
+#endif
+
+       /* link the two objects together */
+
+       thread->object = object;
+
+#if defined(ENABLE_JAVASE)
+       /* is this a daemon thread? */
+
+       if (object->daemon == true)
+               thread->flags |= THREAD_FLAG_DAEMON;
+#endif
+
+#if defined(WITH_CLASSPATH_GNU)
+       assert(object->vmThread);
+       assert(object->vmThread->vmdata == NULL);
+
+       object->vmThread->vmdata = (java_lang_Object *) thread;
+#elif defined(WITH_CLASSPATH_CLDC1_1)
+       object->vm_thread = (java_lang_Object *) thread;
+#endif
+
+       /* Actually start the thread.  Don't pass a function pointer
+          (NULL) since we want Thread.run()V here. */
+
+       threads_start_thread(thread, NULL);
+}
+
+
 /* threads_get_current_tid *****************************************************
 
    Return the tid of the current thread.
@@ -217,14 +284,14 @@ void threads_dump(void)
        java_lang_Thread *t;
        utf              *name;
 
-       thread = mainthreadobj;
-
        /* XXX we should stop the world here */
 
        printf("Full thread dump CACAO "VERSION":\n");
 
        /* iterate over all started threads */
 
+       thread = mainthreadobj;
+
        do {
                /* get thread object */
 
@@ -236,7 +303,7 @@ void threads_dump(void)
                        /* get thread name */
 
 #if defined(ENABLE_JAVASE)
-                       name = javastring_toutf(t->name, false);
+                       name = javastring_toutf((java_objectheader *) t->name, false);
 #elif defined(ENABLE_JAVAME_CLDC1_1)
                        name = t->name;
 #endif
@@ -251,9 +318,11 @@ void threads_dump(void)
                        printf(" prio=%d", t->priority);
 
 #if SIZEOF_VOID_P == 8
-                       printf(" tid=0x%016lx", (ptrint) thread->tid);
+                       printf(" tid=0x%016lx (%ld)",
+                                  (ptrint) thread->tid, (ptrint) thread->tid);
 #else
-                       printf(" tid=0x%08lx", (ptrint) thread->tid);
+                       printf(" tid=0x%08lx (%ld)",
+                                  (ptrint) thread->tid, (ptrint) thread->tid);
 #endif
 
                        /* print thread state */
@@ -286,7 +355,7 @@ void threads_dump(void)
 
                        /* print trace of thread */
 
-                       threads_print_stacktrace(thread);
+                       threads_thread_print_stacktrace(thread);
                }
 
                thread = thread->next;
@@ -294,13 +363,13 @@ void threads_dump(void)
 }
 
 
-/* threads_print_stacktrace ****************************************************
+/* threads_thread_print_stacktrace *********************************************
 
-   Print the current stacktrace of the given thread.
+   Print the current stacktrace of the current thread.
 
 *******************************************************************************/
 
-void threads_print_stacktrace(threadobject *thread)
+void threads_thread_print_stacktrace(threadobject *thread)
 {
        stackframeinfo   *sfi;
        stacktracebuffer *stb;
@@ -329,6 +398,22 @@ void threads_print_stacktrace(threadobject *thread)
 }
 
 
+/* threads_print_stacktrace ****************************************************
+
+   Print the current stacktrace of the current thread.
+
+*******************************************************************************/
+
+void threads_print_stacktrace(void)
+{
+       threadobject *thread;
+
+       thread = THREADOBJECT;
+
+       threads_thread_print_stacktrace(thread);
+}
+
+
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where