Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
- $Id: threads.c 7885 2007-05-07 21:29:55Z twisti $
-
*/
#include "mm/memory.h"
#include "native/jni.h"
+#include "native/llni.h"
#include "native/native.h"
#include "native/include/java_lang_Object.h"
#include "native/include/java_lang_String.h"
#include "threads/native/threads.h"
-#include "toolbox/avl.h"
#include "toolbox/logging.h"
#include "vm/builtin.h"
#endif
/* global mutex for the threads table */
-static pthread_mutex_t mutex_threads_table;
+static pthread_mutex_t mutex_threads_list;
/* global mutex for stop-the-world */
static pthread_mutex_t stopworldlock;
static pthread_mutex_t mutex_join;
static pthread_cond_t cond_join;
+/* XXX We disable that whole bunch of code until we have the exact-GC
+ running. */
+
+#if 0
+
/* this is one of the STOPWORLD_FROM_ constants, telling why the world is */
/* being stopped */
static volatile int stopworldwhere;
static pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
#endif
-static pthread_attr_t threadattr;
+#endif /* 0 */
/* mutexes used by the fake atomic instructions */
#if defined(USE_FAKE_ATOMIC_INSTRUCTIONS)
void lock_stopworld(int where)
{
pthread_mutex_lock(&stopworldlock);
- stopworldwhere = where;
+/* stopworldwhere = where; */
}
void unlock_stopworld(void)
{
- stopworldwhere = 0;
+/* stopworldwhere = 0; */
pthread_mutex_unlock(&stopworldlock);
}
+/* XXX We disable that whole bunch of code until we have the exact-GC
+ running. */
+
+#if 0
+
#if !defined(__DARWIN__)
/* Caller must hold threadlistlock */
-static void threads_cast_sendsignals(int sig)
+static s4 threads_cast_sendsignals(s4 sig)
{
threadobject *t;
threadobject *self;
+ s4 count;
self = THREADOBJECT;
/* iterate over all started threads */
- for (t = threads_table_first(); t != NULL; t = threads_table_next(t)) {
+ count = 0;
+
+ for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
/* don't send the signal to ourself */
- if (t != self)
- pthread_kill(t->tid, sig);
+ if (t == self)
+ continue;
+
+ /* don't send the signal to NEW threads (because they are not
+ completely initialized) */
+
+ if (t->state == THREAD_STATE_NEW)
+ continue;
+
+ /* send the signal */
+
+ pthread_kill(t->tid, sig);
+
+ /* increase threads count */
+
+ count++;
}
+
+ return count;
}
#else
void threads_cast_stopworld(void)
{
#if !defined(__DARWIN__) && !defined(__CYGWIN__)
- int count, i;
+ s4 count, i;
#endif
lock_stopworld(STOPWORLD_FROM_CLASS_NUMBERING);
- /* lock the threads table */
+ /* lock the threads lists */
- threads_table_lock();
+ threads_list_lock();
#if defined(__DARWIN__)
threads_cast_darwinstop();
#else
/* send all threads the suspend signal */
- threads_cast_sendsignals(GC_signum1());
-
- /* wait for all threads to suspend (except the current one) */
+ count = threads_cast_sendsignals(GC_signum1());
- count = threads_table_get_threads() - 1;
+ /* wait for all threads signaled to suspend */
for (i = 0; i < count; i++)
threads_sem_wait(&suspend_ack);
#endif
- /* unlock the threads table */
-
- threads_table_unlock();
+ /* ATTENTION: Don't unlock the threads-lists here so that
+ non-signaled NEW threads can't change their state and execute
+ code. */
}
+
void threads_cast_startworld(void)
{
- /* lock the threads table */
-
- threads_table_lock();
-
#if defined(__DARWIN__)
threads_cast_darwinresume();
#elif defined(__MIPS__)
/* TODO */
assert(0);
#else
- threads_cast_sendsignals(GC_signum2());
+ (void) threads_cast_sendsignals(GC_signum2());
#endif
- /* unlock the threads table */
+ /* unlock the threads lists */
- threads_table_unlock();
+ threads_list_unlock();
unlock_stopworld();
}
#endif
}
+#endif
+
+
/* This function is called from Boehm GC code. */
int cacao_suspendhandler(ucontext_t *_uc)
threads_sigsuspend_handler(_uc);
return 1;
}
-#endif
#endif /* DISABLE_GC */
+#endif /* 0 */
+
/* threads_set_current_threadobject ********************************************
}
-/* threads_init_threadobject **************************************************
+/* threads_impl_thread_new *****************************************************
Initialize implementation fields of a threadobject.
IN:
- thread............the threadobject
+ t....the threadobject
-******************************************************************************/
+*******************************************************************************/
-void threads_init_threadobject(threadobject *thread)
+void threads_impl_thread_new(threadobject *t)
{
/* get the pthread id */
- thread->tid = pthread_self();
+ t->tid = pthread_self();
- thread->index = 0;
+ /* initialize the mutex and the condition */
- /* TODO destroy all those things */
+ pthread_mutex_init(&(t->waitmutex), NULL);
+ pthread_cond_init(&(t->waitcond), NULL);
- pthread_mutex_init(&(thread->waitmutex), NULL);
- pthread_cond_init(&(thread->waitcond), NULL);
+#if defined(ENABLE_DEBUG_FILTER)
+ /* Initialize filter counters */
+ t->filterverbosecallctr[0] = 0;
+ t->filterverbosecallctr[1] = 0;
+#endif
- thread->interrupted = false;
- thread->signaled = false;
- thread->sleeping = false;
+#if !defined(NDEBUG)
+ t->tracejavacallindent = 0;
+ t->tracejavacallcount = 0;
+#endif
+}
+
+
+/* threads_impl_thread_free ****************************************************
+
+ Cleanup thread stuff.
+
+ IN:
+ t....the threadobject
+
+*******************************************************************************/
+
+void threads_impl_thread_free(threadobject *t)
+{
+ /* destroy the mutex and the condition */
+
+ if (pthread_mutex_destroy(&(t->waitmutex)) != 0)
+ vm_abort("threads_impl_thread_free: pthread_mutex_destroy failed: %s",
+ strerror(errno));
+
+ if (pthread_cond_destroy(&(t->waitcond)) != 0)
+ vm_abort("threads_impl_thread_free: pthread_cond_destroy failed: %s",
+ strerror(errno));
}
pthread_mutex_init(&mutex_join, NULL);
pthread_cond_init(&cond_join, NULL);
+ /* initialize the threads-list mutex */
+
+ pthread_mutex_init(&mutex_threads_list, NULL);
+
#if !defined(HAVE___THREAD)
pthread_key_create(&threads_current_threadobject_key, NULL);
#endif
- threads_sem_init(&suspend_ack, 0, 0);
+/* threads_sem_init(&suspend_ack, 0, 0); */
}
-/* threads_table_lock **********************************************************
+/* threads_list_lock ***********************************************************
+
+ Enter the threads table mutex.
- Initialize threads table mutex.
+ NOTE: We need this function as we can't use an internal lock for
+ the threads lists because the thread's lock is initialized in
+ threads_table_add (when we have the thread index), but we
+ already need the lock at the entry of the function.
*******************************************************************************/
-void threads_impl_table_init(void)
+void threads_list_lock(void)
{
- pthread_mutex_init(&mutex_threads_table, NULL);
+ if (pthread_mutex_lock(&mutex_threads_list) != 0)
+ vm_abort("threads_list_lock: pthread_mutex_lock failed: %s",
+ strerror(errno));
}
-/* threads_table_lock **********************************************************
+/* threads_list_unlock *********************************************************
- Enter the threads table mutex.
+ Leave the threads list mutex.
+
+*******************************************************************************/
+
+void threads_list_unlock(void)
+{
+ if (pthread_mutex_unlock(&mutex_threads_list) != 0)
+ vm_abort("threads_list_unlock: pthread_mutex_unlock failed: %s",
+ strerror(errno));
+}
- NOTE: We need this function as we can't use an internal lock for
- the threads table because the thread's lock is initialized in
- threads_table_add (when we have the thread index), but we
- already need the lock at the entry of the function.
+
+/* threads_mutex_join_lock *****************************************************
+
+ Enter the join mutex.
*******************************************************************************/
-void threads_table_lock(void)
+void threads_mutex_join_lock(void)
{
- if (pthread_mutex_lock(&mutex_threads_table) != 0)
- vm_abort("threads_table_lock: pthread_mutex_lock failed: %s",
+ if (pthread_mutex_lock(&mutex_join) != 0)
+ vm_abort("threads_mutex_join_lock: pthread_mutex_lock failed: %s",
strerror(errno));
}
-/* threads_table_unlock ********************************************************
+/* threads_mutex_join_unlock ***************************************************
- Leave the threads table mutex.
+ Leave the join mutex.
*******************************************************************************/
-void threads_table_unlock(void)
+void threads_mutex_join_unlock(void)
{
- if (pthread_mutex_unlock(&mutex_threads_table) != 0)
- vm_abort("threads_table_unlock: pthread_mutex_unlock failed: %s",
+ if (pthread_mutex_unlock(&mutex_join) != 0)
+ vm_abort("threads_mutex_join_unlock: pthread_mutex_unlock failed: %s",
strerror(errno));
}
bool threads_init(void)
{
- threadobject *mainthread;
- java_objectheader *threadname;
- java_lang_Thread *t;
- java_objectheader *o;
+ threadobject *mainthread;
+ java_handle_t *threadname;
+ java_lang_Thread *t;
+ java_handle_t *o;
#if defined(ENABLE_JAVASE)
java_lang_ThreadGroup *threadgroup;
java_lang_VMThread *vmt;
#endif
+ pthread_attr_t attr;
+
/* get methods we need in this file */
#if defined(WITH_CLASSPATH_GNU)
utf_new_char("(Ljava/lang/VMThread;Ljava/lang/String;IZ)V"),
class_java_lang_Thread,
true);
-#else
+#elif defined(WITH_CLASSPATH_SUN)
method_thread_init =
class_resolveclassmethod(class_java_lang_Thread,
utf_init,
utf_new_char("(Ljava/lang/String;)V"),
class_java_lang_Thread,
true);
+#elif defined(WITH_CLASSPATH_CLDC1_1)
+ method_thread_init =
+ class_resolveclassmethod(class_java_lang_Thread,
+ utf_init,
+ utf_new_char("(Ljava/lang/String;)V"),
+ class_java_lang_Thread,
+ true);
+#else
+# error unknown classpath configuration
#endif
if (method_thread_init == NULL)
return false;
/* Get the main-thread (NOTE: The main threads is always the first
- thread in the table). */
+ thread in the list). */
- mainthread = threads_table_first();
+ mainthread = threads_list_first();
/* create a java.lang.Thread for the main thread */
/* set the thread */
- vmt->thread = t;
- vmt->vmdata = (java_lang_Object *) mainthread;
+ LLNI_field_set_ref(vmt, thread, t);
+ LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) mainthread);
/* call java.lang.Thread.<init>(Ljava/lang/VMThread;Ljava/lang/String;IZ)V */
- o = (java_objectheader *) t;
+ o = (java_handle_t *) t;
(void) vm_call_method(method_thread_init, o, vmt, threadname, NORM_PRIORITY,
false);
+
+#elif defined(WITH_CLASSPATH_SUN)
+
+ /* We trick java.lang.Thread.<init>, which sets the priority of
+ the current thread to the parent's one. */
+
+ t->priority = NORM_PRIORITY;
+
+ /* Call java.lang.Thread.<init>(Ljava/lang/String;)V */
+
+ o = (java_object_t *) t;
+
+ (void) vm_call_method(method_thread_init, o, threadname);
+
#elif defined(WITH_CLASSPATH_CLDC1_1)
+
/* set the thread */
t->vm_thread = (java_lang_Object *) mainthread;
/* call public Thread(String name) */
- o = (java_objectheader *) t;
+ o = (java_handle_t *) t;
(void) vm_call_method(method_thread_init, o, threadname);
+#else
+# error unknown classpath configuration
#endif
- if (*exceptionptr)
+ if (exceptions_get_exception())
return false;
#if defined(ENABLE_JAVASE)
- t->group = threadgroup;
+ LLNI_field_set_ref(t, group, threadgroup);
+# if defined(WITH_CLASSPATH_GNU)
/* add main thread to java.lang.ThreadGroup */
m = class_resolveclassmethod(class_java_lang_ThreadGroup,
class_java_lang_ThreadGroup,
true);
- o = (java_objectheader *) threadgroup;
+ o = (java_handle_t *) threadgroup;
(void) vm_call_method(m, o, t);
- if (*exceptionptr)
+ if (exceptions_get_exception())
return false;
+# else
+# warning Do not know what to do here
+# endif
#endif
threads_set_thread_priority(pthread_self(), NORM_PRIORITY);
/* initialize the thread attribute object */
- if (pthread_attr_init(&threadattr)) {
- log_println("pthread_attr_init failed: %s", strerror(errno));
- return false;
- }
+ if (pthread_attr_init(&attr) != 0)
+ vm_abort("threads_init: pthread_attr_init failed: %s", strerror(errno));
+
+ if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
+ vm_abort("threads_init: pthread_attr_setdetachstate failed: %s",
+ strerror(errno));
- pthread_attr_setdetachstate(&threadattr, PTHREAD_CREATE_DETACHED);
+#if !defined(NDEBUG)
+ if (opt_verbosethreads) {
+ printf("[Starting thread ");
+ threads_thread_print_info(mainthread);
+ printf("]\n");
+ }
+#endif
/* everything's ok */
threads_startup.
IN:
- t............the argument passed to pthread_create, ie. a pointer to
+ arg..........the argument passed to pthread_create, ie. a pointer to
a startupinfo struct. CAUTION: When the `psem` semaphore
is posted, the startupinfo struct becomes invalid! (It
is allocated on the stack of threads_start_thread.)
******************************************************************************/
-static void *threads_startup_thread(void *t)
+static void *threads_startup_thread(void *arg)
{
startupinfo *startup;
threadobject *thread;
sem_t *psem;
classinfo *c;
methodinfo *m;
- java_objectheader *o;
+ java_handle_t *o;
functionptr function;
#if defined(ENABLE_INTRP)
/* get passed startupinfo structure and the values in there */
- startup = t;
- t = NULL; /* make sure it's not used wrongly */
+ startup = arg;
thread = startup->thread;
function = startup->function;
psem = startup->psem;
- /* Seems like we've encountered a situation where thread->tid was not set by
- * pthread_create. We alleviate this problem by waiting for pthread_create
- * to return. */
+ /* Seems like we've encountered a situation where thread->tid was
+ not set by pthread_create. We alleviate this problem by waiting
+ for pthread_create to return. */
+
threads_sem_wait(startup->psem_first);
#if defined(__DARWIN__)
threads_set_current_threadobject(thread);
- /* thread is running */
+ /* set our priority */
- thread->state = THREAD_STATE_RUNNABLE;
+ threads_set_thread_priority(thread->tid, LLNI_field_direct(thread->object, priority));
- /* insert the thread into the threads table */
+ /* thread is completely initialized */
- threads_table_add(thread);
+ threads_thread_state_runnable(thread);
/* tell threads_startup_thread that we registered ourselves */
/* CAUTION: *startup becomes invalid with this! */
startup = NULL;
threads_sem_post(psem);
- /* set our priority */
-
- threads_set_thread_priority(thread->tid, thread->object->priority);
-
#if defined(ENABLE_INTRP)
/* set interpreter stack */
jvmti_ThreadStartEnd(JVMTI_EVENT_THREAD_START);
#endif
+#if !defined(NDEBUG)
+ if (opt_verbosethreads) {
+ printf("[Starting thread ");
+ threads_thread_print_info(thread);
+ printf("]\n");
+ }
+#endif
+
/* find and run the Thread.run()V method if no other function was passed */
if (function == NULL) {
- /* this is a normal Java thread */
-
- thread->flags |= THREAD_FLAG_JAVA;
-
#if defined(WITH_CLASSPATH_GNU)
/* We need to start the run method of
java.lang.VMThread. Since this is a final class, we can use
the class object directly. */
- c = class_java_lang_VMThread;
-#elif defined(WITH_CLASSPATH_CLDC1_1)
- c = thread->object->header.vftbl->class;
+ c = class_java_lang_VMThread;
+#elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
+ c = thread->object->header.vftbl->class;
+#else
+# error unknown classpath configuration
#endif
m = class_resolveclassmethod(c, utf_run, utf_void__void, c, true);
#if defined(WITH_CLASSPATH_GNU)
/* we need to start the run method of java.lang.VMThread */
- vmt = (java_lang_VMThread *) thread->object->vmThread;
- o = (java_objectheader *) vmt;
+ vmt = (java_lang_VMThread *) LLNI_field_direct(thread->object, vmThread);
+ o = (java_handle_t *) vmt;
-#elif defined(WITH_CLASSPATH_CLDC1_1)
- o = (java_objectheader *) thread->object;
+#elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
+ o = (java_handle_t *) thread->object;
+#else
+# error unknown classpath configuration
#endif
/* run the thread */
(void) vm_call_method(m, o);
}
else {
- /* this is an internal thread */
-
- thread->flags |= THREAD_FLAG_INTERNAL;
-
/* set ThreadMXBean variables */
_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
(function)();
}
+#if !defined(NDEBUG)
+ if (opt_verbosethreads) {
+ printf("[Stopping thread ");
+ threads_thread_print_info(thread);
+ printf("]\n");
+ }
+#endif
+
#if defined(ENABLE_JVMTI)
/* fire thread end event */
jvmti_ThreadStartEnd(JVMTI_EVENT_THREAD_END);
#endif
- if (!threads_detach_thread(thread))
- vm_abort("threads_startup_thread: threads_detach_thread failed");
+ /* We ignore the return value. */
+
+ (void) threads_detach_thread(thread);
/* set ThreadMXBean variables */
sem_t sem_first;
pthread_attr_t attr;
startupinfo startup;
+ int ret;
/* fill startupinfo structure passed by pthread_create to
* threads_startup_thread */
threads_sem_init(&sem, 0, 0);
threads_sem_init(&sem_first, 0, 0);
- /* initialize thread attribute object */
+ /* initialize thread attributes */
+
+ if (pthread_attr_init(&attr) != 0)
+ vm_abort("threads_impl_thread_start: pthread_attr_init failed: %s",
+ strerror(errno));
- if (pthread_attr_init(&attr))
- vm_abort("pthread_attr_init failed: %s", strerror(errno));
+ if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
+ vm_abort("threads_impl_thread_start: pthread_attr_setdetachstate failed: %s",
+ strerror(errno));
/* initialize thread stacksize */
if (pthread_attr_setstacksize(&attr, opt_stacksize))
- vm_abort("pthread_attr_setstacksize failed: %s", strerror(errno));
+ vm_abort("threads_impl_thread_start: pthread_attr_setstacksize failed: %s",
+ strerror(errno));
/* create the thread */
- if (pthread_create(&(thread->tid), &attr, threads_startup_thread, &startup))
- vm_abort("pthread_create failed: %s", strerror(errno));
+ ret = pthread_create(&(thread->tid), &attr, threads_startup_thread, &startup);
+
+ /* destroy the thread attributes */
+
+ if (pthread_attr_destroy(&attr) != 0)
+ vm_abort("threads_impl_thread_start: pthread_attr_destroy failed: %s",
+ strerror(errno));
+
+ /* check for pthread_create error */
+
+ if (ret != 0)
+ vm_abort("threads_impl_thread_start: pthread_create failed: %s",
+ strerror(errno));
/* signal that pthread_create has returned, so thread->tid is valid */
{
threadobject *thread;
utf *u;
- java_objectheader *s;
- java_objectheader *o;
+ java_handle_t *s;
+ java_handle_t *o;
java_lang_Thread *t;
#if defined(ENABLE_JAVASE)
java_lang_ThreadGroup *group;
threadobject *mainthread;
+ classinfo *c;
methodinfo *m;
#endif
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_create_thread();
+ thread = threads_thread_new();
+
+ /* thread is a Java thread and running */
+
+ thread->flags = THREAD_FLAG_JAVA;
+
+ if (isdaemon)
+ thread->flags |= THREAD_FLAG_DAEMON;
+
+ /* The thread is flagged and (non-)daemon thread, we can leave the
+ mutex. */
+
+ threads_mutex_join_unlock();
/* create a java.lang.Thread object */
t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
+ /* XXX memory leak!!! */
if (t == NULL)
return false;
thread->object = t;
- /* thread is a Java thread and running */
-
- thread->flags = THREAD_FLAG_JAVA;
-
- if (isdaemon)
- thread->flags |= THREAD_FLAG_DAEMON;
+ /* thread is completely initialized */
- thread->state = THREAD_STATE_RUNNABLE;
+ threads_thread_state_runnable(thread);
- /* insert the thread into the threads table */
-
- threads_table_add(thread);
+#if !defined(NDEBUG)
+ if (opt_verbosethreads) {
+ printf("[Attaching thread ");
+ threads_thread_print_info(thread);
+ printf("]\n");
+ }
+#endif
#if defined(ENABLE_INTRP)
/* create interpreter stack */
#endif
#if defined(WITH_CLASSPATH_GNU)
+
/* create a java.lang.VMThread object */
vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
+ /* XXX memory leak!!! */
if (vmt == NULL)
return false;
/* set the thread */
- vmt->thread = t;
- vmt->vmdata = (java_lang_Object *) thread;
+ LLNI_field_set_ref(vmt, thread, t);
+ LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) thread);
+
+#elif defined(WITH_CLASSPATH_SUN)
+
+ vm_abort("threads_attach_current_thread: IMPLEMENT ME!");
+
#elif defined(WITH_CLASSPATH_CLDC1_1)
- t->vm_thread = (java_lang_Object *) thread;
+
+ LLNI_field_set_val(t, vm_thread, (java_lang_Object *) thread);
+
+#else
+# error unknown classpath configuration
#endif
if (vm_aargs != NULL) {
#if defined(ENABLE_JAVASE)
/* get the main thread */
- mainthread = threads_table_first();
- group = mainthread->object->group;
+ mainthread = threads_list_first();
+ group = LLNI_field_direct(mainthread->object, group);
#endif
}
/* for convenience */
- o = (java_objectheader *) thread->object;
+ o = (java_handle_t *) thread->object;
#if defined(WITH_CLASSPATH_GNU)
(void) vm_call_method(method_thread_init, o, vmt, s, NORM_PRIORITY,
(void) vm_call_method(method_thread_init, o, s);
#endif
- if (*exceptionptr)
+ if (exceptions_get_exception())
return false;
#if defined(ENABLE_JAVASE)
/* store the thread group in the object */
- thread->object->group = group;
+ LLNI_field_direct(thread->object, group) = group;
/* add thread to given thread-group */
- m = class_resolveclassmethod(group->header.vftbl->class,
+ LLNI_class_get(group, c);
+
+ m = class_resolveclassmethod(c,
utf_addThread,
utf_java_lang_Thread__V,
class_java_lang_ThreadGroup,
true);
- o = (java_objectheader *) group;
+ o = (java_handle_t *) group;
(void) vm_call_method(m, o, t);
- if (*exceptionptr)
+ if (exceptions_get_exception())
return false;
#endif
*******************************************************************************/
-bool threads_detach_thread(threadobject *thread)
+bool threads_detach_thread(threadobject *t)
{
#if defined(ENABLE_JAVASE)
+ java_lang_Thread *object;
java_lang_ThreadGroup *group;
+ java_handle_t *e;
+ java_lang_Object *handler;
+ classinfo *c;
methodinfo *m;
- java_objectheader *o;
- java_lang_Thread *t;
+ java_handle_t *o;
#endif
- /* Allow lock record pools to be used by other threads. They
- cannot be deleted so we'd better not waste them. */
+#if defined(ENABLE_JAVASE)
+ object = t->object;
- /* XXX We have to find a new way to free lock records */
- /* with the new locking algorithm. */
- /* lock_record_free_pools(thread->ee.lockrecordpools); */
+ group = LLNI_field_direct(object, group);
- /* XXX implement uncaught exception stuff (like JamVM does) */
+ /* If there's an uncaught exception, call uncaughtException on the
+ thread's exception handler, or the thread's group if this is
+ unset. */
-#if defined(ENABLE_JAVASE)
- /* remove thread from the thread group */
+ e = exceptions_get_and_clear_exception();
+
+ if (e != NULL) {
+ /* We use a java_lang_Object here, as it's not trivial to
+ build the java_lang_Thread_UncaughtExceptionHandler header
+ file. */
+
+# if defined(WITH_CLASSPATH_GNU)
+ handler = (java_lang_Object *) LLNI_field_direct(object, exceptionHandler);
+# elif defined(WITH_CLASSPATH_SUN)
+ handler = (java_lang_Object *) LLNI_field_direct(object, uncaughtExceptionHandler);
+# endif
+
+ if (handler != NULL) {
+ LLNI_class_get(handler, c);
+ o = (java_handle_t *) handler;
+ }
+ else {
+ LLNI_class_get(group, c);
+ o = (java_handle_t *) group;
+ }
+
+ m = class_resolveclassmethod(c,
+ utf_uncaughtException,
+ utf_java_lang_Thread_java_lang_Throwable__V,
+ NULL,
+ true);
+
+ if (m == NULL)
+ return false;
+
+ (void) vm_call_method(m, o, object, e);
- group = thread->object->group;
+ if (exceptions_get_exception())
+ return false;
+ }
/* XXX TWISTI: should all threads be in a ThreadGroup? */
+ /* Remove thread from the thread group. */
+
if (group != NULL) {
- m = class_resolveclassmethod(group->header.vftbl->class,
+ LLNI_class_get(group, c);
+
+# if defined(WITH_CLASSPATH_GNU)
+ m = class_resolveclassmethod(c,
utf_removeThread,
utf_java_lang_Thread__V,
class_java_lang_ThreadGroup,
true);
+# elif defined(WITH_CLASSPATH_SUN)
+ m = class_resolveclassmethod(c,
+ utf_remove,
+ utf_java_lang_Thread__V,
+ class_java_lang_ThreadGroup,
+ true);
+# else
+# error unknown classpath configuration
+# endif
if (m == NULL)
return false;
- o = (java_objectheader *) group;
- t = thread->object;
+ o = (java_handle_t *) group;
- (void) vm_call_method(m, o, t);
+ (void) vm_call_method(m, o, object);
- if (*exceptionptr)
+ if (exceptions_get_exception())
return false;
}
#endif
/* thread is terminated */
- thread->state = THREAD_STATE_TERMINATED;
-
- /* remove thread from the threads table */
+ threads_thread_state_terminated(t);
- threads_table_remove(thread);
+#if !defined(NDEBUG)
+ if (opt_verbosethreads) {
+ printf("[Detaching thread ");
+ threads_thread_print_info(t);
+ printf("]\n");
+ }
+#endif
- /* signal that this thread has finished */
+ /* Enter the join-mutex before calling threads_thread_free, so
+ threads_join_all_threads gets the correct number of non-daemon
+ threads. */
- pthread_mutex_lock(&mutex_join);
- pthread_cond_signal(&cond_join);
- pthread_mutex_unlock(&mutex_join);
+ threads_mutex_join_lock();
/* free the vm internal thread object */
-#if defined(ENABLE_GC_BOEHM)
- GCFREE(thread);
-#else
- FREE(thread, threadobject);
-#endif
+ threads_thread_free(t);
-#if defined(ENABLE_STATISTICS)
- if (opt_stat)
- size_threadobject -= sizeof(threadobject);
-#endif
+ /* Signal that this thread has finished and leave the mutex. */
+
+ pthread_cond_signal(&cond_join);
+ threads_mutex_join_unlock();
return true;
}
void threads_join_all_threads(void)
{
- threadobject *thread;
+ threadobject *t;
/* get current thread */
- thread = THREADOBJECT;
+ t = THREADOBJECT;
/* this thread is waiting for all non-daemon threads to exit */
- thread->state = THREAD_STATE_WAITING;
+ threads_thread_state_waiting(t);
/* enter join mutex */
- pthread_mutex_lock(&mutex_join);
+ threads_mutex_join_lock();
/* Wait for condition as long as we have non-daemon threads. We
compare against 1 because the current (main thread) is also a
non-daemon thread. */
- while (threads_table_get_non_daemons() > 1)
+ while (threads_list_get_non_daemons() > 1)
pthread_cond_wait(&cond_join, &mutex_join);
/* leave join mutex */
- pthread_mutex_unlock(&mutex_join);
+ threads_mutex_join_unlock();
}
while (!thread->interrupted && !thread->signaled
&& threads_current_time_is_earlier_than(wakeupTime))
{
- thread->state = THREAD_STATE_TIMED_WAITING;
+ threads_thread_state_timed_waiting(thread);
pthread_cond_timedwait(&thread->waitcond, &thread->waitmutex,
wakeupTime);
- thread->state = THREAD_STATE_RUNNABLE;
+ threads_thread_state_runnable(thread);
}
}
else {
/* no timeout */
while (!thread->interrupted && !thread->signaled) {
- thread->state = THREAD_STATE_WAITING;
+ threads_thread_state_waiting(thread);
pthread_cond_wait(&thread->waitcond, &thread->waitmutex);
- thread->state = THREAD_STATE_RUNNABLE;
+ threads_thread_state_runnable(thread);
}
}