* Removed all Id tags.
[cacao.git] / src / threads / threads-common.c
index a30e3ad5c188ee2a3523f28c63a898f9cd3130c8..89515056c98594f327eee75f526386e795cb11c2 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: threads-common.c 7914 2007-05-18 14:17:34Z 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). */
@@ -96,10 +145,6 @@ void threads_preinit(void)
 
        mainthread = threads_thread_new();
 
-       mainthread->object   = NULL;
-       mainthread->index    = 1;
-       mainthread->thinlock = lock_pre_compute_thinlock(mainthread->index);
-
        /* thread is a Java thread and running */
 
        mainthread->flags = THREAD_FLAG_JAVA;
@@ -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,459 +164,194 @@ void threads_preinit(void)
 }
 
 
-/* threads_table_init **********************************************************
-
-   Initialize the global threads table.  We initialize the table with
-   the main-thread, which has always the index 1.
-
-   IN:
-      mainthread....the main-thread
-
-*******************************************************************************/
-
-#define THREADS_INITIAL_TABLE_SIZE    8
-
-static void threads_table_init(threadobject *mainthread)
-{
-       threads_table_entry_t *ttemain;
-       s4                     size;
-       s4                     i;
-
-       /* initialize the threads table lock */
-
-       threads_impl_table_init();
-
-       /* initialize the table */
-
-       size = THREADS_INITIAL_TABLE_SIZE;
-
-       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;
-       }
-
-       threads_table.table[0].next = 2;
-
-       /* terminate the freelist */
-
-       threads_table.table[size - 1].next = 0;          /* index 0 is never free */
-
-       /* insert the main-thread */
-
-       ttemain = &(threads_table.table[1]);
-
-       ttemain->thread = mainthread;
-       ttemain->next   = 0;
-
-       /* now 1 entry is used */
-
-       threads_table.used = 1;
-}
-
-
-/* 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_first **********************************************************
 
-   IN:
-      thread............the thread to add
+   Return the first entry in the threads list.
 
-   RETURN VALUE:
-      The table index for the newly added thread. This value has also been
-         entered in the threadobject.
+   NOTE: This function does not lock the lists.
 
 *******************************************************************************/
 
-s4 threads_table_add(threadobject *thread)
-{
-       threads_table_entry_t *ttefree;
-       threads_table_entry_t *ttemain;
-       threads_table_entry_t *tte;
-       s4 index;
-       s4 oldsize;
-       s4 newsize;
-       s4 i;
-
-       /* lock the threads table */
-
-       threads_table_lock();
-
-       /* get free and main entry */
-
-       ttefree = &(threads_table.table[0]);
-       ttemain = &(threads_table.table[1]);
-
-       /* get the next free index */
-
-       index = ttefree->next;
-
-       /* no entry free anymore? resize the table */
-
-       if (index == 0) {
-               /* we must grow the table */
-
-               oldsize = threads_table.size;
-               newsize = oldsize * 2;
-
-               threads_table.table = MREALLOC(threads_table.table,
-                                                                          threads_table_entry_t, oldsize, newsize);
-               threads_table.size = newsize;
-
-               /* the addresses have changed, get them again */
-
-               ttefree = &(threads_table.table[0]);
-               ttemain = &(threads_table.table[1]);
-
-               /* link the new entries to a free list */
-
-               for (i = oldsize; i < newsize; i++) {
-                       threads_table.table[i].thread = NULL;
-                       threads_table.table[i].next   = i + 1;
-               }
-
-               ttefree->next = oldsize;
-
-               /* terminate the freelist */
-
-               threads_table.table[newsize - 1].next = 0;   /* index 0 is never free */
-
-               /* use the first of the new entries */
-
-               index = ttefree->next;
-       }
-
-       /* get the entry with the assigned index */
-
-       tte = &(threads_table.table[index]);
-
-       /* store the next free index into the free-list header */
-
-       ttefree->next = tte->next;
-
-       /* store the thread in the table */
-
-       tte->thread = thread;
-
-       /* link the new entry into the used-list */
-
-       tte->next     = ttemain->next;
-       ttemain->next = index;
-
-       /* update the counters */
-
-       threads_table.used++;
-
-       if (thread->flags & THREAD_FLAG_DAEMON)
-               threads_table.daemons++;
-
-       assert(threads_table.used < threads_table.size);
-
-       /* set the thread variables */
-
-       thread->index    = index;
-       thread->thinlock = lock_pre_compute_thinlock(index);
-
-       /* unlock the threads table */
-
-       threads_table_unlock();
-
-       return index;
-}
-
-
-/* threads_table_remove *******************************************************
-
-   Remove a thread from the global threads table.
-
-   IN:
-      thread............the thread to remove
-
-******************************************************************************/
-
-void threads_table_remove(threadobject *thread)
+threadobject *threads_list_first(void)
 {
-       threads_table_entry_t *ttefree;
-       threads_table_entry_t *tte;
-       s4                     index;
-       s4                     i;
-
-       /* lock the threads table */
-
-       threads_table_lock();
-
-       /* get the free entry */
-
-       ttefree = &(threads_table.table[0]);
-
-       /* get the current entry */
-
-       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;
-               }
-       }
-
-       /* clear the thread pointer in the entry */
-
-       tte->thread = NULL;
-
-       /* this entry is free now, add it to the free-list */
-
-       tte->next     = ttefree->next;
-       ttefree->next = index;
-
-       /* update the counters */
-
-       threads_table.used--;
-
-       if (thread->flags & THREAD_FLAG_DAEMON)
-               threads_table.daemons--;
-
-       assert(threads_table.used >= 0);
-
-       /* delete the index in the threadobject to discover bugs */
-#if !defined(NDEBUG)
-       thread->index = 0;
-#endif
+       threadobject *t;
 
