* Removed all Id tags.
[cacao.git] / src / threads / threads-common.c
index 8d8dc04592a89ee76d3c6fe6eba2746c5fc29aea..89515056c98594f327eee75f526386e795cb11c2 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: threads-common.c 7893 2007-05-10 13:27:29Z twisti $
-
 */
 
 
 #include "config.h"
 
 #include <assert.h>
+#include <stdint.h>
+#include <unistd.h>
 
 #include "vm/types.h"
 
+#include "mm/memory.h"
+
 #include "native/jni.h"
+#include "native/llni.h"
 
 #include "native/include/java_lang_Object.h"
 #include "native/include/java_lang_String.h"
@@ -47,6 +50,8 @@
 #include "threads/lock-common.h"
 #include "threads/threads-common.h"
 
+#include "toolbox/list.h"
+
 #include "vm/builtin.h"
 #include "vm/stringlocal.h"
 #include "vm/vm.h"
 
 /* global variables ***********************************************************/
 
-/* global threads table */
-static threads_table_t threads_table;
+/* global threads list */
+static list_t *list_threads;
 
+/* global threads free-list */
+static list_t *list_threads_free;
 
-/* prototypes *****************************************************************/
-
-static void threads_table_init(threadobject *mainthread);
+#if defined(__LINUX__)
+/* XXX Remove for exact-GC. */
+bool threads_pthreads_implementation_nptl;
+#endif
 
 
 /* threads_preinit *************************************************************
@@ -86,6 +94,47 @@ static void threads_table_init(threadobject *mainthread);
 void threads_preinit(void)
 {
        threadobject *mainthread;
+#if defined(__LINUX__) && defined(_CS_GNU_LIBPTHREAD_VERSION)
+       char         *pathbuf;
+       size_t        len;
+#endif
+
+#if defined(__LINUX__)
+       /* XXX Remove for exact-GC. */
+
+       /* On Linux we need to check the pthread implementation. */
+
+       /* _CS_GNU_LIBPTHREAD_VERSION (GNU C library only; since glibc 2.3.2) */
+       /* If the glibc is a pre-2.3.2 version, we fall back to
+          linuxthreads. */
+
+# if defined(_CS_GNU_LIBPTHREAD_VERSION)
+       len = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, (size_t) 0);
+
+       /* Some systems return as length 0 (maybe cross-compilation
+          related).  In this case we also fall back to linuxthreads. */
+
+       if (len > 0) {
+               pathbuf = MNEW(char, len);
+
+               (void) confstr(_CS_GNU_LIBPTHREAD_VERSION, pathbuf, len);
+
+               if (strstr(pathbuf, "NPTL") != NULL)
+                       threads_pthreads_implementation_nptl = true;
+               else
+                       threads_pthreads_implementation_nptl = false;
+       }
+       else
+               threads_pthreads_implementation_nptl = false;
+# else
+       threads_pthreads_implementation_nptl = false;
+# 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). */
@@ -94,11 +143,7 @@ void threads_preinit(void)
 
        /* create internal thread data-structure for the main thread */
 
-       mainthread = threads_create_thread();
-
-       mainthread->object   = NULL;
-       mainthread->index    = 1;
-       mainthread->thinlock = lock_pre_compute_thinlock(mainthread->index);
+       mainthread = threads_thread_new();
 
        /* thread is a Java thread and running */
 
@@ -108,10 +153,6 @@ void threads_preinit(void)
        /* store the internal thread data-structure in the TSD */
 
        threads_set_current_threadobject(mainthread);
-       
-       /* initialize the threads table with the main-thread */
-
-       threads_table_init(mainthread);
 
        /* initialize locking subsystems */
 
@@ -123,569 +164,522 @@ void threads_preinit(void)
 }
 
 
-/* threads_table_init **********************************************************
+/* threads_list_first **********************************************************
 
-   Initialize the global threads table.  We initialize the table with
-   the main-thread, which has always the index 1.
+   Return the first entry in the threads list.
 
-   IN:
-      mainthread....the main-thread
+   NOTE: This function does not lock the lists.
 
 *******************************************************************************/
 
