* src/threads/thread.cpp: Set thread to RUNNABLE during Thread.start.
[cacao.git] / src / threads / posix / thread-posix.cpp
index 8e855c90bed103e4784f7f862137f0f72a42b5f5..859354d2ef680c7bd30a26644c3a52ab768231ea 100644 (file)
@@ -1,6 +1,6 @@
 /* src/threads/posix/thread-posix.cpp - POSIX thread functions
 
-   Copyright (C) 1996-2005, 2006, 2007, 2008
+   Copyright (C) 1996-2011
    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
@@ -98,6 +98,8 @@ typedef struct {
 # define GC_IRIX_THREADS
 #elif defined(__DARWIN__)
 # define GC_DARWIN_THREADS
+#elif defined(__SOLARIS__)
+# define GC_SOLARIS_THREADS
 #endif
 
 #if defined(ENABLE_GC_BOEHM)
@@ -207,7 +209,6 @@ static Mutex* mutex_gc;
 #endif
 
 /* global mutex and condition for joining threads on exit */
-static Mutex* mutex_join;
 static Condition* cond_join;
 
 #if defined(ENABLE_GC_CACAO)
@@ -457,6 +458,7 @@ void threads_impl_thread_clear(threadobject *t)
        t->index = 0;
        t->flags = 0;
        t->state = 0;
+       t->is_in_active_list = false;
 
        t->tid = 0;
 
@@ -530,47 +532,11 @@ void threads_impl_thread_reuse(threadobject *t)
 #endif
 }
 
-
-/* threads_impl_thread_free ****************************************************
-
-   Cleanup thread stuff.
-
-   IN:
-      t....the threadobject
-
-*******************************************************************************/
-
-#if 0
-/* never used */
-void threads_impl_thread_free(threadobject *t)
+void threads_impl_clear_heap_pointers(threadobject *t)
 {
-       int result;
-
-       /* Destroy the mutex and the condition. */
-
-       delete t->flc_lock;
-
-       result = pthread_cond_destroy(&(t->flc_cond));
-
-       if (result != 0)
-               os::abort_errnum(result, "threads_impl_thread_free: pthread_cond_destroy failed");
-
-       delete t->waitmutex;
-
-       result = pthread_cond_destroy(&(t->waitcond));
-
-       if (result != 0)
-               os::abort_errnum(result, "threads_impl_thread_free: pthread_cond_destroy failed");
-
-       delete t->suspendmutex;
-
-       result = pthread_cond_destroy(&(t->suspendcond));
-
-       if (result != 0)
-               os::abort_errnum(result, "threads_impl_thread_free: pthread_cond_destroy failed");
+       t->object = 0;
+       t->flc_object = 0;
 }
-#endif
-
 
 /* threads_impl_preinit ********************************************************
 
@@ -583,14 +549,11 @@ void threads_impl_thread_free(threadobject *t)
 
 void threads_impl_preinit(void)
 {
-       int result;
-
        stopworldlock = new Mutex();
 
        /* initialize exit mutex and condition (on exit we join all
           threads) */
 
-       mutex_join = new Mutex();
        cond_join = new Condition();
 
 #if defined(ENABLE_GC_CACAO)
@@ -601,7 +564,7 @@ void threads_impl_preinit(void)
 #endif
 
 #if !defined(HAVE___THREAD)
-       result = pthread_key_create(&thread_current_key, NULL);
+       int result = pthread_key_create(&thread_current_key, NULL);
        if (result != 0)
                os::abort_errnum(result, "threads_impl_preinit: pthread_key_create failed");
 #endif
@@ -635,30 +598,6 @@ void threads_mutex_gc_unlock(void)
 }
 #endif
 
-/* threads_mutex_join_lock *****************************************************
-
-   Enter the join mutex.
-
-*******************************************************************************/
-
-void threads_mutex_join_lock(void)
-{
-       mutex_join->lock();
-}
-
-
-/* threads_mutex_join_unlock ***************************************************
-
-   Leave the join mutex.
-
-*******************************************************************************/
-
-void threads_mutex_join_unlock(void)
-{
-       mutex_join->unlock();
-}
-
-
 /* threads_impl_init ***********************************************************
 
    Initializes the implementation specific bits.
@@ -776,17 +715,13 @@ static void *threads_startup_thread(void *arg)
 #endif
 
        // Get the java.lang.Thread object for this thread.
-       java_handle_t* object = thread_get_object(t);
+       java_handle_t* object = LLNI_WRAP(t->object);
        java_lang_Thread jlt(object);
 
        /* set our priority */
 
        threads_set_thread_priority(t->tid, jlt.get_priority());
 
-       /* Thread is completely initialized. */
-
-       thread_set_state_runnable(t);
-
        /* tell threads_startup_thread that we registered ourselves */
        /* CAUTION: *startup becomes invalid with this!             */
 
