/* 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.
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
- $Id: threads-common.c 8299 2007-08-13 08:41:18Z michi $
-
*/
#include "native/include/java_lang_Thread.h"
#if defined(WITH_CLASSPATH_GNU)
+# include "native/include/java_lang_Throwable.h"
# include "native/include/java_lang_VMThread.h"
#endif
#include "threads/critical.h"
#include "threads/lock-common.h"
+#include "threads/threadlist.h"
#include "threads/threads-common.h"
#include "toolbox/list.h"
#include "vm/jit/stacktrace.h"
#include "vmcore/class.h"
+#include "vmcore/options.h"
#if defined(ENABLE_STATISTICS)
-# include "vmcore/options.h"
# include "vmcore/statistics.h"
#endif
/* global variables ***********************************************************/
-/* global threads list */
-static list_t *list_threads;
-
-/* global threads free-list */
-static list_t *list_threads_free;
-
#if defined(__LINUX__)
/* XXX Remove for exact-GC. */
bool threads_pthreads_implementation_nptl;
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)
size_t len;
#endif
+ TRACESUBSYSTEMINITIALIZATION("threads_preinit");
+
#if defined(__LINUX__)
/* XXX Remove for exact-GC. */
# 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). */
/* thread is a Java thread and running */
- mainthread->flags = THREAD_FLAG_JAVA;
+ mainthread->flags |= THREAD_FLAG_JAVA;
mainthread->state = THREAD_STATE_RUNNABLE;
/* store the internal thread data-structure in the TSD */
threads_set_current_threadobject(mainthread);
-
- /* initialize locking subsystems */
-
- lock_init();
-
- /* initialize the critical section */
-
- critical_init();
-}
-
-
-/* threads_list_first **********************************************************
-
- Return the first entry in the threads list.
-
- NOTE: This function does not lock the lists.
-
-*******************************************************************************/
-
-threadobject *threads_list_first(void)
-{
- threadobject *t;
-
- t = list_first_unsynced(list_threads);
-
- return t;
-}
-
-
-/* threads_list_next ***********************************************************
-
- Return the next entry in the threads list.
-
- NOTE: This function does not lock the lists.
-
-*******************************************************************************/
-
-threadobject *threads_list_next(threadobject *t)
-{
- threadobject *next;
-
- next = list_next_unsynced(list_threads, t);
-
- return next;
-}
-
-
-/* threads_list_get_non_daemons ************************************************
-
- Return the number of non-daemon threads.
-
- NOTE: This function does a linear-search over the threads list,
- because it's only used for joining the threads.
-
-*******************************************************************************/
-
-s4 threads_list_get_non_daemons(void)
-{
- threadobject *t;
- s4 nondaemons;
-
- /* lock the threads lists */
-
- threads_list_lock();
-
- nondaemons = 0;
-
- for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
- if (!(t->flags & THREAD_FLAG_DAEMON))
- nondaemons++;
- }
-
- /* unlock the threads lists */
-
- threads_list_unlock();
-
- return nondaemons;
}
threadobject *threads_thread_new(void)
{
- threadobject *t;
-
+ int32_t index;
+ threadobject *t;
+
/* lock the threads-lists */
threads_list_lock();
- /* try to get a thread from the free-list */
+ index = threadlist_get_free_index();
+
+ /* Allocate a thread data structure. */
- t = list_first_unsynced(list_threads_free);
+ /* First, try to get one from the free-list. */
- /* is a free thread available? */
+ t = threadlist_free_first();
if (t != NULL) {
- /* yes, remove it from the free list */
+ /* Remove from free list. */
- list_remove_unsynced(list_threads_free, t);
+ threadlist_free_remove(t);
+
+ /* Equivalent of MZERO on the else path */
+
+ threads_impl_thread_clear(t);
}
else {
- /* no, allocate a new one */
-
#if defined(ENABLE_GC_BOEHM)
t = GCNEW_UNCOLLECTABLE(threadobject, 1);
#else
size_threadobject += sizeof(threadobject);
#endif
- /* clear memory */
+ /* Clear memory. */
MZERO(t, threadobject, 1);
- /* set the threads-index */
-
- t->index = list_threads->size + 1;
-
#if defined(ENABLE_GC_CACAO)
- /* register reference to java.lang.Thread with the GC */
+ /* Register reference to java.lang.Thread with the GC. */
+ /* FIXME is it ok to do this only once? */
- gc_reference_register((java_objectheader **) &(t->object));
+ gc_reference_register(&(t->object), GC_REFTYPE_THREADOBJECT);
+ gc_reference_register(&(t->_exceptionptr), GC_REFTYPE_THREADOBJECT);
#endif
+
+ /* Initialize the implementation-specific bits. */
+
+ threads_impl_thread_init(t);
}
- /* pre-compute the thinlock-word */
+ /* Pre-compute the thinlock-word. */
- assert(t->index != 0);
+ assert(index != 0);
- t->thinlock = lock_pre_compute_thinlock(t->index);
- t->flags = 0;
- t->state = THREAD_STATE_NEW;
+ t->index = index;
+ t->thinlock = lock_pre_compute_thinlock(t->index);
+ t->flags = 0;
+ t->state = THREAD_STATE_NEW;
#if defined(ENABLE_GC_CACAO)
- t->flags |= THREAD_FLAG_IN_NATIVE;
+ t->flags |= THREAD_FLAG_IN_NATIVE;
#endif
- /* initialize the implementation-specific bits */
+ /* Initialize the implementation-specific bits. */
- threads_impl_thread_new(t);
+ threads_impl_thread_reuse(t);
- /* add the thread to the threads-list */
+ /* Add the thread to the thread list. */
- list_add_last_unsynced(list_threads, t);
+ threadlist_add(t);
- /* unlock the threads-lists */
+ /* Unlock the threads-lists. */
threads_list_unlock();
/* threads_thread_free *********************************************************
- Frees an internal thread data-structure by removing it from the
- threads-list and adding it to the free-list.
+ Remove the thread from the threads-list and free the internal
+ thread data structure. The thread index is added to the
+ thread-index free-list.
- NOTE: The data-structure is NOT freed, the pointer keeps valid!
+ IN:
+ t....thread data structure
*******************************************************************************/
void threads_thread_free(threadobject *t)
{
- int32_t index;
- uint32_t state;
-
- /* lock the threads-lists */
+ /* Lock the threads lists. */
threads_list_lock();
- /* cleanup the implementation-specific bits */
-
- threads_impl_thread_free(t);
-
- /* remove the thread from the threads-list */
-
- list_remove_unsynced(list_threads, t);
+ /* Remove the thread from the thread-list. */
- /* Clear memory, but keep the thread-index and the
- thread-state. */
+ threadlist_remove(t);
- /* ATTENTION: Do this after list_remove, otherwise the linkage
- pointers are invalid. */
+ /* Add the thread index to the free list. */
- index = t->index;
- state = t->state;
+ threadlist_index_add(t->index);
- MZERO(t, threadobject, 1);
+ /* Add the thread data structure to the free list. */
- t->index = index;
- t->state = state;
+ threads_thread_set_object(t, NULL);
- /* add the thread to the free list */
+ threadlist_free_add(t);
- list_add_first_unsynced(list_threads_free, t);
-
- /* unlock the threads-lists */
+ /* Unlock the threads lists. */
threads_list_unlock();
}
LLNI_field_set_val(object, vm_thread, (java_lang_Object *) t);
#endif
- t->object = object;
+ threads_thread_set_object(t, (java_handle_t *) object);
/* set java.lang.Thread fields */
*******************************************************************************/
-void threads_thread_start(java_lang_Thread *object)
+void threads_thread_start(java_handle_t *object)
{
- threadobject *thread;
+ java_lang_Thread *o;
+ threadobject *thread;
#if defined(WITH_CLASSPATH_GNU)
java_lang_VMThread *vmt;
#endif
+ o = (java_lang_Thread *) object;
+
/* Enter the join-mutex, so if the main-thread is currently
waiting to join all threads, the number of non-daemon threads
is correct. */
#if defined(ENABLE_JAVASE)
/* is this a daemon thread? */
- if (LLNI_field_direct(object, daemon) == true)
+ if (LLNI_field_direct(o, daemon) == true)
thread->flags |= THREAD_FLAG_DAEMON;
#endif
/* link the two objects together */
- thread->object = object;
+ threads_thread_set_object(thread, object);
#if defined(WITH_CLASSPATH_GNU)
- LLNI_field_get_ref(object, vmThread, vmt);
+ LLNI_field_get_ref(o, vmThread, vmt);
assert(vmt);
assert(LLNI_field_direct(vmt, vmdata) == NULL);
LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) thread);
#elif defined(WITH_CLASSPATH_CLDC1_1)
- LLNI_field_set_val(object, vm_thread, (java_lang_Object *) thread);
+ LLNI_field_set_val(o, vm_thread, (java_lang_Object *) thread);
#endif
/* Start the thread. Don't pass a function pointer (NULL) since
void threads_thread_print_info(threadobject *t)
{
java_lang_Thread *object;
+#if defined(WITH_CLASSPATH_GNU)
+ java_lang_String *namestring;
+#endif
utf *name;
assert(t->state != THREAD_STATE_NEW);
/* the thread may be currently in initalization, don't print it */
- object = t->object;
+ object = (java_lang_Thread *) threads_thread_get_object(t);
if (object != NULL) {
/* get thread name */
#if defined(WITH_CLASSPATH_GNU)
- name = javastring_toutf((java_handle_t *) LLNI_field_direct(object, name), false);
+ LLNI_field_get_ref(object, name, namestring);
+ name = javastring_toutf((java_handle_t *) namestring, false);
#elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
/* FIXME: In cldc the name is a char[] */
/* name = object->name; */
}
+/* threads_get_current_object **************************************************
+
+ Return the Java object of the current thread.
+
+ RETURN VALUE:
+ the Java object
+
+*******************************************************************************/
+
+#include "native/include/java_lang_ThreadGroup.h"
+
+java_object_t *threads_get_current_object(void)
+{
+#if defined(ENABLE_THREADS)
+ threadobject *t;
+# if defined(ENABLE_JAVASE)
+ java_lang_ThreadGroup *group;
+# endif
+#endif
+ java_lang_Thread *o;
+
+#if defined(ENABLE_THREADS)
+ t = THREADOBJECT;
+ o = threads_thread_get_object(t);
+
+# if defined(ENABLE_JAVASE)
+ /* TODO Do we really need this code? Or should we check, when we
+ create the threads, that all of them have a group? */
+ /* TWISTI No, we don't need this code! We need to allocate a
+ ThreadGroup before we initialize the main thread. */
+
+ LLNI_field_get_ref(o, group, group);
+
+ if (group == NULL) {
+ /* ThreadGroup of currentThread is not initialized */
+
+ group = (java_lang_ThreadGroup *)
+ native_new_and_init(class_java_lang_ThreadGroup);
+
+ if (group == NULL)
+ vm_abort("unable to create ThreadGroup");
+
+ LLNI_field_set_ref(o, group, group);
+ }
+# endif
+#else
+ /* We just return a fake java.lang.Thread object, otherwise we get
+ NullPointerException's in GNU Classpath. */
+
+ o = builtin_new(class_java_lang_Thread);
+#endif
+
+ return o;
+}
+
+
/* threads_thread_state_runnable ***********************************************
Set the current state of the given thread to THREAD_STATE_RUNNABLE.
+ NOTE: If the thread has already terminated, don't set the state.
+ This is important for threads_detach_thread.
+
*******************************************************************************/
void threads_thread_state_runnable(threadobject *t)
{
- /* set the state inside the lock */
+ /* Set the state inside a lock. */
threads_list_lock();
- t->state = THREAD_STATE_RUNNABLE;
+ if (t->state != THREAD_STATE_TERMINATED)
+ t->state = THREAD_STATE_RUNNABLE;
+
+ DEBUGTHREADS("is RUNNABLE", t);
threads_list_unlock();
}
Set the current state of the given thread to THREAD_STATE_WAITING.
+ NOTE: If the thread has already terminated, don't set the state.
+ This is important for threads_detach_thread.
+
*******************************************************************************/
void threads_thread_state_waiting(threadobject *t)
{
- /* set the state in the lock */
+ /* Set the state inside a lock. */
threads_list_lock();
- t->state = THREAD_STATE_WAITING;
+ if (t->state != THREAD_STATE_TERMINATED)
+ t->state = THREAD_STATE_WAITING;
+
+ DEBUGTHREADS("is WAITING", t);
threads_list_unlock();
}
Set the current state of the given thread to
THREAD_STATE_TIMED_WAITING.
+ NOTE: If the thread has already terminated, don't set the state.
+ This is important for threads_detach_thread.
+
*******************************************************************************/
void threads_thread_state_timed_waiting(threadobject *t)
{
- /* set the state in the lock */
+ /* Set the state inside a lock. */
threads_list_lock();
- t->state = THREAD_STATE_TIMED_WAITING;
+ if (t->state != THREAD_STATE_TERMINATED)
+ t->state = THREAD_STATE_TIMED_WAITING;
+
+ DEBUGTHREADS("is TIMED_WAITING", t);
threads_list_unlock();
}
t->state = THREAD_STATE_TERMINATED;
+ DEBUGTHREADS("is TERMINATED", t);
+
threads_list_unlock();
}
*******************************************************************************/
-bool threads_thread_is_alive(threadobject *thread)
+bool threads_thread_is_alive(threadobject *t)
{
- bool result;
-
- switch (thread->state) {
+ switch (t->state) {
case THREAD_STATE_NEW:
case THREAD_STATE_TERMINATED:
- result = false;
- break;
+ return false;
case THREAD_STATE_RUNNABLE:
case THREAD_STATE_BLOCKED:
case THREAD_STATE_WAITING:
case THREAD_STATE_TIMED_WAITING:
- result = true;
- break;
+ return true;
default:
- vm_abort("threads_is_alive: unknown thread state %d", thread->state);
-
- /* keep compiler happy */
-
- result = false;
+ vm_abort("threads_thread_is_alive: unknown thread state %d", t->state);
}
- return result;
+ /* keep compiler happy */
+
+ return false;
}
/* iterate over all started threads */
- for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
- /* print thread info */
+ for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
+ /* ignore threads which are in state NEW */
+ if (t->state == THREAD_STATE_NEW)
+ continue;
+
+#if defined(ENABLE_GC_CACAO)
+ /* Suspend the thread. */
+ /* XXX Is the suspend reason correct? */
+
+ if (threads_suspend_thread(t, SUSPEND_REASON_JNI) == false)
+ vm_abort("threads_dump: threads_suspend_thread failed");
+#endif
+
+ /* Print thread info. */
printf("\n");
threads_thread_print_info(t);
printf("\n");
- /* print trace of thread */
+ /* Print trace of thread. */
threads_thread_print_stacktrace(t);
+
+#if defined(ENABLE_GC_CACAO)
+ /* Resume the thread. */
+
+ if (threads_resume_thread(t) == false)
+ vm_abort("threads_dump: threads_resume_thread failed");
+#endif
}
/* unlock the threads lists */
/* threads_thread_print_stacktrace *********************************************
- Print the current stacktrace of the current thread.
+ Print the current stacktrace of the given thread.
*******************************************************************************/
void threads_thread_print_stacktrace(threadobject *thread)
{
- stackframeinfo *sfi;
- stacktracebuffer *stb;
- s4 dumpsize;
-
- /* mark start of dump memory area */
+ stackframeinfo_t *sfi;
+ java_handle_bytearray_t *ba;
+ stacktrace_t *st;
- dumpsize = dump_size();
-
- /* create a stacktrace for the passed thread */
+ /* Build a stacktrace for the passed thread. */
sfi = thread->_stackframeinfo;
+ ba = stacktrace_get(sfi);
+
+ if (ba != NULL) {
+ /* We need a critical section here as we use the byte-array
+ data pointer directly. */
+
+ LLNI_CRITICAL_START;
+
+ st = (stacktrace_t *) LLNI_array_data(ba);
- stb = stacktrace_create(sfi);
+ /* Print stacktrace. */
- /* print stacktrace */
+ stacktrace_print(st);
- if (stb != NULL)
- stacktrace_print_trace_from_buffer(stb);
+ LLNI_CRITICAL_END;
+ }
else {
puts("\t<<No stacktrace available>>");
fflush(stdout);
}
-
- dump_release(dumpsize);
}