-       /* unlock the threads table */
+       t = list_first_unsynced(list_threads);
 
-       threads_table_unlock();
+       return t;
 }
 
 
-/* threads_table_get ***********************************************************
+/* threads_list_next ***********************************************************
 
-   Return the thread of the given table-entry index.
+   Return the next entry in the threads list.
 
-   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.
-
-   NOTE: This function does not lock the table.
+   NOTE: This function does not lock the lists.
 
 *******************************************************************************/
 
-static threadobject *threads_table_get(s4 index)
+threadobject *threads_list_next(threadobject *t)
 {
-       threadobject *thread;
+       threadobject *next;
 
-       /* get the requested entry */
+       next = list_next_unsynced(list_threads, t);
 
-       assert((index >= 0) && (index < threads_table.size));
-
-       thread = threads_table.table[index].thread;
-
-       return thread;
+       return next;
 }
 
 
-/* threads_table_get_threads ***************************************************
+/* threads_list_get_non_daemons ************************************************
 
-   Return the number of running threads.
+   Return the number of non-daemon threads.
 
-   NOTE: This function does not lock the table.
+   NOTE: This function does a linear-search over the threads list,
+         because it's only used for joining the threads.
 
 *******************************************************************************/
 
-s4 threads_table_get_threads(void)
+s4 threads_list_get_non_daemons(void)
 {
-       return threads_table.used;
-}
-
-
-/* threads_table_get_non_daemons ***********************************************
-
-   Return the number of non-daemon threads.
-
-*******************************************************************************/
+       threadobject *t;
+       s4            nondaemons;
 
-s4 threads_table_get_non_daemons(void)
-{
-       s4 nondaemons;
+       /* lock the threads lists */
 
-       /* lock the threads table */
+       threads_list_lock();
 
-       threads_table_lock();
+       nondaemons = 0;
 
-       nondaemons = threads_table.used - threads_table.daemons;
+       for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
+               if (!(t->flags & THREAD_FLAG_DAEMON))
+                       nondaemons++;
+       }
 
-       /* unlock the threads table */
+       /* unlock the threads lists */
 
-       threads_table_unlock();
+       threads_list_unlock();
 
        return nondaemons;
 }
 
 
-/* threads_table_first *********************************************************
-
-   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_new **********************************************************
 
-   NOTE: This function does not lock the table.
+   Allocates and initializes an internal thread data-structure and
+   adds it to the threads list.
 
 *******************************************************************************/
 
-threadobject *threads_table_first(void)
+threadobject *threads_thread_new(void)
 {
-       threadobject *thread;
-
-       /* get the requested entry */
-
-       thread = threads_table_get(1);
-
-       return thread;
-}
-
+       threadobject *t;
 
