* src/threads/thread.cpp, src/threads/thread.hpp (thread_is_interrupted,
[cacao.git] / src / threads / thread.cpp
index 3f32509178cb5b385bb64ac10d5ec5738f550bec..cd91b5372535c0a5a4f271c987aae12a2af897ec 100644 (file)
@@ -31,7 +31,7 @@
 
 #include "vm/types.h"
 
-#include "mm/memory.h"
+#include "mm/memory.hpp"
 
 #if defined(ENABLE_GC_BOEHM)
 /* We need to include Boehm's gc.h here for GC_register_my_thread and
 #endif
 
 #include "native/llni.h"
-#include "native/native.h"
+#include "native/native.hpp"
 
 #include "threads/lock.hpp"
-#include "threads/threadlist.h"
+#include "threads/threadlist.hpp"
 #include "threads/thread.hpp"
 
 #include "vm/jit/builtin.hpp"
-#include "vm/class.h"
+#include "vm/class.hpp"
 #include "vm/exceptions.hpp"
 #include "vm/globals.hpp"
 #include "vm/javaobjects.hpp"
-#include "vm/method.h"
+#include "vm/method.hpp"
 #include "vm/options.h"
 #include "vm/string.hpp"
 #include "vm/utf8.h"
@@ -405,7 +405,7 @@ static void thread_create_initial_thread(void)
        /* Get the main-thread (NOTE: The main thread is always the first
           thread in the list). */
 
-       t = threadlist_first();
+       t = ThreadList::get_main_thread();
 
        /* The thread name. */
 
@@ -447,21 +447,17 @@ static threadobject *thread_new(void)
        
        /* Lock the thread lists */
 
-       threadlist_lock();
+       ThreadList::lock();
 
-       index = threadlist_get_free_index();
+       index = ThreadList::get_free_thread_index();
 
        /* Allocate a thread data structure. */
 
        /* First, try to get one from the free-list. */
 
-       t = threadlist_free_first();
+       t = ThreadList::get_free_thread();
 
        if (t != NULL) {
-               /* Remove from free list. */
-
-               threadlist_free_remove(t);
-
                /* Equivalent of MZERO on the else path */
 
                threads_impl_thread_clear(t);
@@ -512,7 +508,7 @@ static threadobject *thread_new(void)
        assert(index != 0);
 
        t->index     = index;
-       t->thinlock  = lock_pre_compute_thinlock(t->index);
+       t->thinlock  = Lockword::pre_compute_thinlock(t->index);
        t->flags     = 0;
        t->state     = THREAD_STATE_NEW;
 
@@ -526,11 +522,11 @@ static threadobject *thread_new(void)
 
        /* Add the thread to the thread list. */
 
-       threadlist_add(t);
+       ThreadList::add_to_active_thread_list(t);
 
        /* Unlock the thread lists. */
 
-       threadlist_unlock();
+       ThreadList::unlock();
 
        return t;
 }
@@ -549,29 +545,13 @@ static threadobject *thread_new(void)
 
 void thread_free(threadobject *t)
 {
-       /* Lock the thread lists. */
-
-       threadlist_lock();
-
-       /* Remove the thread from the thread-list. */
-
-       threadlist_remove(t);
-
-       /* Add the thread index to the free list. */
-
-       threadlist_index_add(t->index);
-
        /* Set the reference to the Java object to NULL. */
 
        thread_set_object(t, NULL);
 
-       /* Add the thread data structure to the free list. */
-
-       threadlist_free_add(t);
-
-       /* Unlock the thread lists. */
+       /* Release the thread. */
 
-       threadlist_unlock();
+       ThreadList::release_thread(t);
 }
 
 
@@ -895,7 +875,7 @@ void thread_fprint_name(threadobject *t, FILE *stream)
 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK) || defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
 
        /* FIXME: In OpenJDK and CLDC the name is a char[]. */
-       java_chararray_t *name;
+       //java_chararray_t *name;
 
        /* FIXME This prints to stdout. */
        utf_display_printable_ascii(utf_null);
