Fix mono_mutex_trylock on Windows
[mono.git] / mono / io-layer / collection.c
index 61ae89d3ad003f9aa017e1b50368dc7ea4e29608..a42bbadcfe0b6827874a2e4c877597b0982527fd 100644 (file)
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/collection.h>
 #include <mono/io-layer/handles-private.h>
+#include <mono/utils/atomic.h>
 
-#undef DEBUG
+#if 0
+// #define DEBUG(...) g_message(__VA_ARGS__)
+#else
+#define DEBUG(...)
+#endif
 
 static pthread_t collection_thread_id;
 
-static gpointer collection_thread (gpointer args) G_GNUC_NORETURN;
 static gpointer collection_thread (gpointer unused G_GNUC_UNUSED)
 {
        struct timespec sleepytime;
@@ -30,46 +34,44 @@ static gpointer collection_thread (gpointer unused G_GNUC_UNUSED)
        sleepytime.tv_sec = _WAPI_HANDLE_COLLECTION_UPDATE_INTERVAL;
        sleepytime.tv_nsec = 0;
 
-       while (1) {
+       while (_wapi_has_shut_down == FALSE) {
                nanosleep (&sleepytime, NULL);
 
                //_wapi_handle_dump ();
                _wapi_handle_update_refs ();
-
-               /* This is an abuse of the collection thread, but it's
-                * better than creating a new thread just for one more
-                * function.
-                */
-               _wapi_process_reap ();
        }
 
-#ifndef __GNUC__
-       /* Even though we tell gcc that this function doesn't return,
-        * other compilers won't see that.
-        */
+       pthread_exit (NULL);
+
        return(NULL);
-#endif
 }
 
 void _wapi_collection_init (void)
 {
        pthread_attr_t attr;
        int ret;
-       
-       ret = pthread_attr_init (&attr);
-       g_assert (ret == 0);
-       
-#ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
-#if defined(__FreeBSD__) || defined(__NetBSD__)
-       ret = pthread_attr_setstacksize (&attr, 65536);
-#else
-       ret = pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
-#endif
-       g_assert (ret == 0);
+        int set_stacksize = 0;
+
+ retry:
+        ret = pthread_attr_init (&attr);
+        g_assert (ret == 0);
+
+#if defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
+        if (set_stacksize == 0) {
+                       ret = pthread_attr_setstacksize (&attr, MAX (65536, PTHREAD_STACK_MIN));
+                       g_assert (ret == 0);
+        } else if (set_stacksize == 1) {
+                       ret = pthread_attr_setstacksize (&attr, 131072);
+                       g_assert (ret == 0);
+        }
 #endif
 
-       ret = pthread_create (&collection_thread_id, &attr, collection_thread,
-                             NULL);
+        ret = pthread_create (&collection_thread_id, &attr, collection_thread,
+                              NULL);
+        if (ret != 0 && set_stacksize < 2) {
+                set_stacksize++;
+                goto retry;
+        }
        if (ret != 0) {
                g_error ("%s: Couldn't create handle collection thread: %s",
                         __func__, g_strerror (ret));
@@ -80,19 +82,20 @@ void _wapi_handle_collect (void)
 {
        guint32 count = _wapi_shared_layout->collection_count;
        int i, thr_ret;
+
+       if (!_wapi_shm_enabled ())
+               return;
        
-#ifdef DEBUG
-       g_message ("%s: (%d) Starting a collection", __func__,
-                  _wapi_getpid ());
-#endif
+       DEBUG ("%s: (%d) Starting a collection", __func__, _wapi_getpid ());
 
        /* Become the collection master */
        thr_ret = _wapi_handle_lock_shared_handles ();
        g_assert (thr_ret == 0);
        
-#ifdef DEBUG
-       g_message ("%s: (%d) Master set", __func__, _wapi_getpid ());
-#endif
+       thr_ret = _wapi_shm_sem_lock (_WAPI_SHARED_SEM_FILESHARE);
+       g_assert (thr_ret == 0);
+       
+       DEBUG ("%s: (%d) Master set", __func__, _wapi_getpid ());
        
        /* If count has changed, someone else jumped in as master */
        if (count == _wapi_shared_layout->collection_count) {
@@ -103,9 +106,7 @@ void _wapi_handle_collect (void)
                        
                        data = &_wapi_shared_layout->handles[i];
                        if (data->timestamp < too_old) {
-#ifdef DEBUG
-                               g_message ("%s: (%d) Deleting handle 0x%x", __func__, _wapi_getpid (), i);
-#endif
+                               DEBUG ("%s: (%d) Deleting handle 0x%x", __func__, _wapi_getpid (), i);
                                memset (&_wapi_shared_layout->handles[i], '\0', sizeof(struct _WapiHandleShared));
                        }
                }
@@ -122,9 +123,10 @@ void _wapi_handle_collect (void)
                InterlockedIncrement ((gint32 *)&_wapi_shared_layout->collection_count);
        }
        
+       thr_ret = _wapi_shm_sem_unlock (_WAPI_SHARED_SEM_FILESHARE);
+       g_assert (thr_ret == 0);
+        
        _wapi_handle_unlock_shared_handles ();
 
-#ifdef DEBUG
-       g_message ("%s: (%d) Collection done", __func__, _wapi_getpid ());
-#endif
+       DEBUG ("%s: (%d) Collection done", __func__, _wapi_getpid ());
 }