-/* threads_table_next **********************************************************
+       /* lock the threads-lists */
 
-   Return the next thread of the threads table relative to the passed
-   one.
+       threads_list_lock();
 
-   NOTE: This function does not lock the table.
+       /* try to get a thread from the free-list */
 
-*******************************************************************************/
+       t = list_first_unsynced(list_threads_free);
 
-threadobject *threads_table_next(threadobject *thread)
-{
-       threads_table_entry_t *tte;
-       threadobject          *next;
-       s4                     index;
+       /* is a free thread available? */
 
-       index = thread->index;
+       if (t != NULL) {
+               /* yes, remove it from the free list */
 
-       /* get the passed entry */
+               list_remove_unsynced(list_threads_free, t);
+       }
+       else {
+               /* no, allocate a new one */
 
-       assert((index > 0) && (index < threads_table.size));
+#if defined(ENABLE_GC_BOEHM)
+               t = GCNEW_UNCOLLECTABLE(threadobject, 1);
+#else
+               t = NEW(threadobject);
+#endif
 
-       tte = &(threads_table.table[index]);
+#if defined(ENABLE_STATISTICS)
+               if (opt_stat)
+                       size_threadobject += sizeof(threadobject);
+#endif
 
-       /* get the requested entry */
+               /* clear memory */
 
-       next = threads_table_get(tte->next);
+               MZERO(t, threadobject, 1);
 
-       return next;
-}
+               /* set the threads-index */
 
+               t->index = list_threads->size + 1;
+       }
 
-/* threads_table_dump *********************************************************
+       /* pre-compute the thinlock-word */
 
-   Dump the threads table for debugging purposes.
+       assert(t->index != 0);
 
-******************************************************************************/
+       t->thinlock = lock_pre_compute_thinlock(t->index);
+       t->state    = THREAD_STATE_NEW;
 
-#if !defined(NDEBUG)
-void threads_table_dump(void)
-{
-       s4 i;
-       s4 size;
-       ptrint index;
+       /* initialize the implementation-specific bits */
 
-       size = threads_table.size;
+       threads_impl_thread_new(t);
 
-       log_println("threads table ==========");
+       /* add the thread to the threads-list */
 
-       log_println("size:    %d", size);
-       log_println("used:    %d", threads_table.used);
-       log_println("daemons: %d", threads_table.daemons);
+       list_add_last_unsynced(list_threads, t);
 
-       for (i = 0; i < size; i++) {
-               index = threads_table.table[i].next;
+       /* unlock the threads-lists */
 
-               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);
-       }
+       threads_list_unlock();
 
-       log_println("end of threads table ==========");
+       return t;
 }
-#endif
 
 
-/* threads_thread_new **********************************************************
+/* threads_thread_free *********************************************************
+
+   Frees an internal thread data-structure by removing it from the
+   threads-list and adding it to the free-list.
 
-   Allocates and initializes an internal thread data-structure.
+   NOTE: The data-structure is NOT freed, the pointer keeps valid!
 
 *******************************************************************************/
 
-threadobject *threads_thread_new(void)
+void threads_thread_free(threadobject *t)
 {
-       threadobject *t;
+       int32_t  index;
+       uint32_t state;
 
-       /* allocate internal thread data-structure */
+       /* lock the threads-lists */
 
-#if defined(ENABLE_GC_BOEHM)
-       t = GCNEW_UNCOLLECTABLE(threadobject, 1);
-#else
-       t = NEW(threadobject);
-#endif
+       threads_list_lock();
 
-#if defined(ENABLE_STATISTICS)
-       if (opt_stat)
-               size_threadobject += sizeof(threadobject);
-#endif
+       /* cleanup the implementation-specific bits */
 
-       /* initialize thread data structure */
+       threads_impl_thread_free(t);
 
-       t->index       = 0;
-       t->interrupted = false;
-       t->signaled    = false;
-       t->sleeping    = false;
+       /* remove the thread from the threads-list */
 
-       threads_impl_thread_new(t);
+       list_remove_unsynced(list_threads, t);
 
-       return t;
-}
+       /* Clear memory, but keep the thread-index and the
+          thread-state. */
 
