Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / metadata / threadpool-worker-default.c
index a0cdd9a2170229d1d83e5777dd0187d36073e6f0..b0ff471fe4ee92f0f8fda2cd3ea423c2f701c807 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * threadpool-worker.c: native threadpool worker
+/**
+ * \file
+ * native threadpool worker
  *
  * Author:
  *     Ludovic Henry (ludovic.henry@xamarin.com)
@@ -109,11 +110,6 @@ typedef struct {
        gdouble accumulated_sample_duration;
 } ThreadPoolHillClimbing;
 
-typedef struct {
-       MonoThreadPoolWorkerCallback callback;
-       gpointer data;
-} ThreadPoolWorkItem;
-
 typedef union {
        struct {
                gint16 max_working; /* determined by heuristic */
@@ -122,25 +118,24 @@ typedef union {
                gint16 parked; /* parked */
        } _;
        gint64 as_gint64;
-} ThreadPoolWorkerCounter;
-
-typedef MonoInternalThread ThreadPoolWorkerThread;
+} ThreadPoolWorkerCounter
+#ifdef __GNUC__
+__attribute__((aligned(64)))
+#endif
+;
 
 typedef struct {
        MonoRefCount ref;
 
+       MonoThreadPoolWorkerCallback callback;
+
        ThreadPoolWorkerCounter counters;
 
-       GPtrArray *threads; // ThreadPoolWorkerThread* []
-       MonoCoopMutex threads_lock; /* protect access to working_threads and parked_threads */
+       MonoCoopMutex parked_threads_lock;
        gint32 parked_threads_count;
        MonoCoopCond parked_threads_cond;
-       MonoCoopCond threads_exit_cond;
 
-       ThreadPoolWorkItem *work_items; // ThreadPoolWorkItem []
-       gint32 work_items_count;
-       gint32 work_items_size;
-       MonoCoopMutex work_items_lock;
+       volatile gint32 work_items_count;
 
        guint32 worker_creation_current_second;
        guint32 worker_creation_current_count;
@@ -221,22 +216,18 @@ rand_next (gpointer *handle, guint32 min, guint32 max)
 static void
 destroy (gpointer data)
 {
-#if 0
-       mono_coop_mutex_destroy (&worker.threads_lock);
+       mono_coop_mutex_destroy (&worker.parked_threads_lock);
        mono_coop_cond_destroy (&worker.parked_threads_cond);
 
-       mono_coop_mutex_destroy (&worker.work_items_lock);
-
        mono_coop_mutex_destroy (&worker.worker_creation_lock);
 
        mono_coop_mutex_destroy (&worker.heuristic_lock);
 
        g_free (worker.cpu_usage_state);
-#endif
 }
 
 void
-mono_threadpool_worker_init (void)
+mono_threadpool_worker_init (MonoThreadPoolWorkerCallback callback)
 {
        ThreadPoolHillClimbing *hc;
        const char *threads_per_cpu_env;
@@ -245,14 +236,11 @@ mono_threadpool_worker_init (void)
 
        mono_refcount_init (&worker, destroy);
 
-       worker.threads = g_ptr_array_new ();
-       mono_coop_mutex_init (&worker.threads_lock);
+       worker.callback = callback;
+
+       mono_coop_mutex_init (&worker.parked_threads_lock);
        worker.parked_threads_count = 0;
        mono_coop_cond_init (&worker.parked_threads_cond);
-       mono_coop_cond_init (&worker.threads_exit_cond);
-
-       /* worker.work_items_size is inited to 0 */
-       mono_coop_mutex_init (&worker.work_items_lock);
 
        worker.worker_creation_current_second = -1;
        mono_coop_mutex_init (&worker.worker_creation_lock);
@@ -298,7 +286,7 @@ mono_threadpool_worker_init (void)
 
        worker.limit_worker_min = threads_count;
 
-#if defined (PLATFORM_ANDROID) || defined (HOST_IOS)
+#if defined (HOST_ANDROID) || defined (HOST_IOS)
        worker.limit_worker_max = CLAMP (threads_count * 100, MIN (threads_count, 200), MAX (threads_count, 200));
 #else
        worker.limit_worker_max = threads_count * 100;
@@ -316,110 +304,36 @@ mono_threadpool_worker_init (void)
 void
 mono_threadpool_worker_cleanup (void)
 {
-       MonoInternalThread *current;
-
-       /* 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 ());
-
-       current = mono_thread_internal_current ();
-
-       while (worker.monitor_status != MONITOR_STATUS_NOT_RUNNING)
-               mono_thread_info_sleep (1, NULL);
-
-       mono_coop_mutex_lock (&worker.threads_lock);
-
-       /* unpark all worker.parked_threads */
-       mono_coop_cond_broadcast (&worker.parked_threads_cond);
-
-#if 0
-       for (;;) {
-               ThreadPoolWorkerCounter counter;
-
-               counter = COUNTER_READ ();
-               if (counter._.starting + counter._.working + counter._.parked == 0)
-                       break;
-
-               if (counter._.starting + counter._.working + counter._.parked == 1) {
-                       if (worker.threads->len == 1 && g_ptr_array_index (worker.threads, 0) == current) {
-                               /* We are waiting on ourselves */
-                               break;
-                       }
-               }
-
-               mono_coop_cond_wait (&worker.threads_exit_cond, &worker.threads_lock);
-       }
-#endif
-
-       mono_coop_mutex_unlock (&worker.threads_lock);
-
        mono_refcount_dec (&worker);
 }
 
 static void
-work_item_lock (void)
-{
-       mono_coop_mutex_lock (&worker.work_items_lock);
-}
-
-static void
-work_item_unlock (void)
-{
-       mono_coop_mutex_unlock (&worker.work_items_lock);
-}
-
-static void
-work_item_push (MonoThreadPoolWorkerCallback callback, gpointer data)
+work_item_push (void)
 {
-       ThreadPoolWorkItem work_item;
-
-       g_assert (callback);
-
-       work_item.callback = callback;
-       work_item.data = data;
-
-       work_item_lock ();
+       gint32 old, new;
 
-       g_assert (worker.work_items_count <= worker.work_items_size);
-
-       if (G_UNLIKELY (worker.work_items_count == worker.work_items_size)) {
-               worker.work_items_size += 64;
-               worker.work_items = g_renew (ThreadPoolWorkItem, worker.work_items, worker.work_items_size);
-       }
-
-       g_assert (worker.work_items);
-
-       worker.work_items [worker.work_items_count ++] = work_item;
-
-       // printf ("[push] worker.work_items = %p, worker.work_items_count = %d, worker.work_items_size = %d\n",
-       //      worker.work_items, worker.work_items_count, worker.work_items_size);
+       do {
+               old = InterlockedRead (&worker.work_items_count);
+               g_assert (old >= 0);
 
-       work_item_unlock ();
+               new = old + 1;
+       } while (InterlockedCompareExchange (&worker.work_items_count, new, old) != old);
 }
 
 static gboolean
-work_item_try_pop (ThreadPoolWorkItem *work_item)
+work_item_try_pop (void)
 {
-       g_assert (work_item);
+       gint32 old, new;
 
-       work_item_lock ();
-
-       // printf ("[pop]  worker.work_items = %p, worker.work_items_count = %d, worker.work_items_size = %d\n",
-       //      worker.work_items, worker.work_items_count, worker.work_items_size);
-
-       if (worker.work_items_count == 0) {
-               work_item_unlock ();
-               return FALSE;
-       }
-
-       *work_item = worker.work_items [-- worker.work_items_count];
+       do {
+               old = InterlockedRead (&worker.work_items_count);
+               g_assert (old >= 0);
 
-       if (G_UNLIKELY (worker.work_items_count >= 64 * 3 && worker.work_items_count < worker.work_items_size / 2)) {
-               worker.work_items_size -= 64;
-               worker.work_items = g_renew (ThreadPoolWorkItem, worker.work_items, worker.work_items_size);
-       }
+               if (old == 0)
+                       return FALSE;
 
-       work_item_unlock ();
+               new = old - 1;
+       } while (InterlockedCompareExchange (&worker.work_items_count, new, old) != old);
 
        return TRUE;
 }
@@ -427,31 +341,41 @@ work_item_try_pop (ThreadPoolWorkItem *work_item)
 static gint32
 work_item_count (void)
 {
-       gint32 count;
-
-       work_item_lock ();
-       count = worker.work_items_count;
-       work_item_unlock ();
-
-       return count;
+       return InterlockedRead (&worker.work_items_count);
 }
 
 static void worker_request (void);
 
 void
-mono_threadpool_worker_enqueue (MonoThreadPoolWorkerCallback callback, gpointer data)
+mono_threadpool_worker_request (void)
 {
-       work_item_push (callback, data);
+       if (!mono_refcount_tryinc (&worker))
+               return;
+
+       work_item_push ();
 
        worker_request ();
+
+       mono_refcount_dec (&worker);
 }
 
 static void
 worker_wait_interrupt (gpointer unused)
 {
-       mono_coop_mutex_lock (&worker.threads_lock);
-       mono_coop_cond_signal (&worker.parked_threads_cond);
-       mono_coop_mutex_unlock (&worker.threads_lock);
+       /* If the runtime is not shutting down, we are not using this mechanism to wake up a unparked thread, and if the
+        * runtime is shutting down, then we need to wake up ALL the threads.
+        * It might be a bit wasteful, but I witnessed shutdown hang where the main thread would abort and then wait for all
+        * background threads to exit (see mono_thread_manage). This would go wrong because not all threadpool threads would
+        * be unparked. It would end up getting unstucked because of the timeout, but that would delay shutdown by 5-60s. */
+       if (!mono_runtime_is_shutting_down ())
+               return;
+
+       if (!mono_refcount_tryinc (&worker))
+               return;
+
+       mono_coop_mutex_lock (&worker.parked_threads_lock);
+       mono_coop_cond_broadcast (&worker.parked_threads_cond);
+       mono_coop_mutex_unlock (&worker.parked_threads_lock);
 
        mono_refcount_dec (&worker);
 }
@@ -461,15 +385,16 @@ static gboolean
 worker_park (void)
 {
        gboolean timeout = FALSE;
+       gboolean interrupted = FALSE;
 
-       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker parking", mono_native_thread_id_get ());
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker parking",
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
 
-       mono_coop_mutex_lock (&worker.threads_lock);
+       mono_coop_mutex_lock (&worker.parked_threads_lock);
 
        if (!mono_runtime_is_shutting_down ()) {
                static gpointer rand_handle = NULL;
                MonoInternalThread *thread;
-               gboolean interrupted = FALSE;
                ThreadPoolWorkerCounter counter;
 
                if (!rand_handle)
@@ -486,19 +411,14 @@ worker_park (void)
 
                worker.parked_threads_count += 1;
 
-               mono_refcount_inc (&worker);
                mono_thread_info_install_interrupt (worker_wait_interrupt, NULL, &interrupted);
-               if (interrupted) {
-                       mono_refcount_dec (&worker);
+               if (interrupted)
                        goto done;
-               }
 
-               if (mono_coop_cond_timedwait (&worker.parked_threads_cond, &worker.threads_lock, rand_next (&rand_handle, 5 * 1000, 60 * 1000)) != 0)
+               if (mono_coop_cond_timedwait (&worker.parked_threads_cond, &worker.parked_threads_lock, rand_next (&rand_handle, 5 * 1000, 60 * 1000)) != 0)
                        timeout = TRUE;
 
                mono_thread_info_uninstall_interrupt (&interrupted);
-               if (!interrupted)
-                       mono_refcount_dec (&worker);
 
 done:
                worker.parked_threads_count -= 1;
@@ -509,9 +429,10 @@ done:
                });
        }
 
-       mono_coop_mutex_unlock (&worker.threads_lock);
+       mono_coop_mutex_unlock (&worker.parked_threads_lock);
 
-       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker unparking, timeout? %s", mono_native_thread_id_get (), timeout ? "yes" : "no");
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker unparking, timeout? %s interrupted? %s",
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), timeout ? "yes" : "no", interrupted ? "yes" : "no");
 
        return timeout;
 }
