Merge pull request #4621 from alexanderkyte/strdup_env
[mono.git] / mono / metadata / threadpool-io.c
index c7986ab0d60dd487d353f3b5cf5a0c1c1a6606d5..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)
@@ -29,6 +30,7 @@
 #include <mono/utils/mono-threads.h>
 #include <mono/utils/mono-lazy-init.h>
 #include <mono/utils/mono-logger-internals.h>
+#include <mono/utils/w32api.h>
 
 typedef struct {
        gboolean (*init) (gint wakeup_pipe_fd);
@@ -306,23 +308,31 @@ wait_callback (gint fd, gint events, gpointer user_data)
 }
 
 static void
+selector_thread_interrupt (gpointer unused)
+{
+       selector_thread_wakeup ();
+}
+
+static gsize WINAPI
 selector_thread (gpointer data)
 {
        MonoError error;
        MonoGHashTable *states;
 
-       io_selector_running = TRUE;
-
        if (mono_runtime_is_shutting_down ()) {
                io_selector_running = FALSE;
-               return;
+               return 0;
        }
 
-       states = mono_g_hash_table_new_type (g_direct_hash, g_direct_equal, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_THREAD_POOL, "i/o thread pool states table");
+       states = mono_g_hash_table_new_type (g_direct_hash, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_THREAD_POOL, "i/o thread pool states table");
 
-       for (;;) {
+       while (!mono_runtime_is_shutting_down ()) {
                gint i, j;
                gint res;
+               gboolean interrupted = FALSE;
+
+               if (mono_thread_interruption_checkpoint ())
+                       continue;
 
                mono_coop_mutex_lock (&threadpool_io->updates_lock);
 
@@ -421,15 +431,27 @@ selector_thread (gpointer data)
 
                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: wai");
 
-               res = threadpool_io->backend.event_wait (wait_callback, states);
+               mono_thread_info_install_interrupt (selector_thread_interrupt, NULL, &interrupted);
+               if (interrupted)
+                       continue;
 
-               if (res == -1 || mono_runtime_is_shutting_down ())
+               res = threadpool_io->backend.event_wait (wait_callback, states);
+               if (res == -1)
                        break;
+
+               mono_thread_info_uninstall_interrupt (&interrupted);
        }
 
        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;
 }
 
 /* Locking: threadpool_io->updates_lock must be held */
@@ -524,7 +546,7 @@ initialize (void)
        threadpool_io->updates_size = 0;
 
        threadpool_io->backend = backend_poll;
-       if (g_getenv ("MONO_ENABLE_AIO") != NULL) {
+       if (g_hasenv ("MONO_ENABLE_AIO")) {
 #if defined(HAVE_EPOLL)
                threadpool_io->backend = backend_epoll;
 #elif defined(HAVE_KQUEUE)
@@ -537,21 +559,21 @@ 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, TRUE, SMALL_STACK, &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
 cleanup (void)
 {
-       /* we make the assumption along the code that we are
-        * cleaning up only if the runtime is shutting down */
-       g_assert (mono_runtime_is_shutting_down ());
-
-       selector_thread_wakeup ();
-       while (io_selector_running)
-               mono_thread_info_usleep (1000);
+       // FIXME destroy everything
 }
 
 void
@@ -579,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);
@@ -606,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;
@@ -628,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;