Oh no, it worketh!
[cacao.git] / src / threads / threads-common.c
index 33691910e3b2aeccf89e96aae7fd64b2dc8dff86..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 */
-static list_t *list_threads_free;
-
 #if defined(__LINUX__)
 /* XXX Remove for exact-GC. */
 bool threads_pthreads_implementation_nptl;
@@ -86,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)
@@ -99,6 +90,8 @@ void threads_preinit(void)
        size_t        len;
 #endif
 
+       TRACESUBSYSTEMINITIALIZATION("threads_preinit");
+
 #if defined(__LINUX__)
        /* XXX Remove for exact-GC. */
 
@@ -131,11 +124,6 @@ void threads_preinit(void)
 # endif
 #endif
 
-       /* initialize the threads lists */
-
-       list_threads      = list_create(OFFSET(threadobject, linkage));
-       list_threads_free = list_create(OFFSET(threadobject, linkage));
-
        /* Initialize the threads implementation (sets the thinlock on the
           main thread). */
 
@@ -147,89 +135,12 @@ void threads_preinit(void)
 
        /* thread is a Java thread and running */
 
-       mainthread->flags = THREAD_FLAG_JAVA;
+       mainthread->flags |= THREAD_FLAG_JAVA;
        mainthread->state = THREAD_STATE_RUNNABLE;
 
        /* 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;
 }
 
 
@@ -242,26 +153,31 @@ s4 threads_list_get_non_daemons(void)
 
 threadobject *threads_thread_new(void)
 {
-       threadobject *t;
-
+       int32_t         index;
+       threadobject   *t;
+       
        /* lock the threads-lists */
 
        threads_list_lock();
 
-       /* try to get a thread from the free-list */
+       index = threadlist_get_free_index();
+
+       /* Allocate a thread data structure. */
 
-       t = list_first_unsynced(list_threads_free);
+       /* First, try to get one from the free-list. */
 
-       /* is a free thread available? */
+       t = threadlist_free_first();
 
        if (t != NULL) {
-               /* yes, remove it from the free list */
+               /* Remove from free list. */
 
-               list_remove_unsynced(list_threads_free, t);
+               threadlist_free_remove(t);
+
+               /* Equivalent of MZERO on the else path */
+
+               threads_impl_thread_clear(t);
        }
        else {
-               /* no, allocate a new one */
-
 #if defined(ENABLE_GC_BOEHM)
                t = GCNEW_UNCOLLECTABLE(threadobject, 1);
 #else
@@ -273,42 +189,45 @@ threadobject *threads_thread_new(void)
                        size_threadobject += sizeof(threadobject);
 #endif
 
-               /* clear memory */
+               /* Clear memory. */
 
                MZERO(t, threadobject, 1);
 
-               /* set the threads-index */
-
-               t->index = list_threads->size + 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 */
+       /* Pre-compute the thinlock-word. */
 
-       assert(t->index != 0);
+       assert(index != 0);
 
-       t->thinlock = lock_pre_compute_thinlock(t->index);
-       t->flags    = 0;
-       t->state    = THREAD_STATE_NEW;
+       t->index     = index;
+       t->thinlock  = lock_pre_compute_thinlock(t->index);
+       t->flags     = 0;
+       t->state     = THREAD_STATE_NEW;
 
 #if defined(ENABLE_GC_CACAO)
-       t->flags |= THREAD_FLAG_IN_NATIVE; 
+       t->flags    |= THREAD_FLAG_IN_NATIVE; 
 #endif
 
-       /* initialize the implementation-specific bits */
+       /* 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 */
+       /* Unlock the threads-lists. */
 
        threads_list_unlock();
 
@@ -318,49 +237,36 @@ threadobject *threads_thread_new(void)
 
 /* threads_thread_free *********************************************************
 
-   Frees an internal thread data-structure by removing it from the
-   threads-list and adding it to the free-list.
+   Remove the thread from the threads-list and free the internal
+   thread data structure.  The thread index is added to the
+   thread-index free-list.
 
-   NOTE: The data-structure is NOT freed, the pointer keeps valid!
+   IN:
+       t....thread data structure
 
 *******************************************************************************/
 
 void threads_thread_free(threadobject *t)
 {
-       int32_t  index;
-       uint32_t state;
-
-       /* lock the threads-lists */
+       /* Lock the threads lists. */
 
        threads_list_lock();
 
-       /* cleanup the implementation-specific bits */
-
-       threads_impl_thread_free(t);
-
-       /* remove the thread from the threads-list */
-
-       list_remove_unsynced(list_threads, t);
+       /* Remove the thread from the thread-list. */
 
-       /* Clear memory, but keep the thread-index and the
-          thread-state. */
+       threadlist_remove(t);
 
-       /* ATTENTION: Do this after list_remove, otherwise the linkage
-          pointers are invalid. */
+       /* Add the thread index to the free list. */
 
-       index = t->index;
-       state = t->state;
+       threadlist_index_add(t->index);
 
-       MZERO(t, threadobject, 1);
+       /* Add the thread data structure to the free list. */
 
-       t->index = index;
-       t->state = state;
+       threads_thread_set_object(t, NULL);
 
-       /* add the thread to the free list */
+       threadlist_free_add(t);
 
-       list_add_first_unsynced(list_threads_free, t);
-
-       /* unlock the threads-lists */
+       /* Unlock the threads lists. */
 
        threads_list_unlock();
 }
@@ -425,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 = object;
+       threads_thread_set_object(t, (java_handle_t *) object);
 
        /* set java.lang.Thread fields */
 
@@ -463,13 +369,16 @@ bool threads_thread_start_internal(utf *name, functionptr f)
 
 *******************************************************************************/
 
-void threads_thread_start(java_lang_Thread *object)
+void threads_thread_start(java_handle_t *object)
 {
-       threadobject *thread;
+       java_lang_Thread   *o;
+       threadobject       *thread;
 #if defined(WITH_CLASSPATH_GNU)
        java_lang_VMThread *vmt;
 #endif
 
+       o = (java_lang_Thread *) object;
+
        /* Enter the join-mutex, so if the main-thread is currently
           waiting to join all threads, the number of non-daemon threads
           is correct. */
@@ -487,7 +396,7 @@ void threads_thread_start(java_lang_Thread *object)
 #if defined(ENABLE_JAVASE)
        /* is this a daemon thread? */
 
-       if (LLNI_field_direct(object, daemon) == true)
+       if (LLNI_field_direct(o, daemon) == true)
                thread->flags |= THREAD_FLAG_DAEMON;
 #endif
 
@@ -498,17 +407,17 @@ void threads_thread_start(java_lang_Thread *object)
 
        /* link the two objects together */
 
-       thread->object = object;
+       threads_thread_set_object(thread, object);
 
 #if defined(WITH_CLASSPATH_GNU)
-       LLNI_field_get_ref(object, vmThread, vmt);
+       LLNI_field_get_ref(o, vmThread, vmt);
 
        assert(vmt);
        assert(LLNI_field_direct(vmt, vmdata) == NULL);
 
        LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) thread);
 #elif defined(WITH_CLASSPATH_CLDC1_1)
