Implement invocation interface; don't create new thread for main.

From-SVN: r42428
This commit is contained in:
Per Bothner
2001-05-21 23:47:48 -07:00
committed by Per Bothner
parent b4fbaca7cb
commit c93d7fae7b
22 changed files with 461 additions and 1945 deletions
+19 -25
View File
@@ -1,6 +1,6 @@
// Thread.java - Thread class.
/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
This file is part of libgcj.
@@ -209,18 +209,20 @@ public class Thread implements Runnable
private final native void initialize_native ();
private final synchronized static String gen_name ()
{
String n;
n = "Thread-" + nextThreadNumber;
++nextThreadNumber;
return n;
}
private final native static String gen_name ();
public Thread (ThreadGroup g, Runnable r, String n)
{
Thread current = currentThread ();
this (currentThread (), g, r, n);
// The Class Libraries book says ``threadName cannot be null''. I
// take this to mean NullPointerException.
if (n == null)
throw new NullPointerException ();
}
private Thread (Thread current, ThreadGroup g, Runnable r, String n)
{
if (g == null)
{
// If CURRENT is null, then we are bootstrapping the first thread.
@@ -233,17 +235,6 @@ public class Thread implements Runnable
else
group = g;
group.checkAccess();
// The Class Libraries book says ``threadName cannot be null''. I
// take this to mean NullPointerException.
if (n == null)
throw new NullPointerException ();
name = n;
group.addThread(this);
runnable = r;
data = null;
interrupt_flag = false;
alive_flag = false;
@@ -251,6 +242,8 @@ public class Thread implements Runnable
if (current != null)
{
group.checkAccess();
daemon_flag = current.isDaemon();
int gmax = group.getMaxPriority();
int pri = current.getPriority();
@@ -263,6 +256,10 @@ public class Thread implements Runnable
priority = NORM_PRIORITY;
}
name = n;
group.addThread(this);
runnable = r;
initialize_native ();
}
@@ -315,9 +312,6 @@ public class Thread implements Runnable
private boolean startable_flag;
private ClassLoader context_class_loader;
// Our native data.
// Our native data - points to an instance of struct natThread.
private Object data;
// Next thread number to assign.
private static int nextThreadNumber = 0;
}
+60 -2
View File
@@ -1,6 +1,6 @@
// natThread.cc - Native part of Thread class.
/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
This file is part of libgcj.
@@ -59,7 +59,7 @@ java::lang::Thread::initialize_native (void)
{
natThread *nt = (natThread *) _Jv_AllocBytes (sizeof (natThread));
// The native thread data is kept in a Object field, not a rawdata, so that
// The native thread data is kept in a Object field, not a RawData, so that
// the GC allocator can be used and a finalizer run after the thread becomes
// unreachable. Note that this relies on the GC's ability to finalize
// non-Java objects. FIXME?
@@ -322,6 +322,34 @@ java::lang::Thread::suspend (void)
(JvNewStringLatin1 ("java::lang::Thread::suspend unimplemented"));
}
static int nextThreadNumber = 0;
jstring
java::lang::Thread::gen_name (void)
{
jint i;
jclass sync = &java::lang::Thread::class$;
{
JvSynchronize dummy(sync);
i = ++nextThreadNumber;
}
// Use an array large enough for "-2147483648"; i.e. 11 chars, + "Thread-".
jchar buffer[7+11];
jchar *bufend = (jchar *) ((char *) buffer + sizeof(buffer));
i = _Jv_FormatInt (bufend, i);
jchar *ptr = bufend - i;
// Prepend "Thread-".
*--ptr = '-';
*--ptr = 'd';
*--ptr = 'a';
*--ptr = 'e';
*--ptr = 'r';
*--ptr = 'h';
*--ptr = 'T';
return JvNewString (ptr, bufend - ptr);
}
void
java::lang::Thread::yield (void)
{
@@ -344,3 +372,33 @@ _Jv_SetCurrentJNIEnv (JNIEnv *env)
JvAssert (t != NULL);
((natThread *) t->data)->jni_env = env;
}
java::lang::Thread*
_Jv_AttachCurrentThread(jstring name, java::lang::ThreadGroup* group)
{
java::lang::Thread *thread = _Jv_ThreadCurrent ();
if (thread != NULL)
return thread;
if (name == NULL)
name = java::lang::Thread::gen_name ();
thread = new java::lang::Thread (NULL, group, NULL, name);
thread->startable_flag = false;
thread->alive_flag = true;
natThread *nt = (natThread *) thread->data;
_Jv_ThreadRegister (nt->thread);
return thread;
}
jint
_Jv_DetachCurrentThread (void)
{
java::lang::Thread *t = _Jv_ThreadCurrent ();
if (t == NULL)
return -1;
_Jv_ThreadUnRegister ();
// Release the monitors.
t->finish_ ();
return 0;
}