Oh no, it worketh!
[cacao.git] / src / threads / threads-common.c
index 39f49418d842bdaa10d2064704611a61637ae148..b8bc7046b866670c33fcb05fe69bb4eb40b39cce 100644 (file)
@@ -1,9 +1,7 @@
 /* src/threads/threads-common.c - machine independent thread functions
 
-   Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel,
-   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
-   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
-   J. Wenninger, Institut f. Computersprachen - TU Wien
+   Copyright (C) 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
 #include "native/include/java_lang_Thread.h"
 
 #if defined(WITH_CLASSPATH_GNU)
+# include "native/include/java_lang_Throwable.h"
 # include "native/include/java_lang_VMThread.h"
 #endif
 
 #include "threads/critical.h"
 #include "threads/lock-common.h"
+#include "threads/threadlist.h"
 #include "threads/threads-common.h"
 
 #include "toolbox/list.h"
@@ -59,9 +59,9 @@
 #include "vm/jit/stacktrace.h"
 
 #include "vmcore/class.h"
+#include "vmcore/options.h"
 
 #if defined(ENABLE_STATISTICS)
-# include "vmcore/options.h"
 # include "vmcore/statistics.h"
 #endif
 
 
 /* global variables ***********************************************************/
 
-/* global threads list */
-static list_t *list_threads;
-
-/* global threads free-list */
-
-typedef struct thread_index_t {
-       int32_t    index;
-       listnode_t linkage;
-} thread_index_t;
-
-static list_t *list_free_thread_index;
-
 #if defined(__LINUX__)
 /* XXX Remove for exact-GC. */
 bool threads_pthreads_implementation_nptl;
@@ -92,9 +80,6 @@ bool threads_pthreads_implementation_nptl;
 
    Do some early initialization of stuff required.
 
-   ATTENTION: Do NOT use any Java heap allocation here, as gc_init()
-   is called AFTER this function!
-
 *******************************************************************************/
 
 void threads_preinit(void)
@@ -105,6 +90,8 @@ void threads_preinit(void)
        size_t        len;
 #endif
 
+       TRACESUBSYSTEMINITIALIZATION("threads_preinit");
+
 #if defined(__LINUX__)
        /* XXX Remove for exact-GC. */
 
@@ -137,11 +124,6 @@ void threads_preinit(void)
 # endif
 #endif
 
-       /* initialize the threads lists */
-
-       list_threads           = list_create(OFFSET(threadobject, linkage));
-       list_free_thread_index = list_create(OFFSET(thread_index_t, linkage));
-
        /* Initialize the threads implementation (sets the thinlock on the
           main thread). */
 
@@ -159,83 +141,6 @@ void threads_preinit(void)
        /* store the internal thread data-structure in the TSD */
 
        threads_set_current_threadobject(mainthread);
-
-       /* initialize locking subsystems */
-
-       lock_init();
-
-       /* initialize the critical section */
-
-       critical_init();
-}
-
-
-/* threads_list_first **********************************************************
-
-   Return the first entry in the threads list.
-
-   NOTE: This function does not lock the lists.
-
-*******************************************************************************/
-
-threadobject *threads_list_first(void)
-{
-       threadobject *t;
-
-       t = list_first_unsynced(list_threads);
-
-       return t;
-}
-
-
-/* threads_list_next ***********************************************************
-
-   Return the next entry in the threads list.
-
-   NOTE: This function does not lock the lists.
-
-*******************************************************************************/
-
-threadobject *threads_list_next(threadobject *t)
-{
-       threadobject *next;
-
-       next = list_next_unsynced(list_threads, t);
-
-       return next;
-}
-
-
-/* threads_list_get_non_daemons ************************************************
-
-   Return the number of non-daemon threads.
-
-   NOTE: This function does a linear-search over the threads list,
-         because it's only used for joining the threads.
-
-*******************************************************************************/
-
-s4 threads_list_get_non_daemons(void)
-{
-       threadobject *t;
-       s4            nondaemons;
-
-       /* lock the threads lists */
-
-       threads_list_lock();
-
-       nondaemons = 0;
-
-       for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
-               if (!(t->flags & THREAD_FLAG_DAEMON))
-                       nondaemons++;
-       }
-
-       /* unlock the threads lists */
-
-       threads_list_unlock();
-
-       return nondaemons;
 }
 
 