@@ -521,16 +442,18 @@ worker_try_unpark (void)
 {
        gboolean res = FALSE;
 
-       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try unpark worker", mono_native_thread_id_get ());
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try unpark worker",
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
 
-       mono_coop_mutex_lock (&worker.threads_lock);
+       mono_coop_mutex_lock (&worker.parked_threads_lock);
        if (worker.parked_threads_count > 0) {
                mono_coop_cond_signal (&worker.parked_threads_cond);
                res = TRUE;
        }
-       mono_coop_mutex_unlock (&worker.threads_lock);
+       mono_coop_mutex_unlock (&worker.parked_threads_lock);
 
-       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try unpark worker, success? %s", mono_native_thread_id_get (), res ? "yes" : "no");
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try unpark worker, success? %s",
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), res ? "yes" : "no");
 
        return res;
 }
@@ -541,7 +464,11 @@ worker_thread (gpointer unused)
        MonoInternalThread *thread;
        ThreadPoolWorkerCounter counter;
 
-       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_THREADPOOL, "[%p] worker starting", mono_native_thread_id_get ());
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker starting",
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
+
+       if (!mono_refcount_tryinc (&worker))
+               return 0;
 
        COUNTER_ATOMIC (counter, {
                counter._.starting --;
@@ -551,17 +478,11 @@ worker_thread (gpointer unused)
        thread = mono_thread_internal_current ();
        g_assert (thread);
 
-       mono_coop_mutex_lock (&worker.threads_lock);
-       g_ptr_array_add (worker.threads, thread);
-       mono_coop_mutex_unlock (&worker.threads_lock);
-
        while (!mono_runtime_is_shutting_down ()) {
-               ThreadPoolWorkItem work_item;
-
                if (mono_thread_interruption_checkpoint ())
                        continue;
 
-               if (!work_item_try_pop (&work_item)) {
+               if (!work_item_try_pop ()) {
                        gboolean timeout;
 
                        timeout = worker_park ();
@@ -571,25 +492,18 @@ worker_thread (gpointer unused)
                        continue;
                }
 
-               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker executing %p (%p)",
-                       mono_native_thread_id_get (), work_item.callback, work_item.data);
+               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker executing",
+                       GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
 
-               work_item.callback (work_item.data);
+               worker.callback ();
        }
 
-       mono_coop_mutex_lock (&worker.threads_lock);
-
        COUNTER_ATOMIC (counter, {
                counter._.working --;
        });
 
-       g_ptr_array_remove (worker.threads, thread);
-
-       mono_coop_cond_signal (&worker.threads_exit_cond);
-
-       mono_coop_mutex_unlock (&worker.threads_lock);
-
-       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_THREADPOOL, "[%p] worker finishing", mono_native_thread_id_get ());
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker finishing",
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
 
        mono_refcount_dec (&worker);
 
@@ -610,7 +524,8 @@ worker_try_create (void)
 
        mono_coop_mutex_lock (&worker.worker_creation_lock);
 
-       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker", mono_native_thread_id_get ());
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker",
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
 
        current_ticks = mono_100ns_ticks ();
        if (0 == current_ticks) {
@@ -624,7 +539,7 @@ worker_try_create (void)
                        g_assert (worker.worker_creation_current_count <= WORKER_CREATION_MAX_PER_SEC);
                        if (worker.worker_creation_current_count == WORKER_CREATION_MAX_PER_SEC) {
                                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, failed: maximum number of worker created per second reached, current count = %d",
-                                       mono_native_thread_id_get (), worker.worker_creation_current_count);
+                                       GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), worker.worker_creation_current_count);
                                mono_coop_mutex_unlock (&worker.worker_creation_lock);
                                return FALSE;
                        }
