Merge pull request #615 from nealef/master
[mono.git] / mono / io-layer / handles.c
index baadb3b62a9a7a149b022f08ccad81bf753108d4..6b5cce5ce18046bda1c5b744c8d52daaeab696b0 100644 (file)
@@ -4,7 +4,8 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2002-2006 Novell, Inc.
+ * (C) 2002-2011 Novell, Inc.
+ * Copyright 2011 Xamarin Inc
  */
 
 #include <config.h>
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/wapi-private.h>
 #include <mono/io-layer/handles-private.h>
-#include <mono/io-layer/mono-mutex.h>
 #include <mono/io-layer/misc-private.h>
 #include <mono/io-layer/shared.h>
 #include <mono/io-layer/collection.h>
 #include <mono/io-layer/process-private.h>
 #include <mono/io-layer/critical-section-private.h>
 
+#include <mono/utils/mono-mutex.h>
 #undef DEBUG_REFS
 
 #if 0
@@ -157,7 +158,6 @@ gboolean _wapi_has_shut_down = FALSE;
 static pid_t _wapi_pid;
 static mono_once_t pid_init_once = MONO_ONCE_INIT;
 
-static gpointer _wapi_handle_real_new (WapiHandleType type, gpointer handle_specific);
 static void _wapi_handle_unref_full (gpointer handle, gboolean ignore_private_busy_handles);
 
 static void pid_init (void)
@@ -173,7 +173,7 @@ pid_t _wapi_getpid (void)
 }
 
 
-static mono_mutex_t scan_mutex = MONO_MUTEX_INITIALIZER;
+static mono_mutex_t scan_mutex;
 
 static void handle_cleanup (void)
 {
@@ -234,19 +234,13 @@ static void handle_cleanup (void)
                g_free (_wapi_private_handles [i]);
 }
 
-void _wapi_cleanup ()
-{
-       g_assert (_wapi_has_shut_down == FALSE);
-       
-       _wapi_has_shut_down = TRUE;
-
-       _wapi_critical_section_cleanup ();
-       _wapi_error_cleanup ();
-       _wapi_thread_cleanup ();
-}
-
-static mono_once_t shared_init_once = MONO_ONCE_INIT;
-static void shared_init (void)
+/*
+ * wapi_init:
+ *
+ *   Initialize the io-layer.
+ */
+void
+wapi_init (void)
 {
        g_assert ((sizeof (handle_ops) / sizeof (handle_ops[0]))
                  == WAPI_HANDLE_COUNT);
@@ -285,13 +279,15 @@ static void shared_init (void)
        if (_wapi_shm_enabled ())
                _wapi_collection_init ();
 #endif
+       _wapi_io_init ();
+       mono_mutex_init (&scan_mutex);
 
-       /* Can't call wapi_handle_new as it calls us recursively */
-       _wapi_global_signal_handle = _wapi_handle_real_new (WAPI_HANDLE_EVENT, NULL);
+       _wapi_global_signal_handle = _wapi_handle_new (WAPI_HANDLE_EVENT, NULL);
 
        _wapi_global_signal_cond = &_WAPI_PRIVATE_HANDLES (GPOINTER_TO_UINT (_wapi_global_signal_handle)).signal_cond;
        _wapi_global_signal_mutex = &_WAPI_PRIVATE_HANDLES (GPOINTER_TO_UINT (_wapi_global_signal_handle)).signal_mutex;
 
+
        /* Using g_atexit here instead of an explicit function call in
         * a cleanup routine lets us cope when a third-party library
         * calls exit (eg if an X client loses the connection to its
@@ -300,6 +296,17 @@ static void shared_init (void)
        g_atexit (handle_cleanup);
 }
 
+void
+wapi_cleanup (void)
+{
+       g_assert (_wapi_has_shut_down == FALSE);
+       
+       _wapi_has_shut_down = TRUE;
+
+       _wapi_error_cleanup ();
+       _wapi_thread_cleanup ();
+}
+
 static void _wapi_handle_init_shared (struct _WapiHandleShared *handle,
                                      WapiHandleType type,
                                      gpointer handle_specific)
@@ -331,7 +338,7 @@ static void _wapi_handle_init (struct _WapiHandleUnshared *handle,
                thr_ret = pthread_cond_init (&handle->signal_cond, NULL);
                g_assert (thr_ret == 0);
                                
-               thr_ret = mono_mutex_init (&handle->signal_mutex, NULL);
+               thr_ret = mono_mutex_init (&handle->signal_mutex);
                g_assert (thr_ret == 0);
 
                if (handle_specific != NULL) {
@@ -448,12 +455,15 @@ again:
        return(0);
 }
 
-static gpointer _wapi_handle_real_new (WapiHandleType type, gpointer handle_specific)
+gpointer 
+_wapi_handle_new (WapiHandleType type, gpointer handle_specific)
 {
        guint32 handle_idx = 0;
        gpointer handle;
        int thr_ret;
-       
+
+       g_assert (_wapi_has_shut_down == FALSE);
+               
        DEBUG ("%s: Creating new handle of type %s", __func__,
                   _wapi_handle_typename[type]);
 
@@ -519,15 +529,6 @@ done:
        return(handle);
 }
 
-gpointer _wapi_handle_new (WapiHandleType type, gpointer handle_specific)
-{
-       g_assert (_wapi_has_shut_down == FALSE);
-       
-       mono_once (&shared_init_once, shared_init);
-
-       return _wapi_handle_real_new (type, handle_specific);
-}
-
 gpointer _wapi_handle_new_from_offset (WapiHandleType type, guint32 offset,
                                       gboolean timestamp)
 {
@@ -538,8 +539,6 @@ gpointer _wapi_handle_new_from_offset (WapiHandleType type, guint32 offset,
        
        g_assert (_wapi_has_shut_down == FALSE);
        
-       mono_once (&shared_init_once, shared_init);
-
        DEBUG ("%s: Creating new handle of type %s to offset %d", __func__,
                   _wapi_handle_typename[type], offset);
 
@@ -668,8 +667,6 @@ gpointer _wapi_handle_new_fd (WapiHandleType type, int fd,
        
        g_assert (_wapi_has_shut_down == FALSE);
        
-       mono_once (&shared_init_once, shared_init);
-       
        DEBUG ("%s: Creating new handle of type %s", __func__,
                   _wapi_handle_typename[type]);
        
@@ -1604,6 +1601,7 @@ _wapi_free_share_info (_WapiFileShare *share_info)
                file_share_hash_lock ();
                g_hash_table_remove (file_share_hash, share_info);
                file_share_hash_unlock ();
+               /* The hashtable dtor frees share_info */
        } else {
                memset (share_info, '\0', sizeof(struct _WapiFileShare));
        }
@@ -1774,7 +1772,7 @@ static void _wapi_handle_check_share_by_pid (struct _WapiFileShare *share_info)
                 */
                DEBUG ("%s: Didn't find it, destroying entry", __func__);
 
-               memset (share_info, '\0', sizeof(struct _WapiFileShare));
+               _wapi_free_share_info (share_info);
        }
 }
 
@@ -1896,7 +1894,7 @@ void _wapi_handle_check_share (struct _WapiFileShare *share_info, int fd)
                /* Blank out this entry, as it is stale */
                DEBUG ("%s: Didn't find it, destroying entry", __func__);
 
-               memset (share_info, '\0', sizeof(struct _WapiFileShare));
+               _wapi_free_share_info (share_info);
        }
 
 done: