Oh no, it worketh!
[cacao.git] / src / threads / threads-common.c
index 034db137f6db746007dcff3fe56a228a8e372522..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.
 
@@ -49,6 +47,7 @@
 
 #include "threads/critical.h"
 #include "threads/lock-common.h"
+#include "threads/threadlist.h"
 #include "threads/threads-common.h"
 
 #include "toolbox/list.h"
 
 /* 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;
@@ -93,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)
@@ -106,6 +90,8 @@ void threads_preinit(void)
        size_t        len;
 #endif
 
+       TRACESUBSYSTEMINITIALIZATION("threads_preinit");
+
 #if defined(__LINUX__)
        /* XXX Remove for exact-GC. */
 
@@ -138,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). */
 
@@ -160,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;
 }
 
 
@@ -249,7 +153,6 @@ s4 threads_list_get_non_daemons(void)
 
 threadobject *threads_thread_new(void)
 {
-       thread_index_t *ti;
        int32_t         index;
        threadobject   *t;
        
@@ -257,57 +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(&(t->object), GC_REFTYPE_THREADOBJECT);
-       gc_reference_register(&(t->_exceptionptr), 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);
@@ -323,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. */
 
@@ -350,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 thread-list. */
 
-       /* Remove the thread from the threads-list. */
-
-       list_remove_unsynced(list_threads, t);
+       threadlist_remove(t);
 
        /* Add the thread index to the free list. */
 
-       ti = NEW(thread_index_t);
-
-#if defined(ENABLE_STATISTICS)
-       if (opt_stat)
-               size_thread_index_t += sizeof(thread_index_t);
-#endif
+       threadlist_index_add(t->index);
 
-       ti->index = t->index;
+       /* Add the thread data structure to the free list. */
 
-       list_add_last_unsynced(list_free_thread_index, ti);
+       threads_thread_set_object(t, NULL);
 
-       /* Free the thread data structure. */
-
-#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. */
 
@@ -657,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.
@@ -842,20 +774,35 @@ void threads_dump(void)
 
        /* iterate over all started threads */
 
-       for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
+       for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
                /* ignore threads which are in state NEW */
                if (t->state == THREAD_STATE_NEW)
                        continue;
 
-               /* print thread info */
+#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 */
@@ -866,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_t *sfi;
-       stacktracebuffer *stb;
-       s4                dumpsize;
+       stackframeinfo_t        *sfi;
+       java_handle_bytearray_t *ba;
+       stacktrace_t            *st;
 
-       /* mark start of dump memory area */
-
-       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. */
 
-       if (stb != NULL)
-               stacktrace_print_trace_from_buffer(stb);
+               stacktrace_print(st);
+
+               LLNI_CRITICAL_END;
+       }
        else {
                puts("\t<<No stacktrace available>>");
                fflush(stdout);
        }
-
-       dump_release(dumpsize);
 }