Fri May 31 16:21:54 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / io-layer / timed-thread.c
index 7a6fcf228e576eb798c24c8bba0250035c7d36e8..f955607614fc698c8f5dd5a46d5d85fbe8718b52 100644 (file)
@@ -1,10 +1,24 @@
+/*
+ * timed-thread.c:  Implementation of timed thread joining
+ *
+ * Author:
+ *     Dick Porter (dick@ximian.com)
+ *
+ * (C) 2002 Ximian, Inc.
+ */
+
 #include <config.h>
 #include <glib.h>
+#if HAVE_BOEHM_GC
+#include <gc/gc.h>
+#endif
 #include <pthread.h>
 
 #include "timed-thread.h"
 
-#define DEBUG
+#include "mono-mutex.h"
+
+#undef DEBUG
 
 /*
  * Implementation of timed thread joining from the P1003.1d/D14 (July 1999)
@@ -32,7 +46,7 @@ void _wapi_timed_thread_exit(guint32 exitstatus)
        
        thread=(TimedThread *)specific;
        
-       pthread_mutex_lock(&thread->join_mutex);
+       mono_mutex_lock(&thread->join_mutex);
        
        /* Tell a joiner that we're exiting.
         */
@@ -50,7 +64,7 @@ void _wapi_timed_thread_exit(guint32 exitstatus)
        }
        
        pthread_cond_signal(&thread->exit_cond);
-       pthread_mutex_unlock(&thread->join_mutex);
+       mono_mutex_unlock(&thread->join_mutex);
        
        /* Call pthread_exit() to call destructors and really exit the
         * thread.
@@ -68,6 +82,7 @@ static void *timed_thread_start_routine(gpointer args)
        
        pthread_once(&timed_thread_once, timed_thread_init);
        pthread_setspecific(timed_thread_key, (void *)thread);
+       pthread_detach(thread->id);
        _wapi_timed_thread_exit(thread->start_routine(thread->arg));
 }
 
@@ -84,7 +99,7 @@ int _wapi_timed_thread_create(TimedThread **threadp,
        
        thread=(TimedThread *)g_new0(TimedThread, 1);
        
-       pthread_mutex_init(&thread->join_mutex, NULL);
+       mono_mutex_init(&thread->join_mutex, NULL);
        pthread_cond_init(&thread->exit_cond, NULL);
        thread->start_routine = start_routine;
        thread->exit_routine = exit_routine;
@@ -102,7 +117,6 @@ int _wapi_timed_thread_create(TimedThread **threadp,
                return(result);
        }
        
-       pthread_detach(thread->id);
        return(0);
 }
 
@@ -111,7 +125,7 @@ int _wapi_timed_thread_join(TimedThread *thread, struct timespec *timeout,
 {
        int result;
        
-       pthread_mutex_lock(&thread->join_mutex);
+       mono_mutex_lock(&thread->join_mutex);
        result=0;
        
        /* Wait until the thread announces that it's exiting, or until
@@ -128,7 +142,7 @@ int _wapi_timed_thread_join(TimedThread *thread, struct timespec *timeout,
                }
        }
        
-       pthread_mutex_unlock(&thread->join_mutex);
+       mono_mutex_unlock(&thread->join_mutex);
        if(result == 0 && thread->exiting) {
                if(exitstatus!=NULL) {
                        *exitstatus = thread->exitstatus;
@@ -137,3 +151,10 @@ int _wapi_timed_thread_join(TimedThread *thread, struct timespec *timeout,
        return(result);
 }
 
+void _wapi_timed_thread_destroy (TimedThread *thread)
+{
+       mono_mutex_destroy (&thread->join_mutex);
+       pthread_cond_destroy (&thread->exit_cond);
+       
+       g_free(thread);
+}