Merge pull request #2338 from BogdanovKirill/httpwritefix3
[mono.git] / mono / io-layer / processes.c
index 94938f53704a8d4213ef0bd118ba658488cc124c..851ffaffb6eb1b6f2712a38daca9cec9d7c59cea 100644 (file)
@@ -83,7 +83,6 @@
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/wapi-private.h>
 #include <mono/io-layer/handles-private.h>
-#include <mono/io-layer/misc-private.h>
 #include <mono/io-layer/process-private.h>
 #include <mono/io-layer/threads.h>
 #include <mono/utils/strenc.h>
 #include <mono/io-layer/timefuncs-private.h>
 #include <mono/utils/mono-time.h>
 #include <mono/utils/mono-membar.h>
-#include <mono/utils/mono-mutex.h>
+#include <mono/utils/mono-os-mutex.h>
 #include <mono/utils/mono-signal-handler.h>
 #include <mono/utils/mono-proclib.h>
+#include <mono/utils/mono-once.h>
 
 /* The process' environment strings */
 #if defined(__APPLE__)
@@ -1037,7 +1037,7 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
        mono_process = (struct MonoProcess *) g_malloc0 (sizeof (struct MonoProcess));
        mono_process->pid = pid;
        mono_process->handle_count = 1;
-       if (MONO_SEM_INIT (&mono_process->exit_sem, 0) != 0) {
+       if (mono_os_sem_init (&mono_process->exit_sem, 0) != 0) {
                /* If we can't create the exit semaphore, we just don't add anything
                 * to our list of mono processes. Waiting on the process will return 
                 * immediately. */
@@ -1051,10 +1051,10 @@ gboolean CreateProcess (const gunichar2 *appname, const gunichar2 *cmdline,
 
                process_handle_data->mono_process = mono_process;
 
-               mono_mutex_lock (&mono_processes_mutex);
+               mono_os_mutex_lock (&mono_processes_mutex);
                mono_process->next = mono_processes;
                mono_processes = mono_process;
-               mono_mutex_unlock (&mono_processes_mutex);
+               mono_os_mutex_unlock (&mono_processes_mutex);
        }
        
        if (process_info != NULL) {
@@ -1137,8 +1137,7 @@ wapi_processes_init (void)
        WapiHandle_process process_handle = {0};
 
        _wapi_handle_register_capabilities (WAPI_HANDLE_PROCESS,
-                                           WAPI_HANDLE_CAP_WAIT |
-                                           WAPI_HANDLE_CAP_SPECIAL_WAIT);
+               (WapiHandleCapability)(WAPI_HANDLE_CAP_WAIT | WAPI_HANDLE_CAP_SPECIAL_WAIT));
        
        process_handle.id = pid;
 
@@ -1148,6 +1147,8 @@ wapi_processes_init (void)
        current_process = _wapi_handle_new (WAPI_HANDLE_PROCESS,
                                            &process_handle);
        g_assert (current_process);
+
+       mono_os_mutex_init (&mono_processes_mutex);
 }
 
 gpointer
@@ -1780,7 +1781,7 @@ gboolean EnumProcessModules (gpointer process, gpointer *modules,
                        return FALSE;
                }
                pid = process_handle->id;
-               proc_name = process_handle->proc_name;
+               proc_name = g_strdup (process_handle->proc_name);
        }
        
 #if defined(PLATFORM_MACOSX) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__HAIKU__)
@@ -1798,6 +1799,7 @@ gboolean EnumProcessModules (gpointer process, gpointer *modules,
                 */
                modules[0] = NULL;
                *needed = sizeof(gpointer);
+               g_free (proc_name);
                return TRUE;
        }
        mods = load_modules (fp);
@@ -1828,10 +1830,11 @@ gboolean EnumProcessModules (gpointer process, gpointer *modules,
        }
                
        for (i = 0; i < count; i++) {
-               free_procmodule (g_slist_nth_data (mods, i));
+               free_procmodule ((WapiProcModule *)g_slist_nth_data (mods, i));
        }
        g_slist_free (mods);
