#include "mm/memory.h"
+#if defined(ENABLE_GC_BOEHM)
+/* We need to include Boehm's gc.h here for GC_register_my_thread and
+ friends. */
+# include "mm/boehm-gc/include/gc.h"
+#endif
+
#include "native/jni.h"
#include "native/llni.h"
#include "native/native.h"
# include "native/include/java_lang_ThreadGroup.h"
#endif
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
# include "native/include/java_lang_VMThread.h"
#endif
-#include "threads/critical.h"
#include "threads/lock-common.h"
#include "threads/threadlist.h"
#include "threads/thread.h"
#include "vm/builtin.h"
#include "vm/exceptions.h"
#include "vm/stringlocal.h"
-#include "vm/vm.h"
+#include "vm/vm.hpp"
-#include "vm/jit/stacktrace.h"
+#include "vm/jit/stacktrace.hpp"
#include "vmcore/class.h"
+#include "vmcore/globals.hpp"
#include "vmcore/method.h"
#include "vmcore/options.h"
mainthread = thread_new();
+ /* The main thread should always have index 1. */
+
+ if (mainthread->index != 1)
+ vm_abort("threads_preinit: main thread index not 1: %d != 1",
+ mainthread->index);
+
/* thread is a Java thread and running */
mainthread->flags |= THREAD_FLAG_JAVA;
/* Cache the java.lang.Thread initialization method. */
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
thread_method_init =
class_resolveclassmethod(class_java_lang_Thread,
class_java_lang_Thread,
true);
-#elif defined(WITH_CLASSPATH_SUN)
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
thread_method_init =
class_resolveclassmethod(class_java_lang_Thread,
class_java_lang_Thread,
true);
-#elif defined(WITH_CLASSPATH_CLDC1_1)
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
thread_method_init =
class_resolveclassmethod(class_java_lang_Thread,
java_handle_t *o;
java_lang_Thread *to;
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
java_lang_VMThread *vmto;
classinfo *c;
methodinfo *m;
thread_set_object(t, (java_handle_t *) to);
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
/* Create a java.lang.VMThread Java object. */
if (exceptions_get_exception())
return false;
-#elif defined(WITH_CLASSPATH_SUN)
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
/* OpenJDK's java.lang.Thread does not have a VMThread field in
the class. Nothing to do here. */
if (exceptions_get_exception())
return false;
-#elif defined(WITH_CLASSPATH_CLDC1_1)
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
/* Set the thread data-structure in the Java thread object. */
static void thread_create_initial_threadgroups(void)
{
#if defined(ENABLE_JAVASE)
-# if defined(WITH_CLASSPATH_GNU)
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
/* Allocate and initialize the main thread group. */
threadgroup_system = threadgroup_main;
-# elif defined(WITH_CLASSPATH_SUN)
+# elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
java_handle_t *name;
methodinfo *m;
{
java_lang_Thread *to;
threadobject *t;
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
java_lang_VMThread *vmto;
#endif
thread_set_object(t, object);
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
/* Get the java.lang.VMThread object and do some sanity checks. */
LLNI_field_set_val(vmto, vmdata, (java_lang_Object *) t);
-#elif defined(WITH_CLASSPATH_SUN)
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
/* Nothing to do. */
-#elif defined(WITH_CLASSPATH_CLDC1_1)
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
LLNI_field_set_val(to, vm_thread, (java_lang_Object *) t);
}
-/* threads_attach_current_thread ***********************************************
-
- Attaches the current thread to the VM. Used in JNI.
-
-*******************************************************************************/
-
-bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
+/**
+ * Attaches the current thread to the VM.
+ *
+ * @param vm_aargs Attach arguments.
+ * @param isdaemon true if the attached thread should be a daemon
+ * thread.
+ *
+ * @return true on success, false otherwise.
+ */
+bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
{
bool result;
threadobject *t;
if (isdaemon)
t->flags |= THREAD_FLAG_DAEMON;
+ /* Store the internal thread data-structure in the TSD. */
+
+ thread_set_current(t);
+
/* The thread is flagged and (non-)daemon thread, we can leave the
mutex. */
if (vm_aargs != NULL)
group = (java_handle_t *) vm_aargs->group;
-
+ else
+ group = NULL;
+
/* If no threadgroup was given, use the main threadgroup. */
if (group == NULL)
/* The thread is completely initialized. */
- threads_thread_state_runnable(t);
+ thread_set_state_runnable(t);
+
+ return true;
+}
+
+
+/**
+ * Attaches the current external thread to the VM. This function is
+ * called by JNI's AttachCurrentThread.
+ *
+ * @param vm_aargs Attach arguments.
+ * @param isdaemon true if the attached thread should be a daemon
+ * thread.
+ *
+ * @return true on success, false otherwise.
+ */
+bool thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
+{
+ int result;
+
+#if defined(ENABLE_GC_BOEHM)
+ struct GC_stack_base sb;
+#endif
+
+#if defined(ENABLE_GC_BOEHM)
+ /* Register the thread with Boehm-GC. This must happen before the
+ thread allocates any memory from the GC heap.*/
+
+ result = GC_get_stack_base(&sb);
+
+ if (result != GC_SUCCESS)
+ vm_abort("threads_attach_current_thread: GC_get_stack_base failed");
+
+ GC_register_my_thread(&sb);
+#endif
+
+ result = thread_attach_current_thread(vm_aargs, isdaemon);
+
+ if (result == false) {
+#if defined(ENABLE_GC_BOEHM)
+ /* Unregister the thread. */
+
+ GC_unregister_my_thread();
+#endif
+
+ return false;
+ }
+
+ return true;
+}
+
+
+/**
+ * Detaches the current external thread from the VM. This function is
+ * called by JNI's DetachCurrentThread.
+ *
+ * @return true on success, false otherwise.
+ */
+bool thread_detach_current_external_thread(void)
+{
+ int result;
+
+ result = thread_detach_current_thread();
+
+ if (result == false)
+ return false;
+
+#if defined(ENABLE_GC_BOEHM)
+ /* Unregister the thread with Boehm-GC. This must happen after
+ the thread allocates any memory from the GC heap. */
+
+ /* Don't detach the main thread. This is a workaround for
+ OpenJDK's java binary. */
+ if (thread_get_current()->index != 1)
+ GC_unregister_my_thread();
+#endif
return true;
}
{
java_lang_Thread *to;
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
java_lang_String *name;
-#elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK) || defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
java_chararray_t *name;
#endif
LLNI_field_get_ref(to, name, name);
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
javastring_fprint((java_handle_t *) name, stream);
-#elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK) || defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
/* FIXME: In OpenJDK and CLDC the name is a char[]. */
/* FIXME This prints to stdout. */
void thread_print_info(threadobject *t)
{
java_lang_Thread *to;
+ int state;
/* If the thread is currently in initalization, don't print it. */
/* Print thread state. */
- switch (t->state) {
+ state = cacaothread_get_state(t);
+
+ switch (state) {
case THREAD_STATE_NEW:
printf(" new");
break;
printf(" terminated");
break;
default:
- vm_abort("thread_print_info: unknown thread state %d", t->state);
+ vm_abort("thread_print_info: unknown thread state %d", state);
}
}
}
-/* threads_thread_state_runnable ***********************************************
+/* thread_set_state_runnable ***************************************************
Set the current state of the given thread to THREAD_STATE_RUNNABLE.
*******************************************************************************/
-void threads_thread_state_runnable(threadobject *t)
+void thread_set_state_runnable(threadobject *t)
{
/* Set the state inside a lock. */
threadlist_lock();
- if (t->state != THREAD_STATE_TERMINATED)
+ if (t->state != THREAD_STATE_TERMINATED) {
t->state = THREAD_STATE_RUNNABLE;
- DEBUGTHREADS("is RUNNABLE", t);
+ DEBUGTHREADS("is RUNNABLE", t);
+ }
threadlist_unlock();
}
-/* threads_thread_state_waiting ************************************************
+/* thread_set_state_waiting ****************************************************
Set the current state of the given thread to THREAD_STATE_WAITING.
*******************************************************************************/
-void threads_thread_state_waiting(threadobject *t)
+void thread_set_state_waiting(threadobject *t)
{
/* Set the state inside a lock. */
threadlist_lock();
- if (t->state != THREAD_STATE_TERMINATED)
+ if (t->state != THREAD_STATE_TERMINATED) {
t->state = THREAD_STATE_WAITING;
- DEBUGTHREADS("is WAITING", t);
+ DEBUGTHREADS("is WAITING", t);
+ }
threadlist_unlock();
}
-/* threads_thread_state_timed_waiting ******************************************
+/* thread_set_state_timed_waiting **********************************************
Set the current state of the given thread to
THREAD_STATE_TIMED_WAITING.
*******************************************************************************/
-void threads_thread_state_timed_waiting(threadobject *t)
+void thread_set_state_timed_waiting(threadobject *t)
{
/* Set the state inside a lock. */
threadlist_lock();
- if (t->state != THREAD_STATE_TERMINATED)
+ if (t->state != THREAD_STATE_TERMINATED) {
t->state = THREAD_STATE_TIMED_WAITING;
- DEBUGTHREADS("is TIMED_WAITING", t);
+ DEBUGTHREADS("is TIMED_WAITING", t);
+ }
threadlist_unlock();
}
-/* threads_thread_state_terminated *********************************************
+/* thread_set_state_terminated *************************************************
Set the current state of the given thread to
THREAD_STATE_TERMINATED.
*******************************************************************************/
-void threads_thread_state_terminated(threadobject *t)
+void thread_set_state_terminated(threadobject *t)
{
- /* set the state in the lock */
+ /* Set the state inside a lock. */
threadlist_lock();
}
-/* threads_thread_get_state ****************************************************
-
- Returns the current state of the given thread.
-
-*******************************************************************************/
-
-utf *threads_thread_get_state(threadobject *t)
-{
- utf *u;
-
- switch (t->state) {
- case THREAD_STATE_NEW:
- u = utf_new_char("NEW");
- break;
- case THREAD_STATE_RUNNABLE:
- u = utf_new_char("RUNNABLE");
- break;
- case THREAD_STATE_BLOCKED:
- u = utf_new_char("BLOCKED");
- break;
- case THREAD_STATE_WAITING:
- u = utf_new_char("WAITING");
- break;
- case THREAD_STATE_TIMED_WAITING:
- u = utf_new_char("TIMED_WAITING");
- break;
- case THREAD_STATE_TERMINATED:
- u = utf_new_char("TERMINATED");
- break;
- default:
- vm_abort("threads_get_state: unknown thread state %d", t->state);
-
- /* keep compiler happy */
-
- u = NULL;
- }
-
- return u;
-}
-
-
/* thread_get_thread **********************************************************
Return the thread data structure of the given Java thread object.
threadobject *thread_get_thread(java_handle_t *h)
{
threadobject *t;
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
java_lang_VMThread *vmto;
java_lang_Object *to;
#endif
-#if defined(WITH_CLASSPATH_SUN)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
bool equal;
#endif
-#if defined(WITH_CLASSPATH_GNU)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
vmto = (java_lang_VMThread *) h;
t = (threadobject *) to;
-#elif defined(WITH_CLASSPATH_SUN)
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
/* XXX This is just a quick hack. */
threadlist_unlock();
-#elif defined(WITH_CLASSPATH_CLDC1_1)
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
log_println("threads_get_thread: IMPLEMENT ME!");
bool threads_thread_is_alive(threadobject *t)
{
- switch (t->state) {
+ int state;
+
+ state = cacaothread_get_state(t);
+
+ switch (state) {
case THREAD_STATE_NEW:
case THREAD_STATE_TERMINATED:
return false;
return true;
default:
- vm_abort("threads_thread_is_alive: unknown thread state %d", t->state);
+ vm_abort("threads_thread_is_alive: unknown thread state %d", state);
}
/* keep compiler happy */
/* Print trace of thread. */
- threads_thread_print_stacktrace(t);
+ stacktrace_print_of_thread(t);
#if defined(ENABLE_GC_CACAO)
/* Resume the thread. */
}
-/* threads_thread_print_stacktrace *********************************************
-
- Print the current stacktrace of the given thread.
-
-*******************************************************************************/
-
-void threads_thread_print_stacktrace(threadobject *thread)
-{
- stackframeinfo_t *sfi;
- java_handle_bytearray_t *ba;
- stacktrace_t *st;
-
- /* 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);
-
- /* Print stacktrace. */
-
- stacktrace_print(st);
-
- LLNI_CRITICAL_END;
- }
- else {
- puts("\t<<No stacktrace available>>");
- fflush(stdout);
- }
-}
-
-
-/* threads_print_stacktrace ****************************************************
-
- Print the current stacktrace of the current thread.
-
-*******************************************************************************/
-
-void threads_print_stacktrace(void)
-{
- threadobject *thread;
-
- thread = THREADOBJECT;
-
- threads_thread_print_stacktrace(thread);
-}
-
-
/*
* These are local overrides for various environment variables in Emacs.
* Please do not remove this and leave it at the end of the file, where