+       /* ATTENTION: Do this after list_remove, otherwise the linkage
+          pointers are invalid. */
 
-/* threads_thread_free *********************************************************
+       index = t->index;
+       state = t->state;
 
-   Frees an internal thread data-structure.
+       MZERO(t, threadobject, 1);
 
-*******************************************************************************/
+       t->index = index;
+       t->state = state;
 
-void threads_thread_free(threadobject *t)
-{
-       /* cleanup the implementation-specific bits */
+       /* add the thread to the free list */
 
-       threads_impl_thread_free(t);
+       list_add_first_unsynced(list_threads_free, t);
 
-#if defined(ENABLE_GC_BOEHM)
-       GCFREE(t);
-#else
-       FREE(t, threadobject);
-#endif
+       /* unlock the threads-lists */
 
-#if defined(ENABLE_STATISTICS)
-       if (opt_stat)
-               size_threadobject -= sizeof(threadobject);
-#endif
+       threads_list_unlock();
 }
 
 
@@ -592,52 +368,73 @@ void threads_thread_free(threadobject *t)
 
 bool threads_thread_start_internal(utf *name, functionptr f)
 {
-       threadobject       *thread;
-       java_lang_Thread   *t;
+       threadobject       *t;
+       java_lang_Thread   *object;
 #if defined(WITH_CLASSPATH_GNU)
        java_lang_VMThread *vmt;
 #endif
 
+       /* Enter the join-mutex, so if the main-thread is currently
+          waiting to join all threads, the number of non-daemon threads
+          is correct. */
+
+       threads_mutex_join_lock();
+
        /* create internal thread data-structure */
 
-       thread = threads_thread_new();
+       t = threads_thread_new();
+
+       t->flags = THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
+
+       /* The thread is flagged as (non-)daemon thread, we can leave the
+          mutex. */
+
+       threads_mutex_join_unlock();
 
        /* create the java thread object */
 
-       t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
+       object = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
 
-       if (t == NULL)
+       /* XXX memory leak!!! */
+       if (object == NULL)
                return false;
 
 #if defined(WITH_CLASSPATH_GNU)
        vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
 
+       /* XXX memory leak!!! */
        if (vmt == NULL)
                return false;
 
-       vmt->thread = t;
-       vmt->vmdata = (java_lang_Object *) thread;
+       LLNI_field_set_ref(vmt, thread, object);
+       LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) t);
 
-       t->vmThread = vmt;
+       LLNI_field_set_ref(object, vmThread, vmt);
 #elif defined(WITH_CLASSPATH_CLDC1_1)
-       t->vm_thread = (java_lang_Object *) thread;
+       LLNI_field_set_val(object, vm_thread, (java_lang_Object *) t);
 #endif
 
-       thread->object = t;
-
-       thread->flags = THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
+       t->object = object;
 
        /* set java.lang.Thread fields */
 
-       t->name     = (java_lang_String *) javastring_new(name);
+#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
+
 #if defined(ENABLE_JAVASE)
-       t->daemon   = true;
+       LLNI_field_set_val(object, daemon  , true);
 #endif
-       t->priority = NORM_PRIORITY;
+
+       LLNI_field_set_val(object, priority, NORM_PRIORITY);
 
        /* start the thread */
 
-       threads_impl_thread_start(thread, f);
+       threads_impl_thread_start(t, f);
 
        /* everything's ok */
 
