Merge pull request #4621 from alexanderkyte/strdup_env
[mono.git] / mono / metadata / threadpool-io.c
index 6b394e0f8c87f5c242a5fd1dac777fc97779ad11..a40ed6de5ea1e62c8c98d1082d18b5f3b919b365 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * threadpool-io.c: Microsoft IO threadpool runtime support
+/**
+ * \file
+ * Microsoft IO threadpool runtime support
  *
  * Author:
  *     Ludovic Henry (ludovic.henry@xamarin.com)
@@ -318,8 +319,6 @@ selector_thread (gpointer data)
        MonoError error;
        MonoGHashTable *states;
 
-       io_selector_running = TRUE;
-
        if (mono_runtime_is_shutting_down ()) {
                io_selector_running = FALSE;
                return 0;
@@ -445,7 +444,12 @@ selector_thread (gpointer data)
 
        mono_g_hash_table_destroy (states);
 
+       mono_coop_mutex_lock (&threadpool_io->updates_lock);
+
        io_selector_running = FALSE;
+       mono_coop_cond_broadcast (&threadpool_io->updates_cond);
+
+       mono_coop_mutex_unlock (&threadpool_io->updates_lock);
 
        return 0;
 }
@@ -555,9 +559,15 @@ initialize (void)
        if (!threadpool_io->backend.init (threadpool_io->wakeup_pipes [0]))
                g_error ("initialize: backend->init () failed");
 
+       mono_coop_mutex_lock (&threadpool_io->updates_lock);
+
+       io_selector_running = TRUE;
+
        MonoError error;
        if (!mono_thread_create_internal (mono_get_root_domain (), selector_thread, NULL, MONO_THREAD_CREATE_FLAGS_THREADPOOL | MONO_THREAD_CREATE_FLAGS_SMALL_STACK, &error))
                g_error ("initialize: mono_thread_create_internal () failed due to %s", mono_error_get_message (&error));
+
+       mono_coop_mutex_unlock (&threadpool_io->updates_lock);
 }
 
 static void
@@ -591,6 +601,11 @@ ves_icall_System_IOSelector_Add (gpointer handle, MonoIOSelectorJob *job)
 
        mono_coop_mutex_lock (&threadpool_io->updates_lock);
 
+       if (!io_selector_running) {
+               mono_coop_mutex_unlock (&threadpool_io->updates_lock);
+               return;
+       }
+
        update = update_get_new ();
        update->type = UPDATE_ADD;
        update->data.add.fd = GPOINTER_TO_INT (handle);
@@ -618,6 +633,11 @@ mono_threadpool_io_remove_socket (int fd)
 
        mono_coop_mutex_lock (&threadpool_io->updates_lock);
 
+       if (!io_selector_running) {
+               mono_coop_mutex_unlock (&threadpool_io->updates_lock);
+               return;
+       }
+
        update = update_get_new ();
        update->type = UPDATE_REMOVE_SOCKET;
        update->data.add.fd = fd;
@@ -640,6 +660,11 @@ mono_threadpool_io_remove_domain_jobs (MonoDomain *domain)
 
        mono_coop_mutex_lock (&threadpool_io->updates_lock);
 
+       if (!io_selector_running) {
+               mono_coop_mutex_unlock (&threadpool_io->updates_lock);
+               return;
+       }
+
        update = update_get_new ();
        update->type = UPDATE_REMOVE_DOMAIN;
        update->data.remove_domain.domain = domain;