-#define THREADS_INITIAL_TABLE_SIZE    8
-
-static void threads_table_init(threadobject *mainthread)
+threadobject *threads_list_first(void)
 {
-       threads_table_entry_t *ttemain;
-       s4                     size;
-       s4                     i;
-
-       /* initialize the threads table lock */
-
-       threads_impl_table_init();
-
-       /* initialize the table */
+       threadobject *t;
 
-       size = THREADS_INITIAL_TABLE_SIZE;
+       t = list_first_unsynced(list_threads);
 
-       threads_table.table   = MNEW(threads_table_entry_t, size);
-       threads_table.size    = size;
-       threads_table.used    = 0;
-       threads_table.daemons = 0;
-
-       /* Link the entries in a freelist.  Skip 2 entries: 0 is the
-          free-list header and 1 is the main thread. */
-
-       for (i = 2; i < size; i++) {
-               threads_table.table[i].thread = NULL;
-               threads_table.table[i].next   = i + 1;
-       }
+       return t;
+}
 
-       threads_table.table[0].next = 2;
 
-       /* terminate the freelist */
+/* threads_list_next ***********************************************************
 
-       threads_table.table[size - 1].next = 0;          /* index 0 is never free */
+   Return the next entry in the threads list.
 
-       /* insert the main-thread */
+   NOTE: This function does not lock the lists.
 
-       ttemain = &(threads_table.table[1]);
+*******************************************************************************/
 
-       ttemain->thread = mainthread;
-       ttemain->next   = 0;
+threadobject *threads_list_next(threadobject *t)
+{
+       threadobject *next;
 
-       /* now 1 entry is used */
+       next = list_next_unsynced(list_threads, t);
 
-       threads_table.used = 1;
+       return next;
 }
 
 
-/* threads_table_add ***********************************************************
-
-   Add a thread to the global threads table. The index is entered in the
-   threadobject. The thinlock value for the thread is pre-computed.
+/* threads_list_get_non_daemons ************************************************
 
-   IN:
-      thread............the thread to add
+   Return the number of non-daemon threads.
 
-   RETURN VALUE:
-      The table index for the newly added thread. This value has also been
-         entered in the threadobject.
+   NOTE: This function does a linear-search over the threads list,
+         because it's only used for joining the threads.
 
 *******************************************************************************/
 
-s4 threads_table_add(threadobject *thread)
+s4 threads_list_get_non_daemons(void)
 {
-       threads_table_entry_t *ttefree;
-       threads_table_entry_t *ttemain;
-       threads_table_entry_t *tte;
-       s4 index;
-       s4 oldsize;
-       s4 newsize;
-       s4 i;
+       threadobject *t;
+       s4            nondaemons;
 
-       /* lock the threads table */
+       /* lock the threads lists */
 
-       threads_table_lock();
+       threads_list_lock();
 
-       /* get free and main entry */
+       nondaemons = 0;
 
-       ttefree = &(threads_table.table[0]);
-       ttemain = &(threads_table.table[1]);
+       for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
+               if (!(t->flags & THREAD_FLAG_DAEMON))
+                       nondaemons++;
+       }
 
-       /* get the next free index */
+       /* unlock the threads lists */
 
-       index = ttefree->next;
+       threads_list_unlock();
 
-       /* no entry free anymore? resize the table */
+       return nondaemons;
+}
 
