[runtime] Move more thread attach code to mono-threads from io-layer.
[mono.git] / mono / io-layer / handles.c
index cab7e849c6a2ac324da3d52fbf5b0367d242b670..ea9cbeccbc138f262c8b23a4fc25fd1889ebebb7 100644 (file)
@@ -13,6 +13,7 @@
 #include <pthread.h>
 #include <errno.h>
 #include <unistd.h>
+#include <signal.h>
 #include <string.h>
 #include <sys/types.h>
 #ifdef HAVE_SYS_SOCKET_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
@@ -173,7 +174,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)
 {
@@ -189,29 +190,8 @@ static void handle_cleanup (void)
        for(i = SLOT_INDEX (0); _wapi_private_handles[i] != NULL; i++) {
                for(j = SLOT_OFFSET (0); j < _WAPI_HANDLE_INITIAL_COUNT; j++) {
                        struct _WapiHandleUnshared *handle_data = &_wapi_private_handles[i][j];
-                       int type = handle_data->type;
                        gpointer handle = GINT_TO_POINTER (i*_WAPI_HANDLE_INITIAL_COUNT+j);
-                       
-                       if (_WAPI_SHARED_HANDLE (type)) {
-                               if (type == WAPI_HANDLE_THREAD) {
-                                       /* Special-case thread handles
-                                        * because they need extra
-                                        * cleanup.  This also avoids
-                                        * a race condition between
-                                        * the application exit and
-                                        * the finalizer thread - if
-                                        * it finishes up between now
-                                        * and actual app termination
-                                        * it will find all its handle
-                                        * details have been blown
-                                        * away, so this sets those
-                                        * anyway.
-                                        */
-                                       g_assert (0); /*This condition is freaking impossible*/
-                                       _wapi_thread_set_termination_details (handle, 0);
-                               }
-                       }
-                               
+
                        for(k = handle_data->ref; k > 0; k--) {
                                DEBUG ("%s: unreffing %s handle %p", __func__, _wapi_handle_typename[type], handle);
                                        
@@ -279,12 +259,15 @@ wapi_init (void)
        if (_wapi_shm_enabled ())
                _wapi_collection_init ();
 #endif
+       _wapi_io_init ();
+       mono_mutex_init (&scan_mutex);
 
        _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,7 +283,6 @@ wapi_cleanup (void)
        
        _wapi_has_shut_down = TRUE;
 
-       _wapi_critical_section_cleanup ();
        _wapi_error_cleanup ();
        _wapi_thread_cleanup ();
 }
@@ -336,7 +318,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) {
@@ -1761,6 +1743,9 @@ gboolean _wapi_handle_get_or_set_share (dev_t device, ino_t inode,
  */
 static void _wapi_handle_check_share_by_pid (struct _WapiFileShare *share_info)
 {
+#if defined(__native_client__)
+       g_assert_not_reached ();
+#else
        if (kill (share_info->opened_by_pid, 0) == -1 &&
            (errno == ESRCH ||
             errno == EPERM)) {
@@ -1772,6 +1757,7 @@ static void _wapi_handle_check_share_by_pid (struct _WapiFileShare *share_info)
 
                _wapi_free_share_info (share_info);
        }
+#endif
 }
 
 #ifdef __linux__