@@ -968,6 +948,12 @@ void thread_print_info(threadobject *t)
        case THREAD_STATE_TIMED_WAITING:
                printf(" waiting on condition");
                break;
+       case THREAD_STATE_PARKED:
+               printf(" parked");
+               break;
+       case THREAD_STATE_TIMED_PARKED:
+               printf(" timed parked");
+               break;
        case THREAD_STATE_TERMINATED:
                printf(" terminated");
                break;
@@ -1001,6 +987,26 @@ intptr_t threads_get_current_tid(void)
 }
 
 
+/**
+ * Set the current state of the given thread. This method should only
+ * be called while holding the threadlist-lock and after checking that
+ * the new state is valid. It is best to not call this method directly
+ * but call the specific setter methods below.
+ */
+static inline void thread_set_state(threadobject *t, int state)
+{
+       // Set the state of our internal threadobject.
+       t->state = state;
+
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
+       // Set the state of the java.lang.Thread object.
+       java_lang_Thread thread(thread_get_object(t));
+       assert(thread.is_non_null());
+       thread.set_threadStatus(state);
+#endif
+}
+
+
 /* thread_set_state_runnable ***************************************************
 
    Set the current state of the given thread to THREAD_STATE_RUNNABLE.
@@ -1014,15 +1020,15 @@ void thread_set_state_runnable(threadobject *t)
 {
        /* Set the state inside a lock. */
 
-       threadlist_lock();
+       ThreadList::lock();
 
        if (t->state != THREAD_STATE_TERMINATED) {
-               t->state = THREAD_STATE_RUNNABLE;
+               thread_set_state(t, THREAD_STATE_RUNNABLE);
 
                DEBUGTHREADS("is RUNNABLE", t);
        }
 
-       threadlist_unlock();
+       ThreadList::unlock();
 }
 
 
@@ -1039,15 +1045,15 @@ void thread_set_state_waiting(threadobject *t)
 {
        /* Set the state inside a lock. */
 
-       threadlist_lock();
+       ThreadList::lock();
 
        if (t->state != THREAD_STATE_TERMINATED) {
-               t->state = THREAD_STATE_WAITING;
+               thread_set_state(t, THREAD_STATE_WAITING);
 
                DEBUGTHREADS("is WAITING", t);
        }
 
-       threadlist_unlock();
+       ThreadList::unlock();
 }
 
 
@@ -1065,15 +1071,65 @@ void thread_set_state_timed_waiting(threadobject *t)
 {
        /* Set the state inside a lock. */
 
-       threadlist_lock();
+       ThreadList::lock();
 
        if (t->state != THREAD_STATE_TERMINATED) {
-               t->state = THREAD_STATE_TIMED_WAITING;
+               thread_set_state(t, THREAD_STATE_TIMED_WAITING);
 
                DEBUGTHREADS("is TIMED_WAITING", t);
        }
 
-       threadlist_unlock();
+       ThreadList::unlock();
+}
+
+
+/* thread_set_state_parked *****************************************************
+
+   Set the current state of the given thread to THREAD_STATE_PARKED.
+
+   NOTE: If the thread has already terminated, don't set the state.
+         This is important for threads_detach_thread.
+
+*******************************************************************************/
+
+void thread_set_state_parked(threadobject *t)
+{
+       /* Set the state inside a lock. */
+
+       ThreadList::lock();
+
+       if (t->state != THREAD_STATE_TERMINATED) {
+               thread_set_state(t, THREAD_STATE_PARKED);
+
+               DEBUGTHREADS("is PARKED", t);
+       }
+
+       ThreadList::unlock();
+}
+
+
+/* thread_set_state_timed_parked ***********************************************
+
+   Set the current state of the given thread to THREAD_STATE_TIMED_PARKED.
+
+   NOTE: If the thread has already terminated, don't set the state.
+         This is important for threads_detach_thread.
+
+*******************************************************************************/
+
+void thread_set_state_timed_parked(threadobject *t)
+{
+       /* Set the state inside a lock. */
+
+       ThreadList::lock();
+
+       if (t->state != THREAD_STATE_TERMINATED) {
+               thread_set_state(t, THREAD_STATE_TIMED_PARKED);
+
+               DEBUGTHREADS("is TIMED_PARKED", t);
+       }
+
+       ThreadList::unlock();
 }
 
 
