[runtime] Use the mono_mutex types/functions instead of CriticalSection's.
[mono.git] / mono / metadata / threadpool.c
index 811c2852045508e0e56656b8287d9b27b2a79d71..39ade54daa14f83a12c5982a15be9d014e30f289 100644 (file)
@@ -19,6 +19,7 @@
 #include <mono/metadata/threadpool-internals.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/environment.h>
+#include <mono/metadata/mono-config.h>
 #include <mono/metadata/mono-mlist.h>
 #include <mono/metadata/mono-perfcounters.h>
 #include <mono/metadata/socket-io.h>
@@ -83,13 +84,13 @@ enum {
 };
 
 typedef struct {
-       CRITICAL_SECTION io_lock; /* access to sock_to_state */
+       mono_mutex_t io_lock; /* access to sock_to_state */
        int inited; // 0 -> not initialized , 1->initializing, 2->initialized, 3->cleaned up
        MonoGHashTable *sock_to_state;
 
        gint event_system;
        gpointer event_data;
-       void (*modify) (gpointer event_data, int fd, int operation, int events, gboolean is_new);
+       void (*modify) (gpointer p, int fd, int operation, int events, gboolean is_new);
        void (*wait) (gpointer sock_data);
        void (*shutdown) (gpointer event_data);
 } SocketIOData;
@@ -152,13 +153,14 @@ static void socket_io_cleanup (SocketIOData *data);
 static MonoObject *get_io_event (MonoMList **list, gint event);
 static int get_events_from_list (MonoMList *list);
 static int get_event_from_state (MonoSocketAsyncResult *state);
+static void check_for_interruption_critical (void);
 
 static MonoClass *async_call_klass;
 static MonoClass *socket_async_call_klass;
 static MonoClass *process_async_call_klass;
 
 static GPtrArray *wsqs;
-CRITICAL_SECTION wsqs_lock;
+mono_mutex_t wsqs_lock;
 static gboolean suspended;
 
 /* Hooks */
@@ -312,14 +314,14 @@ get_events_from_list (MonoMList *list)
 static void
 socket_io_cleanup (SocketIOData *data)
 {
-       EnterCriticalSection (&data->io_lock);
+       mono_mutex_lock (&data->io_lock);
        if (data->inited != 2) {
-               LeaveCriticalSection (&data->io_lock);
+               mono_mutex_unlock (&data->io_lock);
                return;
        }
        data->inited = 3;
        data->shutdown (data->event_data);
-       LeaveCriticalSection (&data->io_lock);
+       mono_mutex_unlock (&data->io_lock);
 }
 
 static int
@@ -441,15 +443,15 @@ mono_thread_pool_remove_socket (int sock)
        if (socket_io_data.inited == 0)
                return;
 
-       EnterCriticalSection (&socket_io_data.io_lock);
+       mono_mutex_lock (&socket_io_data.io_lock);
        if (socket_io_data.sock_to_state == NULL) {
-               LeaveCriticalSection (&socket_io_data.io_lock);
+               mono_mutex_unlock (&socket_io_data.io_lock);
                return;
        }
        list = mono_g_hash_table_lookup (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
        if (list)
                mono_g_hash_table_remove (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
-       LeaveCriticalSection (&socket_io_data.io_lock);
+       mono_mutex_unlock (&socket_io_data.io_lock);
        
        while (list) {
                state = (MonoSocketAsyncResult *) mono_mlist_get_data (list);
@@ -504,7 +506,7 @@ socket_io_init (SocketIOData *data)
                }
        }
 
-       EnterCriticalSection (&data->io_lock);
+       mono_mutex_lock (&data->io_lock);
        data->sock_to_state = mono_g_hash_table_new_type (g_direct_hash, g_direct_equal, MONO_HASH_VALUE_GC);
 #ifdef HAVE_EPOLL
        data->event_system = EPOLL_BACKEND;
@@ -517,8 +519,8 @@ socket_io_init (SocketIOData *data)
                data->event_system = POLL_BACKEND;
 
        init_event_system (data);
-       mono_thread_create_internal (mono_get_root_domain (), data->wait, data, TRUE, FALSE, SMALL_STACK);
-       LeaveCriticalSection (&data->io_lock);
+       mono_thread_create_internal (mono_get_root_domain (), data->wait, data, TRUE, SMALL_STACK);
+       mono_mutex_unlock (&data->io_lock);
        data->inited = 2;
        threadpool_start_thread (&async_io_tp);
 }
@@ -541,9 +543,9 @@ socket_io_add (MonoAsyncResult *ares, MonoSocketAsyncResult *state)
        MONO_OBJECT_SETREF (state, ares, ares);
 
        fd = GPOINTER_TO_INT (state->handle);