@@ -634,17 +549,17 @@ worker_try_create (void)
        COUNTER_ATOMIC (counter, {
                if (counter._.working >= counter._.max_working) {
                        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, failed: maximum number of working threads reached",
-                               mono_native_thread_id_get ());
+                               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
                        mono_coop_mutex_unlock (&worker.worker_creation_lock);
                        return FALSE;
                }
                counter._.starting ++;
        });
 
-       mono_refcount_inc (&worker);
-       thread = mono_thread_create_internal (mono_get_root_domain (), worker_thread, NULL, TRUE, 0, &error);
+       thread = mono_thread_create_internal (mono_get_root_domain (), worker_thread, NULL, MONO_THREAD_CREATE_FLAGS_THREADPOOL, &error);
        if (!thread) {
-               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, failed: could not create thread due to %s", mono_native_thread_id_get (), mono_error_get_message (&error));
+               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, failed: could not create thread due to %s",
+                       GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), mono_error_get_message (&error));
                mono_error_cleanup (&error);
 
                COUNTER_ATOMIC (counter, {
@@ -653,15 +568,13 @@ worker_try_create (void)
 
                mono_coop_mutex_unlock (&worker.worker_creation_lock);
 
-               mono_refcount_dec (&worker);
-
                return FALSE;
        }
 
        worker.worker_creation_current_count += 1;
 
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, created %p, now = %d count = %d",
-               mono_native_thread_id_get (), (gpointer) thread->tid, now, worker.worker_creation_current_count);
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), (gpointer) thread->tid, now, worker.worker_creation_current_count);
 
        mono_coop_mutex_unlock (&worker.worker_creation_lock);
        return TRUE;
