First set of licensing changes
[mono.git] / mono / metadata / threadpool-ms-io.c
index e2b777c5d1b25c08dedf2f79f45ffd2a7306f4bc..5f4f36a193f92ec0ee314160d9c1ea6db52dfc75 100644 (file)
@@ -5,6 +5,7 @@
  *     Ludovic Henry (ludovic.henry@xamarin.com)
  *
  * Copyright 2015 Xamarin, Inc (http://www.xamarin.com)
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include <config.h>
@@ -92,8 +93,8 @@ typedef struct {
 
        ThreadPoolIOUpdate updates [UPDATES_CAPACITY];
        gint updates_size;
-       mono_mutex_t updates_lock;
-       mono_cond_t updates_cond;
+       MonoCoopMutex updates_lock;
+       MonoCoopCond updates_cond;
 
 #if !defined(HOST_WIN32)
        gint wakeup_pipes [2];
@@ -203,12 +204,12 @@ static void
 filter_jobs_for_domain (gpointer key, gpointer value, gpointer user_data)
 {
        FilterSockaresForDomainData *data;
-       MonoMList *list = value, *element;
+       MonoMList *list = (MonoMList *)value, *element;
        MonoDomain *domain;
        MonoGHashTable *states;
 
        g_assert (user_data);
-       data = user_data;
+       data = (FilterSockaresForDomainData *)user_data;
        domain = data->domain;
        states = data->states;
 
@@ -245,6 +246,8 @@ filter_jobs_for_domain (gpointer key, gpointer value, gpointer user_data)
 static void
 wait_callback (gint fd, gint events, gpointer user_data)
 {
+       MonoError error;
+
        if (mono_runtime_is_shutting_down ())
                return;
 
@@ -259,7 +262,7 @@ wait_callback (gint fd, gint events, gpointer user_data)
                gint operations;
 
                g_assert (user_data);
-               states = user_data;
+               states = (MonoGHashTable *)user_data;
 
                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: cal fd %3d, events = %2s | %2s | %3s",
                        fd, (events & EVENT_IN) ? "RD" : "..", (events & EVENT_OUT) ? "WR" : "..", (events & EVENT_ERR) ? "ERR" : "...");
@@ -269,13 +272,18 @@ wait_callback (gint fd, gint events, gpointer user_data)
 
                if (list && (events & EVENT_IN) != 0) {
                        MonoIOSelectorJob *job = get_job_for_event (&list, EVENT_IN);
-                       if (job)
-                               mono_threadpool_ms_enqueue_work_item (((MonoObject*) job)->vtable->domain, (MonoObject*) job);
+                       if (job) {
+                               mono_threadpool_ms_enqueue_work_item (((MonoObject*) job)->vtable->domain, (MonoObject*) job, &error);
+                               mono_error_raise_exception (&error); /* FIXME don't raise here */
+                       }
+
                }
                if (list && (events & EVENT_OUT) != 0) {
                        MonoIOSelectorJob *job = get_job_for_event (&list, EVENT_OUT);
-                       if (job)
-                               mono_threadpool_ms_enqueue_work_item (((MonoObject*) job)->vtable->domain, (MonoObject*) job);
+                       if (job) {
+                               mono_threadpool_ms_enqueue_work_item (((MonoObject*) job)->vtable->domain, (MonoObject*) job, &error);
+                               mono_error_raise_exception (&error); /* FIXME don't raise here */
+                       }
                }
 
                remove_fd = (events & EVENT_ERR) == EVENT_ERR;
@@ -301,6 +309,7 @@ wait_callback (gint fd, gint events, gpointer user_data)
 static void
 selector_thread (gpointer data)
 {
+       MonoError error;
        MonoGHashTable *states;
 
        io_selector_running = TRUE;
@@ -316,7 +325,7 @@ selector_thread (gpointer data)
                gint i, j;
                gint res;
 
-               mono_mutex_lock (&threadpool_io->updates_lock);
+               mono_coop_mutex_lock (&threadpool_io->updates_lock);
 
                for (i = 0; i < threadpool_io->updates_size; ++i) {
                        ThreadPoolIOUpdate *update = &threadpool_io->updates [i];
@@ -368,8 +377,10 @@ selector_thread (gpointer data)
                                                        memset (update, 0, sizeof (ThreadPoolIOUpdate));
                                        }
 
-                                       for (; list; list = mono_mlist_remove_item (list, list))
-                                               mono_threadpool_ms_enqueue_work_item (mono_object_domain (mono_mlist_get_data (list)), mono_mlist_get_data (list));
+                                       for (; list; list = mono_mlist_remove_item (list, list)) {
+                                               mono_threadpool_ms_enqueue_work_item (mono_object_domain (mono_mlist_get_data (list)), mono_mlist_get_data (list), &error);
+                                               mono_error_raise_exception (&error); /* FIXME don't raise here */
+                                       }
 
                                        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: del fd %3d", fd);
                                        threadpool_io->backend.remove_fd (fd);
@@ -399,14 +410,14 @@ selector_thread (gpointer data)
                        }
                }
 
-               mono_cond_broadcast (&threadpool_io->updates_cond);
+               mono_coop_cond_broadcast (&threadpool_io->updates_cond);
 
                if (threadpool_io->updates_size > 0) {
                        threadpool_io->updates_size = 0;
                        memset (&threadpool_io->updates, 0, UPDATES_CAPACITY * sizeof (ThreadPoolIOUpdate));
                }
 
-               mono_mutex_unlock (&threadpool_io->updates_lock);
+               mono_coop_mutex_unlock (&threadpool_io->updates_lock);
 
                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_THREADPOOL, "io threadpool: wai");
 
@@ -432,7 +443,7 @@ update_get_new (void)
                /* we wait for updates to be applied in the selector_thread and we loop
                 * as long as none are available. if it happends too much, then we need
                 * to increase UPDATES_CAPACITY */
-               mono_cond_wait (&threadpool_io->updates_cond, &threadpool_io->updates_lock);
+               mono_coop_cond_wait (&threadpool_io->updates_cond, &threadpool_io->updates_lock);
        }
 
        g_assert (threadpool_io->updates_size < UPDATES_CAPACITY);