-       EnterCriticalSection (&data->io_lock);
+       mono_mutex_lock (&data->io_lock);
        if (data->sock_to_state == NULL) {
-               LeaveCriticalSection (&data->io_lock);
+               mono_mutex_unlock (&data->io_lock);
                return;
        }
        list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
@@ -557,8 +559,8 @@ socket_io_add (MonoAsyncResult *ares, MonoSocketAsyncResult *state)
 
        mono_g_hash_table_replace (data->sock_to_state, state->handle, list);
        ievt = get_events_from_list (list);
-       LeaveCriticalSection (&data->io_lock);
-       data->modify (data->event_data, fd, state->operation, ievt, is_new);
+       /* The modify function leaves the io_lock critical section. */
+       data->modify (data, fd, state->operation, ievt, is_new);
 }
 
 #ifndef DISABLE_SOCKETS
@@ -675,7 +677,7 @@ threadpool_start_idle_threads (ThreadPool *tp)
 #ifndef DISABLE_PERFCOUNTERS
                mono_perfcounter_update_value (tp->pc_nthreads, TRUE, 1);
 #endif
-               mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, tp, TRUE, FALSE, stack_size);
+               mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, tp, TRUE, stack_size);
                SleepEx (100, TRUE);
        } while (1);
 }
@@ -729,11 +731,11 @@ print_pool_info (ThreadPool *tp)
        g_print ("Queued: %d\n", (tp->tail - tp->head));
        if (tp == &async_tp) {
                int i;
-               EnterCriticalSection (&wsqs_lock);
+               mono_mutex_lock (&wsqs_lock);
                for (i = 0; i < wsqs->len; i++) {
                        g_print ("\tWSQ %d: %d\n", i, mono_wsq_count (g_ptr_array_index (wsqs, i)));
                }
-               LeaveCriticalSection (&wsqs_lock);
+               mono_mutex_unlock (&wsqs_lock);
        } else {
                g_print ("\tSockets: %d\n", mono_g_hash_table_size (socket_io_data.sock_to_state));
        }
@@ -796,7 +798,7 @@ monitor_thread (gpointer unused)
                                continue;
                        need_one = (mono_cq_count (tp->queue) > 0);
                        if (!need_one && !tp->is_io) {
-                               EnterCriticalSection (&wsqs_lock);
+                               mono_mutex_lock (&wsqs_lock);
                                for (i = 0; wsqs != NULL && i < wsqs->len; i++) {
                                        MonoWSQ *wsq;
                                        wsq = g_ptr_array_index (wsqs, i);
@@ -805,7 +807,7 @@ monitor_thread (gpointer unused)
                                                break;
                                        }
                                }
-                               LeaveCriticalSection (&wsqs_lock);
+                               mono_mutex_unlock (&wsqs_lock);
                        }
                        if (need_one)
                                threadpool_start_thread (tp);
@@ -840,7 +842,7 @@ mono_thread_pool_init (void)
        }
 
        MONO_GC_REGISTER_ROOT_FIXED (socket_io_data.sock_to_state);
-       InitializeCriticalSection (&socket_io_data.io_lock);
+       mono_mutex_init_recursive (&socket_io_data.io_lock);
        if (g_getenv ("MONO_THREADS_PER_CPU") != NULL) {
                threads_per_cpu = atoi (g_getenv ("MONO_THREADS_PER_CPU"));
                if (threads_per_cpu < 1)
@@ -855,7 +857,7 @@ mono_thread_pool_init (void)
        async_call_klass = mono_class_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
        g_assert (async_call_klass);
 
-       InitializeCriticalSection (&wsqs_lock);
+       mono_mutex_init_recursive (&wsqs_lock);
        wsqs = g_ptr_array_sized_new (MAX (100 * cpu_count, thread_count));
 
 #ifndef DISABLE_PERFCOUNTERS
@@ -1004,12 +1006,12 @@ mono_thread_pool_cleanup (void)
        }
 
        if (wsqs) {
-               EnterCriticalSection (&wsqs_lock);
+               mono_mutex_lock (&wsqs_lock);
                mono_wsq_cleanup ();
                if (wsqs)
                        g_ptr_array_free (wsqs, TRUE);
                wsqs = NULL;
-               LeaveCriticalSection (&wsqs_lock);
+               mono_mutex_unlock (&wsqs_lock);
                MONO_SEM_DESTROY (&async_tp.new_job);
        }
 }
@@ -1026,7 +1028,7 @@ threadpool_start_thread (ThreadPool *tp)
 #ifndef DISABLE_PERFCOUNTERS
                        mono_perfcounter_update_value (tp->pc_nthreads, TRUE, 1);
 #endif
-                       mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, tp, TRUE, FALSE, stack_size);
+                       mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, tp, TRUE, stack_size);
                        return TRUE;
                }
        }
