2009-01-26 Jeffrey Stedfast <fejj@novell.com>
[mono.git] / mono / io-layer / collection.c
index bb205710913e6cfb2d9e771f42b759d1059543c2..2da5505a7fcf9d9fa344f05a66dc0cfdf60c1c26 100644 (file)
@@ -4,7 +4,7 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2004 Novell, Inc.
+ * (C) 2004-2006 Novell, Inc.
  */
 
 #include <config.h>
@@ -30,7 +30,9 @@ 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 ();
 
@@ -39,10 +41,10 @@ static gpointer collection_thread (gpointer unused G_GNUC_UNUSED)
                 * function.
                 */
                _wapi_process_reap ();
-
-               nanosleep (&sleepytime, NULL);
        }
 
+       pthread_exit (NULL);
+
 #ifndef __GNUC__
        /* Even though we tell gcc that this function doesn't return,
         * other compilers won't see that.
@@ -60,7 +62,7 @@ void _wapi_collection_init (void)
        g_assert (ret == 0);
        
 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
-#ifdef __FreeBSD__
+#if defined(__FreeBSD__) || defined(__NetBSD__)
        ret = pthread_attr_setstacksize (&attr, 65536);
 #else
        ret = pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
@@ -79,51 +81,39 @@ void _wapi_collection_init (void)
 void _wapi_handle_collect (void)
 {
        guint32 count = _wapi_shared_layout->collection_count;
-       int i;
+       int i, thr_ret;
        
 #ifdef DEBUG
-       g_message ("%s: (%d) Starting a collection", __func__, getpid ());
+       g_message ("%s: (%d) Starting a collection", __func__,
+                  _wapi_getpid ());
 #endif
 
        /* Become the collection master */
-       _WAPI_HANDLE_COLLECTION_UNSAFE;
+       thr_ret = _wapi_handle_lock_shared_handles ();
+       g_assert (thr_ret == 0);
        
 #ifdef DEBUG
-       g_message ("%s: (%d) Master set", __func__, getpid ());
+       g_message ("%s: (%d) Master set", __func__, _wapi_getpid ());
 #endif
        
        /* If count has changed, someone else jumped in as master */
        if (count == _wapi_shared_layout->collection_count) {
+               guint32 too_old = (guint32)(time(NULL) & 0xFFFFFFFF) - _WAPI_HANDLE_COLLECTION_EXPIRED_INTERVAL;
+
                for (i = 0; i < _WAPI_HANDLE_INITIAL_COUNT; i++) {
-                       struct _WapiHandleShared *shared;
-                       struct _WapiHandleSharedMetadata *meta;
-                       guint32 too_old = (guint32)(time(NULL) & 0xFFFFFFFF) - _WAPI_HANDLE_COLLECTION_EXPIRED_INTERVAL;
+                       struct _WapiHandleShared *data;
                        
-                       meta = &_wapi_shared_layout->metadata[i];
-                       if (meta->timestamp < too_old && meta->offset != 0) {
-#ifdef DEBUG
-                               g_message ("%s: (%d) Deleting metadata slot 0x%x handle 0x%x", __func__, getpid (), i, meta->offset);
-#endif
-                               memset (&_wapi_shared_layout->handles[meta->offset], '\0', sizeof(struct _WapiHandleShared));
-                               memset (&_wapi_shared_layout->metadata[i], '\0', sizeof(struct _WapiHandleSharedMetadata));
-                       }
-
-                       /* Need to blank any handles data that is no
-                        * longer pointed to by a metadata entry too
-                        */
-                       shared = &_wapi_shared_layout->handles[i];
-                       if (shared->stale == TRUE) {
+                       data = &_wapi_shared_layout->handles[i];
+                       if (data->timestamp < too_old) {
 #ifdef DEBUG
-                               g_message ("%s: (%d) Deleting stale handle 0x%x", __func__, getpid (), i);
+                               g_message ("%s: (%d) Deleting handle 0x%x", __func__, _wapi_getpid (), i);
 #endif
-                               memset (&_wapi_shared_layout->handles[i], '\0',
-                                       sizeof(struct _WapiHandleShared));
+                               memset (&_wapi_shared_layout->handles[i], '\0', sizeof(struct _WapiHandleShared));
                        }
                }
 
                for (i = 0; i < _wapi_fileshare_layout->hwm; i++) {
                        struct _WapiFileShare *file_share = &_wapi_fileshare_layout->share_info[i];
-                       guint32 too_old = (guint32)(time(NULL) & 0xFFFFFFFF) - _WAPI_HANDLE_COLLECTION_EXPIRED_INTERVAL;
                        
                        if (file_share->timestamp < too_old) {
                                memset (file_share, '\0',
@@ -131,12 +121,12 @@ void _wapi_handle_collect (void)
                        }
                }
 
-               InterlockedIncrement (&_wapi_shared_layout->collection_count);
+               InterlockedIncrement ((gint32 *)&_wapi_shared_layout->collection_count);
        }
        
-       _WAPI_HANDLE_COLLECTION_SAFE;
+       _wapi_handle_unlock_shared_handles ();
 
 #ifdef DEBUG
-       g_message ("%s: (%d) Collection done", __func__, getpid ());
+       g_message ("%s: (%d) Collection done", __func__, _wapi_getpid ());
 #endif
 }