@@ -1088,13 +1144,13 @@ void thread_set_state_terminated(threadobject *t)
 {
        /* Set the state inside a lock. */
 
-       threadlist_lock();
+       ThreadList::lock();
 
-       t->state = THREAD_STATE_TERMINATED;
+       thread_set_state(t, THREAD_STATE_TERMINATED);
 
        DEBUGTHREADS("is TERMINATED", t);
 
-       threadlist_unlock();
+       ThreadList::unlock();
 }
 
 
@@ -1120,23 +1176,11 @@ threadobject *thread_get_thread(java_handle_t *h)
 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
 
        /* XXX This is just a quick hack. */
-       threadobject* t;
-       bool          equal;
-
-       threadlist_lock();
-
-       for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
-               LLNI_equals(t->object, h, equal);
-
-               if (equal == true)
-                       break;
-       }
-
-       threadlist_unlock();
+       threadobject* t = ThreadList::get_thread_from_java_object(h);
 
 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
 
-       log_println("threads_get_thread: IMPLEMENT ME!");
+       log_println("thread_get_thread: IMPLEMENT ME!");
        threadobject* t = NULL;
 
 #else
@@ -1168,6 +1212,8 @@ bool threads_thread_is_alive(threadobject *t)
        case THREAD_STATE_BLOCKED:
        case THREAD_STATE_WAITING:
        case THREAD_STATE_TIMED_WAITING:
+       case THREAD_STATE_PARKED:
+       case THREAD_STATE_TIMED_PARKED:
                return true;
 
        default:
@@ -1179,65 +1225,50 @@ bool threads_thread_is_alive(threadobject *t)
        return false;
 }
 
+/* thread_is_interrupted *******************************************************
 
-/* threads_dump ****************************************************************
+   Check if the given thread has been interrupted.
+
+   ARGUMENTS:
+       t ... the thread to check
 
-   Dumps info for all threads running in the JVM.  This function is
-   called when SIGQUIT (<ctrl>-\) is sent to CACAO.
+   RETURN VALUE:
+      true, if the given thread had been interrupted
 
 *******************************************************************************/
 
-void threads_dump(void)
+bool thread_is_interrupted(threadobject *t)
 {
-       threadobject *t;
-
-       /* XXX we should stop the world here */
-
-       /* Lock the thread lists. */
-
-       threadlist_lock();
-
-       printf("Full thread dump CACAO "VERSION":\n");
+       /* We need the mutex because classpath will call this function when
+          a blocking system call is interrupted. The mutex ensures that it will
+          see the correct value for the interrupted flag. */
 
-       /* iterate over all started threads */
-
-       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
+       t->waitmutex->lock();
+       bool interrupted = t->interrupted;
+       t->waitmutex->unlock();
 
-               /* Print thread info. */
-
-               printf("\n");
-               thread_print_info(t);
-               printf("\n");
+       return interrupted;
+}
 
-               /* Print trace of thread. */
 
-               stacktrace_print_of_thread(t);
+/* thread_set_interrupted ******************************************************
 
-#if defined(ENABLE_GC_CACAO)
-               /* Resume the thread. */
+   Set the interrupted flag to the given value.
 
-               if (threads_resume_thread(t) == false)
-                       vm_abort("threads_dump: threads_resume_thread failed");
-#endif
-       }
+   ARGUMENTS:
+       interrupted ... value to set
 
-       /* Unlock the thread lists. */
+*******************************************************************************/
 
-       threadlist_unlock();
+void thread_set_interrupted(threadobject *t, bool interrupted)
+{
+       t->waitmutex->lock();
+       t->interrupted = interrupted;
+       t->waitmutex->unlock();
 }
 
 
+
 /*
  * 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