diff options
author | Per Bothner <per@bothner.com> | 2001-05-21 23:47:48 -0700 |
---|---|---|
committer | Per Bothner <bothner@gcc.gnu.org> | 2001-05-21 23:47:48 -0700 |
commit | c93d7fae7b9825449c023d42eb40641c0ad80be2 (patch) | |
tree | ecfc0fb5d915f5f6596ef061114d1b6b7ec5270a /libjava/java/lang/natThread.cc | |
parent | b4fbaca7cb5c6ff5fb5e24e554d510257cb488eb (diff) | |
download | gcc-c93d7fae7b9825449c023d42eb40641c0ad80be2.zip gcc-c93d7fae7b9825449c023d42eb40641c0ad80be2.tar.gz gcc-c93d7fae7b9825449c023d42eb40641c0ad80be2.tar.bz2 |
Implement invocation interface; don't create new thread for main.
From-SVN: r42428
Diffstat (limited to 'libjava/java/lang/natThread.cc')
-rw-r--r-- | libjava/java/lang/natThread.cc | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/libjava/java/lang/natThread.cc b/libjava/java/lang/natThread.cc index 042ec36..66ce7d2 100644 --- a/libjava/java/lang/natThread.cc +++ b/libjava/java/lang/natThread.cc @@ -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; +} |