GNU header update.
[cacao.git] / src / native / vm / VMThread.c
index 66fea7a7b3a2dc43c0e994b50c07509c697baf52..dd179e82dc479d89caadc50da153297d49b0bcb2 100644 (file)
@@ -1,16 +1,61 @@
-/* class: java/lang/Thread */
+/* native/vm/VMThread.c - java/lang/VMThread
 
+   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
+   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
+   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
+   Institut f. Computersprachen - TU Wien
 
-#include "jni.h"
+   This file is part of CACAO.
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2, or (at
+   your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+   02111-1307, USA.
+
+   Contact: cacao@complang.tuwien.ac.at
+
+   Authors: Roman Obermaiser
+
+   Changes: Joseph Wenninger
+
+   $Id: VMThread.c 1735 2004-12-07 14:33:27Z twisti $
+
+*/
+
+
+#include "config.h"
 #include "types.h"
-#include "native.h"
-#include "loader.h"
-#include "tables.h"
-#include "threads/thread.h"
-#include "java_lang_ThreadGroup.h"
-#include "java_lang_Object.h"         /* needed for java_lang_Thread.h */
-#include "java_lang_Throwable.h"      /* needed for java_lang_Thread.h */
-#include "java_lang_Thread.h"
+#include "native/jni.h"
+#include "native/native.h"
+#include "native/include/java_lang_ThreadGroup.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_VMThread.h"
+#include "native/include/java_lang_Thread.h"
+
+#if defined(USE_THREADS)
+# if defined(NATIVE_THREADS)
+#  include "threads/native/threads.h"
+# else
+#  include "threads/green/threads.h"
+# endif
+#endif
+
+#include "toolbox/logging.h"
+#include "vm/builtin.h"
+#include "vm/exceptions.h"
+#include "vm/options.h"
+#include "vm/tables.h"
 
 
 /*
  * Method:    countStackFrames
  * Signature: ()I
  */
-JNIEXPORT s4 JNICALL Java_java_lang_Thread_countStackFrames ( JNIEnv *env ,  struct java_lang_Thread* this)
+JNIEXPORT s4 JNICALL Java_java_lang_VMThread_countStackFrames(JNIEnv *env, java_lang_VMThread *this)
 {
-    log_text ("java_lang_Thread_countStackFrames called");
-    return 0;         /* not yet implemented */
+    log_text("java_lang_VMThread_countStackFrames called");
+
+    return 0;
 }
 
 /*
@@ -29,61 +75,75 @@ JNIEXPORT s4 JNICALL Java_java_lang_Thread_countStackFrames ( JNIEnv *env ,  str
  * Method:    currentThread
  * Signature: ()Ljava/lang/Thread;
  */