@@ -678,16 +591,19 @@ worker_request (void)
        monitor_ensure_running ();
 
        if (worker_try_unpark ()) {
-               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, unparked", mono_native_thread_id_get ());
+               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, unparked",
+                       GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
                return;
        }
 
        if (worker_try_create ()) {
-               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, created", mono_native_thread_id_get ());
+               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, created",
+                       GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
                return;
        }
 
-       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, failed", mono_native_thread_id_get ());
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, failed",
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
 }
 
 static gboolean
@@ -752,6 +668,9 @@ monitor_thread (gpointer unused)
        MonoInternalThread *internal;
        guint i;
 
+       if (!mono_refcount_tryinc (&worker))
+               return 0;
+
        internal = mono_thread_internal_current ();
        g_assert (internal);
 
@@ -759,7 +678,8 @@ monitor_thread (gpointer unused)
 
        // printf ("monitor_thread: start\n");
 
-       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, started", mono_native_thread_id_get ());
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, started",
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
 
        do {
                ThreadPoolWorkerCounter counter;
@@ -785,7 +705,6 @@ monitor_thread (gpointer unused)
                                break;
                        interval_left -= mono_msec_ticks () - ts;
 
-                       g_assert (!(internal->state & ThreadState_StopRequested));
                        mono_thread_interruption_checkpoint ();
                } while (interval_left > 0 && ++awake < 10);
 
