* src/threads/threads-common.c (threads_thread_start_internal):
authortwisti <none@none>
Sun, 3 Jun 2007 18:42:09 +0000 (18:42 +0000)
committertwisti <none@none>
Sun, 3 Jun 2007 18:42:09 +0000 (18:42 +0000)
Changed order of initialization stuff, so we can leave the join-mutex
very early.
(threads_thread_start): Likewise.

* src/threads/native/threads.c (threads_mutex_join_lock): New
function.
(threads_mutex_join_unlock): Likewise.
(threads_attach_current_thread): Changed order of initialization
stuff, so we can leave the join-mutex very early.
(threads_detach_thread): Send the signal inside the join-mutex.
(threads_join_all_threads): Use join-mutex functions.

* src/threads/threads-common.h (threads_mutex_join_lock): Added.
(threads_mutex_join_unlock): Likewise.

src/threads/native/threads.c
src/threads/threads-common.c
src/threads/threads-common.h

index 57f8401d2fd048abc3383b6223b0edc005a5b597..c2ed762d0f617a511a7450f1d25e5dacc628b414 100644 (file)
@@ -22,7 +22,7 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: threads.c 7984 2007-05-30 20:30:00Z twisti $
+   $Id: threads.c 8003 2007-06-03 18:42:09Z twisti $
 
 */
 
@@ -747,6 +747,34 @@ void threads_list_unlock(void)
 }
 
 
+/* threads_mutex_join_lock *****************************************************
+
+   Enter the join mutex.
+
+*******************************************************************************/
+
+void threads_mutex_join_lock(void)
+{
+       if (pthread_mutex_lock(&mutex_join) != 0)
+               vm_abort("threads_mutex_join_lock: pthread_mutex_lock failed: %s",
+                                strerror(errno));
+}
+
+
+/* threads_mutex_join_unlock ***************************************************
+
+   Leave the join mutex.
+
+*******************************************************************************/
+
+void threads_mutex_join_unlock(void)
+{
+       if (pthread_mutex_unlock(&mutex_join) != 0)
+               vm_abort("threads_mutex_join_unlock: pthread_mutex_unlock failed: %s",
+                                strerror(errno));
+}
+
+
 /* threads_init ****************************************************************
 
    Initializes the threads required by the JVM: main, finalizer.
@@ -1216,26 +1244,38 @@ bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
        java_lang_VMThread    *vmt;
 #endif
 
+       /* Enter the join-mutex, so if the main-thread is currently
+          waiting to join all threads, the number of non-daemon threads
+          is correct. */
+
+       threads_mutex_join_lock();
+
        /* create internal thread data-structure */
 
        thread = threads_thread_new();
 
+       /* thread is a Java thread and running */
+
+       thread->flags = THREAD_FLAG_JAVA;
+
+       if (isdaemon)
+               thread->flags |= THREAD_FLAG_DAEMON;
+
+       /* The thread is flagged and (non-)daemon thread, we can leave the
+          mutex. */
+
+       threads_mutex_join_unlock();
+
        /* create a java.lang.Thread object */
 
        t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
 
+       /* XXX memory leak!!! */
        if (t == NULL)
                return false;
 
        thread->object = t;
 
-       /* thread is a Java thread and running */
-
-       thread->flags = THREAD_FLAG_JAVA;
-
-       if (isdaemon)
-               thread->flags |= THREAD_FLAG_DAEMON;
-
        /* thread is completely initialized */
 
        threads_thread_state_runnable(thread);
@@ -1262,6 +1302,7 @@ bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
 
        vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
 
+       /* XXX memory leak!!! */
        if (vmt == NULL)
                return false;
 
@@ -1388,16 +1429,21 @@ bool threads_detach_thread(threadobject *thread)
        }
 #endif
 
-       /* signal that this thread has finished */
+       /* Enter the join-mutex before calling threads_thread_free, so
+          threads_join_all_threads gets the correct number of non-daemon
+          threads. */
 
-       pthread_mutex_lock(&mutex_join);
-       pthread_cond_signal(&cond_join);
-       pthread_mutex_unlock(&mutex_join);
+       threads_mutex_join_lock();
 
        /* free the vm internal thread object */
 
        threads_thread_free(thread);
 