@@ -1065,13 +1067,15 @@ threadpool_append_jobs (ThreadPool *tp, MonoObject **jobs, gint njobs)
 
        if (tp->pool_status == 0 && InterlockedCompareExchange (&tp->pool_status, 1, 0) == 0) {
                if (!tp->is_io) {
-                       mono_thread_create_internal (mono_get_root_domain (), monitor_thread, NULL, TRUE, FALSE, SMALL_STACK);
+                       mono_thread_create_internal (mono_get_root_domain (), monitor_thread, NULL, TRUE, SMALL_STACK);
                        threadpool_start_thread (tp);
                }
                /* Create on demand up to min_threads to avoid startup penalty for apps that don't use
                 * the threadpool that much
-               * mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, tp, TRUE, FALSE, SMALL_STACK);
-               */
+                */
+               if (mono_config_is_server_mode ()) {
+                       mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, tp, TRUE, SMALL_STACK);
+               }
        }
 
        for (i = 0; i < njobs; i++) {
@@ -1157,11 +1161,11 @@ mono_thread_pool_remove_domain_jobs (MonoDomain *domain, int timeout)
        threadpool_clear_queue (&async_tp, domain);
        threadpool_clear_queue (&async_io_tp, domain);
 
-       EnterCriticalSection (&socket_io_data.io_lock);
+       mono_mutex_lock (&socket_io_data.io_lock);
        if (socket_io_data.sock_to_state)
                mono_g_hash_table_foreach_remove (socket_io_data.sock_to_state, remove_sockstate_for_domain, domain);
 
-       LeaveCriticalSection (&socket_io_data.io_lock);
+       mono_mutex_unlock (&socket_io_data.io_lock);
        
        /*
         * There might be some threads out that could be about to execute stuff from the given domain.
@@ -1214,21 +1218,21 @@ add_wsq (void)
        int i;
        MonoWSQ *wsq;
 
-       EnterCriticalSection (&wsqs_lock);
+       mono_mutex_lock (&wsqs_lock);
        wsq = mono_wsq_create ();
        if (wsqs == NULL) {
-               LeaveCriticalSection (&wsqs_lock);
+               mono_mutex_unlock (&wsqs_lock);
                return NULL;
        }
        for (i = 0; i < wsqs->len; i++) {
                if (g_ptr_array_index (wsqs, i) == NULL) {
                        wsqs->pdata [i] = wsq;
-                       LeaveCriticalSection (&wsqs_lock);
+                       mono_mutex_unlock (&wsqs_lock);
                        return wsq;
                }
        }
        g_ptr_array_add (wsqs, wsq);
-       LeaveCriticalSection (&wsqs_lock);
+       mono_mutex_unlock (&wsqs_lock);
        return wsq;
 }
 
@@ -1240,9 +1244,9 @@ remove_wsq (MonoWSQ *wsq)
        if (wsq == NULL)
                return;
 
-       EnterCriticalSection (&wsqs_lock);
+       mono_mutex_lock (&wsqs_lock);
        if (wsqs == NULL) {
-               LeaveCriticalSection (&wsqs_lock);
+               mono_mutex_unlock (&wsqs_lock);
                return;
        }
        g_ptr_array_remove_fast (wsqs, wsq);
@@ -1258,7 +1262,7 @@ remove_wsq (MonoWSQ *wsq)
                }
        }
        mono_wsq_destroy (wsq);
-       LeaveCriticalSection (&wsqs_lock);
+       mono_mutex_unlock (&wsqs_lock);
 }
 
 static void
@@ -1275,7 +1279,7 @@ try_steal (MonoWSQ *local_wsq, gpointer *data, gboolean retry)
                if (mono_runtime_is_shutting_down ())
                        return;
 
-               EnterCriticalSection (&wsqs_lock);
+               mono_mutex_lock (&wsqs_lock);
                for (i = 0; wsqs != NULL && i < wsqs->len; i++) {
                        MonoWSQ *wsq;
 
@@ -1284,11 +1288,11 @@ try_steal (MonoWSQ *local_wsq, gpointer *data, gboolean retry)
                                continue;
                        mono_wsq_try_steal (wsqs->pdata [i], data, ms);
                        if (*data != NULL) {
-                               LeaveCriticalSection (&wsqs_lock);
+                               mono_mutex_unlock (&wsqs_lock);
                                return;
                        }
                }
-               LeaveCriticalSection (&wsqs_lock);
+               mono_mutex_unlock (&wsqs_lock);
                ms += 10;
        } while (retry && ms < 11);
 }
@@ -1296,9 +1300,10 @@ try_steal (MonoWSQ *local_wsq, gpointer *data, gboolean retry)
 static gboolean
 dequeue_or_steal (ThreadPool *tp, gpointer *data, MonoWSQ *local_wsq)
 {
-       if (mono_runtime_is_shutting_down ())
+       MonoCQ *queue = tp->queue;
+       if (mono_runtime_is_shutting_down () || !queue)
                return FALSE;
-       mono_cq_dequeue (tp->queue, (MonoObject **) data);
+       mono_cq_dequeue (queue, (MonoObject **) data);
        if (!tp->is_io && !*data)
                try_steal (local_wsq, data, FALSE);
        return (*data != NULL);
@@ -1391,26 +1396,63 @@ should_i_die (ThreadPool *tp)
        return result;
 }
 
+static void
+set_tp_thread_info (ThreadPool *tp)
+{
+       const gchar *name;
+       MonoInternalThread *thread = mono_thread_internal_current ();
+
+       mono_profiler_thread_start (thread->tid);
+       name = (tp->is_io) ? "IO Threadpool worker" : "Threadpool worker";
+       mono_thread_set_name_internal (thread, mono_string_new (mono_domain_get (), name), FALSE);
+}
+
+static void
+clear_thread_state (void)
+{
+       MonoInternalThread *thread = mono_thread_internal_current ();
+       /* If the callee changes the background status, set it back to TRUE */
+       mono_thread_clr_state (thread , ~ThreadState_Background);
+       if (!mono_thread_test_state (thread , ThreadState_Background))
+               ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
+}
+
+static void
+check_for_interruption_critical (void)
+{
+       MonoInternalThread *thread;
+       /*RULE NUMBER ONE OF SKIP_THREAD: NEVER POKE MANAGED STATE.*/
+       mono_gc_set_skip_thread (FALSE);
+
+       thread = mono_thread_internal_current ();
+       if (THREAD_WANTS_A_BREAK (thread))
+               mono_thread_interruption_checkpoint ();
+
+       /*RULE NUMBER TWO OF SKIP_THREAD: READ RULE NUMBER ONE.*/
+       mono_gc_set_skip_thread (TRUE);
+}
+
+static void
+fire_profiler_thread_end (void)
+{
+       MonoInternalThread *thread = mono_thread_internal_current ();
+       mono_profiler_thread_end (thread->tid);
+}
+
 static void
 async_invoke_thread (gpointer data)
 {
        MonoDomain *domain;
-       MonoInternalThread *thread;
        MonoWSQ *wsq;
        ThreadPool *tp;
        gboolean must_die;
-       const gchar *name;
   
        tp = data;
        wsq = NULL;
        if (!tp->is_io)
                wsq = add_wsq ();
 
-       thread = mono_thread_internal_current ();
-
-       mono_profiler_thread_start (thread->tid);
-       name = (tp->is_io) ? "IO Threadpool worker" : "Threadpool worker";
-       mono_thread_set_name_internal (thread, mono_string_new (mono_domain_get (), name), FALSE);
+       set_tp_thread_info (tp);
 
        if (tp_start_func)
                tp_start_func (tp_hooks_user_data);
@@ -1492,10 +1534,7 @@ async_invoke_thread (gpointer data)
                                }
                                mono_thread_pop_appdomain_ref ();
                                InterlockedDecrement (&tp->busy_threads);