@@ -823,12 +742,14 @@ monitor_thread (gpointer unused)
                                break;
 
                        if (worker_try_unpark ()) {
-                               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, unparked", mono_native_thread_id_get ());
+                               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, unparked",
+                                       GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
                                break;
                        }
 
                        if (worker_try_create ()) {
-                               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, created", mono_native_thread_id_get ());
+                               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, created",
+                                       GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
                                break;
                        }
                }
@@ -836,8 +757,10 @@ monitor_thread (gpointer unused)
 
        // printf ("monitor_thread: stop\n");
 
-       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, finished", mono_native_thread_id_get ());
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, finished",
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
 
+       mono_refcount_dec (&worker);
        return 0;
 }
 
@@ -860,10 +783,11 @@ monitor_ensure_running (void)
                                return;
                        if (InterlockedCompareExchange (&worker.monitor_status, MONITOR_STATUS_REQUESTED, MONITOR_STATUS_NOT_RUNNING) == MONITOR_STATUS_NOT_RUNNING) {
                                // printf ("monitor_thread: creating\n");
-                               if (!mono_thread_create_internal (mono_get_root_domain (), monitor_thread, NULL, TRUE, SMALL_STACK, &error)) {
+                               if (!mono_thread_create_internal (mono_get_root_domain (), monitor_thread, NULL, MONO_THREAD_CREATE_FLAGS_THREADPOOL | MONO_THREAD_CREATE_FLAGS_SMALL_STACK, &error)) {
                                        // printf ("monitor_thread: creating failed\n");
                                        worker.monitor_status = MONITOR_STATUS_NOT_RUNNING;
                                        mono_error_cleanup (&error);
+                                       mono_refcount_dec (&worker);
                                }
                                return;
                        }
@@ -880,7 +804,8 @@ hill_climbing_change_thread_count (gint16 new_thread_count, ThreadPoolHeuristicS
 
        hc = &worker.heuristic_hill_climbing;
 
-       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_THREADPOOL, "[%p] hill climbing, change max number of threads %d", mono_native_thread_id_get (), new_thread_count);
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] hill climbing, change max number of threads %d",
+               GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), new_thread_count);
 
        hc->last_thread_count = new_thread_count;
        hc->current_sample_interval = rand_next (&hc->random_interval_generator, hc->sample_interval_low, hc->sample_interval_high);
