Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
- $Id: threads-common.c 7875 2007-05-07 11:35:30Z twisti $
+ $Id: threads-common.c 7963 2007-05-24 10:21:16Z twisti $
*/
#include "vm/types.h"
+#include "mm/memory.h"
+
#include "native/jni.h"
#include "native/include/java_lang_Object.h"
#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;
-
-
-/* prototypes *****************************************************************/
+/* global threads list */
+static list_t *list_threads;
-static void threads_table_init(threadobject *mainthread);
+/* global threads free-list */
+static list_t *list_threads_free;
/* threads_preinit *************************************************************
{
threadobject *mainthread;
+ /* 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). */
/* 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 */
/* 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 */
}
-/* 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 */
-
- 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. */
+ threadobject *t;
- 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 */
+ t = list_first_unsynced(list_threads);
- threads_table.used = 1;
+ return t;
}
-/* threads_table_add ***********************************************************
+/* threads_list_next ***********************************************************
- Add a thread to the global threads table. The index is entered in the
- threadobject. The thinlock value for the thread is pre-computed.
+ Return the next entry in the threads list.
- IN:
- thread............the thread to add
-
- 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)
+threadobject *threads_list_next(threadobject *t)
{
- 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);
+ threadobject *next;
- /* unlock the threads table */
+ next = list_next_unsynced(list_threads, t);
- threads_table_unlock();
-
- return index;
+ return next;
}
-/* threads_table_remove *******************************************************
+/* threads_list_get_non_daemons ************************************************
- Remove a thread from the global threads table.
+ Return the number of non-daemon threads.
- IN:
- thread............the thread to remove
+ NOTE: This function does a linear-search over the threads list,
+ because it's only used for joining the threads.
-******************************************************************************/
+*******************************************************************************/
-void threads_table_remove(threadobject *thread)
+s4 threads_list_get_non_daemons(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 */
+ threadobject *t;
+ s4 nondaemons;
- ttefree = &(threads_table.table[0]);
+ /* lock the threads lists */
- /* get the current entry */
+ threads_list_lock();
- index = thread->index;
- tte = &(threads_table.table[index]);
+ nondaemons = 0;
- 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;
- }
+ for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
+ if (!(t->flags & THREAD_FLAG_DAEMON))
+ nondaemons++;
}
- /* 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 */
+ /* unlock the threads lists */
- threads_table.used--;
+ threads_list_unlock();
- 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
-
- /* unlock the threads table */
-
- threads_table_unlock();
+ return nondaemons;
}
-/* threads_table_get ***********************************************************
-
- Return the thread of the given table-entry index.
-
- 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.
+/* 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.
*******************************************************************************/
-static threadobject *threads_table_get(s4 index)
+threadobject *threads_thread_new(void)
{
- threadobject *thread;
-
- /* get the requested entry */
-
- assert((index >= 0) && (index < threads_table.size));
-
- thread = threads_table.table[index].thread;
-
- return thread;
-}
-
+ threadobject *t;
-/* threads_table_get_threads ***************************************************
+ /* lock the threads-lists */
- Return the number of running threads.
+ threads_list_lock();
- NOTE: This function does not lock the table.
+ /* try to get a thread from the free-list */
-*******************************************************************************/
-
-s4 threads_table_get_threads(void)
-{
- return threads_table.used;
-}
+ t = list_first_unsynced(list_threads_free);
+ /* is a free thread available? */
-/* threads_table_get_non_daemons ***********************************************
-
- Return the number of non-daemon threads.
-
-*******************************************************************************/
+ if (t != NULL) {
+ /* yes, remove it from the free list */
-s4 threads_table_get_non_daemons(void)
-{
- s4 nondaemons;
+ list_remove_unsynced(list_threads_free, t);
+ }
+ else {
+ /* no, allocate a new one */
- /* lock the threads table */
+#if defined(ENABLE_GC_BOEHM)
+ t = GCNEW_UNCOLLECTABLE(threadobject, 1);
+#else
+ t = NEW(threadobject);
+#endif
- threads_table_lock();
+#if defined(ENABLE_STATISTICS)
+ if (opt_stat)
+ size_threadobject += sizeof(threadobject);
+#endif
- nondaemons = threads_table.used - threads_table.daemons;
+ /* clear memory */
- /* unlock the threads table */
+ MZERO(t, threadobject, 1);
- threads_table_unlock();
+ /* set the threads-index */
- return nondaemons;
-}
+ t->index = list_threads->size + 1;
+ }
+ /* pre-compute the thinlock-word */
-/* threads_table_first *********************************************************
+ assert(t->index != 0);
- Return the first thread of the threads table.
+ t->thinlock = lock_pre_compute_thinlock(t->index);
+ t->state = THREAD_STATE_NEW;
- NOTE: This is always the entry with index 1 and must be the main
- thread.
+ /* initialize the implementation-specific bits */
- NOTE: This function does not lock the table.
+ threads_impl_thread_new(t);
-*******************************************************************************/
+ /* add the thread to the threads-list */
-threadobject *threads_table_first(void)
-{
- threadobject *thread;
+ list_add_last_unsynced(list_threads, t);
- /* get the requested entry */
+ /* unlock the threads-lists */
- thread = threads_table_get(1);
+ threads_list_unlock();
- return thread;
+ return t;
}
-/* threads_table_next **********************************************************
+/* threads_thread_free *********************************************************
- Return the next thread of the threads table relative to the passed
- one.
+ Frees an internal thread data-structure by removing it from the
+ threads-list and adding it to the free-list.
- NOTE: This function does not lock the table.
+ NOTE: The data-structure is NOT freed, the pointer keeps valid!
*******************************************************************************/
-threadobject *threads_table_next(threadobject *thread)
-{
- threads_table_entry_t *tte;
- threadobject *next;
- s4 index;
-
- index = thread->index;
-
- /* get the passed entry */
-
- assert((index > 0) && (index < threads_table.size));
-
- tte = &(threads_table.table[index]);
-
- /* get the requested entry */
-
- next = threads_table_get(tte->next);
-
- return next;
-}
-
-
-/* threads_table_dump *********************************************************
-
- Dump the threads table for debugging purposes.
-
-******************************************************************************/
-
-#if !defined(NDEBUG)
-void threads_table_dump(void)
+void threads_thread_free(threadobject *t)
{
- s4 i;
- s4 size;
- ptrint index;
-
- size = threads_table.size;
-
- log_println("threads table ==========");
-
- log_println("size: %d", size);
- log_println("used: %d", threads_table.used);
- log_println("daemons: %d", threads_table.daemons);
+ s4 index;
- for (i = 0; i < size; i++) {
- index = threads_table.table[i].next;
+ /* lock 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_lock();
- log_println("end of threads table ==========");
-}
-#endif
+ /* cleanup the implementation-specific bits */
+ threads_impl_thread_free(t);
-/* threads_create_thread *******************************************************
+ /* remove the thread from the threads-list */
- Creates and initializes an internal thread data-structure.
+ list_remove_unsynced(list_threads, t);
-*******************************************************************************/
+ /* Clear memory, but keep the thread-index. */
+ /* ATTENTION: Do this after list_remove, otherwise the linkage
+ pointers are invalid. */
-threadobject *threads_create_thread(void)
-{
- threadobject *thread;
+ index = t->index;
- /* allocate internal thread data-structure */
+ MZERO(t, threadobject, 1);
-#if defined(ENABLE_GC_BOEHM)
- thread = GCNEW_UNCOLLECTABLE(threadobject, 1);
-#else
- thread = NEW(threadobject);
-#endif
+ t->index = index;
-#if defined(ENABLE_STATISTICS)
- if (opt_stat)
- size_threadobject += sizeof(threadobject);
-#endif
+ /* add the thread to the free list */
- /* initialize thread data structure */
+ list_add_first_unsynced(list_threads_free, t);
- threads_init_threadobject(thread);
- lock_init_execution_env(thread);
+ /* unlock the threads-lists */
- return thread;
+ threads_list_unlock();
}
/* create internal thread data-structure */
- thread = threads_create_thread();
+ thread = threads_thread_new();
/* create the java thread object */
t->vm_thread = (java_lang_Object *) thread;
#endif
- thread->object = t;
- thread->flags = THREAD_FLAG_DAEMON;
+ thread->object = t;
+
+ thread->flags = THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
/* set java.lang.Thread fields */
/* create internal thread data-structure */
- thread = threads_create_thread();
+ thread = threads_thread_new();
/* link the two objects together */
thread->object = object;
+ /* this is a normal Java thread */
+
+ thread->flags = THREAD_FLAG_JAVA;
+
#if defined(ENABLE_JAVASE)
/* is this a daemon thread? */
}
+/* threads_thread_print_info ***************************************************
+
+ Print information of the passed thread.
+
+*******************************************************************************/
+
+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;
+
+ 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("\"");
+ utf_display_printable_ascii(name);
+ printf("\"");
+
+ if (t->flags & THREAD_FLAG_DAEMON)
+ printf(" daemon");
+
+ printf(" prio=%d", object->priority);
+
+#if SIZEOF_VOID_P == 8
+ printf(" t=0x%016lx tid=0x%016lx (%ld)",
+ (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
+#else
+ printf(" t=0x%08x tid=0x%08x (%d)",
+ (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
+#endif
+
+ printf(" index=%d", t->index);
+
+ /* 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_thread_print_info: unknown thread state %d",
+ t->state);
+ }
+ }
+}
+
+
/* threads_get_current_tid *****************************************************
Return the tid of the current thread.
}
+/* 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;
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;
default:
vm_abort("threads_is_alive: unknown thread state %d", thread->state);
+
+ /* keep compiler happy */
+
+ result = false;
}
return result;
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 */
+ for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
+ /* print thread info */
- 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");
+ threads_thread_print_info(t);
+ printf("\n");
- printf("\n\"");
- utf_display_printable_ascii(name);
- printf("\"");
+ /* print trace of thread */
- if (thread->flags & THREAD_FLAG_DAEMON)
- printf(" daemon");
-
- printf(" prio=%d", object->priority);
-
-#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 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();
}