-       if (index == 0) {
-               /* we must grow the table */
 
-               oldsize = threads_table.size;
-               newsize = oldsize * 2;
+/* threads_thread_new **********************************************************
 
-               threads_table.table = MREALLOC(threads_table.table,
-                                                                          threads_table_entry_t, oldsize, newsize);
-               threads_table.size = newsize;
+   Allocates and initializes an internal thread data-structure and
+   adds it to the threads list.
 
-               /* the addresses have changed, get them again */
+*******************************************************************************/
 
-               ttefree = &(threads_table.table[0]);
-               ttemain = &(threads_table.table[1]);
+threadobject *threads_thread_new(void)
+{
+       threadobject *t;
 
-               /* link the new entries to a free list */
+       /* lock the threads-lists */
 
-               for (i = oldsize; i < newsize; i++) {
-                       threads_table.table[i].thread = NULL;
-                       threads_table.table[i].next   = i + 1;
-               }
+       threads_list_lock();
 
-               ttefree->next = oldsize;
+       /* try to get a thread from the free-list */
 
-               /* terminate the freelist */
+       t = list_first_unsynced(list_threads_free);
 
-               threads_table.table[newsize - 1].next = 0;   /* index 0 is never free */
+       /* is a free thread available? */
 
-               /* use the first of the new entries */
+       if (t != NULL) {
+               /* yes, remove it from the free list */
 
-               index = ttefree->next;
+               list_remove_unsynced(list_threads_free, t);
        }
+       else {
+               /* no, allocate a new one */
 
-       /* get the entry with the assigned index */
-
-       tte = &(threads_table.table[index]);
+#if defined(ENABLE_GC_BOEHM)
+               t = GCNEW_UNCOLLECTABLE(threadobject, 1);
+#else
+               t = NEW(threadobject);
+#endif
 
-       /* store the next free index into the free-list header */
+#if defined(ENABLE_STATISTICS)
+               if (opt_stat)
+                       size_threadobject += sizeof(threadobject);
+#endif
 
-       ttefree->next = tte->next;
+               /* clear memory */
 
-       /* store the thread in the table */
+               MZERO(t, threadobject, 1);
 
-       tte->thread = thread;
+               /* set the threads-index */
 
-       /* link the new entry into the used-list */
+               t->index = list_threads->size + 1;
+       }
 
-       tte->next     = ttemain->next;
-       ttemain->next = index;
+       /* pre-compute the thinlock-word */
 
-       /* update the counters */
+       assert(t->index != 0);
 
-       threads_table.used++;
+       t->thinlock = lock_pre_compute_thinlock(t->index);
+       t->state    = THREAD_STATE_NEW;
 
-       if (thread->flags & THREAD_FLAG_DAEMON)
-               threads_table.daemons++;
+       /* initialize the implementation-specific bits */
 
-       assert(threads_table.used < threads_table.size);
+       threads_impl_thread_new(t);
 
-       /* set the thread variables */
+       /* add the thread to the threads-list */
 
-       thread->index    = index;
-       thread->thinlock = lock_pre_compute_thinlock(index);
+       list_add_last_unsynced(list_threads, t);
 
-       /* unlock the threads table */
+       /* unlock the threads-lists */
 
-       threads_table_unlock();
+       threads_list_unlock();
 
-       return index;
+       return t;
 }
 
 
-/* threads_table_remove *******************************************************
+/* threads_thread_free *********************************************************
 
-   Remove a thread from the global threads table.
+   Frees an internal thread data-structure by removing it from the
+   threads-list and adding it to the free-list.
 
-   IN:
-      thread............the thread to remove
+   NOTE: The data-structure is NOT freed, the pointer keeps valid!
 
-******************************************************************************/
+*******************************************************************************/
 
-void threads_table_remove(threadobject *thread)
+void threads_thread_free(threadobject *t)
 {
-       threads_table_entry_t *ttefree;
-       threads_table_entry_t *tte;
-       s4                     index;
-       s4                     i;
-
-       /* lock the threads table */
-
-       threads_table_lock();
+       int32_t  index;
+       uint32_t state;
 
-       /* get the free entry */
+       /* lock the threads-lists */
 
-       ttefree = &(threads_table.table[0]);
+       threads_list_lock();
 
-       /* get the current entry */
+       /* cleanup the implementation-specific bits */
 
-       index = thread->index;
-       tte   = &(threads_table.table[index]);
-
-       assert(tte->thread == thread);
-
-       /* Find the entry which has the one to be removed as next entry (I
-          think it's better to do it at the removal in linear time than
-          to have a list or to do it every time we iterate over all
-          threads). */
-
-       for (i = 0; i < threads_table.size; i++) {
-               if (threads_table.table[i].next == index) {
-                       threads_table.table[i].next = tte->next;
-                       break;
-               }
-       }
+       threads_impl_thread_free(t);
 
-       /* clear the thread pointer in the entry */
+       /* remove the thread from the threads-list */
 
-       tte->thread = NULL;
+       list_remove_unsynced(list_threads, t);
 
-       /* this entry is free now, add it to the free-list */
+       /* Clear memory, but keep the thread-index and the
+          thread-state. */
 
-       tte->next     = ttefree->next;
-       ttefree->next = index;
+       /* ATTENTION: Do this after list_remove, otherwise the linkage
+          pointers are invalid. */
 
-       /* update the counters */
+       index = t->index;
+       state = t->state;
 
-       threads_table.used--;
+       MZERO(t, threadobject, 1);
 
-       if (thread->flags & THREAD_FLAG_DAEMON)
-               threads_table.daemons--;
+       t->index = index;
+       t->state = state;
 
-       assert(threads_table.used >= 0);
+       /* add the thread to the free list */
 
-       /* delete the index in the threadobject to discover bugs */
-#if !defined(NDEBUG)
-       thread->index = 0;
-#endif
+       list_add_first_unsynced(list_threads_free, t);
 
-       /* unlock the threads table */
+       /* unlock the threads-lists */
 
-       threads_table_unlock();
+       threads_list_unlock();
 }
 
 
-/* threads_table_get ***********************************************************
-
-   Return the thread of the given table-entry index.
+/* threads_thread_start_internal ***********************************************
 
-   NOTE: It is valid to pass and index of 0, as this entry is the
-         free-list header where the thread pointer is always NULL and
-         this is thre expected behavior.
+   Start an internal thread in the JVM.  No Java thread objects exists
+   so far.
 
-   NOTE: This function does not lock the table.
+   IN:
+      name.......UTF-8 name of the thread
+      f..........function pointer to C function to start
 
 *******************************************************************************/
 
-static threadobject *threads_table_get(s4 index)
+bool threads_thread_start_internal(utf *name, functionptr f)
 {
-       threadobject *thread;
+       threadobject       *t;
+       java_lang_Thread   *object;
+#if defined(WITH_CLASSPATH_GNU)
+       java_lang_VMThread *vmt;
+#endif
 
-       /* get the requested entry */
+       /* Enter the join-mutex, so if the main-thread is currently
+          waiting to join all threads, the number of non-daemon threads
+          is correct. */
 
-       assert((index >= 0) && (index < threads_table.size));
+       threads_mutex_join_lock();
 
-       thread = threads_table.table[index].thread;
+       /* create internal thread data-structure */
 
-       return thread;
-}
+       t = threads_thread_new();
 
+       t->flags = THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
 
-/* threads_table_get_threads ***************************************************
+       /* The thread is flagged as (non-)daemon thread, we can leave the
+          mutex. */
 
-   Return the number of running threads.
+       threads_mutex_join_unlock();
 
-   NOTE: This function does not lock the table.
+       /* create the java thread object */
 
-*******************************************************************************/
+       object = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
 
-s4 threads_table_get_threads(void)
-{
-       return threads_table.used;
-}
+       /* XXX memory leak!!! */
+       if (object == NULL)
+               return false;
 
+#if defined(WITH_CLASSPATH_GNU)
+       vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
 
-/* threads_table_get_non_daemons ***********************************************
+       /* XXX memory leak!!! */
+       if (vmt == NULL)
+               return false;
 
-   Return the number of non-daemon threads.
+       LLNI_field_set_ref(vmt, thread, object);
+       LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) t);
 