-
+       g_free (proc_name);
+       
        return TRUE;
 }
 
@@ -2583,10 +2586,10 @@ mono_processes_cleanup (void)
                if (mp->pid == 0 && mp->handle) {
                        /* This process has exited and we need to remove the artifical ref
                         * on the handle */
-                       mono_mutex_lock (&mono_processes_mutex);
+                       mono_os_mutex_lock (&mono_processes_mutex);
                        unref_handle = mp->handle;
                        mp->handle = NULL;
-                       mono_mutex_unlock (&mono_processes_mutex);
+                       mono_os_mutex_unlock (&mono_processes_mutex);
                        if (unref_handle)
                                _wapi_handle_unref (unref_handle);
                }
@@ -2598,7 +2601,7 @@ mono_processes_cleanup (void)
         * asynchronously. The handler requires that the mono_processes list
         * remain valid.
         */
-       mono_mutex_lock (&mono_processes_mutex);
+       mono_os_mutex_lock (&mono_processes_mutex);
 
        mp = mono_processes;
        while (mp) {
@@ -2629,13 +2632,13 @@ mono_processes_cleanup (void)
                 * they have the 'finished' flag set, which means the sigchld handler is done
                 * accessing them.
                 */
-               mp = l->data;
-               MONO_SEM_DESTROY (&mp->exit_sem);
+               mp = (MonoProcess *)l->data;
+               mono_os_sem_destroy (&mp->exit_sem);
                g_free (mp);
        }
        g_slist_free (finished);
 
-       mono_mutex_unlock (&mono_processes_mutex);
+       mono_os_mutex_unlock (&mono_processes_mutex);
 
        DEBUG ("%s done", __func__);
 
@@ -2687,7 +2690,7 @@ MONO_SIGNAL_HANDLER_FUNC (static, mono_sigchld_signal_handler, (int _dummy, sigi
                if (p) {
                        p->pid = 0; /* this pid doesn't exist anymore, clear it */
                        p->status = status;
-                       MONO_SEM_POST (&p->exit_sem);
+                       mono_os_sem_post (&p->exit_sem);
                        mono_memory_barrier ();
                        /* Mark this as freeable, the pointer becomes invalid afterwards */
                        p->freeable = TRUE;
@@ -2757,12 +2760,11 @@ process_wait (gpointer handle, guint32 timeout, gboolean alertable)
                if (timeout != INFINITE) {
                        DEBUG ("%s (%p, %u): waiting on semaphore for %li ms...", 
                                   __func__, handle, timeout, (timeout - (now - start)));
-
-                       ret = MONO_SEM_TIMEDWAIT_ALERTABLE (&mp->exit_sem, (timeout - (now - start)), alertable);
+                       ret = mono_os_sem_timedwait (&mp->exit_sem, (timeout - (now - start)), alertable ? MONO_SEM_FLAGS_ALERTABLE : MONO_SEM_FLAGS_NONE);
                } else {
                        DEBUG ("%s (%p, %u): waiting on semaphore forever...", 
                                   __func__, handle, timeout);
-                       ret = MONO_SEM_WAIT_ALERTABLE (&mp->exit_sem, alertable);
+                       ret = mono_os_sem_wait (&mp->exit_sem, alertable ? MONO_SEM_FLAGS_ALERTABLE : MONO_SEM_FLAGS_NONE);
                }
 
                if (ret == -1 && errno != EINTR && errno != ETIMEDOUT) {
@@ -2773,7 +2775,7 @@ process_wait (gpointer handle, guint32 timeout, gboolean alertable)
 
                if (ret == 0) {
                        /* Success, process has exited */
-                       MONO_SEM_POST (&mp->exit_sem);
+                       mono_os_sem_post (&mp->exit_sem);
                        break;
                }