@@ -506,9 +517,9 @@ initialize (void)
        threadpool_io = g_new0 (ThreadPoolIO, 1);
        g_assert (threadpool_io);
 
-       mono_mutex_init_recursive (&threadpool_io->updates_lock);
-       mono_cond_init (&threadpool_io->updates_cond);
-       mono_gc_register_root ((void*)&threadpool_io->updates [0], sizeof (threadpool_io->updates), MONO_GC_DESCRIPTOR_NULL, MONO_ROOT_SOURCE_THREAD_POOL, "i/o thread pool updates list");
+       mono_coop_mutex_init (&threadpool_io->updates_lock);
+       mono_coop_cond_init (&threadpool_io->updates_cond);
+       mono_gc_register_root ((char *)&threadpool_io->updates [0], sizeof (threadpool_io->updates), MONO_GC_DESCRIPTOR_NULL, MONO_ROOT_SOURCE_THREAD_POOL, "i/o thread pool updates list");
 
        threadpool_io->updates_size = 0;
 
@@ -539,10 +550,10 @@ cleanup (void)
 
        selector_thread_wakeup ();
        while (io_selector_running)
-               g_usleep (1000);
+               mono_thread_info_usleep (1000);
 
-       mono_mutex_destroy (&threadpool_io->updates_lock);
-       mono_cond_destroy (&threadpool_io->updates_cond);
+       mono_coop_mutex_destroy (&threadpool_io->updates_lock);
+       mono_coop_cond_destroy (&threadpool_io->updates_cond);
 
        threadpool_io->backend.cleanup ();
 
@@ -583,7 +594,7 @@ ves_icall_System_IOSelector_Add (gpointer handle, MonoIOSelectorJob *job)
 
        mono_lazy_initialize (&io_status, initialize);
 
-       mono_mutex_lock (&threadpool_io->updates_lock);
+       mono_coop_mutex_lock (&threadpool_io->updates_lock);
 
        update = update_get_new ();
        update->type = UPDATE_ADD;
@@ -593,7 +604,7 @@ ves_icall_System_IOSelector_Add (gpointer handle, MonoIOSelectorJob *job)
 
        selector_thread_wakeup ();
 
-       mono_mutex_unlock (&threadpool_io->updates_lock);
+       mono_coop_mutex_unlock (&threadpool_io->updates_lock);
 }
 
 void
@@ -610,7 +621,7 @@ mono_threadpool_ms_io_remove_socket (int fd)
        if (!mono_lazy_is_initialized (&io_status))
                return;
 
-       mono_mutex_lock (&threadpool_io->updates_lock);
+       mono_coop_mutex_lock (&threadpool_io->updates_lock);
 
        update = update_get_new ();
        update->type = UPDATE_REMOVE_SOCKET;
@@ -619,9 +630,9 @@ mono_threadpool_ms_io_remove_socket (int fd)
 
        selector_thread_wakeup ();
 
-       mono_cond_wait (&threadpool_io->updates_cond, &threadpool_io->updates_lock);
+       mono_coop_cond_wait (&threadpool_io->updates_cond, &threadpool_io->updates_lock);
 
-       mono_mutex_unlock (&threadpool_io->updates_lock);
+       mono_coop_mutex_unlock (&threadpool_io->updates_lock);
 }
 
 void
@@ -632,7 +643,7 @@ mono_threadpool_ms_io_remove_domain_jobs (MonoDomain *domain)
        if (!mono_lazy_is_initialized (&io_status))
                return;
 
-       mono_mutex_lock (&threadpool_io->updates_lock);
+       mono_coop_mutex_lock (&threadpool_io->updates_lock);
 
        update = update_get_new ();
        update->type = UPDATE_REMOVE_DOMAIN;
@@ -641,9 +652,9 @@ mono_threadpool_ms_io_remove_domain_jobs (MonoDomain *domain)
 
        selector_thread_wakeup ();
 
-       mono_cond_wait (&threadpool_io->updates_cond, &threadpool_io->updates_lock);
+       mono_coop_cond_wait (&threadpool_io->updates_cond, &threadpool_io->updates_lock);
 
-       mono_mutex_unlock (&threadpool_io->updates_lock);
+       mono_coop_mutex_unlock (&threadpool_io->updates_lock);
 }
 
 #else