-*******************************************************************************/
+       LLNI_field_set_ref(object, vmThread, vmt);
+#elif defined(WITH_CLASSPATH_CLDC1_1)
+       LLNI_field_set_val(object, vm_thread, (java_lang_Object *) t);
+#endif
 
-s4 threads_table_get_non_daemons(void)
-{
-       s4 nondaemons;
+       t->object = object;
 
-       /* lock the threads table */
+       /* set java.lang.Thread fields */
 
-       threads_table_lock();
+#if defined(WITH_CLASSPATH_GNU)
+       LLNI_field_set_ref(object, name    , (java_lang_String *) javastring_new(name));
+#elif defined(WITH_CLASSPATH_CLDC1_1)
+       /* FIXME: In cldc the name is a char[] */
+/*     LLNI_field_set_ref(object, name    , (java_chararray *) javastring_new(name)); */
+       LLNI_field_set_ref(object, name    , NULL);
+#endif
 
-       nondaemons = threads_table.used - threads_table.daemons;
+#if defined(ENABLE_JAVASE)
+       LLNI_field_set_val(object, daemon  , true);
+#endif
 
-       /* unlock the threads table */
+       LLNI_field_set_val(object, priority, NORM_PRIORITY);
 
-       threads_table_unlock();
+       /* start the thread */
 
-       return nondaemons;
-}
+       threads_impl_thread_start(t, f);
 