@@ -1204,7 +1129,15 @@ mono_threadpool_worker_notify_completed (void)
 gint32
 mono_threadpool_worker_get_min (void)
 {
-       return worker.limit_worker_min;
+       gint32 ret;
+
+       if (!mono_refcount_tryinc (&worker))
+               return 0;
+
+       ret = worker.limit_worker_min;
+
+       mono_refcount_dec (&worker);
+       return ret;
 }
 
 gboolean
@@ -1213,14 +1146,27 @@ mono_threadpool_worker_set_min (gint32 value)
        if (value <= 0 || value > worker.limit_worker_max)
                return FALSE;
 
+       if (!mono_refcount_tryinc (&worker))
+               return FALSE;
+
        worker.limit_worker_min = value;
+
+       mono_refcount_dec (&worker);
        return TRUE;
 }
 
 gint32
 mono_threadpool_worker_get_max (void)
 {
-       return worker.limit_worker_max;
+       gint32 ret;
+
+       if (!mono_refcount_tryinc (&worker))
+               return 0;
+
+       ret = worker.limit_worker_max;
+
+       mono_refcount_dec (&worker);
+       return ret;
 }
 
 gboolean
@@ -1232,17 +1178,24 @@ mono_threadpool_worker_set_max (gint32 value)
        if (value < worker.limit_worker_min || value < cpu_count)
                return FALSE;
 
-       if (value < worker.limit_worker_min || value < cpu_count)
+       if (!mono_refcount_tryinc (&worker))
                return FALSE;
 
        worker.limit_worker_max = value;
+
+       mono_refcount_dec (&worker);
        return TRUE;
 }
 
 void
 mono_threadpool_worker_set_suspended (gboolean suspended)
 {
+       if (!mono_refcount_tryinc (&worker))
+               return;
+
        worker.suspended = suspended;
        if (!suspended)
                worker_request ();
+
+       mono_refcount_dec (&worker);
 }