* Removed all Id tags.
[cacao.git] / src / native / vm / java_lang_Thread.c
index 860eef14d68820039137a430aaea4719edf2ca57..a823022f355be84757dc174346be60bc92b9dc0c 100644 (file)
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: java_lang_VMThread.c 6213 2006-12-18 17:36:06Z twisti $
-
 */
 
 
 #include "vm/types.h"
 
 #include "native/jni.h"
+#include "native/llni.h"
 #include "native/native.h"
 
-#if defined(ENABLE_JAVASE)
-# include "native/include/java_lang_ThreadGroup.h"
-# include "native/include/java_lang_VMThread.h"
-#endif
-
 #include "native/include/java_lang_String.h"
 #include "native/include/java_lang_Object.h"            /* java_lang_Thread.h */
 #include "native/include/java_lang_Throwable.h"         /* java_lang_Thread.h */
 #include "native/include/java_lang_Thread.h"
 
-#if defined(ENABLE_THREADS)
-# include "threads/native/threads.h"
+#if defined(ENABLE_JAVASE)
+# include "native/include/java_lang_ThreadGroup.h"
+
+# if defined(WITH_CLASSPATH_GNU)
+#  include "native/include/java_lang_VMThread.h"
+# endif
 #endif
 
+#include "threads/lock-common.h"
+#include "threads/threads-common.h"
+
 #include "toolbox/logging.h"
 
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
+#include "vm/stringlocal.h"
 
 #include "vmcore/options.h"
 
-/* XXX REMOVE ME only for vm_abort */
-#include "vm/vm.h"
-
 
 /*
  * Class:     java/lang/Thread
@@ -92,7 +91,7 @@ void _Jv_java_lang_Thread_sleep(s8 millis)
 void _Jv_java_lang_Thread_start(java_lang_Thread *this, s8 stacksize)
 {
 #if defined(ENABLE_THREADS)
-       threads_start_javathread(this);
+       threads_thread_start(this);
 #endif
 }
 
@@ -108,7 +107,7 @@ void _Jv_java_lang_Thread_interrupt(java_lang_Thread *this)
        threadobject *thread;
 
 #if defined(WITH_CLASSPATH_GNU)
-       thread = (threadobject *) this->vmThread->vmdata;
+       thread = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
 #elif defined(WITH_CLASSPATH_CLDC1_1)
        thread = (threadobject *) this->vm_thread;
 #endif
@@ -118,6 +117,55 @@ void _Jv_java_lang_Thread_interrupt(java_lang_Thread *this)
 }
 
 
+/*
+ * Class:     java/lang/Thread
+ * Method:    isAlive
+ * Signature: ()Z
+ */
+s4 _Jv_java_lang_Thread_isAlive(java_lang_Thread *this)
+{
+#if defined(ENABLE_THREADS)
+       threadobject *t;
+
+# if defined(WITH_CLASSPATH_GNU)
+
+       t = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
+
+# elif defined(WITH_CLASSPATH_SUN)
+
+       /* XXX this is just a quick hack */
+
+       for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
+               if (t->object == this)
+                       break;
+       }
+
+       /* The threadobject is null when a thread is created in Java. The
+          priority is set later during startup. */
+
+       if (t == NULL)
+               return 0;
+
+# elif defined(WITH_CLASSPATH_CLDC1_1)
+
+       t = (threadobject *) this->vm_thread;
+
+       if (t == NULL)
+               return 0;
+
+# else
+#  error unknown classpath configuration
+# endif
+
+       return threads_thread_is_alive(t);
+#else
+       /* if threads are disabled, the only thread running is alive */
+
+       return 1;
+#endif
+}
+
+
 /*
  * Class:     java/lang/Thread
  * Method:    isInterrupted
@@ -126,15 +174,24 @@ void _Jv_java_lang_Thread_interrupt(java_lang_Thread *this)
 s4 _Jv_java_lang_Thread_isInterrupted(java_lang_Thread *this)
 {
 #if defined(ENABLE_THREADS)
-       threadobject *thread;
+       threadobject *t;
 
-#if defined(WITH_CLASSPATH_GNU)
-       thread = (threadobject *) this->vmThread->vmdata;
-#elif defined(WITH_CLASSPATH_CLDC1_1)
-       thread = (threadobject *) this->vm_thread;
-#endif
+# if defined(WITH_CLASSPATH_GNU)
+       t = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
+# elif defined(WITH_CLASSPATH_SUN)
+       /* XXX this is just a quick hack */
 