+       /* Signal that this thread has finished and leave the mutex. */
+
+       pthread_cond_signal(&cond_join);
+       threads_mutex_join_unlock();
+
        return true;
 }
 
@@ -1422,7 +1468,7 @@ void threads_join_all_threads(void)
 
        /* enter join mutex */
 
-       pthread_mutex_lock(&mutex_join);
+       threads_mutex_join_lock();
 
        /* Wait for condition as long as we have non-daemon threads.  We
           compare against 1 because the current (main thread) is also a
@@ -1433,7 +1479,7 @@ void threads_join_all_threads(void)
 
        /* leave join mutex */
 
-       pthread_mutex_unlock(&mutex_join);
+       threads_mutex_join_unlock();
 }
 
 
index b0733150500bf16d4716b61ea44b6e38fb03347c..1775a1853460f5b200e21dc34386901c1100ba0f 100644 (file)
@@ -22,7 +22,7 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: threads-common.c 7998 2007-06-01 00:29:51Z twisti $
+   $Id: threads-common.c 8003 2007-06-03 18:42:09Z twisti $
 
 */
 
@@ -362,20 +362,35 @@ bool threads_thread_start_internal(utf *name, functionptr f)
        java_lang_VMThread *vmt;
 #endif
 
+       /* Enter the join-mutex, so if the main-thread is currently
+          waiting to join all threads, the number of non-daemon threads
+          is correct. */
+
+       threads_mutex_join_lock();
+
        /* create internal thread data-structure */
 
        t = threads_thread_new();
 
+       t->flags = THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
+
+       /* The thread is flagged as (non-)daemon thread, we can leave the
+          mutex. */
+
+       threads_mutex_join_unlock();
+
        /* create the java thread object */
 
        object = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
 
+       /* XXX memory leak!!! */
        if (object == NULL)
                return false;
 
 #if defined(WITH_CLASSPATH_GNU)
        vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
 
+       /* XXX memory leak!!! */
        if (vmt == NULL)
                return false;
 
@@ -389,8 +404,6 @@ bool threads_thread_start_internal(utf *name, functionptr f)
 
        t->object = object;
 
-       t->flags = THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
-
        /* set java.lang.Thread fields */
 
 #if defined(WITH_CLASSPATH_GNU)
@@ -431,13 +444,15 @@ void threads_thread_start(java_lang_Thread *object)
 {
        threadobject *thread;
 
-       /* create internal thread data-structure */
+       /* Enter the join-mutex, so if the main-thread is currently
+          waiting to join all threads, the number of non-daemon threads
+          is correct. */
 
-       thread = threads_thread_new();
+       threads_mutex_join_lock();
 
-       /* link the two objects together */
+       /* create internal thread data-structure */
 
-       thread->object = object;
+       thread = threads_thread_new();
 
        /* this is a normal Java thread */
 
@@ -450,6 +465,15 @@ void threads_thread_start(java_lang_Thread *object)
                thread->flags |= THREAD_FLAG_DAEMON;
 #endif
 
+       /* The thread is flagged and (non-)daemon thread, we can leave the
+          mutex. */
+
+       threads_mutex_join_unlock();
+
+       /* link the two objects together */
+
+       thread->object = object;
+
 #if defined(WITH_CLASSPATH_GNU)
        assert(object->vmThread);
        assert(object->vmThread->vmdata == NULL);
index 39b1f2782ab843b443a19ab64e0c00c3c6697244..9fab77ae9c9a6910839e3fd3cd6def10c799a5c8 100644 (file)
@@ -22,7 +22,7 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: threads-common.h 7998 2007-06-01 00:29:51Z twisti $
+   $Id: threads-common.h 8003 2007-06-03 18:42:09Z twisti $
 
 */
 
@@ -111,6 +111,9 @@ void          threads_impl_preinit(void);
 void          threads_list_lock(void);
 void          threads_list_unlock(void);
 
+void          threads_mutex_join_lock(void);
+void          threads_mutex_join_unlock(void);
+
 void          threads_set_current_threadobject(threadobject *thread);
 void          threads_impl_thread_new(threadobject *t);
 void          threads_impl_thread_free(threadobject *t);