Fix mono_mutex_trylock on Windows
[mono.git] / mono / io-layer / collection.c
index c8ec28e4c8ebd0d10be40c5b47634dd0411567fe..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>
 
-#define LOGDEBUG(...)
-// #define LOGDEBUG(...) g_message(__VA_ARGS__)
+#if 0
+// #define DEBUG(...) g_message(__VA_ARGS__)
+#else
+#define DEBUG(...)
+#endif
 
 static pthread_t collection_thread_id;
 
@@ -35,12 +39,6 @@ static gpointer collection_thread (gpointer unused G_GNUC_UNUSED)
 
                //_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 ();
        }
 
        pthread_exit (NULL);
@@ -58,17 +56,13 @@ void _wapi_collection_init (void)
         ret = pthread_attr_init (&attr);
         g_assert (ret == 0);
 
-#ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
+#if defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
         if (set_stacksize == 0) {
-#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);
+                       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);
+                       ret = pthread_attr_setstacksize (&attr, 131072);
+                       g_assert (ret == 0);
         }
 #endif
 
@@ -88,14 +82,20 @@ void _wapi_handle_collect (void)
 {
        guint32 count = _wapi_shared_layout->collection_count;
        int i, thr_ret;
+
+       if (!_wapi_shm_enabled ())
+               return;
        
-       LOGDEBUG ("%s: (%d) Starting a collection", __func__, _wapi_getpid ());
+       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);
        
-       LOGDEBUG ("%s: (%d) Master set", __func__, _wapi_getpid ());
+       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) {
@@ -106,7 +106,7 @@ void _wapi_handle_collect (void)
                        
                        data = &_wapi_shared_layout->handles[i];
                        if (data->timestamp < too_old) {
-                               LOGDEBUG ("%s: (%d) Deleting handle 0x%x", __func__, _wapi_getpid (), i);
+                               DEBUG ("%s: (%d) Deleting handle 0x%x", __func__, _wapi_getpid (), i);
                                memset (&_wapi_shared_layout->handles[i], '\0', sizeof(struct _WapiHandleShared));
                        }
                }
@@ -123,7 +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 ();
 
-       LOGDEBUG ("%s: (%d) Collection done", __func__, _wapi_getpid ());
+       DEBUG ("%s: (%d) Collection done", __func__, _wapi_getpid ());
 }