gcc/libjava/java/lang/Thread.java

290 lines
6.2 KiB
Java
Raw Normal View History

1999-04-07 16:42:40 +02:00
// Thread.java - Thread class.
/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
1999-04-07 16:42:40 +02:00
This file is part of libgcj.
This software is copyrighted work licensed under the terms of the
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */
package java.lang;
natNativeThread.cc: New file. * gnu/gcj/jni/natNativeThread.cc: New file. * gnu/gcj/jni/NativeThread.java: New file. * java/lang/Thread.java (data): Now a RawData. * include/jvm.h (_Jv_GetCurrentJNIEnv, _Jv_SetCurrentJNIEnv): Declare. * Makefile.in: Rebuilt. * Makefile.am (java/lang/Thread.h): New target. (ordinary_java_source_files): Added NativeThread.java. (nat_source_files): Added natNativeThread.cc. * java/lang/natThread.cc: Include <jni.h> (struct natThread): Added `jni_env' field. (_Jv_GetCurrentJNIEnv): New function. (_Jv_SetCurrentJNIEnv): Likewise. (initialize_native): Initialize jni_env. Include RawData.h. * jni.cc (ThreadGroupClass): New define. (_Jv_JNI_InvokeFunctions): New structure. (JNI_GetCreatedJavaVMs): New function. (the_vm): New global. (JNI_GetDefaultJavaVMInitArgs): New function. Include NativeThread.h. (NativeThreadClass): New define. (_Jv_JNI_EnsureLocalCapacity): Return JNI_ERR, not -1. (_Jv_JNI_DestroyJavaVM): New function. (_Jv_JNI_AttachCurrentThread): New function. (_Jv_JNI_DetachCurrentThread): New function. (_Jv_JNI_GetEnv): New function. (JNI_CreateJavaVM): New function. (_Jv_JNI_GetJavaVM): New function. (_Jv_JNIFunctions): Added entry for GetJavaVM. * include/jni.h (JavaVMAttachArgs): New structure. (JNI_EDETACHED): New define. (JNI_EVERSION): Likewise. (JavaVM): Define properly. (struct JNIInvokeInterface): New structure. (class _Jv_JavaVM): New class. (JNI_OnLoad, JNI_OnUnload): Declare. (JNI_GetDefaultJavaVMInitArgs, JNI_CreateJavaVM, JNI_GetCreatedJavaVMs): Declare. (JavaVMInitArgs): New typedef. (JavaVMOption): Likewise. (JNI_ERR): New define. (JNI_OK): Likewise. From-SVN: r31901
2000-02-10 21:31:48 +01:00
import gnu.gcj.RawData;
1999-04-07 16:42:40 +02:00
/**
* @author Tom Tromey <tromey@cygnus.com>
* @date August 24, 1998
*/
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
* "The Java Language Specification", ISBN 0-201-63451-1
* plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
* Status: Complete to version 1.1, with caveats
* Known problems:
* No attempt was made to implement suspend/resume
* (this could be done in some cases)
* Various methods which assume a VM are likewise unimplemented
* We do implement stop() even though it is deprecated.
*/
public class Thread implements Runnable
{
public final static int MAX_PRIORITY = 10;
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public static int activeCount ()
{
return currentThread().getThreadGroup().activeCount();
}
public void checkAccess ()
{
SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkAccess(this);
}
public native int countStackFrames ();
public static native Thread currentThread ();
public native void destroy ();
public static void dumpStack ()
{
(new Exception ("Stack trace")).printStackTrace ();
}
1999-04-07 16:42:40 +02:00
public static int enumerate (Thread[] threads)
{
return currentThread().group.enumerate(threads);
}
public final String getName ()
{
return name;
}
public final int getPriority ()
{
return priority;
}
public final ThreadGroup getThreadGroup ()
{
return group;
}
public native void interrupt ();
public static boolean interrupted ()
{
return currentThread().isInterrupted_();
1999-04-07 16:42:40 +02:00
}
// FIXME: it seems to me that this should be synchronized.
// Check the threads interrupted status. Note that this does not clear the
// threads interrupted status (per JDK 1.2 online API documentation).
1999-04-07 16:42:40 +02:00
public boolean isInterrupted ()
{
return interrupt_flag;
1999-04-07 16:42:40 +02:00
}
public final boolean isAlive ()
{
return alive_flag;
}
public final boolean isDaemon ()
{
return daemon_flag;
}
public final void join () throws InterruptedException
{
join (0, 0);
}
public final void join (long timeout) throws InterruptedException
{
join (timeout, 0);
}
public final native void join (long timeout, int nanos)
throws InterruptedException;
public final native void resume ();
// This method exists only to avoid a warning from the C++ compiler.
java-interp.h: Don't include MethodInvocation.h. * include/java-interp.h: Don't include MethodInvocation.h. (class _Jv_InterpMethod): Don't make MethodInvocation a friend. * Makefile.in: Rebuilt. * Makefile.am (gnu/gcj/runtime/MethodInvocation.h): Removed. (ordinary_java_source_files): Don't mention MethodInvocation.java. * gnu/gcj/runtime/MethodInvocation.java: Removed. * interpret.cc (MethodInvocation::continue1): Removed. (run): Handle exceptions here. * java/lang/ClassLoader.java (defineClass1, defineClass2): Removed. * java/lang/natClassLoader.cc (defineClass0): Catch exceptions here. (defineClass2): Removed. * java/lang/reflect/Method.java (hack_trampoline, hack_call): Removed. * java/lang/reflect/natMethod.cc (_Jv_CallAnyMethodA): Catch exceptions here. (hack_call): Removed. * java/lang/Class.h (Class): Removed hackRunInitializers, hackTrampoline. * java/lang/natClass.cc (hackRunInitializers): Removed. (initializeClass): Catch exceptions here. Include ExceptionInInitializerError.h. * java/lang/Class.java (hackTrampoline, hackRunInitializers): Removed. * java/lang/Object.h (Object): Don't mention hack12_6. * java/lang/natObject.cc (_Jv_FinalizeObject): Catch exceptions here. * java/lang/Object.java (hack12_6): Removed. * java/lang/natThread.cc (run_): Renamed. Catch exceptions here. (start): Use run_, not run__. * java/lang/Thread.java (run_): Renamed from run__; old run_ removed. * jni.cc (_Jv_JNI_FindClass): Handle exceptions. (_Jv_JNI_EnsureLocalCapacity): Likewise. (_Jv_JNI_DefineClass): Likewise. (_Jv_JNI_ThrowNew): Likewise. (_Jv_JNI_AllocObject): Likewise. (_Jv_JNI_GetAnyMethodID): Likewise. (_Jv_JNI_CallAnyMethodV): Likewise. (_Jv_JNI_CallAnyMethodA): Likewise. (_Jv_JNI_CallAnyVoidMethodV): Likewise. (_Jv_JNI_CallAnyVoidMethodA): Likewise. (_Jv_JNI_GetAnyFieldID): Likewise. (_Jv_JNI_NewString): Likewise. (_Jv_JNI_NewStringUTF): Likewise. (_Jv_JNI_GetStringUTFChars): Likewise. (_Jv_JNI_NewObjectArray): Likewise. (_Jv_JNI_NewPrimitiveArray): Likewise. (_Jv_JNI_GetPrimitiveArrayRegion): Likewise. (_Jv_JNI_GetStringRegion): Likewise. (_Jv_JNI_GetStringUTFRegion): Likewise. (_Jv_JNI_SetPrimitiveArrayRegion): Likewise. (_Jv_JNI_MonitorEnter): Likewise. (_Jv_JNI_MonitorExit): Likewise. (_Jv_JNI_ToReflectedField): Likewise. (_Jv_JNI_ToReflectedMethod): Likewise. (_Jv_JNI_RegisterNatives): Likewise. (_Jv_JNI_AttachCurrentThread): Likewise. (_Jv_JNI_DestroyJavaVM): Likewise. From-SVN: r32294
2000-03-02 21:25:20 +01:00
private static final native void run_ (Object obj);
private final native void finish_ ();
// Convenience method to check and clear the thread's interrupted status.
private boolean isInterrupted_ ()
{
boolean r = interrupt_flag;
interrupt_flag = false;
return r;
}
1999-04-07 16:42:40 +02:00
public void run ()
{
if (runnable != null)
runnable.run();
}
public final void setDaemon (boolean status)
{
checkAccess ();
if (isAlive ())
throw new IllegalThreadStateException ();
daemon_flag = status;
}
// TODO12:
// public ClassLoader getContextClassLoader()
// {
// }
// TODO12:
// public void setContextClassLoader(ClassLoader cl)
// {
// }
public final void setName (String n)
{
checkAccess ();
// The Class Libraries book says ``threadName cannot be null''. I
// take this to mean NullPointerException.
if (n == null)
throw new NullPointerException ();
name = n;
}
public final native void setPriority (int newPriority);
public static void sleep (long timeout) throws InterruptedException
{
sleep (timeout, 0);
}
public static native void sleep (long timeout, int nanos)
throws InterruptedException;
public synchronized native void start ();
public final void stop ()
{
// Argument doesn't matter, because this is no longer
// supported.
stop (null);
1999-04-07 16:42:40 +02:00
}
public final synchronized native void stop (Throwable e);
public final native void suspend ();
private final native void initialize_native ();
private final synchronized static String gen_name ()
{
String n;
n = "Thread-" + nextThreadNumber;
++nextThreadNumber;
return n;
}
public Thread (ThreadGroup g, Runnable r, String n)
{
// Note that CURRENT can be null when we are creating the very
// first thread. That's why we check it below.
Thread current = currentThread ();
if (g != null)
{
// If CURRENT is null, then we are creating the first thread.
// In this case we don't do the security check.
if (current != null)
g.checkAccess();
}
else
g = current.getThreadGroup();
// The Class Libraries book says ``threadName cannot be null''. I
// take this to mean NullPointerException.
if (n == null)
throw new NullPointerException ();
name = n;
group = g;
g.add(this);
runnable = r;
data = null;
interrupt_flag = false;
alive_flag = false;
if (current != null)
{
daemon_flag = current.isDaemon();
priority = current.getPriority();
}
else
{
daemon_flag = false;
priority = NORM_PRIORITY;
}
initialize_native ();
}
public Thread ()
{
this (null, null, gen_name ());
}
public Thread (Runnable r)
{
this (null, r, gen_name ());
}
public Thread (String n)
{
this (null, null, n);
}
public Thread (ThreadGroup g, Runnable r)
{
this (g, r, gen_name ());
}
public Thread (ThreadGroup g, String n)
{
this (g, null, n);
}
public Thread (Runnable r, String n)
{
this (null, r, n);
}
public String toString ()
{
return "Thread[" + name + "," + priority + "," + group.getName() + "]";
}
public static native void yield ();
// Private data.
private ThreadGroup group;
private String name;
private Runnable runnable;
private int priority;
private boolean daemon_flag;
private boolean interrupt_flag;
private boolean alive_flag;
natNativeThread.cc: New file. * gnu/gcj/jni/natNativeThread.cc: New file. * gnu/gcj/jni/NativeThread.java: New file. * java/lang/Thread.java (data): Now a RawData. * include/jvm.h (_Jv_GetCurrentJNIEnv, _Jv_SetCurrentJNIEnv): Declare. * Makefile.in: Rebuilt. * Makefile.am (java/lang/Thread.h): New target. (ordinary_java_source_files): Added NativeThread.java. (nat_source_files): Added natNativeThread.cc. * java/lang/natThread.cc: Include <jni.h> (struct natThread): Added `jni_env' field. (_Jv_GetCurrentJNIEnv): New function. (_Jv_SetCurrentJNIEnv): Likewise. (initialize_native): Initialize jni_env. Include RawData.h. * jni.cc (ThreadGroupClass): New define. (_Jv_JNI_InvokeFunctions): New structure. (JNI_GetCreatedJavaVMs): New function. (the_vm): New global. (JNI_GetDefaultJavaVMInitArgs): New function. Include NativeThread.h. (NativeThreadClass): New define. (_Jv_JNI_EnsureLocalCapacity): Return JNI_ERR, not -1. (_Jv_JNI_DestroyJavaVM): New function. (_Jv_JNI_AttachCurrentThread): New function. (_Jv_JNI_DetachCurrentThread): New function. (_Jv_JNI_GetEnv): New function. (JNI_CreateJavaVM): New function. (_Jv_JNI_GetJavaVM): New function. (_Jv_JNIFunctions): Added entry for GetJavaVM. * include/jni.h (JavaVMAttachArgs): New structure. (JNI_EDETACHED): New define. (JNI_EVERSION): Likewise. (JavaVM): Define properly. (struct JNIInvokeInterface): New structure. (class _Jv_JavaVM): New class. (JNI_OnLoad, JNI_OnUnload): Declare. (JNI_GetDefaultJavaVMInitArgs, JNI_CreateJavaVM, JNI_GetCreatedJavaVMs): Declare. (JavaVMInitArgs): New typedef. (JavaVMOption): Likewise. (JNI_ERR): New define. (JNI_OK): Likewise. From-SVN: r31901
2000-02-10 21:31:48 +01:00
// Our native data.
private RawData data;
1999-04-07 16:42:40 +02:00
// Next thread number to assign.
private static int nextThreadNumber = 0;
}