-JNIEXPORT struct java_lang_Thread* JNICALL Java_java_lang_Thread_currentThread ( JNIEnv *env ,jclass clazz )
+JNIEXPORT java_lang_Thread* JNICALL Java_java_lang_VMThread_currentThread(JNIEnv *env, jclass clazz)
 {
-  struct java_lang_Thread* t;
-
-  if (runverbose)
-    log_text ("java_lang_Thread_currentThread called");
-
+       java_lang_Thread *t;
 
-  #ifdef USE_THREADS
+       if (runverbose)
+               log_text("java_lang_VMThread_currentThread called");
 
-        t = (struct java_lang_Thread*) currentThread; 
+#if defined(USE_THREADS)
+#if !defined(NATIVE_THREADS)
+       t = (java_lang_Thread *) currentThread;
+#else
+       t = ((threadobject*) THREADOBJECT)->o.thread;
+#endif
   
-        if (!t->group) {
-               log_text("java_lang_Thread_currentThread: t->group=NULL");
-               /* ThreadGroup of currentThread is not initialized */
+       if (!t->group) {
+               log_text("java_lang_VMThread_currentThread: t->group=NULL");
+               /* ThreadGroup of currentThread is not initialized */
 
-               t->group = (java_lang_ThreadGroup *) 
-                       native_new_and_init(loader_load(utf_new_char("java/lang/ThreadGroup")));
+               t->group = (java_lang_ThreadGroup *) 
+                       native_new_and_init(class_new(utf_new_char("java/lang/ThreadGroup")));
 
-               if (t->group == 0) 
-                       log_text("unable to create ThreadGroup");
+               if (t->group == 0) 
+                       log_text("unable to create ThreadGroup");
        }
 
-       return (struct java_lang_Thread*) currentThread;
-  #else
+       return t;
+#else
        return 0;       
-  #endif
+#endif
 }
 
+
 /*
  * Class:     java/lang/Thread
  * Method:    nativeInterrupt
  * Signature: ()V
  */
-JNIEXPORT void JNICALL Java_java_lang_Thread_nativeInterrupt ( JNIEnv *env ,  struct java_lang_Thread* this)
+JNIEXPORT void JNICALL Java_java_lang_VMThread_interrupt(JNIEnv *env, java_lang_VMThread *this)
 {
-  log_text("Java_java_lang_Thread_interrupt0  called");
-  /* not yet implemented */
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+       interruptThread(this);
+#else
+       log_text("Java_java_lang_VMThread_interrupt0 called");
+#endif
 }
 
+
 /*
  * Class:     java/lang/Thread
  * Method:    isAlive
  * Signature: ()Z
  */
-JNIEXPORT s4 JNICALL Java_java_lang_Thread_isAlive ( JNIEnv *env ,  struct java_lang_Thread* this)
+JNIEXPORT s4 JNICALL Java_java_lang_VMThread_isAlive(JNIEnv *env, java_lang_VMThread *this)
 {
-    if (runverbose)
-       log_text ("java_lang_Thread_isAlive called");
+       if (runverbose)
+               log_text("java_lang_VMThread_isAlive called");
 
-#ifdef USE_THREADS
-    return aliveThread((thread*)this);
+#if defined(USE_THREADS)
+#if !defined(NATIVE_THREADS)
+       return aliveThread((thread *) this->thread);
 #else
-    return 0;
+       /* This method is implemented in classpath. */
+       throw_cacao_exception_exit(string_java_lang_InternalError, "aliveThread");
+#endif
 #endif
+
+       /* keep compiler happy */
+
+       return 0;
 }
 
 
@@ -93,49 +153,56 @@ JNIEXPORT s4 JNICALL Java_java_lang_Thread_isAlive ( JNIEnv *env ,  struct java_
  * Method:    isInterrupted
  * Signature: ()Z
  */
-JNIEXPORT s4 JNICALL Java_java_lang_Thread_isInterrupted (JNIEnv *env ,  struct java_lang_Thread* this )
+JNIEXPORT s4 JNICALL Java_java_lang_VMThread_isInterrupted(JNIEnv *env, java_lang_VMThread *this)
 {
-    log_text("Java_java_lang_Thread_isInterrupted  called");
-    return 0;                  /* not yet implemented */
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+       return isInterruptedThread(this);
+#else
+       log_text("Java_java_lang_VMThread_isInterrupted  called");
+       return 0;
+#endif
 }
 
+
 /*
  * Class:     java/lang/Thread
  * Method:    registerNatives
  * Signature: ()V
  */
-JNIEXPORT void JNICALL Java_java_lang_Thread_registerNatives ( JNIEnv *env ,jclass clazz )
+JNIEXPORT void JNICALL Java_java_lang_VMThread_registerNatives(JNIEnv *env, jclass clazz)
 {
-  /* empty */
+       /* empty */
 }
 
+
 /*
  * Class:     java/lang/Thread
  * Method:    resume0
  * Signature: ()V
  */
-JNIEXPORT void JNICALL Java_java_lang_Thread_nativeResume ( JNIEnv *env ,  struct java_lang_Thread* this)
+JNIEXPORT void JNICALL Java_java_lang_VMThread_resume(JNIEnv *env, java_lang_VMThread *this)
 {
-    if (runverbose)
-       log_text ("java_lang_Thread_resume0 called");
+       if (runverbose)
+               log_text("java_lang_VMThread_resume0 called");
 
-#ifdef USE_THREADS
-    resumeThread((thread*)this);
+#if defined(USE_THREADS) && !defined(NATIVE_THREADS)
+       resumeThread((thread *) this->thread);
 #endif
 }
 
+
 /*
  * Class:     java/lang/Thread
  * Method:    setPriority0
  * Signature: (I)V
  */
-JNIEXPORT void JNICALL Java_java_lang_Thread_nativeSetPriority ( JNIEnv *env ,  struct java_lang_Thread* this, s4 par1)
+JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeSetPriority(JNIEnv *env, java_lang_VMThread *this, s4 par1)
 {
     if (runverbose) 
-       log_text ("java_lang_Thread_setPriority0 called");
+               log_text("java_lang_VMThread_setPriority0 called");
 
-#ifdef USE_THREADS
-    setPriorityThread((thread*)this, par1);
+#if defined(USE_THREADS)
+       setPriorityThread((thread *) this->thread, par1);
 #endif
 }
 
@@ -145,119 +212,138 @@ JNIEXPORT void JNICALL Java_java_lang_Thread_nativeSetPriority ( JNIEnv *env ,
  * Method:    sleep
  * Signature: (JI)V
  */
-JNIEXPORT void JNICALL Java_java_lang_Thread_sleep (JNIEnv *env , jclass clazz, s8 millis, s4 par2)
+JNIEXPORT void JNICALL Java_java_lang_VMThread_sleep(JNIEnv *env, jclass clazz, s8 millis, s4 nanos)
 {
-    if (runverbose)
-       log_text ("java_lang_Thread_sleep called");
-
-#ifdef USE_THREADS
-    sleepThread(millis);
+#if defined(USE_THREADS)
+       sleepThread(millis, nanos);
 #endif
 }
 
+
 /*
  * Class:     java/lang/Thread
  * Method:    start
  * Signature: ()V
  */
-JNIEXPORT void JNICALL Java_java_lang_Thread_start ( JNIEnv *env ,  struct java_lang_Thread* this)
+JNIEXPORT void JNICALL Java_java_lang_VMThread_start(JNIEnv *env, java_lang_VMThread *this, s8 par1)
 {
-  if (runverbose) 
-    log_text ("java_lang_Thread_start called");
-    /*printf("THREAD PRIORITY: %d\n",this->priority);*/
+       if (runverbose) 
+               log_text("java_lang_VMThread_start called");
 
-  #ifdef USE_THREADS
-       startThread((thread*)this);
-  #endif
+#if defined(USE_THREADS)
+       this->thread->vmThread = this;
+       startThread((thread *) this->thread);
+#endif
 }
 
+
 /*
  * Class:     java/lang/Thread
  * Method:    stop0
  * Signature: (Ljava/lang/Object;)V
  */
-JNIEXPORT void JNICALL Java_java_lang_Thread_nativeStop ( JNIEnv *env ,  struct java_lang_Thread* this, struct java_lang_Throwable* par1)
+JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeStop(JNIEnv *env, java_lang_VMThread *this, java_lang_Throwable *par1)
 {
-  if (runverbose)
-    log_text ("java_lang_Thread_stop0 called");
-
-
-  #ifdef USE_THREADS
-       if (currentThread == (thread*)this)
-       {
-           log_text("killing");
-           killThread(0);
-           /*
-               exceptionptr = proto_java_lang_ThreadDeath;
-               return;
-           */
+       if (runverbose)
+               log_text ("java_lang_VMThread_stop0 called");
+
+
+#if defined(USE_THREADS) && !defined(NATIVE_THREADS)
+       if (currentThread == (thread*)this->thread) {
+               log_text("killing");
+               killThread(0);
+               /*
+                 exceptionptr = proto_java_lang_ThreadDeath;
+                 return;
+               */
+
+       } else {
+               /*CONTEXT((thread*)this)*/ this->flags |= THREAD_FLAGS_KILLED;
+               resumeThread((thread*)this->thread);
        }
-       else
-       {
-               CONTEXT((thread*)this).flags |= THREAD_FLAGS_KILLED;
-               resumeThread((thread*)this);
-       }
-   #endif
+#endif
 }
 
+
 /*
  * Class:     java/lang/Thread
  * Method:    suspend0
  * Signature: ()V
  */
-JNIEXPORT void JNICALL Java_java_lang_Thread_nativeSuspend ( JNIEnv *env ,  struct java_lang_Thread* this)
+JNIEXPORT void JNICALL Java_java_lang_VMThread_suspend(JNIEnv *env, java_lang_VMThread *this)
 {
-  if (runverbose)
-    log_text ("java_lang_Thread_suspend0 called");
-
-  #ifdef USE_THREADS
-       suspendThread((thread*)this);
-  #endif
+       if (runverbose)
+               log_text("java_lang_VMThread_suspend0 called");
 
+#if defined(USE_THREADS) && !defined(NATIVE_THREADS)
+       suspendThread((thread*)this->thread);
+#endif
 }
 
+
 /*
  * Class:     java/lang/Thread
  * Method:    yield
  * Signature: ()V
  */
-JNIEXPORT void JNICALL Java_java_lang_Thread_yield ( JNIEnv *env  ,jclass clazz)
+JNIEXPORT void JNICALL Java_java_lang_VMThread_yield(JNIEnv *env, jclass clazz)
 {
-  if (runverbose)
-    log_text ("java_lang_Thread_yield called");
-  #ifdef USE_THREADS
+       if (runverbose)
+               log_text("java_lang_VMThread_yield called");
+
+#if defined(USE_THREADS)
        yieldThread();
-  #endif
+#endif
 }
 
+
 /*
  * Class:     java_lang_Thread
  * Method:    interrupted
  * Signature: ()Z
  */
-JNIEXPORT s4 JNICALL Java_java_lang_Thread_interrupted (JNIEnv *env ,jclass clazz) {
-       log_text("Java_java_lang_Thread_interrupted");
+JNIEXPORT s4 JNICALL Java_java_lang_VMThread_interrupted(JNIEnv *env, jclass clazz)
+{
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+       return interruptedThread();
+#else
+       log_text("Java_java_lang_VMThread_interrupted");
        return 0;
+#endif
 }
+
+
 /*
  * Class:     java_lang_Thread
  * Method:    nativeInit
  * Signature: (J)V
  */
-JNIEXPORT void JNICALL Java_java_lang_Thread_nativeInit (JNIEnv *env ,  struct java_lang_Thread* this , s8 par1) {
-       log_text("Thread_nativeInit");
-       if (exceptionptr) log_text("There has been an exception, strange...");
-       this->priority=5;
+JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeInit(JNIEnv *env, java_lang_VMThread *this, s8 par1)
+{
+/*
+       if (*exceptionptr)
+               log_text("There has been an exception, strange...");*/
+
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+       initThread(this);
+#endif
 }
 
+
 /*
- * Class:     java_lang_Thread
+ * Class:     java/lang/VMThread
  * Method:    holdsLock
  * Signature: (Ljava/lang/Object;)Z
  */
-JNIEXPORT s4 JNICALL Java_java_lang_Thread_holdsLock (JNIEnv *env , jclass clazz, struct java_lang_Object* par1)
+JNIEXPORT s4 JNICALL Java_java_lang_VMThread_holdsLock(JNIEnv *env, jclass clazz, java_lang_Object* o)
 {
-  return 0;
+#if defined(USE_THREADS) && defined(NATIVE_THREADS)
+       return threadHoldsLock((threadobject*) THREADOBJECT,
+                                                  (java_objectheader *) o);
+#else
+       /* I don't know how to find out [stefan] */
+       return 0;
+#endif
 }