@@ -808,36 +743,14 @@ static void *threads_startup_thread(void *arg)
        /* find and run the Thread.run()V method if no other function was passed */
 
        if (function == NULL) {
-#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
-               /* 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_JAVA_RUNTIME_LIBRARY_OPENJDK) || defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
-               LLNI_class_get(object, c);
-#else
-# error unknown classpath configuration
-#endif
+               c = ThreadRuntime::get_thread_class_from_object(object);
 
                m = class_resolveclassmethod(c, utf_run, utf_void__void, c, true);
 
                if (m == NULL)
                        vm_abort("threads_startup_thread: run() method not found in class");
 
-#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
-
-               // We need to start the run method of java.lang.VMThread.
-               java_lang_VMThread jlvmt(jlt.get_vmThread());
-               java_handle_t* h = jlvmt.get_handle();
-
-#elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK) || defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
-
-               java_handle_t* h = jlt.get_handle();
-
-#else
-# error unknown classpath configuration
-#endif
+               java_handle_t *h = ThreadRuntime::get_vmthread_handle(jlt);
 
                /* Run the thread. */
 
@@ -983,7 +896,7 @@ bool thread_detach_current_thread(void)
 
        DEBUGTHREADS("detaching", t);
 
-       java_handle_t* object = thread_get_object(t);
+       java_handle_t* object = LLNI_WRAP(t->object);
        java_lang_Thread jlt(object);
 
 #if defined(ENABLE_JAVASE)
@@ -1000,15 +913,7 @@ bool thread_detach_current_thread(void)
                   to build the java_lang_Thread_UncaughtExceptionHandler
                   header file with cacaoh. */
 
-# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
-
-               java_handle_t* handler = jlt.get_exceptionHandler();
-
-# elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
-
-               java_handle_t* handler = jlt.get_uncaughtExceptionHandler();
-
-# endif
+               java_handle_t *handler = ThreadRuntime::get_thread_exception_handler(jlt);
 
                classinfo*     c;
                java_handle_t* h;
@@ -1045,21 +950,7 @@ bool thread_detach_current_thread(void)
                classinfo* c;
                LLNI_class_get(group, c);
 
-# if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
-               methodinfo* m = class_resolveclassmethod(c,
-                                                                                                utf_removeThread,
-                                                                                                utf_java_lang_Thread__V,
-                                                                                                class_java_lang_ThreadGroup,
-                                                                                                true);
-# elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
-               methodinfo* m = class_resolveclassmethod(c,
-                                                                                                utf_remove,
-                                                                                                utf_java_lang_Thread__V,
-                                                                                                class_java_lang_ThreadGroup,
-                                                                                                true);
-# else
-#  error unknown classpath configuration
-# endif
+               methodinfo *m = ThreadRuntime::get_threadgroup_remove_method(c);
 
                if (m == NULL)
                        return false;
@@ -1090,11 +981,11 @@ bool thread_detach_current_thread(void)
        /* XXX Care about exceptions? */
        (void) lock_monitor_exit(jlt.get_handle());
 
-       /* Enter the join-mutex before calling thread_free, so
-          threads_join_all_threads gets the correct number of non-daemon
-          threads. */
+       t->waitmutex->lock();
+       t->tid = 0;
+       t->waitmutex->unlock();
 
-       threads_mutex_join_lock();
+       ThreadList::lock();
 
        /* Free the internal thread data-structure. */
 
@@ -1103,7 +994,10 @@ bool thread_detach_current_thread(void)
        /* Signal that this thread has finished and leave the mutex. */
 
        cond_join->signal();
-       threads_mutex_join_unlock();
+       ThreadList::unlock();
+
+       t->suspendmutex->lock();
+       t->suspendmutex->unlock();
 
        return true;
 }
@@ -1137,7 +1031,8 @@ static void threads_suspend_self()
 #endif
 
        // Release the suspension mutex and wait till we are resumed.
-       thread->suspendcond->wait(thread->suspendmutex);
+       while (thread->suspend_reason != SUSPEND_REASON_NONE)
+               thread->suspendcond->wait(thread->suspendmutex);
 
 #if defined(ENABLE_GC_CACAO)
        // XXX This is propably not ok!
@@ -1157,7 +1052,7 @@ static void threads_suspend_self()
 
 /**
  * Suspend the passed thread. Execution of that thread stops until the thread
- * is explicitly resumend again.
+ * is explicitly resumed again.
  *
  * @param thread The thread to be suspended.
  * @param reason Reason for suspending the given thread.
@@ -1189,13 +1084,14 @@ bool threads_suspend_thread(threadobject *thread, int32_t reason)
        }
        else {
                // Send the suspend signal to the other thread.
+               if (!thread->tid)
+                       return false;
                if (pthread_kill(thread->tid, SIGUSR1) != 0)
                        os::abort_errno("threads_suspend_thread: pthread_kill failed");
 
                // Wait for the thread to acknowledge the suspension.
-               // XXX A possible optimization would be to not wait here, but you
-               //     better think this through twice before trying it!
-               thread->suspendcond->wait(thread->suspendmutex);
+               while (!thread->suspended)
+                       thread->suspendcond->wait(thread->suspendmutex);
        }
 
        return true;
@@ -1271,18 +1167,18 @@ void threads_join_all_threads(void)
 
        /* enter join mutex */
 
-       threads_mutex_join_lock();
+       ThreadList::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 (ThreadList::get_number_of_non_daemon_threads() > 1)
-               cond_join->wait(mutex_join);
+               ThreadList::wait_cond(cond_join);
 
        /* leave join mutex */
 
-       threads_mutex_join_unlock();
+       ThreadList::unlock();
 }
 
 
@@ -1486,7 +1382,8 @@ void threads_thread_interrupt(threadobject *t)
 
        /* Interrupt blocking system call using a signal. */
 
-       pthread_kill(t->tid, Signal_INTERRUPT_SYSTEM_CALL);
+       if (t->tid)
+               pthread_kill(t->tid, Signal_INTERRUPT_SYSTEM_CALL);
 
        t->waitcond->signal();