-       return threads_thread_has_been_interrupted(thread);
+       for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
+               if (t->object == this)
+                       break;
+       }
+# elif defined(WITH_CLASSPATH_CLDC1_1)
+       t = (threadobject *) this->vm_thread;
+# else
+#  error unknown classpath configuration
+# endif
+
+       return threads_thread_has_been_interrupted(t);
 #else
        return 0;
 #endif
@@ -173,21 +230,36 @@ void _Jv_java_lang_Thread_resume(java_lang_Thread *this)
 void _Jv_java_lang_Thread_setPriority(java_lang_Thread *this, s4 priority)
 {
 #if defined(ENABLE_THREADS)
-       threadobject *thread;
+       threadobject *t;
 
-#if defined(WITH_CLASSPATH_GNU)
-       thread = (threadobject *) this->vmThread->vmdata;
-#elif defined(WITH_CLASSPATH_CLDC1_1)
-       thread = (threadobject *) this->vm_thread;
+# if defined(WITH_CLASSPATH_GNU)
+       t = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
+# elif defined(WITH_CLASSPATH_SUN)
+       /* XXX this is just a quick hack */
+
+       for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
+               if (t->object == this)
+                       break;
+       }
 
        /* The threadobject is null when a thread is created in Java. The
           priority is set later during startup. */
 
-       if (thread == NULL)
+       if (t == NULL)
                return;
-#endif
+# elif defined(WITH_CLASSPATH_CLDC1_1)
+       t = (threadobject *) this->vm_thread;
+
+       /* The threadobject is null when a thread is created in Java. The
+          priority is set later during startup. */
+
+       if (t == NULL)
+               return;
+# else
+#  error unknown classpath configuration
+# endif
 
-       threads_set_thread_priority(thread->tid, priority);
+       threads_set_thread_priority(t->tid, priority);
 #endif
 }
 
@@ -219,19 +291,19 @@ java_lang_Thread *_Jv_java_lang_Thread_currentThread(void)
 #if defined(ENABLE_THREADS)
        thread = THREADOBJECT;
 
-       t = (java_lang_Thread *) thread->object;
+       t = thread->object;
 
        if (t == NULL)
                log_text("t ptr is NULL\n");
-  
+
 # if defined(ENABLE_JAVASE)
-       if (t->group == NULL) {
+       if (LLNI_field_direct(t, group) == NULL) {
                /* ThreadGroup of currentThread is not initialized */
 
-               t->group = (java_lang_ThreadGroup *)
+               LLNI_field_direct(t, group) = (java_lang_ThreadGroup *)
                        native_new_and_init(class_java_lang_ThreadGroup);
 
-               if (t->group == NULL)
+               if (LLNI_field_direct(t, group) == NULL)
                        log_text("unable to create ThreadGroup");
        }
 # endif
@@ -282,9 +354,9 @@ s4 _Jv_java_lang_Thread_interrupted(void)
 s4 _Jv_java_lang_Thread_holdsLock(java_lang_Object* obj)
 {
 #if defined(ENABLE_THREADS)
-       java_objectheader *o;
+       java_handle_t *o;
 
-       o = (java_objectheader *) obj;
+       o = (java_handle_t *) obj;
 
        if (o == NULL) {
                exceptions_throw_nullpointerexception();
@@ -305,9 +377,24 @@ s4 _Jv_java_lang_Thread_holdsLock(java_lang_Object* obj)
  */
 java_lang_String *_Jv_java_lang_Thread_getState(java_lang_Thread *this)
 {
-       vm_abort("Java_java_lang_Thread_getState: IMPLEMENT ME!");
+#if defined(ENABLE_THREADS)
+       threadobject  *thread;
+       utf           *u;
+       java_handle_t *o;
 
+# if defined(WITH_CLASSPATH_GNU)
+       thread = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
+# elif defined(WITH_CLASSPATH_CLDC1_1)
+       thread = (threadobject *) this->vm_thread;
+# endif
+
+       u = threads_thread_get_state(thread);
+       o = javastring_new(u);
+
+       return (java_lang_String *) o;
+#else
        return NULL;
+#endif
 }