+       /* everything's ok */
 
-/* threads_table_first *********************************************************
+       return true;
+}
 
-   Return the first thread of the threads table.
 
-   NOTE: This is always the entry with index 1 and must be the main
-         thread.
+/* threads_thread_start ********************************************************
 
-   NOTE: This function does not lock the table.
+   Start a Java thread in the JVM.  Only the java thread object exists
+   so far.
+
+   IN:
+      object.....the java thread object java.lang.Thread
 
 *******************************************************************************/
 
-threadobject *threads_table_first(void)
+void threads_thread_start(java_lang_Thread *object)
 {
        threadobject *thread;
+#if defined(WITH_CLASSPATH_GNU)
+       java_lang_VMThread *vmt;
+#endif
 
-       /* get the requested entry */
+       /* Enter the join-mutex, so if the main-thread is currently
+          waiting to join all threads, the number of non-daemon threads
+          is correct. */
 
-       thread = threads_table_get(1);
+       threads_mutex_join_lock();
 
-       return thread;
-}
+       /* create internal thread data-structure */
 
+       thread = threads_thread_new();
 
-/* threads_table_next **********************************************************
+       /* this is a normal Java thread */
 
-   Return the next thread of the threads table relative to the passed
-   one.
+       thread->flags = THREAD_FLAG_JAVA;
 
-   NOTE: This function does not lock the table.
+#if defined(ENABLE_JAVASE)
+       /* is this a daemon thread? */
 
-*******************************************************************************/
+       if (LLNI_field_direct(object, daemon) == true)
+               thread->flags |= THREAD_FLAG_DAEMON;
+#endif
 
-threadobject *threads_table_next(threadobject *thread)
-{
-       threads_table_entry_t *tte;
-       threadobject          *next;
-       s4                     index;
+       /* The thread is flagged and (non-)daemon thread, we can leave the
+          mutex. */
 
-       index = thread->index;
+       threads_mutex_join_unlock();
 
-       /* get the passed entry */
+       /* link the two objects together */
+
+       thread->object = object;
 
-       assert((index > 0) && (index < threads_table.size));
+#if defined(WITH_CLASSPATH_GNU)
+       LLNI_field_get_ref(object, vmThread, vmt);
 
-       tte = &(threads_table.table[index]);
+       assert(vmt);
+       assert(LLNI_field_direct(vmt, vmdata) == NULL);
 
-       /* get the requested entry */
+       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);
+#endif
 
-       next = threads_table_get(tte->next);
+       /* Start the thread.  Don't pass a function pointer (NULL) since
+          we want Thread.run()V here. */
 
-       return next;
+       threads_impl_thread_start(thread, NULL);
 }
 
 
-/* threads_table_dump *********************************************************
+/* threads_thread_print_info ***************************************************
 
-   Dump the threads table for debugging purposes.
-
-******************************************************************************/
+   Print information of the passed thread.
+   
+*******************************************************************************/
 