@@ -248,7 +153,6 @@ s4 threads_list_get_non_daemons(void)
 
 threadobject *threads_thread_new(void)
 {
-       thread_index_t *ti;
        int32_t         index;
        threadobject   *t;
        
@@ -256,56 +160,52 @@ threadobject *threads_thread_new(void)
 
        threads_list_lock();
 
-       /* Try to get a thread index from the free-list. */
+       index = threadlist_get_free_index();
 
-       ti = list_first_unsynced(list_free_thread_index);
+       /* Allocate a thread data structure. */
 
-       /* Is a free thread index available? */
+       /* First, try to get one from the free-list. */
 
-       if (ti != NULL) {
-               /* Yes, remove it from the free list, get the index and free
-                  the entry. */
+       t = threadlist_free_first();
 
-               list_remove_unsynced(list_free_thread_index, ti);
+       if (t != NULL) {
+               /* Remove from free list. */
 
-               index = ti->index;
+               threadlist_free_remove(t);
 
-               FREE(ti, thread_index_t);
+               /* Equivalent of MZERO on the else path */
 
-#if defined(ENABLE_STATISTICS)
-               if (opt_stat)
-                       size_thread_index_t -= sizeof(thread_index_t);
-#endif
+               threads_impl_thread_clear(t);
        }
        else {
-               /* Get a new the thread index. */
-
-               index = list_threads->size + 1;
-       }
-
-       /* Allocate a thread data structure. */
-
 #if defined(ENABLE_GC_BOEHM)
-       t = GCNEW_UNCOLLECTABLE(threadobject, 1);
+               t = GCNEW_UNCOLLECTABLE(threadobject, 1);
 #else
-       t = NEW(threadobject);
+               t = NEW(threadobject);
 #endif
 
 #if defined(ENABLE_STATISTICS)
-       if (opt_stat)
-               size_threadobject += sizeof(threadobject);
+               if (opt_stat)
+                       size_threadobject += sizeof(threadobject);
 #endif
 
-       /* Clear memory. */
+               /* Clear memory. */
 
-       MZERO(t, threadobject, 1);
+               MZERO(t, threadobject, 1);
 
 #if defined(ENABLE_GC_CACAO)
-       /* Register reference to java.lang.Thread with the GC. */
+               /* Register reference to java.lang.Thread with the GC. */
+               /* FIXME is it ok to do this only once? */
 
-       gc_reference_register((java_object_t **) &(t->object), GC_REFTYPE_THREADOBJECT);
+               gc_reference_register(&(t->object), GC_REFTYPE_THREADOBJECT);
+               gc_reference_register(&(t->_exceptionptr), GC_REFTYPE_THREADOBJECT);
 #endif
 
+               /* Initialize the implementation-specific bits. */
+
+               threads_impl_thread_init(t);
+       }
+
        /* Pre-compute the thinlock-word. */
 
        assert(index != 0);
@@ -321,11 +221,11 @@ threadobject *threads_thread_new(void)
 
        /* Initialize the implementation-specific bits. */
 
-       threads_impl_thread_new(t);
+       threads_impl_thread_reuse(t);
 
-       /* Add the thread to the threads-list. */
+       /* Add the thread to the thread list. */
 
-       list_add_last_unsynced(list_threads, t);
+       threadlist_add(t);
 
        /* Unlock the threads-lists. */
 
@@ -348,45 +248,23 @@ threadobject *threads_thread_new(void)
 
 void threads_thread_free(threadobject *t)
 {
-       thread_index_t *ti;
-
        /* Lock the threads lists. */
 
        threads_list_lock();
 
-       /* Cleanup the implementation specific bits. */
-
-       threads_impl_thread_free(t);
-
-       /* Remove the thread from the threads-list. */
+       /* Remove the thread from the thread-list. */
 
-       list_remove_unsynced(list_threads, t);
+       threadlist_remove(t);
 
        /* Add the thread index to the free list. */
 
-       ti = NEW(thread_index_t);
+       threadlist_index_add(t->index);
 
-#if defined(ENABLE_STATISTICS)
-       if (opt_stat)
-               size_thread_index_t += sizeof(thread_index_t);
-#endif
-
-       ti->index = t->index;
-
-       list_add_last_unsynced(list_free_thread_index, ti);
+       /* Add the thread data structure to the free list. */
 
-       /* Free the thread data structure. */
+       threads_thread_set_object(t, NULL);
 
-#if defined(ENABLE_GC_BOEHM)
-       GCFREE(t);
-#else
-       FREE(t, threadobject);
-#endif
-
-#if defined(ENABLE_STATISTICS)
-       if (opt_stat)
-               size_threadobject -= sizeof(threadobject);
-#endif
+       threadlist_free_add(t);
 
        /* Unlock the threads lists. */
 
@@ -453,7 +331,7 @@ bool threads_thread_start_internal(utf *name, functionptr f)
        LLNI_field_set_val(object, vm_thread, (java_lang_Object *) t);
 #endif
 
-       t->object = LLNI_DIRECT(object);
+       threads_thread_set_object(t, (java_handle_t *) object);
 
        /* set java.lang.Thread fields */
 
@@ -529,7 +407,7 @@ void threads_thread_start(java_handle_t *object)
 
        /* link the two objects together */
 
-       thread->object = LLNI_DIRECT(object);
+       threads_thread_set_object(thread, object);
 
 #if defined(WITH_CLASSPATH_GNU)
        LLNI_field_get_ref(o, vmThread, vmt);
@@ -567,7 +445,7 @@ void threads_thread_print_info(threadobject *t)
 
        /* the thread may be currently in initalization, don't print it */
 
-       object = (java_lang_Thread *) LLNI_WRAP(t->object);
+       object = (java_lang_Thread *) threads_thread_get_object(t);
 
        if (object != NULL) {
                /* get thread name */
@@ -655,6 +533,62 @@ ptrint threads_get_current_tid(void)
 }
 
 
+/* threads_get_current_object **************************************************
+
+   Return the Java object of the current thread.
+   
+   RETURN VALUE:
+       the Java object
+
+*******************************************************************************/
+
+#include "native/include/java_lang_ThreadGroup.h"
+
+java_object_t *threads_get_current_object(void)
+{
+#if defined(ENABLE_THREADS)
+       threadobject  *t;
+# if defined(ENABLE_JAVASE)
+       java_lang_ThreadGroup *group;
+# endif
+#endif
+       java_lang_Thread *o;
+
+#if defined(ENABLE_THREADS)
+       t = THREADOBJECT;
+       o = threads_thread_get_object(t);
+
+# if defined(ENABLE_JAVASE)
+       /* TODO Do we really need this code?  Or should we check, when we
+          create the threads, that all of them have a group? */
+       /* TWISTI No, we don't need this code!  We need to allocate a
+          ThreadGroup before we initialize the main thread. */
+
+       LLNI_field_get_ref(o, group, group);
+
+       if (group == NULL) {
+               /* ThreadGroup of currentThread is not initialized */
+
+               group = (java_lang_ThreadGroup *)
+                       native_new_and_init(class_java_lang_ThreadGroup);
+
+               if (group == NULL)
+                       vm_abort("unable to create ThreadGroup");
+
+               LLNI_field_set_ref(o, group, group);
+       }
+# endif
+#else
+       /* We just return a fake java.lang.Thread object, otherwise we get
+          NullPointerException's in GNU Classpath. */
+
+       o = builtin_new(class_java_lang_Thread);
+#endif
+
+       return o;
+}
+
+
 /* threads_thread_state_runnable ***********************************************
 
    Set the current state of the given thread to THREAD_STATE_RUNNABLE.
@@ -673,6 +607,8 @@ void threads_thread_state_runnable(threadobject *t)
        if (t->state != THREAD_STATE_TERMINATED)
                t->state = THREAD_STATE_RUNNABLE;
 
+       DEBUGTHREADS("is RUNNABLE", t);
+
        threads_list_unlock();
 }
 
@@ -695,6 +631,8 @@ void threads_thread_state_waiting(threadobject *t)
        if (t->state != THREAD_STATE_TERMINATED)
                t->state = THREAD_STATE_WAITING;
 
+       DEBUGTHREADS("is WAITING", t);
+
        threads_list_unlock();
 }
 
@@ -718,6 +656,8 @@ void threads_thread_state_timed_waiting(threadobject *t)
        if (t->state != THREAD_STATE_TERMINATED)
                t->state = THREAD_STATE_TIMED_WAITING;
 
+       DEBUGTHREADS("is TIMED_WAITING", t);
+
        threads_list_unlock();
 }
 
@@ -737,6 +677,8 @@ void threads_thread_state_terminated(threadobject *t)
 
        t->state = THREAD_STATE_TERMINATED;
 
+       DEBUGTHREADS("is TERMINATED", t);
+
        threads_list_unlock();
 }
 
@@ -832,16 +774,35 @@ void threads_dump(void)
 
        /* iterate over all started threads */
 
-       for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
-               /* print thread info */
+       for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
+               /* ignore threads which are in state NEW */
+               if (t->state == THREAD_STATE_NEW)
+                       continue;
+
+#if defined(ENABLE_GC_CACAO)
+               /* Suspend the thread. */
+               /* XXX Is the suspend reason correct? */
+
+               if (threads_suspend_thread(t, SUSPEND_REASON_JNI) == false)
+                       vm_abort("threads_dump: threads_suspend_thread failed");
+#endif
+
+               /* Print thread info. */
 
                printf("\n");
                threads_thread_print_info(t);
                printf("\n");
 
-               /* print trace of thread */
+               /* Print trace of thread. */
 
                threads_thread_print_stacktrace(t);
+
+#if defined(ENABLE_GC_CACAO)
+               /* Resume the thread. */
+
+               if (threads_resume_thread(t) == false)
+                       vm_abort("threads_dump: threads_resume_thread failed");
+#endif
        }
 
        /* unlock the threads lists */
@@ -852,36 +813,39 @@ void threads_dump(void)
 
 /* threads_thread_print_stacktrace *********************************************
 
-   Print the current stacktrace of the current thread.
+   Print the current stacktrace of the given thread.
 
 *******************************************************************************/
 
 void threads_thread_print_stacktrace(threadobject *thread)
 {
-       stackframeinfo   *sfi;
-       stacktracebuffer *stb;
-       s4                dumpsize;
-
-       /* mark start of dump memory area */
+       stackframeinfo_t        *sfi;
+       java_handle_bytearray_t *ba;
+       stacktrace_t            *st;
 
-       dumpsize = dump_size();
-
-       /* create a stacktrace for the passed thread */
+       /* Build a stacktrace for the passed thread. */
 
        sfi = thread->_stackframeinfo;
+       ba  = stacktrace_get(sfi);
+       
+       if (ba != NULL) {
+               /* We need a critical section here as we use the byte-array
+                  data pointer directly. */
 
-       stb = stacktrace_create(sfi);
+               LLNI_CRITICAL_START;
+       
+               st = (stacktrace_t *) LLNI_array_data(ba);
+
+               /* Print stacktrace. */
 
-       /* print stacktrace */
+               stacktrace_print(st);
 
-       if (stb != NULL)
-               stacktrace_print_trace_from_buffer(stb);
+               LLNI_CRITICAL_END;
+       }
        else {
                puts("\t<<No stacktrace available>>");
                fflush(stdout);
        }
-
-       dump_release(dumpsize);
 }