-       LLNI_field_set_val(object, vm_thread, (java_lang_Object *) thread);
+       LLNI_field_set_val(o, vm_thread, (java_lang_Object *) thread);
 #endif
 
        /* Start the thread.  Don't pass a function pointer (NULL) since
@@ -536,7 +445,7 @@ void threads_thread_print_info(threadobject *t)
 
        /* the thread may be currently in initalization, don't print it */
 
-       object = t->object;
+       object = (java_lang_Thread *) threads_thread_get_object(t);
 
        if (object != NULL) {
                /* get thread name */
@@ -624,19 +533,81 @@ 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.
 
+   NOTE: If the thread has already terminated, don't set the state.
+         This is important for threads_detach_thread.
+
 *******************************************************************************/
 
 void threads_thread_state_runnable(threadobject *t)
 {
-       /* set the state inside the lock */
+       /* Set the state inside a lock. */
 
        threads_list_lock();
 
-       t->state = THREAD_STATE_RUNNABLE;
+       if (t->state != THREAD_STATE_TERMINATED)
+               t->state = THREAD_STATE_RUNNABLE;
+
+       DEBUGTHREADS("is RUNNABLE", t);
 
        threads_list_unlock();
 }
@@ -646,15 +617,21 @@ void threads_thread_state_runnable(threadobject *t)
 
    Set the current state of the given thread to THREAD_STATE_WAITING.
 
+   NOTE: If the thread has already terminated, don't set the state.
+         This is important for threads_detach_thread.
+
 *******************************************************************************/
 
 void threads_thread_state_waiting(threadobject *t)
 {
-       /* set the state in the lock */
+       /* Set the state inside a lock. */
 
        threads_list_lock();
 
-       t->state = THREAD_STATE_WAITING;
+       if (t->state != THREAD_STATE_TERMINATED)
+               t->state = THREAD_STATE_WAITING;
+
+       DEBUGTHREADS("is WAITING", t);
 
        threads_list_unlock();
 }
@@ -665,15 +642,21 @@ void threads_thread_state_waiting(threadobject *t)
    Set the current state of the given thread to
    THREAD_STATE_TIMED_WAITING.
 
+   NOTE: If the thread has already terminated, don't set the state.
+         This is important for threads_detach_thread.
+
 *******************************************************************************/
 
 void threads_thread_state_timed_waiting(threadobject *t)
 {
-       /* set the state in the lock */
+       /* Set the state inside a lock. */
 
        threads_list_lock();
 
-       t->state = THREAD_STATE_TIMED_WAITING;
+       if (t->state != THREAD_STATE_TERMINATED)
+               t->state = THREAD_STATE_TIMED_WAITING;
+
+       DEBUGTHREADS("is TIMED_WAITING", t);
 
        threads_list_unlock();
 }
@@ -694,6 +677,8 @@ void threads_thread_state_terminated(threadobject *t)
 
        t->state = THREAD_STATE_TERMINATED;
 
+       DEBUGTHREADS("is TERMINATED", t);
+
        threads_list_unlock();
 }
 
@@ -745,32 +730,26 @@ utf *threads_thread_get_state(threadobject *t)
 
 *******************************************************************************/
 
-bool threads_thread_is_alive(threadobject *thread)
+bool threads_thread_is_alive(threadobject *t)
 {
-       bool result;
-
-       switch (thread->state) {
+       switch (t->state) {
        case THREAD_STATE_NEW:
        case THREAD_STATE_TERMINATED:
-               result = false;
-               break;
+               return false;
 
        case THREAD_STATE_RUNNABLE:
        case THREAD_STATE_BLOCKED:
        case THREAD_STATE_WAITING:
        case THREAD_STATE_TIMED_WAITING:
-               result = true;
-               break;
+               return true;
 
        default:
-               vm_abort("threads_is_alive: unknown thread state %d", thread->state);
-
-               /* keep compiler happy */
-
-               result = false;
+               vm_abort("threads_thread_is_alive: unknown thread state %d", t->state);
        }
 
-       return result;
+       /* keep compiler happy */
+
+       return false;
 }
 
 
@@ -795,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 */
@@ -815,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. */
+
+               LLNI_CRITICAL_START;
+       
+               st = (stacktrace_t *) LLNI_array_data(ba);
 
-       stb = stacktrace_create(sfi);
+               /* 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);
 }