@@ -658,14 +455,19 @@ bool threads_thread_start_internal(utf *name, functionptr f)
 void threads_thread_start(java_lang_Thread *object)
 {
        threadobject *thread;
+#if defined(WITH_CLASSPATH_GNU)
+       java_lang_VMThread *vmt;
+#endif
 
-       /* create internal thread data-structure */
+       /* 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_thread_new();
+       threads_mutex_join_lock();
 
-       /* link the two objects together */
+       /* create internal thread data-structure */
 
-       thread->object = object;
+       thread = threads_thread_new();
 
        /* this is a normal Java thread */
 
@@ -674,17 +476,28 @@ void threads_thread_start(java_lang_Thread *object)
 #if defined(ENABLE_JAVASE)
        /* is this a daemon thread? */
 
-       if (object->daemon == true)
+       if (LLNI_field_direct(object, daemon) == true)
                thread->flags |= THREAD_FLAG_DAEMON;
 #endif
 
+       /* The thread is flagged and (non-)daemon thread, we can leave the
+          mutex. */
+
+       threads_mutex_join_unlock();
+
+       /* link the two objects together */
+
+       thread->object = object;
+
 #if defined(WITH_CLASSPATH_GNU)
-       assert(object->vmThread);
-       assert(object->vmThread->vmdata == NULL);
+       LLNI_field_get_ref(object, vmThread, vmt);
 
-       object->vmThread->vmdata = (java_lang_Object *) thread;
+       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)
-       object->vm_thread = (java_lang_Object *) thread;
+       LLNI_field_set_val(object, vm_thread, (java_lang_Object *) thread);
 #endif
 
        /* Start the thread.  Don't pass a function pointer (NULL) since
@@ -705,6 +518,8 @@ void threads_thread_print_info(threadobject *t)
        java_lang_Thread *object;
        utf              *name;
 
+       assert(t->state != THREAD_STATE_NEW);
+
        /* the thread may be currently in initalization, don't print it */
 
        object = t->object;
@@ -712,10 +527,14 @@ void threads_thread_print_info(threadobject *t)
        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;
+#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("\"");
@@ -725,7 +544,7 @@ void threads_thread_print_info(threadobject *t)
                if (t->flags & THREAD_FLAG_DAEMON)
                        printf(" daemon");
 
-               printf(" prio=%d", object->priority);
+               printf(" prio=%d", LLNI_field_direct(object, priority));
 
 #if SIZEOF_VOID_P == 8
                printf(" t=0x%016lx tid=0x%016lx (%ld)",
@@ -735,6 +554,8 @@ void threads_thread_print_info(threadobject *t)
                           (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
 #endif
 
+               printf(" index=%d", t->index);
+
                /* print thread state */
 
                switch (t->state) {
@@ -788,17 +609,91 @@ ptrint threads_get_current_tid(void)
 }
 
 
+/* threads_thread_state_runnable ***********************************************
+
+   Set the current state of the given thread to THREAD_STATE_RUNNABLE.
+
+*******************************************************************************/
+
+void threads_thread_state_runnable(threadobject *t)
+{
+       /* set the state inside the lock */
+
+       threads_list_lock();
+
+       t->state = THREAD_STATE_RUNNABLE;
+
+       threads_list_unlock();
+}
+
+
+/* threads_thread_state_waiting ************************************************
+
+   Set the current state of the given thread to THREAD_STATE_WAITING.
+
+*******************************************************************************/
+
+void threads_thread_state_waiting(threadobject *t)
+{
+       /* set the state in the lock */
+
+       threads_list_lock();
+
+       t->state = THREAD_STATE_WAITING;
+
+       threads_list_unlock();
+}
+
+
+/* threads_thread_state_timed_waiting ******************************************
+
+   Set the current state of the given thread to
+   THREAD_STATE_TIMED_WAITING.
+
+*******************************************************************************/
+
+void threads_thread_state_timed_waiting(threadobject *t)
+{
+       /* set the state in the lock */
+
+       threads_list_lock();
+
+       t->state = THREAD_STATE_TIMED_WAITING;
+
+       threads_list_unlock();
+}
+
+
+/* threads_thread_state_terminated *********************************************
+
+   Set the current state of the given thread to
+   THREAD_STATE_TERMINATED.
+
+*******************************************************************************/
+
+void threads_thread_state_terminated(threadobject *t)
+{
+       /* set the state in the lock */
+
+       threads_list_lock();
+
+       t->state = THREAD_STATE_TERMINATED;
+
+       threads_list_unlock();
+}
+
+
 /* threads_thread_get_state ****************************************************
 
    Returns the current state of the given thread.
 
 *******************************************************************************/
 
-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;
@@ -818,7 +713,7 @@ 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 */
 
@@ -877,15 +772,15 @@ void threads_dump(void)
 
        /* 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)) {
+       for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
                /* print thread info */
 
                printf("\n");
@@ -897,9 +792,9 @@ void threads_dump(void)
                threads_thread_print_stacktrace(t);
        }
 
-       /* unlock the threads table */
+       /* unlock the threads lists */
 
-       threads_table_unlock();
+       threads_list_unlock();
 }