* src/native/jni.c (jni_attach_current_thread): New help function.
authortwisti <none@none>
Thu, 5 Oct 2006 17:28:13 +0000 (17:28 +0000)
committertwisti <none@none>
Thu, 5 Oct 2006 17:28:13 +0000 (17:28 +0000)
(_Jv_JNI_AttachCurrentThread): Use helper function.
(_Jv_JNI_AttachCurrentThreadAsDaemon): Likewise.
(_Jv_JNI_DetachCurrentThread): Implemented.

* src/threads/native/threads.c (method_thread_init)
(method_threadgroup_add_: Made static.
(threads_init): Code rearranged.
(threads_startup_thread): Removed thread detach code and call
threads_detach_thread.
(threads_detach_thread): New function.

* src/threads/native/threads.h (threads_detach_thread): Added.

src/native/jni.c
src/threads/native/threads.c
src/threads/native/threads.h

index ecc2a6e5f8ab3b85d874a465f34bd8598ef70e30..bce898f4bc3f5bd0bbd0e79ecb40d082117d8291 100644 (file)
@@ -32,7 +32,7 @@
             Christian Thalinger
                        Edwin Steiner
 
-   $Id: jni.c 5691 2006-10-05 14:13:06Z twisti $
+   $Id: jni.c 5698 2006-10-05 17:28:13Z twisti $
 
 */
 
@@ -5540,14 +5540,10 @@ jint _Jv_JNI_DestroyJavaVM(JavaVM *vm)
 
 *******************************************************************************/
 
-jint _Jv_JNI_AttachCurrentThread(JavaVM *vm, void **p_env, void *thr_args)
+static s4 jni_attach_current_thread(void **p_env, void *thr_args, bool isdaemon)
 {
        JavaVMAttachArgs *vm_aargs;
 
-       STATISTICS(jniinvokation());
-
-       log_text("JNI-Call: AttachCurrentThread");
-
 #if defined(ENABLE_THREADS)
        if (threads_get_current_threadobject() == NULL) {
                vm_aargs = (JavaVMAttachArgs *) thr_args;
@@ -5572,6 +5568,14 @@ jint _Jv_JNI_AttachCurrentThread(JavaVM *vm, void **p_env, void *thr_args)
 }
 
 
+jint _Jv_JNI_AttachCurrentThread(JavaVM *vm, void **p_env, void *thr_args)
+{
+       STATISTICS(jniinvokation());
+
+       return jni_attach_current_thread(p_env, thr_args, false);
+}
+
+
 /* DetachCurrentThread *********************************************************
 
    Detaches the current thread from a Java VM. All Java monitors held
@@ -5591,11 +5595,19 @@ jint _Jv_JNI_AttachCurrentThread(JavaVM *vm, void **p_env, void *thr_args)
 
 jint _Jv_JNI_DetachCurrentThread(JavaVM *vm)
 {
+       threadobject *thread;
+
        STATISTICS(jniinvokation());
 
-       log_text("JNI-Call: DetachCurrentThread: IMPLEMENT ME!");
+       thread = threads_get_current_threadobject();
 
-       return 0;
+       if (thread == NULL)
+               return JNI_ERR;
+
+       if (!threads_detach_thread(thread))
+               return JNI_ERR;
+
+       return JNI_OK;
 }
 
 
@@ -5667,9 +5679,7 @@ jint _Jv_JNI_AttachCurrentThreadAsDaemon(JavaVM *vm, void **penv, void *args)
 {
        STATISTICS(jniinvokation());
 
-       log_text("JNI-Call: AttachCurrentThreadAsDaemon: IMPLEMENT ME!");
-
-       return 0;
+       return jni_attach_current_thread(penv, args, true);
 }
 
 
index 7400fc0438487da5a13537e722871ea7ecdf207a..74aac7df3ac9d22ce152f1b6760b78728160c68f 100644 (file)
@@ -29,7 +29,7 @@
    Changes: Christian Thalinger
                        Edwin Steiner
 
-   $Id: threads.c 5691 2006-10-05 14:13:06Z twisti $
+   $Id: threads.c 5698 2006-10-05 17:28:13Z twisti $
 
 */
 
@@ -212,8 +212,8 @@ static void threads_table_dump(FILE *file);
 /* the main thread                                                            */
 threadobject *mainthreadobj;
 
-methodinfo *method_thread_init;
-methodinfo *method_threadgroup_add;
+static methodinfo *method_thread_init;
+static methodinfo *method_threadgroup_add;
 
 /* the thread object of the current thread                                    */
 /* This is either a thread-local variable defined with __thread, or           */
@@ -751,6 +751,28 @@ bool threads_init(void)
 
        class_java_lang_VMThread->instancesize = sizeof(threadobject);
 
+       /* get methods we need in this file */
+
+       method_thread_init =
+               class_resolveclassmethod(class_java_lang_Thread,
+                                                                utf_init,
+                                                                utf_new_char("(Ljava/lang/VMThread;Ljava/lang/String;IZ)V"),
+                                                                class_java_lang_Thread,
+                                                                true);
+
+       if (method_thread_init == NULL)
+               return false;
+
+       method_threadgroup_add =
+               class_resolveclassmethod(class_java_lang_ThreadGroup,
+                                                                utf_new_char("addThread"),
+                                                                utf_new_char("(Ljava/lang/Thread;)V"),
+                                                                class_java_lang_ThreadGroup,
+                                                                true);
+
+       if (method_threadgroup_add == NULL)
+               return false;
+
        /* create a VMThread */
 
        mainthreadobj = (threadobject *) builtin_new(class_java_lang_VMThread);
@@ -805,16 +827,6 @@ bool threads_init(void)
 
        /* call Thread.<init>(Ljava/lang/VMThread;Ljava/lang/String;IZ)V */
 
-       method_thread_init =
-               class_resolveclassmethod(class_java_lang_Thread,
-                                                                utf_init,
-                                                                utf_new_char("(Ljava/lang/VMThread;Ljava/lang/String;IZ)V"),
-                                                                class_java_lang_Thread,
-                                                                true);
-
-       if (method_thread_init == NULL)
-               return false;
-
        (void) vm_call_method(method_thread_init, (java_objectheader *) mainthread,
                                                  mainthreadobj, threadname, NORM_PRIORITY, false);
 
@@ -825,16 +837,6 @@ bool threads_init(void)
 
        /* add mainthread to ThreadGroup */
 
-       method_threadgroup_add =
-               class_resolveclassmethod(class_java_lang_ThreadGroup,
-                                                                utf_new_char("addThread"),
-                                                                utf_new_char("(Ljava/lang/Thread;)V"),
-                                                                class_java_lang_ThreadGroup,
-                                                                true);
-
-       if (method_threadgroup_add == NULL)
-               return false;
-
        (void) vm_call_method(method_threadgroup_add, 
                                                  (java_objectheader *) threadgroup,
                                                  mainthread);
@@ -1107,13 +1109,12 @@ static void *threads_startup_thread(void *t)
                thread->_global_sp = (void *) (intrp_thread_stack + opt_stacksize);
 #endif
 
-
-
 #if defined(ENABLE_JVMTI)
        /* fire thread start event */
-       if (jvmti) jvmti_ThreadStartEnd(JVMTI_EVENT_THREAD_START);
-#endif
 
+       if (jvmti) 
+               jvmti_ThreadStartEnd(JVMTI_EVENT_THREAD_START);
+#endif
 
        /* find and run the Thread.run()V method if no other function was passed */
 
@@ -1153,37 +1154,12 @@ static void *threads_startup_thread(void *t)
 
 #if defined(ENABLE_JVMTI)
        /* fire thread end event */
-       if (jvmti) jvmti_ThreadStartEnd(JVMTI_EVENT_THREAD_END);
-#endif
-
-
-       /* Allow lock record pools to be used by other threads. They
-          cannot be deleted so we'd better not waste them. */
-
-       /* XXX We have to find a new way to free lock records */
-       /*     with the new locking algorithm.                */
-       /* lock_record_free_pools(thread->ee.lockrecordpools); */
-
-       /* remove thread from thread list and threads table, do this inside a lock */
-
-       pthread_mutex_lock(&threadlistlock);
-
-       thread->next->prev = thread->prev;
-       thread->prev->next = thread->next;
-
-       threads_table_remove(thread);
-
-       pthread_mutex_unlock(&threadlistlock);
-
-       /* reset thread id (lock on joinmutex? TWISTI) */
 
-       pthread_mutex_lock(&thread->joinmutex);
-       thread->tid = 0;
-       pthread_mutex_unlock(&thread->joinmutex);
-
-       /* tell everyone that a thread has finished */
+       if (jvmti)
+               jvmti_ThreadStartEnd(JVMTI_EVENT_THREAD_END);
+#endif
 
-       pthread_cond_broadcast(&thread->joincond);
+       threads_detach_thread(thread);
 
        return NULL;
 }
@@ -1253,46 +1229,46 @@ void threads_start_thread(java_lang_Thread *t, functionptr function)
 
 /* threads_attach_current_thread ***********************************************
 
-   Attaches the current thread to the VM.  Required in JNI.
+   Attaches the current thread to the VM.  Used in JNI.
 
 *******************************************************************************/
 
 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
 {
-       threadobject          *t;
-       java_lang_Thread      *thread;
+       threadobject          *thread;
+       java_lang_Thread      *t;
        utf                   *u;
        java_lang_String      *s;
-       char                  *name;
        java_lang_ThreadGroup *group;
+       java_objectheader     *o;
 
        /* create a java.lang.VMThread object */
 
-       t = (threadobject *) builtin_new(class_java_lang_VMThread);
+       thread = (threadobject *) builtin_new(class_java_lang_VMThread);
 
-       if (t == NULL)
+       if (thread == NULL)
                return false;
 
-       threads_init_threadobject(&t->o);
-       threads_set_current_threadobject(t);
-       lock_init_execution_env(t);
+       threads_init_threadobject(&thread->o);
+       threads_set_current_threadobject(thread);
+       lock_init_execution_env(thread);
 
        /* insert the thread into the threadlist and the threads table */
 
        pthread_mutex_lock(&threadlistlock);
 
-       t->prev             = mainthreadobj;
-       t->next             = mainthreadobj->next;
-       mainthreadobj->next = t;
-       t->next->prev       = t;
+       thread->prev        = mainthreadobj;
+       thread->next        = mainthreadobj->next;
+       mainthreadobj->next = thread;
+       thread->next->prev  = thread;
 
-       threads_table_add(t);
+       threads_table_add(thread);
 
        pthread_mutex_unlock(&threadlistlock);
 
        /* mark main thread as Java thread */
 
-       t->flags = THREAD_FLAG_JAVA;
+       thread->flags = THREAD_FLAG_JAVA;
 
 #if defined(ENABLE_INTRP)
        /* create interpreter stack */
@@ -1305,12 +1281,12 @@ bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
 
        /* create a java.lang.Thread object */
 
-       thread = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
+       t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
 
-       if (thread == NULL)
+       if (t == NULL)
                return false;
 
-       t->o.thread = thread;
+       thread->o.thread = t;
 
        if (vm_aargs != NULL) {
                u     = utf_new_char(vm_aargs->name);
@@ -1323,17 +1299,21 @@ bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
 
        s = javastring_new(u);
 
-       (void) vm_call_method(method_thread_init, (java_objectheader *) thread, t,
-                                                 s, NORM_PRIORITY, isdaemon);
+       o = (java_objectheader *) t;
+
+       (void) vm_call_method(method_thread_init, o, thread, s, NORM_PRIORITY,
+                                                 isdaemon);
 
        if (*exceptionptr)
                return false;
 
        /* store the thread group in the object */
 
-       thread->group = group;
+       t->group = group;
+
+       o = (java_objectheader *) group;
 
-       (void) vm_call_method(method_threadgroup_add, group, thread);
+       (void) vm_call_method(method_threadgroup_add, o, t);
 
        if (*exceptionptr)
                return false;
@@ -1342,6 +1322,79 @@ bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
 }
 
 
+/* threads_detach_thread *******************************************************
+
+   Detaches the passed thread from the VM.  Used in JNI.
+
+*******************************************************************************/
+
+bool threads_detach_thread(threadobject *thread)
+{
+       java_lang_Thread      *t;
+       java_lang_ThreadGroup *group;
+       methodinfo            *m;
+       java_objectheader     *o;
+
+       /* Allow lock record pools to be used by other threads. They
+          cannot be deleted so we'd better not waste them. */
+
+       /* XXX We have to find a new way to free lock records */
+       /*     with the new locking algorithm.                */
+       /* lock_record_free_pools(thread->ee.lockrecordpools); */
+
+       /* XXX implement uncaught exception stuff (like JamVM does) */
+
+       /* remove thread from the thread group */
+
+       t     = thread->o.thread;
+       group = t->group;
+
+       /* XXX TWISTI: should all threads be in a ThreadGroup? */
+
+       if (group != NULL) {
+               m = class_resolveclassmethod(group->header.vftbl->class,
+                                                                        utf_removeThread,
+                                                                        utf_java_lang_Thread__V,
+                                                                        class_java_lang_ThreadGroup,
+                                                                        true);
+
+               if (m == NULL)
+                       return false;
+
+               o = (java_objectheader *) group;
+
+               (void) vm_call_method(m, o, t);
+
+               if (*exceptionptr)
+                       return false;
+       }
+
+       /* remove thread from thread list and threads table, do this
+          inside a lock */
+
+       pthread_mutex_lock(&threadlistlock);
+
+       thread->next->prev = thread->prev;
+       thread->prev->next = thread->next;
+
+       threads_table_remove(thread);
+
+       pthread_mutex_unlock(&threadlistlock);
+
+       /* reset thread id (lock on joinmutex? TWISTI) */
+
+       pthread_mutex_lock(&thread->joinmutex);
+       thread->tid = 0;
+       pthread_mutex_unlock(&thread->joinmutex);
+
+       /* tell everyone that a thread has finished */
+
+       pthread_cond_broadcast(&thread->joincond);
+
+       return true;
+}
+
+
 /* threads_find_non_daemon_thread **********************************************
 
    Helper function used by threads_join_all_threads for finding
index c74ca6e3892e2e66e0ab0e81d09c4d10468ec02f..1f5da3c35544e1df5bbef27b2c2a9f8bf730575f 100644 (file)
@@ -29,7 +29,7 @@
 
    Changes: Christian Thalinger
 
-   $Id: threads.h 5691 2006-10-05 14:13:06Z twisti $
+   $Id: threads.h 5698 2006-10-05 17:28:13Z twisti $
 
 */
 
@@ -209,6 +209,7 @@ void threads_init_threadobject(java_lang_VMThread *);
 void threads_start_thread(java_lang_Thread *t, functionptr function);
 
 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
+bool threads_detach_thread(threadobject *thread);
 
 void threads_join_all_threads(void);