-                               /* If the callee changes the background status, set it back to TRUE */
-                               mono_thread_clr_state (thread , ~ThreadState_Background);
-                               if (!mono_thread_test_state (thread , ThreadState_Background))
-                                       ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
+                               clear_thread_state ();
                        }
                }
 
@@ -1528,8 +1567,7 @@ async_invoke_thread (gpointer data)
 #endif
                                if (mono_runtime_is_shutting_down ())
                                        break;
-                               if (THREAD_WANTS_A_BREAK (thread))
-                                       mono_thread_interruption_checkpoint ();
+                               check_for_interruption_critical ();
                        }
                        InterlockedDecrement (&tp->waiting);
 
@@ -1566,7 +1604,7 @@ async_invoke_thread (gpointer data)
                                                remove_wsq (wsq);
                                        }
 
-                                       mono_profiler_thread_end (thread->tid);
+                                       fire_profiler_thread_end ();
 
                                        if (tp_finish_func)
                                                tp_finish_func (tp_hooks_user_data);
@@ -1617,9 +1655,9 @@ ves_icall_System_Threading_ThreadPool_SetMinThreads (gint workerThreads, gint co
        InterlockedExchange (&async_tp.min_threads, workerThreads);
        InterlockedExchange (&async_io_tp.min_threads, completionPortThreads);
        if (workerThreads > async_tp.nthreads)
-               mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, &async_tp, TRUE, FALSE, SMALL_STACK);
+               mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, &async_tp, TRUE, SMALL_STACK);
        if (completionPortThreads > async_io_tp.nthreads)
-               mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, &async_io_tp, TRUE, FALSE, SMALL_STACK);
+               mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, &async_io_tp, TRUE, SMALL_STACK);
        return TRUE;
 }