2 * threadpool-ms.c: Microsoft threadpool runtime support
5 * Ludovic Henry (ludovic.henry@xamarin.com)
7 * Copyright 2015 Xamarin, Inc (http://www.xamarin.com)
11 // Copyright (c) Microsoft. All rights reserved.
12 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 // - src/vm/comthreadpool.cpp
16 // - src/vm/win32threadpoolcpp
17 // - src/vm/threadpoolrequest.cpp
18 // - src/vm/hillclimbing.cpp
20 // Ported from C++ to C and adjusted to Mono runtime
23 #define _USE_MATH_DEFINES // needed by MSVC to define math constants
28 #include <mono/metadata/class-internals.h>
29 #include <mono/metadata/exception.h>
30 #include <mono/metadata/gc-internal.h>
31 #include <mono/metadata/object.h>
32 #include <mono/metadata/object-internals.h>
33 #include <mono/metadata/threadpool-ms.h>
34 #include <mono/metadata/threadpool-ms-io.h>
35 #include <mono/utils/atomic.h>
36 #include <mono/utils/mono-compiler.h>
37 #include <mono/utils/mono-complex.h>
38 #include <mono/utils/mono-proclib.h>
39 #include <mono/utils/mono-threads.h>
40 #include <mono/utils/mono-time.h>
41 #include <mono/utils/mono-rand.h>
43 #define CPU_USAGE_LOW 80
44 #define CPU_USAGE_HIGH 95
46 #define MONITOR_INTERVAL 100 // ms
48 /* The exponent to apply to the gain. 1.0 means to use linear gain,
49 * higher values will enhance large moves and damp small ones.
51 #define HILL_CLIMBING_GAIN_EXPONENT 2.0
53 /* The 'cost' of a thread. 0 means drive for increased throughput regardless
54 * of thread count, higher values bias more against higher thread counts.
56 #define HILL_CLIMBING_BIAS 0.15
58 #define HILL_CLIMBING_WAVE_PERIOD 4
59 #define HILL_CLIMBING_MAX_WAVE_MAGNITUDE 20
60 #define HILL_CLIMBING_WAVE_MAGNITUDE_MULTIPLIER 1.0
61 #define HILL_CLIMBING_WAVE_HISTORY_SIZE 8
62 #define HILL_CLIMBING_TARGET_SIGNAL_TO_NOISE_RATIO 3.0
63 #define HILL_CLIMBING_MAX_CHANGE_PER_SECOND 4
64 #define HILL_CLIMBING_MAX_CHANGE_PER_SAMPLE 20
65 #define HILL_CLIMBING_SAMPLE_INTERVAL_LOW 10
66 #define HILL_CLIMBING_SAMPLE_INTERVAL_HIGH 200
67 #define HILL_CLIMBING_ERROR_SMOOTHING_FACTOR 0.01
68 #define HILL_CLIMBING_MAX_SAMPLE_ERROR_PERCENT 0.15
72 gint16 max_working; /* determined by heuristic */
73 gint16 active; /* executing worker_thread */
74 gint16 working; /* actively executing worker_thread, not parked */
75 gint16 parked; /* parked */
82 gint32 outstanding_request;
85 typedef MonoInternalThread ThreadPoolWorkingThread;
86 typedef mono_cond_t ThreadPoolParkedThread;
90 gint32 samples_to_measure;
91 gdouble target_throughput_ratio;
92 gdouble target_signal_to_noise_ratio;
93 gdouble max_change_per_second;
94 gdouble max_change_per_sample;
95 gint32 max_thread_wave_magnitude;
96 gint32 sample_interval_low;
97 gdouble thread_magnitude_multiplier;
98 gint32 sample_interval_high;
99 gdouble throughput_error_smoothing_factor;
100 gdouble gain_exponent;
101 gdouble max_sample_error;
103 gdouble current_control_setting;
104 gint64 total_samples;
105 gint16 last_thread_count;
106 gdouble elapsed_since_last_change;
107 gdouble completions_since_last_change;
109 gdouble average_throughput_noise;
112 gdouble *thread_counts;
114 guint32 current_sample_interval;
115 gpointer random_interval_generator;
117 gint32 accumulated_completion_count;
118 gdouble accumulated_sample_duration;
119 } ThreadPoolHillClimbing;
122 ThreadPoolCounter counters;
124 GPtrArray *domains; // ThreadPoolDomain* []
125 mono_mutex_t domains_lock;
127 GPtrArray *working_threads; // ThreadPoolWorkingThread* []
128 GPtrArray *parked_threads; // ThreadPoolParkedThread* []
129 mono_mutex_t active_threads_lock; /* protect access to working_threads and parked_threads */
131 gint32 heuristic_completions;
132 guint32 heuristic_sample_start;
133 guint32 heuristic_last_dequeue; // ms
134 guint32 heuristic_last_adjustment; // ms
135 guint32 heuristic_adjustment_interval; // ms
136 ThreadPoolHillClimbing heuristic_hill_climbing;
137 mono_mutex_t heuristic_lock;
139 gint32 limit_worker_min;
140 gint32 limit_worker_max;
144 MonoCpuUsageState *cpu_usage_state;
147 /* suspended by the debugger */
153 TRANSITION_INITIALIZING,
154 TRANSITION_RANDOM_MOVE,
155 TRANSITION_CLIMBING_MOVE,
156 TRANSITION_CHANGE_POINT,
157 TRANSITION_STABILIZING,
158 TRANSITION_STARVATION,
159 TRANSITION_THREAD_TIMED_OUT,
160 TRANSITION_UNDEFINED,
161 } ThreadPoolHeuristicStateTransition;
164 MONITOR_STATUS_REQUESTED,
165 MONITOR_STATUS_WAITING_FOR_REQUEST,
166 MONITOR_STATUS_NOT_RUNNING,
169 static gint32 status = STATUS_NOT_INITIALIZED;
170 static gint32 monitor_status = MONITOR_STATUS_NOT_RUNNING;
172 static ThreadPool* threadpool;
174 #define COUNTER_CHECK(counter) \
176 g_assert (counter._.max_working > 0); \
177 g_assert (counter._.working >= 0); \
178 g_assert (counter._.active >= 0); \
181 #define COUNTER_READ() (InterlockedRead64 (&threadpool->counters.as_gint64))
183 #define COUNTER_ATOMIC(var,block) \
185 ThreadPoolCounter __old; \
187 g_assert (threadpool); \
188 __old.as_gint64 = COUNTER_READ (); \
191 COUNTER_CHECK (var); \
192 } while (InterlockedCompareExchange64 (&threadpool->counters.as_gint64, (var).as_gint64, __old.as_gint64) != __old.as_gint64); \
195 #define COUNTER_TRY_ATOMIC(res,var,block) \
197 ThreadPoolCounter __old; \
199 g_assert (threadpool); \
200 __old.as_gint64 = COUNTER_READ (); \
204 COUNTER_CHECK (var); \
205 (res) = InterlockedCompareExchange64 (&threadpool->counters.as_gint64, (var).as_gint64, __old.as_gint64) == __old.as_gint64; \
213 return mono_rand_init (NULL, 0);
217 rand_next (gpointer *handle, guint32 min, guint32 max)
220 if (!mono_rand_try_get_uint32 (handle, &val, min, max)) {
221 // FIXME handle error
222 g_assert_not_reached ();
228 rand_free (gpointer handle)
230 mono_rand_close (handle);
234 ensure_initialized (MonoBoolean *enable_worker_tracking)
236 ThreadPoolHillClimbing *hc;
237 const char *threads_per_cpu_env;
238 gint threads_per_cpu;
241 if (enable_worker_tracking) {
242 // TODO implement some kind of switch to have the possibily to use it
243 *enable_worker_tracking = FALSE;
246 if (status >= STATUS_INITIALIZED)
248 if (status == STATUS_INITIALIZING || InterlockedCompareExchange (&status, STATUS_INITIALIZING, STATUS_NOT_INITIALIZED) != STATUS_NOT_INITIALIZED) {
249 while (status == STATUS_INITIALIZING)
250 mono_thread_info_yield ();
251 g_assert (status >= STATUS_INITIALIZED);
255 g_assert (!threadpool);
256 threadpool = g_new0 (ThreadPool, 1);
257 g_assert (threadpool);
259 threadpool->domains = g_ptr_array_new ();
260 mono_mutex_init_recursive (&threadpool->domains_lock);
262 threadpool->parked_threads = g_ptr_array_new ();
263 threadpool->working_threads = g_ptr_array_new ();
264 mono_mutex_init (&threadpool->active_threads_lock);
266 threadpool->heuristic_adjustment_interval = 10;
267 mono_mutex_init (&threadpool->heuristic_lock);
271 hc = &threadpool->heuristic_hill_climbing;
273 hc->wave_period = HILL_CLIMBING_WAVE_PERIOD;
274 hc->max_thread_wave_magnitude = HILL_CLIMBING_MAX_WAVE_MAGNITUDE;
275 hc->thread_magnitude_multiplier = (gdouble) HILL_CLIMBING_WAVE_MAGNITUDE_MULTIPLIER;
276 hc->samples_to_measure = hc->wave_period * HILL_CLIMBING_WAVE_HISTORY_SIZE;
277 hc->target_throughput_ratio = (gdouble) HILL_CLIMBING_BIAS;
278 hc->target_signal_to_noise_ratio = (gdouble) HILL_CLIMBING_TARGET_SIGNAL_TO_NOISE_RATIO;
279 hc->max_change_per_second = (gdouble) HILL_CLIMBING_MAX_CHANGE_PER_SECOND;
280 hc->max_change_per_sample = (gdouble) HILL_CLIMBING_MAX_CHANGE_PER_SAMPLE;
281 hc->sample_interval_low = HILL_CLIMBING_SAMPLE_INTERVAL_LOW;
282 hc->sample_interval_high = HILL_CLIMBING_SAMPLE_INTERVAL_HIGH;
283 hc->throughput_error_smoothing_factor = (gdouble) HILL_CLIMBING_ERROR_SMOOTHING_FACTOR;
284 hc->gain_exponent = (gdouble) HILL_CLIMBING_GAIN_EXPONENT;
285 hc->max_sample_error = (gdouble) HILL_CLIMBING_MAX_SAMPLE_ERROR_PERCENT;
286 hc->current_control_setting = 0;
287 hc->total_samples = 0;
288 hc->last_thread_count = 0;
289 hc->average_throughput_noise = 0;
290 hc->elapsed_since_last_change = 0;
291 hc->accumulated_completion_count = 0;
292 hc->accumulated_sample_duration = 0;
293 hc->samples = g_new0 (gdouble, hc->samples_to_measure);
294 hc->thread_counts = g_new0 (gdouble, hc->samples_to_measure);
295 hc->random_interval_generator = rand_create ();
296 hc->current_sample_interval = rand_next (&hc->random_interval_generator, hc->sample_interval_low, hc->sample_interval_high);
298 if (!(threads_per_cpu_env = g_getenv ("MONO_THREADS_PER_CPU")))
301 threads_per_cpu = CLAMP (atoi (threads_per_cpu_env), 1, 50);
303 threads_count = mono_cpu_count () * threads_per_cpu;
305 threadpool->limit_worker_min = threadpool->limit_io_min = threads_count;
306 threadpool->limit_worker_max = threadpool->limit_io_max = threads_count * 100;
308 threadpool->counters._.max_working = threadpool->limit_worker_min;
310 threadpool->cpu_usage_state = g_new0 (MonoCpuUsageState, 1);
312 threadpool->suspended = FALSE;
314 status = STATUS_INITIALIZED;
317 static void worker_unpark (ThreadPoolParkedThread *thread);
318 static void worker_kill (ThreadPoolWorkingThread *thread);
321 ensure_cleanedup (void)
325 if (status == STATUS_NOT_INITIALIZED && InterlockedCompareExchange (&status, STATUS_CLEANED_UP, STATUS_NOT_INITIALIZED) == STATUS_NOT_INITIALIZED)
327 if (status == STATUS_INITIALIZING) {
328 while (status == STATUS_INITIALIZING)
329 mono_thread_info_yield ();
331 if (status == STATUS_CLEANED_UP)
333 if (status == STATUS_CLEANING_UP || InterlockedCompareExchange (&status, STATUS_CLEANING_UP, STATUS_INITIALIZED) != STATUS_INITIALIZED) {
334 while (status == STATUS_CLEANING_UP)
335 mono_thread_info_yield ();
336 g_assert (status == STATUS_CLEANED_UP);
340 /* we make the assumption along the code that we are
341 * cleaning up only if the runtime is shutting down */
342 g_assert (mono_runtime_is_shutting_down ());
344 while (monitor_status != MONITOR_STATUS_NOT_RUNNING)
347 mono_mutex_lock (&threadpool->active_threads_lock);
349 /* stop all threadpool->working_threads */
350 for (i = 0; i < threadpool->working_threads->len; ++i)
351 worker_kill ((ThreadPoolWorkingThread*) g_ptr_array_index (threadpool->working_threads, i));
353 /* unpark all threadpool->parked_threads */
354 for (i = 0; i < threadpool->parked_threads->len; ++i)
355 worker_unpark ((ThreadPoolParkedThread*) g_ptr_array_index (threadpool->parked_threads, i));
357 mono_mutex_unlock (&threadpool->active_threads_lock);
359 status = STATUS_CLEANED_UP;
363 mono_threadpool_ms_enqueue_work_item (MonoDomain *domain, MonoObject *work_item)
365 static MonoClass *threadpool_class = NULL;
366 static MonoMethod *unsafe_queue_custom_work_item_method = NULL;
367 MonoDomain *current_domain;
371 g_assert (work_item);
373 if (!threadpool_class)
374 threadpool_class = mono_class_from_name (mono_defaults.corlib, "System.Threading", "ThreadPool");
375 g_assert (threadpool_class);
377 if (!unsafe_queue_custom_work_item_method)
378 unsafe_queue_custom_work_item_method = mono_class_get_method_from_name (threadpool_class, "UnsafeQueueCustomWorkItem", 2);
379 g_assert (unsafe_queue_custom_work_item_method);
383 args [0] = (gpointer) work_item;
384 args [1] = (gpointer) &f;
386 current_domain = mono_domain_get ();
387 if (current_domain == domain) {
388 mono_runtime_invoke (unsafe_queue_custom_work_item_method, NULL, args, NULL);
390 mono_thread_push_appdomain_ref (domain);
391 if (mono_domain_set (domain, FALSE)) {
392 mono_runtime_invoke (unsafe_queue_custom_work_item_method, NULL, args, NULL);
393 mono_domain_set (current_domain, TRUE);
395 mono_thread_pop_appdomain_ref ();
400 domain_add (ThreadPoolDomain *tpdomain)
406 mono_mutex_lock (&threadpool->domains_lock);
407 len = threadpool->domains->len;
408 for (i = 0; i < len; ++i) {
409 if (g_ptr_array_index (threadpool->domains, i) == tpdomain)
413 g_ptr_array_add (threadpool->domains, tpdomain);
414 mono_mutex_unlock (&threadpool->domains_lock);
418 domain_remove (ThreadPoolDomain *tpdomain)
424 mono_mutex_lock (&threadpool->domains_lock);
425 res = g_ptr_array_remove (threadpool->domains, tpdomain);
426 mono_mutex_unlock (&threadpool->domains_lock);
431 static ThreadPoolDomain *
432 domain_get (MonoDomain *domain, gboolean create)
434 ThreadPoolDomain *tpdomain = NULL;
439 mono_mutex_lock (&threadpool->domains_lock);
440 for (i = 0; i < threadpool->domains->len; ++i) {
441 ThreadPoolDomain *tmp = g_ptr_array_index (threadpool->domains, i);
442 if (tmp->domain == domain) {
447 if (!tpdomain && create) {
448 tpdomain = g_new0 (ThreadPoolDomain, 1);
449 tpdomain->domain = domain;
450 domain_add (tpdomain);
452 mono_mutex_unlock (&threadpool->domains_lock);
457 domain_free (ThreadPoolDomain *tpdomain)
463 domain_any_has_request (void)
465 gboolean res = FALSE;
468 mono_mutex_lock (&threadpool->domains_lock);
469 for (i = 0; i < threadpool->domains->len; ++i) {
470 ThreadPoolDomain *tmp = g_ptr_array_index (threadpool->domains, i);
471 if (tmp->outstanding_request > 0) {
476 mono_mutex_unlock (&threadpool->domains_lock);
480 static ThreadPoolDomain *
481 domain_get_next (ThreadPoolDomain *current)
483 ThreadPoolDomain *tpdomain = NULL;
486 mono_mutex_lock (&threadpool->domains_lock);
487 len = threadpool->domains->len;
489 guint i, current_idx = -1;
491 for (i = 0; i < len; ++i) {
492 if (current == g_ptr_array_index (threadpool->domains, i)) {
497 g_assert (current_idx >= 0);
499 for (i = current_idx + 1; i < len + current_idx + 1; ++i) {
500 ThreadPoolDomain *tmp = g_ptr_array_index (threadpool->domains, i % len);
501 if (tmp->outstanding_request > 0) {
507 mono_mutex_unlock (&threadpool->domains_lock);
515 MonoInternalThread *thread = mono_thread_internal_current ();
517 mono_cond_init (&cond, NULL);
519 mono_gc_set_skip_thread (TRUE);
521 mono_mutex_lock (&threadpool->active_threads_lock);
523 if (!mono_runtime_is_shutting_down ()) {
524 g_ptr_array_add (threadpool->parked_threads, &cond);
525 g_ptr_array_remove_fast (threadpool->working_threads, thread);
527 mono_cond_wait (&cond, &threadpool->active_threads_lock);
529 g_ptr_array_add (threadpool->working_threads, thread);
530 g_ptr_array_remove (threadpool->parked_threads, &cond);
533 mono_mutex_unlock (&threadpool->active_threads_lock);
535 mono_gc_set_skip_thread (FALSE);
537 mono_cond_destroy (&cond);
541 worker_try_unpark (void)
543 gboolean res = FALSE;
546 mono_mutex_lock (&threadpool->active_threads_lock);
547 len = threadpool->parked_threads->len;
549 mono_cond_t *cond = (mono_cond_t*) g_ptr_array_index (threadpool->parked_threads, len - 1);
550 mono_cond_signal (cond);
553 mono_mutex_unlock (&threadpool->active_threads_lock);
558 worker_unpark (ThreadPoolParkedThread *thread)
560 mono_cond_signal ((mono_cond_t*) thread);
564 worker_kill (ThreadPoolWorkingThread *thread)
566 if (thread == mono_thread_internal_current ())
569 mono_thread_internal_stop ((MonoInternalThread*) thread);
573 worker_thread (gpointer data)
575 static MonoClass *threadpool_wait_callback_class = NULL;
576 static MonoMethod *perform_wait_callback_method = NULL;
577 MonoInternalThread *thread;
578 ThreadPoolDomain *tpdomain, *previous_tpdomain;
579 ThreadPoolCounter counter;
580 gboolean retire = FALSE;
582 g_assert (status >= STATUS_INITIALIZED);
584 if (!threadpool_wait_callback_class)
585 threadpool_wait_callback_class = mono_class_from_name (mono_defaults.corlib, "System.Threading", "_ThreadPoolWaitCallback");
586 g_assert (threadpool_wait_callback_class);
588 if (!perform_wait_callback_method)
589 perform_wait_callback_method = mono_class_get_method_from_name (threadpool_wait_callback_class, "PerformWaitCallback", 0);
590 g_assert (perform_wait_callback_method);
592 g_assert (threadpool);
594 thread = mono_thread_internal_current ();
597 mono_thread_set_name_internal (thread, mono_string_new (mono_domain_get (), "Threadpool worker"), FALSE);
599 mono_mutex_lock (&threadpool->active_threads_lock);
600 g_ptr_array_add (threadpool->working_threads, thread);
601 mono_mutex_unlock (&threadpool->active_threads_lock);
603 previous_tpdomain = NULL;
605 mono_mutex_lock (&threadpool->domains_lock);
607 while (!mono_runtime_is_shutting_down ()) {
610 if ((thread->state & (ThreadState_StopRequested | ThreadState_SuspendRequested)) != 0) {
611 mono_mutex_unlock (&threadpool->domains_lock);
612 mono_thread_interruption_checkpoint ();
613 mono_mutex_lock (&threadpool->domains_lock);
616 if (retire || !(tpdomain = domain_get_next (previous_tpdomain))) {
617 COUNTER_ATOMIC (counter, {
618 counter._.working --;
622 mono_mutex_unlock (&threadpool->domains_lock);
624 mono_mutex_lock (&threadpool->domains_lock);
626 COUNTER_ATOMIC (counter, {
627 counter._.working ++;
637 tpdomain->outstanding_request --;
638 g_assert (tpdomain->outstanding_request >= 0);
640 g_assert (tpdomain->domain);
641 g_assert (tpdomain->domain->threadpool_jobs >= 0);
642 tpdomain->domain->threadpool_jobs ++;
644 mono_mutex_unlock (&threadpool->domains_lock);
646 mono_thread_push_appdomain_ref (tpdomain->domain);
647 if (mono_domain_set (tpdomain->domain, FALSE)) {
648 MonoObject *exc = NULL;
649 MonoObject *res = mono_runtime_invoke (perform_wait_callback_method, NULL, NULL, &exc);
651 mono_thread_internal_unhandled_exception (exc);
652 else if (res && *(MonoBoolean*) mono_object_unbox (res) == FALSE)
655 mono_thread_clr_state (thread , ~ThreadState_Background);
656 if (!mono_thread_test_state (thread , ThreadState_Background))
657 ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
659 mono_domain_set (mono_get_root_domain (), TRUE);
661 mono_thread_pop_appdomain_ref ();
663 mono_mutex_lock (&threadpool->domains_lock);
665 tpdomain->domain->threadpool_jobs --;
666 g_assert (tpdomain->domain->threadpool_jobs >= 0);
668 if (tpdomain->domain->threadpool_jobs == 0 && mono_domain_is_unloading (tpdomain->domain)) {
669 gboolean removed = domain_remove (tpdomain);
671 if (tpdomain->domain->cleanup_semaphore)
672 ReleaseSemaphore (tpdomain->domain->cleanup_semaphore, 1, NULL);
673 domain_free (tpdomain);
677 previous_tpdomain = tpdomain;
680 mono_mutex_unlock (&threadpool->domains_lock);
682 mono_mutex_lock (&threadpool->active_threads_lock);
683 g_ptr_array_remove_fast (threadpool->working_threads, thread);
684 mono_mutex_unlock (&threadpool->active_threads_lock);
686 COUNTER_ATOMIC (counter, {
693 worker_try_create (void)
695 ThreadPoolCounter counter;
697 COUNTER_ATOMIC (counter, {
698 if (counter._.working >= counter._.max_working)
700 counter._.working ++;
704 if (mono_thread_create_internal (mono_get_root_domain (), worker_thread, NULL, TRUE, 0) != NULL)
707 COUNTER_ATOMIC (counter, {
708 counter._.working --;
715 static void monitor_ensure_running (void);
718 worker_request (MonoDomain *domain)
720 ThreadPoolDomain *tpdomain;
723 g_assert (threadpool);
725 if (mono_runtime_is_shutting_down ())
728 mono_mutex_lock (&threadpool->domains_lock);
730 /* synchronize check with worker_thread */
731 if (mono_domain_is_unloading (domain)) {
732 mono_mutex_unlock (&threadpool->domains_lock);
736 tpdomain = domain_get (domain, TRUE);
738 tpdomain->outstanding_request ++;
740 mono_mutex_unlock (&threadpool->domains_lock);
742 if (threadpool->suspended)
745 monitor_ensure_running ();
747 if (worker_try_unpark ())
750 if (worker_try_create ())
757 monitor_should_keep_running (void)
759 g_assert (monitor_status == MONITOR_STATUS_WAITING_FOR_REQUEST || monitor_status == MONITOR_STATUS_REQUESTED);
761 if (InterlockedExchange (&monitor_status, MONITOR_STATUS_WAITING_FOR_REQUEST) == MONITOR_STATUS_WAITING_FOR_REQUEST) {
762 if (mono_runtime_is_shutting_down () || !domain_any_has_request ()) {
763 if (InterlockedCompareExchange (&monitor_status, MONITOR_STATUS_NOT_RUNNING, MONITOR_STATUS_WAITING_FOR_REQUEST) == MONITOR_STATUS_WAITING_FOR_REQUEST)
768 g_assert (monitor_status == MONITOR_STATUS_WAITING_FOR_REQUEST || monitor_status == MONITOR_STATUS_REQUESTED);
774 monitor_sufficient_delay_since_last_dequeue (void)
778 g_assert (threadpool);
780 if (threadpool->cpu_usage < CPU_USAGE_LOW) {
781 threshold = MONITOR_INTERVAL;
783 ThreadPoolCounter counter;
784 counter.as_gint64 = COUNTER_READ();
785 threshold = counter._.max_working * MONITOR_INTERVAL * 2;
788 return mono_msec_ticks () >= threadpool->heuristic_last_dequeue + threshold;
791 static void hill_climbing_force_change (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition);
794 monitor_thread (void)
796 MonoInternalThread *current_thread = mono_thread_internal_current ();
799 mono_cpu_usage (threadpool->cpu_usage_state);
802 MonoInternalThread *thread;
803 gboolean all_waitsleepjoin = TRUE;
804 gint32 interval_left = MONITOR_INTERVAL;
805 gint32 awake = 0; /* number of spurious awakes we tolerate before doing a round of rebalancing */
807 g_assert (monitor_status != MONITOR_STATUS_NOT_RUNNING);
809 mono_gc_set_skip_thread (TRUE);
814 if (mono_runtime_is_shutting_down ())
817 ts = mono_msec_ticks ();
818 if (SleepEx (interval_left, TRUE) == 0)
820 interval_left -= mono_msec_ticks () - ts;
822 mono_gc_set_skip_thread (FALSE);
823 if ((current_thread->state & (ThreadState_StopRequested | ThreadState_SuspendRequested)) != 0)
824 mono_thread_interruption_checkpoint ();
825 mono_gc_set_skip_thread (TRUE);
826 } while (interval_left > 0 && ++awake < 10);
828 mono_gc_set_skip_thread (FALSE);
830 if (threadpool->suspended)
833 if (mono_runtime_is_shutting_down () || !domain_any_has_request ())
836 mono_mutex_lock (&threadpool->active_threads_lock);
837 for (i = 0; i < threadpool->working_threads->len; ++i) {
838 thread = g_ptr_array_index (threadpool->working_threads, i);
839 if ((thread->state & ThreadState_WaitSleepJoin) == 0) {
840 all_waitsleepjoin = FALSE;
844 mono_mutex_unlock (&threadpool->active_threads_lock);
846 if (all_waitsleepjoin) {
847 ThreadPoolCounter counter;
848 COUNTER_ATOMIC (counter, { counter._.max_working ++; });
849 hill_climbing_force_change (counter._.max_working, TRANSITION_STARVATION);
852 threadpool->cpu_usage = mono_cpu_usage (threadpool->cpu_usage_state);
854 if (monitor_sufficient_delay_since_last_dequeue ()) {
855 for (i = 0; i < 5; ++i) {
856 if (mono_runtime_is_shutting_down ())
859 if (worker_try_unpark ())
862 if (worker_try_create ())
866 } while (monitor_should_keep_running ());
870 monitor_ensure_running (void)
873 switch (monitor_status) {
874 case MONITOR_STATUS_REQUESTED:
876 case MONITOR_STATUS_WAITING_FOR_REQUEST:
877 InterlockedCompareExchange (&monitor_status, MONITOR_STATUS_REQUESTED, MONITOR_STATUS_WAITING_FOR_REQUEST);
879 case MONITOR_STATUS_NOT_RUNNING:
880 if (mono_runtime_is_shutting_down ())
882 if (InterlockedCompareExchange (&monitor_status, MONITOR_STATUS_REQUESTED, MONITOR_STATUS_NOT_RUNNING) == MONITOR_STATUS_NOT_RUNNING) {
883 if (!mono_thread_create_internal (mono_get_root_domain (), monitor_thread, NULL, TRUE, SMALL_STACK))
884 monitor_status = MONITOR_STATUS_NOT_RUNNING;
888 default: g_assert_not_reached ();
894 hill_climbing_change_thread_count (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition)
896 ThreadPoolHillClimbing *hc;
898 g_assert (threadpool);
900 hc = &threadpool->heuristic_hill_climbing;
902 hc->last_thread_count = new_thread_count;
903 hc->current_sample_interval = rand_next (&hc->random_interval_generator, hc->sample_interval_low, hc->sample_interval_high);
904 hc->elapsed_since_last_change = 0;
905 hc->completions_since_last_change = 0;
909 hill_climbing_force_change (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition)
911 ThreadPoolHillClimbing *hc;
913 g_assert (threadpool);
915 hc = &threadpool->heuristic_hill_climbing;
917 if (new_thread_count != hc->last_thread_count) {
918 hc->current_control_setting += new_thread_count - hc->last_thread_count;
919 hill_climbing_change_thread_count (new_thread_count, transition);
923 static double_complex
924 hill_climbing_get_wave_component (gdouble *samples, guint sample_count, gdouble period)
926 ThreadPoolHillClimbing *hc;
927 gdouble w, cosine, sine, coeff, q0, q1, q2;
930 g_assert (threadpool);
931 g_assert (sample_count >= period);
932 g_assert (period >= 2);
934 hc = &threadpool->heuristic_hill_climbing;
936 w = 2.0 * M_PI / period;
939 coeff = 2.0 * cosine;
942 for (i = 0; i < sample_count; ++i) {
943 q0 = coeff * q1 - q2 + samples [(hc->total_samples - sample_count + i) % hc->samples_to_measure];
948 return mono_double_complex_scalar_div (mono_double_complex_make (q1 - q2 * cosine, (q2 * sine)), ((gdouble)sample_count));
952 hill_climbing_update (gint16 current_thread_count, guint32 sample_duration, gint32 completions, guint32 *adjustment_interval)
954 ThreadPoolHillClimbing *hc;
955 ThreadPoolHeuristicStateTransition transition;
957 gdouble throughput_error_estimate;
963 gint new_thread_wave_magnitude;
964 gint new_thread_count;
965 double_complex thread_wave_component;
966 double_complex throughput_wave_component;
967 double_complex ratio;
969 g_assert (threadpool);
970 g_assert (adjustment_interval);
972 hc = &threadpool->heuristic_hill_climbing;
974 /* If someone changed the thread count without telling us, update our records accordingly. */
975 if (current_thread_count != hc->last_thread_count)
976 hill_climbing_force_change (current_thread_count, TRANSITION_INITIALIZING);
978 /* Update the cumulative stats for this thread count */
979 hc->elapsed_since_last_change += sample_duration;
980 hc->completions_since_last_change += completions;
982 /* Add in any data we've already collected about this sample */
983 sample_duration += hc->accumulated_sample_duration;
984 completions += hc->accumulated_completion_count;
986 /* We need to make sure we're collecting reasonably accurate data. Since we're just counting the end
987 * of each work item, we are goinng to be missing some data about what really happened during the
988 * sample interval. The count produced by each thread includes an initial work item that may have
989 * started well before the start of the interval, and each thread may have been running some new
990 * work item for some time before the end of the interval, which did not yet get counted. So
991 * our count is going to be off by +/- threadCount workitems.
993 * The exception is that the thread that reported to us last time definitely wasn't running any work
994 * at that time, and the thread that's reporting now definitely isn't running a work item now. So
995 * we really only need to consider threadCount-1 threads.
997 * Thus the percent error in our count is +/- (threadCount-1)/numCompletions.
999 * We cannot rely on the frequency-domain analysis we'll be doing later to filter out this error, because
1000 * of the way it accumulates over time. If this sample is off by, say, 33% in the negative direction,
1001 * then the next one likely will be too. The one after that will include the sum of the completions
1002 * we missed in the previous samples, and so will be 33% positive. So every three samples we'll have
1003 * two "low" samples and one "high" sample. This will appear as periodic variation right in the frequency
1004 * range we're targeting, which will not be filtered by the frequency-domain translation. */
1005 if (hc->total_samples > 0 && ((current_thread_count - 1.0) / completions) >= hc->max_sample_error) {
1006 /* Not accurate enough yet. Let's accumulate the data so
1007 * far, and tell the ThreadPool to collect a little more. */
1008 hc->accumulated_sample_duration = sample_duration;
1009 hc->accumulated_completion_count = completions;
1010 *adjustment_interval = 10;
1011 return current_thread_count;
1014 /* We've got enouugh data for our sample; reset our accumulators for next time. */
1015 hc->accumulated_sample_duration = 0;
1016 hc->accumulated_completion_count = 0;
1018 /* Add the current thread count and throughput sample to our history. */
1019 throughput = ((gdouble) completions) / sample_duration;
1021 sample_index = hc->total_samples % hc->samples_to_measure;
1022 hc->samples [sample_index] = throughput;
1023 hc->thread_counts [sample_index] = current_thread_count;
1024 hc->total_samples ++;
1026 /* Set up defaults for our metrics. */
1027 thread_wave_component = mono_double_complex_make(0, 0);
1028 throughput_wave_component = mono_double_complex_make(0, 0);
1029 throughput_error_estimate = 0;
1030 ratio = mono_double_complex_make(0, 0);
1033 transition = TRANSITION_WARMUP;
1035 /* How many samples will we use? It must be at least the three wave periods we're looking for, and it must also
1036 * be a whole multiple of the primary wave's period; otherwise the frequency we're looking for will fall between
1037 * two frequency bands in the Fourier analysis, and we won't be able to measure it accurately. */
1038 sample_count = ((gint) MIN (hc->total_samples - 1, hc->samples_to_measure) / hc->wave_period) * hc->wave_period;
1040 if (sample_count > hc->wave_period) {
1042 gdouble average_throughput;
1043 gdouble average_thread_count;
1044 gdouble sample_sum = 0;
1045 gdouble thread_sum = 0;
1047 /* Average the throughput and thread count samples, so we can scale the wave magnitudes later. */
1048 for (i = 0; i < sample_count; ++i) {
1049 guint j = (hc->total_samples - sample_count + i) % hc->samples_to_measure;
1050 sample_sum += hc->samples [j];
1051 thread_sum += hc->thread_counts [j];
1054 average_throughput = sample_sum / sample_count;
1055 average_thread_count = thread_sum / sample_count;
1057 if (average_throughput > 0 && average_thread_count > 0) {
1058 gdouble noise_for_confidence, adjacent_period_1, adjacent_period_2;
1060 /* Calculate the periods of the adjacent frequency bands we'll be using to
1061 * measure noise levels. We want the two adjacent Fourier frequency bands. */
1062 adjacent_period_1 = sample_count / (((gdouble) sample_count) / ((gdouble) hc->wave_period) + 1);
1063 adjacent_period_2 = sample_count / (((gdouble) sample_count) / ((gdouble) hc->wave_period) - 1);
1065 /* Get the the three different frequency components of the throughput (scaled by average
1066 * throughput). Our "error" estimate (the amount of noise that might be present in the
1067 * frequency band we're really interested in) is the average of the adjacent bands. */
1068 throughput_wave_component = mono_double_complex_scalar_div (hill_climbing_get_wave_component (hc->samples, sample_count, hc->wave_period), average_throughput);
1069 throughput_error_estimate = cabs (mono_double_complex_scalar_div (hill_climbing_get_wave_component (hc->samples, sample_count, adjacent_period_1), average_throughput));
1071 if (adjacent_period_2 <= sample_count) {
1072 throughput_error_estimate = MAX (throughput_error_estimate, cabs (mono_double_complex_scalar_div (hill_climbing_get_wave_component (
1073 hc->samples, sample_count, adjacent_period_2), average_throughput)));
1076 /* Do the same for the thread counts, so we have something to compare to. We don't
1077 * measure thread count noise, because there is none; these are exact measurements. */
1078 thread_wave_component = mono_double_complex_scalar_div (hill_climbing_get_wave_component (hc->thread_counts, sample_count, hc->wave_period), average_thread_count);
1080 /* Update our moving average of the throughput noise. We'll use this
1081 * later as feedback to determine the new size of the thread wave. */
1082 if (hc->average_throughput_noise == 0) {
1083 hc->average_throughput_noise = throughput_error_estimate;
1085 hc->average_throughput_noise = (hc->throughput_error_smoothing_factor * throughput_error_estimate)
1086 + ((1.0 + hc->throughput_error_smoothing_factor) * hc->average_throughput_noise);
1089 if (cabs (thread_wave_component) > 0) {
1090 /* Adjust the throughput wave so it's centered around the target wave,
1091 * and then calculate the adjusted throughput/thread ratio. */
1092 ratio = mono_double_complex_div (mono_double_complex_sub (throughput_wave_component, mono_double_complex_scalar_mul(thread_wave_component, hc->target_throughput_ratio)), thread_wave_component);
1093 transition = TRANSITION_CLIMBING_MOVE;
1095 ratio = mono_double_complex_make (0, 0);
1096 transition = TRANSITION_STABILIZING;
1099 noise_for_confidence = MAX (hc->average_throughput_noise, throughput_error_estimate);
1100 if (noise_for_confidence > 0) {
1101 confidence = cabs (thread_wave_component) / noise_for_confidence / hc->target_signal_to_noise_ratio;
1103 /* there is no noise! */
1109 /* We use just the real part of the complex ratio we just calculated. If the throughput signal
1110 * is exactly in phase with the thread signal, this will be the same as taking the magnitude of
1111 * the complex move and moving that far up. If they're 180 degrees out of phase, we'll move
1112 * backward (because this indicates that our changes are having the opposite of the intended effect).
1113 * If they're 90 degrees out of phase, we won't move at all, because we can't tell wether we're
1114 * having a negative or positive effect on throughput. */
1115 move = creal (ratio);
1116 move = CLAMP (move, -1.0, 1.0);
1118 /* Apply our confidence multiplier. */
1119 move *= CLAMP (confidence, -1.0, 1.0);
1121 /* Now apply non-linear gain, such that values around zero are attenuated, while higher values
1122 * are enhanced. This allows us to move quickly if we're far away from the target, but more slowly
1123 * if we're getting close, giving us rapid ramp-up without wild oscillations around the target. */
1124 gain = hc->max_change_per_second * sample_duration;
1125 move = pow (fabs (move), hc->gain_exponent) * (move >= 0.0 ? 1 : -1) * gain;
1126 move = MIN (move, hc->max_change_per_sample);
1128 /* If the result was positive, and CPU is > 95%, refuse the move. */
1129 if (move > 0.0 && threadpool->cpu_usage > CPU_USAGE_HIGH)
1132 /* Apply the move to our control setting. */
1133 hc->current_control_setting += move;
1135 /* Calculate the new thread wave magnitude, which is based on the moving average we've been keeping of the
1136 * throughput error. This average starts at zero, so we'll start with a nice safe little wave at first. */
1137 new_thread_wave_magnitude = (gint)(0.5 + (hc->current_control_setting * hc->average_throughput_noise
1138 * hc->target_signal_to_noise_ratio * hc->thread_magnitude_multiplier * 2.0));
1139 new_thread_wave_magnitude = CLAMP (new_thread_wave_magnitude, 1, hc->max_thread_wave_magnitude);
1141 /* Make sure our control setting is within the ThreadPool's limits. */
1142 hc->current_control_setting = CLAMP (hc->current_control_setting, threadpool->limit_worker_min, threadpool->limit_worker_max - new_thread_wave_magnitude);
1144 /* Calculate the new thread count (control setting + square wave). */
1145 new_thread_count = (gint)(hc->current_control_setting + new_thread_wave_magnitude * ((hc->total_samples / (hc->wave_period / 2)) % 2));
1147 /* Make sure the new thread count doesn't exceed the ThreadPool's limits. */
1148 new_thread_count = CLAMP (new_thread_count, threadpool->limit_worker_min, threadpool->limit_worker_max);
1150 if (new_thread_count != current_thread_count)
1151 hill_climbing_change_thread_count (new_thread_count, transition);
1153 if (creal (ratio) < 0.0 && new_thread_count == threadpool->limit_worker_min)
1154 *adjustment_interval = (gint)(0.5 + hc->current_sample_interval * (10.0 * MAX (-1.0 * creal (ratio), 1.0)));
1156 *adjustment_interval = hc->current_sample_interval;
1158 return new_thread_count;
1162 heuristic_notify_work_completed (void)
1164 g_assert (threadpool);
1166 InterlockedIncrement (&threadpool->heuristic_completions);
1167 threadpool->heuristic_last_dequeue = mono_msec_ticks ();
1171 heuristic_should_adjust (void)
1173 g_assert (threadpool);
1175 if (threadpool->heuristic_last_dequeue > threadpool->heuristic_last_adjustment + threadpool->heuristic_adjustment_interval) {
1176 ThreadPoolCounter counter;
1177 counter.as_gint64 = COUNTER_READ();
1178 if (counter._.working <= counter._.max_working)
1186 heuristic_adjust (void)
1188 g_assert (threadpool);
1190 if (mono_mutex_trylock (&threadpool->heuristic_lock) == 0) {
1191 gint32 completions = InterlockedExchange (&threadpool->heuristic_completions, 0);
1192 guint32 sample_end = mono_msec_ticks ();
1193 guint32 sample_duration = sample_end - threadpool->heuristic_sample_start;
1195 if (sample_duration >= threadpool->heuristic_adjustment_interval / 2) {
1196 ThreadPoolCounter counter;
1197 gint16 new_thread_count;
1199 counter.as_gint64 = COUNTER_READ ();
1200 new_thread_count = hill_climbing_update (counter._.max_working, sample_duration, completions, &threadpool->heuristic_adjustment_interval);
1202 COUNTER_ATOMIC (counter, { counter._.max_working = new_thread_count; });
1204 if (new_thread_count > counter._.max_working)
1205 worker_request (mono_domain_get ());
1207 threadpool->heuristic_sample_start = sample_end;
1208 threadpool->heuristic_last_adjustment = mono_msec_ticks ();
1211 mono_mutex_unlock (&threadpool->heuristic_lock);
1216 mono_threadpool_ms_cleanup (void)
1218 #ifndef DISABLE_SOCKETS
1219 mono_threadpool_ms_io_cleanup ();
1221 ensure_cleanedup ();
1225 mono_threadpool_ms_begin_invoke (MonoDomain *domain, MonoObject *target, MonoMethod *method, gpointer *params)
1227 static MonoClass *async_call_klass = NULL;
1228 MonoMethodMessage *message;
1229 MonoAsyncResult *async_result;
1230 MonoAsyncCall *async_call;
1231 MonoDelegate *async_callback = NULL;
1232 MonoObject *state = NULL;
1234 if (!async_call_klass)
1235 async_call_klass = mono_class_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
1236 g_assert (async_call_klass);
1238 ensure_initialized (NULL);
1240 message = mono_method_call_message_new (method, params, mono_get_delegate_invoke (method->klass), (params != NULL) ? (&async_callback) : NULL, (params != NULL) ? (&state) : NULL);
1242 async_call = (MonoAsyncCall*) mono_object_new (domain, async_call_klass);
1243 MONO_OBJECT_SETREF (async_call, msg, message);
1244 MONO_OBJECT_SETREF (async_call, state, state);
1246 if (async_callback) {
1247 MONO_OBJECT_SETREF (async_call, cb_method, mono_get_delegate_invoke (((MonoObject*) async_callback)->vtable->klass));
1248 MONO_OBJECT_SETREF (async_call, cb_target, async_callback);
1251 async_result = mono_async_result_new (domain, NULL, async_call->state, NULL, (MonoObject*) async_call);
1252 MONO_OBJECT_SETREF (async_result, async_delegate, target);
1254 #ifndef DISABLE_SOCKETS
1255 if (mono_threadpool_ms_is_io (target, state))
1256 return mono_threadpool_ms_io_add (async_result, (MonoSocketAsyncResult*) state);
1259 mono_threadpool_ms_enqueue_work_item (domain, (MonoObject*) async_result);
1261 return async_result;
1265 mono_threadpool_ms_end_invoke (MonoAsyncResult *ares, MonoArray **out_args, MonoObject **exc)
1270 g_assert (out_args);
1275 /* check if already finished */
1276 mono_monitor_enter ((MonoObject*) ares);
1278 if (ares->endinvoke_called) {
1279 *exc = (MonoObject*) mono_get_exception_invalid_operation (NULL);
1280 mono_monitor_exit ((MonoObject*) ares);
1284 MONO_OBJECT_SETREF (ares, endinvoke_called, 1);
1286 /* wait until we are really finished */
1287 if (ares->completed) {
1288 mono_monitor_exit ((MonoObject *) ares);
1290 gpointer wait_event;
1292 wait_event = mono_wait_handle_get_handle ((MonoWaitHandle*) ares->handle);
1294 wait_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1295 g_assert(wait_event);
1296 MONO_OBJECT_SETREF (ares, handle, (MonoObject*) mono_wait_handle_new (mono_object_domain (ares), wait_event));
1298 mono_monitor_exit ((MonoObject*) ares);
1299 MONO_PREPARE_BLOCKING
1300 WaitForSingleObjectEx (wait_event, INFINITE, TRUE);
1301 MONO_FINISH_BLOCKING
1304 ac = (MonoAsyncCall*) ares->object_data;
1307 *exc = ac->msg->exc; /* FIXME: GC add write barrier */
1308 *out_args = ac->out_args;
1313 mono_threadpool_ms_remove_domain_jobs (MonoDomain *domain, int timeout)
1315 gboolean res = TRUE;
1320 g_assert (timeout >= -1);
1322 g_assert (mono_domain_is_unloading (domain));
1325 start = mono_msec_ticks ();
1327 #ifndef DISABLE_SOCKETS
1328 mono_threadpool_ms_io_remove_domain_jobs (domain);
1329 if (timeout != -1) {
1330 timeout -= mono_msec_ticks () - start;
1337 * There might be some threads out that could be about to execute stuff from the given domain.
1338 * We avoid that by setting up a semaphore to be pulsed by the thread that reaches zero.
1340 sem = domain->cleanup_semaphore = CreateSemaphore (NULL, 0, 1, NULL);
1343 * The memory barrier here is required to have global ordering between assigning to cleanup_semaphone
1344 * and reading threadpool_jobs. Otherwise this thread could read a stale version of threadpool_jobs
1347 mono_memory_write_barrier ();
1349 while (domain->threadpool_jobs) {
1350 MONO_PREPARE_BLOCKING
1351 WaitForSingleObject (sem, timeout);
1352 MONO_FINISH_BLOCKING
1353 if (timeout != -1) {
1354 timeout -= mono_msec_ticks () - start;
1362 domain->cleanup_semaphore = NULL;
1369 mono_threadpool_ms_suspend (void)
1372 threadpool->suspended = TRUE;
1376 mono_threadpool_ms_resume (void)
1379 threadpool->suspended = FALSE;
1383 ves_icall_System_Threading_ThreadPool_GetAvailableThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads)
1385 if (!worker_threads || !completion_port_threads)
1388 ensure_initialized (NULL);
1390 *worker_threads = threadpool->limit_worker_max;
1391 *completion_port_threads = threadpool->limit_io_max;
1395 ves_icall_System_Threading_ThreadPool_GetMinThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads)
1397 if (!worker_threads || !completion_port_threads)
1400 ensure_initialized (NULL);
1402 *worker_threads = threadpool->limit_worker_min;
1403 *completion_port_threads = threadpool->limit_io_min;
1407 ves_icall_System_Threading_ThreadPool_GetMaxThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads)
1409 if (!worker_threads || !completion_port_threads)
1412 ensure_initialized (NULL);
1414 *worker_threads = threadpool->limit_worker_max;
1415 *completion_port_threads = threadpool->limit_io_max;
1419 ves_icall_System_Threading_ThreadPool_SetMinThreadsNative (gint32 worker_threads, gint32 completion_port_threads)
1421 ensure_initialized (NULL);
1423 if (worker_threads <= 0 || worker_threads > threadpool->limit_worker_max)
1425 if (completion_port_threads <= 0 || completion_port_threads > threadpool->limit_io_max)
1428 threadpool->limit_worker_max = worker_threads;
1429 threadpool->limit_io_max = completion_port_threads;
1435 ves_icall_System_Threading_ThreadPool_SetMaxThreadsNative (gint32 worker_threads, gint32 completion_port_threads)
1437 gint cpu_count = mono_cpu_count ();
1439 ensure_initialized (NULL);
1441 if (worker_threads < threadpool->limit_worker_min || worker_threads < cpu_count)
1443 if (completion_port_threads < threadpool->limit_io_min || completion_port_threads < cpu_count)
1446 threadpool->limit_worker_max = worker_threads;
1447 threadpool->limit_io_max = completion_port_threads;
1453 ves_icall_System_Threading_ThreadPool_InitializeVMTp (MonoBoolean *enable_worker_tracking)
1455 ensure_initialized (enable_worker_tracking);
1459 ves_icall_System_Threading_ThreadPool_NotifyWorkItemComplete (void)
1461 ThreadPoolCounter counter;
1463 if (mono_domain_is_unloading (mono_domain_get ()) || mono_runtime_is_shutting_down ())
1466 heuristic_notify_work_completed ();
1468 if (heuristic_should_adjust ())
1469 heuristic_adjust ();
1471 counter.as_gint64 = COUNTER_READ ();
1472 return counter._.working <= counter._.max_working;
1476 ves_icall_System_Threading_ThreadPool_NotifyWorkItemProgressNative (void)
1478 heuristic_notify_work_completed ();
1480 if (heuristic_should_adjust ())
1481 heuristic_adjust ();
1485 ves_icall_System_Threading_ThreadPool_ReportThreadStatus (MonoBoolean is_working)
1488 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1492 ves_icall_System_Threading_ThreadPool_RequestWorkerThread (void)
1494 return worker_request (mono_domain_get ());
1497 MonoBoolean G_GNUC_UNUSED
1498 ves_icall_System_Threading_ThreadPool_PostQueuedCompletionStatus (MonoNativeOverlapped *native_overlapped)
1500 /* This copy the behavior of the current Mono implementation */
1501 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1505 MonoBoolean G_GNUC_UNUSED
1506 ves_icall_System_Threading_ThreadPool_BindIOCompletionCallbackNative (gpointer file_handle)
1508 /* This copy the behavior of the current Mono implementation */
1512 MonoBoolean G_GNUC_UNUSED
1513 ves_icall_System_Threading_ThreadPool_IsThreadPoolHosted (void)