-#if !defined(NDEBUG)
-void threads_table_dump(void)
+void threads_thread_print_info(threadobject *t)
 {
-       s4 i;
-       s4 size;
-       ptrint index;
+       java_lang_Thread *object;
+       utf              *name;
 
-       size = threads_table.size;
+       assert(t->state != THREAD_STATE_NEW);
 
-       log_println("threads table ==========");
+       /* the thread may be currently in initalization, don't print it */
 
-       log_println("size:    %d", size);
-       log_println("used:    %d", threads_table.used);
-       log_println("daemons: %d", threads_table.daemons);
+       object = t->object;
 
-       for (i = 0; i < size; i++) {
-               index = threads_table.table[i].next;
+       if (object != NULL) {
+               /* get thread name */
 
-               if (threads_table.table[i].thread != NULL)
-                       log_println("%4d: thread=0x%08x, next=%d", i,
-                                               threads_table.table[i].thread->tid, (int) index);
-               else
-                       log_println("%4d: free, next=%d", i, (int) index);
-       }
-
-       log_println("end of threads table ==========");
-}
+#if defined(WITH_CLASSPATH_GNU)
+               name = javastring_toutf((java_handle_t *) LLNI_field_direct(object, name), false);
+#elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
+               /* FIXME: In cldc the name is a char[] */
+/*             name = object->name; */
+               name = utf_null;
+#else
+# error unknown classpath configuration
 #endif
 
+               printf("\"");
+               utf_display_printable_ascii(name);
+               printf("\"");
 
-/* threads_create_thread *******************************************************
+               if (t->flags & THREAD_FLAG_DAEMON)
+                       printf(" daemon");
 
-   Creates and initializes an internal thread data-structure.
+               printf(" prio=%d", LLNI_field_direct(object, priority));
 
-*******************************************************************************/
-
-threadobject *threads_create_thread(void)
-{
-       threadobject *thread;
-
-       /* allocate internal thread data-structure */
-
-#if defined(ENABLE_GC_BOEHM)
-       thread = GCNEW_UNCOLLECTABLE(threadobject, 1);
+#if SIZEOF_VOID_P == 8
+               printf(" t=0x%016lx tid=0x%016lx (%ld)",
+                          (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
 #else
-       thread = NEW(threadobject);
+               printf(" t=0x%08x tid=0x%08x (%d)",
+                          (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
 #endif
 
-#if defined(ENABLE_STATISTICS)
-       if (opt_stat)
-               size_threadobject += sizeof(threadobject);
-#endif
-
-       /* initialize thread data structure */
+               printf(" index=%d", t->index);
 
-       threads_init_threadobject(thread);
-       lock_init_execution_env(thread);
+               /* print thread state */
 
-       return thread;
+               switch (t->state) {
+               case THREAD_STATE_NEW:
+                       printf(" new");
+                       break;
+               case THREAD_STATE_RUNNABLE:
+                       printf(" runnable");
+                       break;
+               case THREAD_STATE_BLOCKED:
+                       printf(" blocked");
+                       break;
+               case THREAD_STATE_WAITING:
+                       printf(" waiting");
+                       break;
+               case THREAD_STATE_TIMED_WAITING:
+                       printf(" waiting on condition");
+                       break;
+               case THREAD_STATE_TERMINATED:
+                       printf(" terminated");
+                       break;
+               default:
+                       vm_abort("threads_thread_print_info: unknown thread state %d",
+                                        t->state);
+               }
+       }
 }
 
 
-/* threads_thread_start_internal ***********************************************
-
-   Start an internal thread in the JVM.  No Java thread objects exists
-   so far.
+/* threads_get_current_tid *****************************************************
 
-   IN:
-      name.......UTF-8 name of the thread
-      f..........function pointer to C function to start
+   Return the tid of the current thread.
+   
+   RETURN VALUE:
+       the current tid
 
 *******************************************************************************/
 
-bool threads_thread_start_internal(utf *name, functionptr f)
+ptrint threads_get_current_tid(void)
 {
-       threadobject       *thread;
-       java_lang_Thread   *t;
-#if defined(WITH_CLASSPATH_GNU)
-       java_lang_VMThread *vmt;
-#endif
-
-       /* create internal thread data-structure */
-
-       thread = threads_create_thread();
-
-       /* create the java thread object */
-
-       t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
-
-       if (t == NULL)
-               return false;
+       threadobject *thread;
 
-#if defined(WITH_CLASSPATH_GNU)
-       vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
+       thread = THREADOBJECT;
 
-       if (vmt == NULL)
-               return false;
+       /* this may happen during bootstrap */
 
-       vmt->thread = t;
-       vmt->vmdata = (java_lang_Object *) thread;
+       if (thread == NULL)
+               return 0;
 
-       t->vmThread = vmt;
-#elif defined(WITH_CLASSPATH_CLDC1_1)
-       t->vm_thread = (java_lang_Object *) thread;
-#endif
+       return (ptrint) thread->tid;
+}
 
-       thread->object = t;
 
-       thread->flags = THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
+/* threads_thread_state_runnable ***********************************************
 
-       /* set java.lang.Thread fields */
+   Set the current state of the given thread to THREAD_STATE_RUNNABLE.
 
-       t->name     = (java_lang_String *) javastring_new(name);
-#if defined(ENABLE_JAVASE)
-       t->daemon   = true;
-#endif
-       t->priority = NORM_PRIORITY;
+*******************************************************************************/
 
-       /* start the thread */
+void threads_thread_state_runnable(threadobject *t)
+{
+       /* set the state inside the lock */
 
-       threads_impl_thread_start(thread, f);
+       threads_list_lock();
 
-       /* everything's ok */
+       t->state = THREAD_STATE_RUNNABLE;
 
-       return true;
+       threads_list_unlock();
 }
 
 
-/* threads_thread_start ********************************************************
+/* threads_thread_state_waiting ************************************************
 
-   Start a Java thread in the JVM.  Only the java thread object exists
-   so far.
-
-   IN:
-      object.....the java thread object java.lang.Thread
+   Set the current state of the given thread to THREAD_STATE_WAITING.
 
 *******************************************************************************/
 
-void threads_thread_start(java_lang_Thread *object)
+void threads_thread_state_waiting(threadobject *t)
 {
-       threadobject *thread;
-
-       /* create internal thread data-structure */
+       /* set the state in the lock */
 
-       thread = threads_create_thread();
+       threads_list_lock();
 
-       /* link the two objects together */
+       t->state = THREAD_STATE_WAITING;
 
-       thread->object = object;
+       threads_list_unlock();
+}
 
-       /* this is a normal Java thread */
 
-       thread->flags = THREAD_FLAG_JAVA;
+/* threads_thread_state_timed_waiting ******************************************
 
-#if defined(ENABLE_JAVASE)
-       /* is this a daemon thread? */
+   Set the current state of the given thread to
+   THREAD_STATE_TIMED_WAITING.
 
-       if (object->daemon == true)
-               thread->flags |= THREAD_FLAG_DAEMON;
-#endif
+*******************************************************************************/
 
-#if defined(WITH_CLASSPATH_GNU)
-       assert(object->vmThread);
-       assert(object->vmThread->vmdata == NULL);
+void threads_thread_state_timed_waiting(threadobject *t)
+{
+       /* set the state in the lock */
 
-       object->vmThread->vmdata = (java_lang_Object *) thread;
-#elif defined(WITH_CLASSPATH_CLDC1_1)
-       object->vm_thread = (java_lang_Object *) thread;
-#endif
+       threads_list_lock();
 
-       /* Start the thread.  Don't pass a function pointer (NULL) since
-          we want Thread.run()V here. */
+       t->state = THREAD_STATE_TIMED_WAITING;
 
-       threads_impl_thread_start(thread, NULL);
+       threads_list_unlock();
 }
 
 
-/* threads_get_current_tid *****************************************************
+/* threads_thread_state_terminated *********************************************
 
-   Return the tid of the current thread.
-   
-   RETURN VALUE:
-       the current tid
+   Set the current state of the given thread to
+   THREAD_STATE_TERMINATED.
 
 *******************************************************************************/
 
-ptrint threads_get_current_tid(void)
+void threads_thread_state_terminated(threadobject *t)
 {
-       threadobject *thread;
+       /* set the state in the lock */
 
-       thread = THREADOBJECT;
+       threads_list_lock();
 
-       /* this may happen during bootstrap */
+       t->state = THREAD_STATE_TERMINATED;
 
-       if (thread == NULL)
-               return 0;
-
-       return (ptrint) thread->tid;
+       threads_list_unlock();
 }
 
 
@@ -695,11 +689,11 @@ ptrint threads_get_current_tid(void)
 
 *******************************************************************************/
 
-utf *threads_thread_get_state(threadobject *thread)
+utf *threads_thread_get_state(threadobject *t)
 {
        utf *u;
 
-       switch (thread->state) {
+       switch (t->state) {
        case THREAD_STATE_NEW:
                u = utf_new_char("NEW");
                break;
@@ -719,7 +713,11 @@ utf *threads_thread_get_state(threadobject *thread)
                u = utf_new_char("TERMINATED");
                break;
        default:
-               vm_abort("threads_get_state: unknown thread state %d", thread->state);
+               vm_abort("threads_get_state: unknown thread state %d", t->state);
+
+               /* keep compiler happy */
+
+               u = NULL;
        }
 
        return u;
@@ -751,6 +749,10 @@ bool threads_thread_is_alive(threadobject *thread)
 
        default:
                vm_abort("threads_is_alive: unknown thread state %d", thread->state);
+
+               /* keep compiler happy */
+
+               result = false;
        }
 
        return result;
@@ -766,87 +768,33 @@ bool threads_thread_is_alive(threadobject *thread)
 
 void threads_dump(void)
 {
-       threadobject     *t;
-       java_lang_Thread *object;
-       utf              *name;
+       threadobject *t;
 
        /* XXX we should stop the world here */
 
-       /* lock the threads table */
+       /* lock the threads lists */
 
-       threads_table_lock();
+       threads_list_lock();
 
        printf("Full thread dump CACAO "VERSION":\n");
 
        /* iterate over all started threads */
 
-       for (t = threads_table_first(); t != NULL; t = threads_table_next(t)) {
-               /* get thread object */
-
-               object = t->object;
-
-               /* the thread may be currently in initalization, don't print it */
-
-               if (object != NULL) {
-                       /* get thread name */
-
-#if defined(ENABLE_JAVASE)
-                       name = javastring_toutf((java_objectheader *) object->name, false);
-#elif defined(ENABLE_JAVAME_CLDC1_1)
-                       name = object->name;
-#endif
-
-                       printf("\n\"");
-                       utf_display_printable_ascii(name);
-                       printf("\"");
-
-                       if (t->flags & THREAD_FLAG_DAEMON)
-                               printf(" daemon");
+       for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
+               /* print thread info */
 
-                       printf(" prio=%d", object->priority);
+               printf("\n");
+               threads_thread_print_info(t);
+               printf("\n");
 
-#if SIZEOF_VOID_P == 8
-                       printf(" tid=0x%016lx (%ld)", (ptrint) t->tid, (ptrint) t->tid);
-#else
-                       printf(" tid=0x%08x (%d)", (ptrint) t->tid, (ptrint) t->tid);
-#endif
+               /* print trace of thread */
 
-                       /* print thread state */
-
-                       switch (t->state) {
-                       case THREAD_STATE_NEW:
-                               printf(" new");
-                               break;
-                       case THREAD_STATE_RUNNABLE:
-                               printf(" runnable");
-                               break;
-                       case THREAD_STATE_BLOCKED:
-                               printf(" blocked");
-                               break;
-                       case THREAD_STATE_WAITING:
-                               printf(" waiting");
-                               break;
-                       case THREAD_STATE_TIMED_WAITING:
-                               printf(" waiting on condition");
-                               break;
-                       case THREAD_STATE_TERMINATED:
-                               printf(" terminated");
-                               break;
-                       default:
-                               vm_abort("threads_dump: unknown thread state %d", t->state);
-                       }
-
-                       printf("\n");
-
-                       /* print trace of thread */
-
-                       threads_thread_print_stacktrace(t);
-               }
+               threads_thread_print_stacktrace(t);
        }
 
-       /* unlock the threads table */
+       /* unlock the threads lists */
 
-       threads_table_unlock();
+       threads_list_unlock();
 }