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
27 #if !defined (HAVE_COMPLEX_H)
28 #include <../../support/libm/complex.h>
33 #include <mono/metadata/class-internals.h>
34 #include <mono/metadata/exception.h>
35 #include <mono/metadata/gc-internal.h>
36 #include <mono/metadata/object.h>
37 #include <mono/metadata/object-internals.h>
38 #include <mono/metadata/threadpool-ms.h>
39 #include <mono/metadata/threadpool-ms-io.h>
40 #include <mono/metadata/threadpool-internals.h>
41 #include <mono/utils/atomic.h>
42 #include <mono/utils/mono-compiler.h>
43 #include <mono/utils/mono-proclib.h>
44 #include <mono/utils/mono-threads.h>
45 #include <mono/utils/mono-time.h>
46 #include <mono/utils/mono-rand.h>
48 #define CPU_USAGE_LOW 80
49 #define CPU_USAGE_HIGH 95
51 #define MONITOR_INTERVAL 100 // ms
53 /* The exponent to apply to the gain. 1.0 means to use linear gain,
54 * higher values will enhance large moves and damp small ones.
56 #define HILL_CLIMBING_GAIN_EXPONENT 2.0
58 /* The 'cost' of a thread. 0 means drive for increased throughput regardless
59 * of thread count, higher values bias more against higher thread counts.
61 #define HILL_CLIMBING_BIAS 0.15
63 #define HILL_CLIMBING_WAVE_PERIOD 4
64 #define HILL_CLIMBING_MAX_WAVE_MAGNITUDE 20
65 #define HILL_CLIMBING_WAVE_MAGNITUDE_MULTIPLIER 1.0
66 #define HILL_CLIMBING_WAVE_HISTORY_SIZE 8
67 #define HILL_CLIMBING_TARGET_SIGNAL_TO_NOISE_RATIO 3.0
68 #define HILL_CLIMBING_MAX_CHANGE_PER_SECOND 4
69 #define HILL_CLIMBING_MAX_CHANGE_PER_SAMPLE 20
70 #define HILL_CLIMBING_SAMPLE_INTERVAL_LOW 10
71 #define HILL_CLIMBING_SAMPLE_INTERVAL_HIGH 200
72 #define HILL_CLIMBING_ERROR_SMOOTHING_FACTOR 0.01
73 #define HILL_CLIMBING_MAX_SAMPLE_ERROR_PERCENT 0.15
75 /* Keep in sync with System.Threading.RuntimeWorkItem */
76 struct _MonoRuntimeWorkItem {
78 MonoAsyncResult *async_result;
83 gint16 max_working; /* determined by heuristic */
84 gint16 active; /* working or waiting on thread_work_sem; warm threads */
93 gint32 outstanding_request;
98 gint32 samples_to_measure;
99 gdouble target_throughput_ratio;
100 gdouble target_signal_to_noise_ratio;
101 gdouble max_change_per_second;
102 gdouble max_change_per_sample;
103 gint32 max_thread_wave_magnitude;
104 gint32 sample_interval_low;
105 gdouble thread_magnitude_multiplier;
106 gint32 sample_interval_high;
107 gdouble throughput_error_smoothing_factor;
108 gdouble gain_exponent;
109 gdouble max_sample_error;
111 gdouble current_control_setting;
112 gint64 total_samples;
113 gint16 last_thread_count;
114 gdouble elapsed_since_last_change;
115 gdouble completions_since_last_change;
117 gdouble average_throughput_noise;
120 gdouble *thread_counts;
122 guint32 current_sample_interval;
123 gpointer random_interval_generator;
125 gint32 accumulated_completion_count;
126 gdouble accumulated_sample_duration;
127 } ThreadPoolHillClimbing;
130 ThreadPoolCounter counters;
132 GPtrArray *domains; // ThreadPoolDomain* []
133 mono_mutex_t domains_lock;
135 GPtrArray *working_threads; // MonoInternalThread* []
136 mono_mutex_t working_threads_lock;
138 GPtrArray *parked_threads; // mono_cond_t* []
139 mono_mutex_t parked_threads_lock;
141 gint32 heuristic_completions;
142 guint32 heuristic_sample_start;
143 guint32 heuristic_last_dequeue; // ms
144 guint32 heuristic_last_adjustment; // ms
145 guint32 heuristic_adjustment_interval; // ms
146 ThreadPoolHillClimbing heuristic_hill_climbing;
147 mono_mutex_t heuristic_lock;
149 gint32 limit_worker_min;
150 gint32 limit_worker_max;
154 MonoCpuUsageState *cpu_usage_state;
157 /* suspended by the debugger */
163 TRANSITION_INITIALIZING,
164 TRANSITION_RANDOM_MOVE,
165 TRANSITION_CLIMBING_MOVE,
166 TRANSITION_CHANGE_POINT,
167 TRANSITION_STABILIZING,
168 TRANSITION_STARVATION,
169 TRANSITION_THREAD_TIMED_OUT,
170 TRANSITION_UNDEFINED,
171 } ThreadPoolHeuristicStateTransition;
174 MONITOR_STATUS_REQUESTED,
175 MONITOR_STATUS_WAITING_FOR_REQUEST,
176 MONITOR_STATUS_NOT_RUNNING,
179 static gint32 status = STATUS_NOT_INITIALIZED;
180 static gint32 monitor_status = MONITOR_STATUS_NOT_RUNNING;
182 static ThreadPool* threadpool;
184 #define COUNTER_CHECK(counter) \
186 g_assert (counter._.max_working > 0); \
187 g_assert (counter._.active >= 0); \
190 #define COUNTER_READ() ((ThreadPoolCounter) InterlockedRead64 (&threadpool->counters.as_gint64))
192 #define COUNTER_ATOMIC(var,block) \
194 ThreadPoolCounter __old; \
196 g_assert (threadpool); \
197 (var) = __old = COUNTER_READ (); \
199 COUNTER_CHECK (var); \
200 } while (InterlockedCompareExchange64 (&threadpool->counters.as_gint64, (var).as_gint64, __old.as_gint64) != __old.as_gint64); \
203 #define COUNTER_TRY_ATOMIC(res,var,block) \
205 ThreadPoolCounter __old; \
207 g_assert (threadpool); \
208 (var) = __old = COUNTER_READ (); \
211 COUNTER_CHECK (var); \
212 (res) = InterlockedCompareExchange64 (&threadpool->counters.as_gint64, (var).as_gint64, __old.as_gint64) == __old.as_gint64; \
220 return mono_rand_init (NULL, 0);
224 rand_next (gpointer *handle, guint32 min, guint32 max)
227 if (!mono_rand_try_get_uint32 (handle, &val, min, max)) {
228 // FIXME handle error
229 g_assert_not_reached ();
235 rand_free (gpointer handle)
237 mono_rand_close (handle);
241 ensure_initialized (MonoBoolean *enable_worker_tracking)
243 ThreadPoolHillClimbing *hc;
244 const char *threads_per_cpu_env;
245 gint threads_per_cpu;
248 if (enable_worker_tracking) {
249 // TODO implement some kind of switch to have the possibily to use it
250 *enable_worker_tracking = FALSE;
253 if (status >= STATUS_INITIALIZED)
255 if (status == STATUS_INITIALIZING || InterlockedCompareExchange (&status, STATUS_INITIALIZING, STATUS_NOT_INITIALIZED) != STATUS_NOT_INITIALIZED) {
256 while (status == STATUS_INITIALIZING)
257 mono_thread_info_yield ();
258 g_assert (status >= STATUS_INITIALIZED);
262 g_assert (!threadpool);
263 threadpool = g_new0 (ThreadPool, 1);
264 g_assert (threadpool);
266 threadpool->domains = g_ptr_array_new ();
267 mono_mutex_init_recursive (&threadpool->domains_lock);
269 threadpool->parked_threads = g_ptr_array_new ();
270 mono_mutex_init (&threadpool->parked_threads_lock);
272 threadpool->working_threads = g_ptr_array_new ();
273 mono_mutex_init (&threadpool->working_threads_lock);
275 threadpool->heuristic_adjustment_interval = 10;
276 mono_mutex_init (&threadpool->heuristic_lock);
280 hc = &threadpool->heuristic_hill_climbing;
282 hc->wave_period = HILL_CLIMBING_WAVE_PERIOD;
283 hc->max_thread_wave_magnitude = HILL_CLIMBING_MAX_WAVE_MAGNITUDE;
284 hc->thread_magnitude_multiplier = (gdouble) HILL_CLIMBING_WAVE_MAGNITUDE_MULTIPLIER;
285 hc->samples_to_measure = hc->wave_period * HILL_CLIMBING_WAVE_HISTORY_SIZE;
286 hc->target_throughput_ratio = (gdouble) HILL_CLIMBING_BIAS;
287 hc->target_signal_to_noise_ratio = (gdouble) HILL_CLIMBING_TARGET_SIGNAL_TO_NOISE_RATIO;
288 hc->max_change_per_second = (gdouble) HILL_CLIMBING_MAX_CHANGE_PER_SECOND;
289 hc->max_change_per_sample = (gdouble) HILL_CLIMBING_MAX_CHANGE_PER_SAMPLE;
290 hc->sample_interval_low = HILL_CLIMBING_SAMPLE_INTERVAL_LOW;
291 hc->sample_interval_high = HILL_CLIMBING_SAMPLE_INTERVAL_HIGH;
292 hc->throughput_error_smoothing_factor = (gdouble) HILL_CLIMBING_ERROR_SMOOTHING_FACTOR;
293 hc->gain_exponent = (gdouble) HILL_CLIMBING_GAIN_EXPONENT;
294 hc->max_sample_error = (gdouble) HILL_CLIMBING_MAX_SAMPLE_ERROR_PERCENT;
295 hc->current_control_setting = 0;
296 hc->total_samples = 0;
297 hc->last_thread_count = 0;
298 hc->average_throughput_noise = 0;
299 hc->elapsed_since_last_change = 0;
300 hc->accumulated_completion_count = 0;
301 hc->accumulated_sample_duration = 0;
302 hc->samples = g_new0 (gdouble, hc->samples_to_measure);
303 hc->thread_counts = g_new0 (gdouble, hc->samples_to_measure);
304 hc->random_interval_generator = rand_create ();
305 hc->current_sample_interval = rand_next (&hc->random_interval_generator, hc->sample_interval_low, hc->sample_interval_high);
307 if (!(threads_per_cpu_env = g_getenv ("MONO_THREADS_PER_CPU")))
310 threads_per_cpu = CLAMP (atoi (threads_per_cpu_env), 1, 50);
312 threads_count = mono_cpu_count () * threads_per_cpu;
314 threadpool->limit_worker_min = threadpool->limit_io_min = threads_count;
315 threadpool->limit_worker_max = threadpool->limit_io_max = threads_count * 100;
317 threadpool->counters._.max_working = threadpool->limit_worker_min;
319 threadpool->cpu_usage_state = g_new0 (MonoCpuUsageState, 1);
321 threadpool->suspended = FALSE;
323 status = STATUS_INITIALIZED;
327 ensure_cleanedup (void)
329 if (status == STATUS_NOT_INITIALIZED && InterlockedCompareExchange (&status, STATUS_CLEANED_UP, STATUS_NOT_INITIALIZED) == STATUS_NOT_INITIALIZED)
331 if (status == STATUS_INITIALIZING) {
332 while (status == STATUS_INITIALIZING)
333 mono_thread_info_yield ();
335 if (status == STATUS_CLEANED_UP)
337 if (status == STATUS_CLEANING_UP || InterlockedCompareExchange (&status, STATUS_CLEANING_UP, STATUS_INITIALIZED) != STATUS_INITIALIZED) {
338 while (status == STATUS_CLEANING_UP)
339 mono_thread_info_yield ();
340 g_assert (status == STATUS_CLEANED_UP);
344 /* we make the assumption along the code that we are
345 * cleaning up only if the runtime is shutting down */
346 g_assert (mono_runtime_is_shutting_down ());
348 /* Unpark all worker threads */
349 mono_mutex_lock (&threadpool->parked_threads_lock);
352 ThreadPoolCounter counter = COUNTER_READ ();
353 if (counter._.active == 0 && counter._.parked == 0)
355 if (counter._.active == 1) {
356 MonoInternalThread *thread = mono_thread_internal_current ();
357 if (thread->threadpool_thread) {
358 /* if there is only one active thread
359 * left and it's the current one */
363 for (i = 0; i < threadpool->parked_threads->len; ++i) {
364 mono_cond_t *cond = (mono_cond_t*) g_ptr_array_index (threadpool->parked_threads, i);
365 mono_cond_signal (cond);
367 mono_mutex_unlock (&threadpool->parked_threads_lock);
369 mono_mutex_lock (&threadpool->parked_threads_lock);
371 mono_mutex_unlock (&threadpool->parked_threads_lock);
373 while (monitor_status != MONITOR_STATUS_NOT_RUNNING)
376 g_ptr_array_free (threadpool->domains, TRUE);
377 mono_mutex_destroy (&threadpool->domains_lock);
379 g_ptr_array_free (threadpool->parked_threads, TRUE);
380 mono_mutex_destroy (&threadpool->parked_threads_lock);
382 g_ptr_array_free (threadpool->working_threads, TRUE);
383 mono_mutex_destroy (&threadpool->working_threads_lock);
385 mono_mutex_destroy (&threadpool->heuristic_lock);
386 g_free (threadpool->heuristic_hill_climbing.samples);
387 g_free (threadpool->heuristic_hill_climbing.thread_counts);
388 rand_free (threadpool->heuristic_hill_climbing.random_interval_generator);
390 g_free (threadpool->cpu_usage_state);
392 g_assert (threadpool);
395 g_assert (!threadpool);
397 status = STATUS_CLEANED_UP;
401 mono_threadpool_ms_enqueue_work_item (MonoDomain *domain, MonoObject *work_item)
403 static MonoClass *threadpool_class = NULL;
404 static MonoMethod *unsafe_queue_custom_work_item_method = NULL;
405 MonoDomain *current_domain;
409 g_assert (work_item);
411 if (!threadpool_class)
412 threadpool_class = mono_class_from_name (mono_defaults.corlib, "System.Threading", "ThreadPool");
413 g_assert (threadpool_class);
415 if (!unsafe_queue_custom_work_item_method)
416 unsafe_queue_custom_work_item_method = mono_class_get_method_from_name (threadpool_class, "UnsafeQueueCustomWorkItem", 2);
417 g_assert (unsafe_queue_custom_work_item_method);
421 args [0] = (gpointer) work_item;
422 args [1] = (gpointer) &f;
424 current_domain = mono_domain_get ();
425 if (current_domain == domain) {
426 mono_runtime_invoke (unsafe_queue_custom_work_item_method, NULL, args, NULL);
428 mono_thread_push_appdomain_ref (domain);
429 if (mono_domain_set (domain, FALSE)) {
430 mono_runtime_invoke (unsafe_queue_custom_work_item_method, NULL, args, NULL);
431 mono_domain_set (current_domain, TRUE);
433 mono_thread_pop_appdomain_ref ();
438 mono_threadpool_ms_enqueue_async_result (MonoDomain *domain, MonoAsyncResult *ares)
440 static MonoClass *runtime_work_item_class = NULL;
441 MonoRuntimeWorkItem *rwi;
445 if (!runtime_work_item_class)
446 runtime_work_item_class = mono_class_from_name (mono_defaults.corlib, "System.Threading", "MonoRuntimeWorkItem");
447 g_assert (runtime_work_item_class);
449 rwi = (MonoRuntimeWorkItem*) mono_object_new (domain, runtime_work_item_class);
450 MONO_OBJECT_SETREF (rwi, async_result, ares);
452 mono_threadpool_ms_enqueue_work_item (domain, (MonoObject*) rwi);
456 domain_add (ThreadPoolDomain *tpdomain)
462 mono_mutex_lock (&threadpool->domains_lock);
463 len = threadpool->domains->len;
464 for (i = 0; i < len; ++i) {
465 if (g_ptr_array_index (threadpool->domains, i) == tpdomain)
469 g_ptr_array_add (threadpool->domains, tpdomain);
470 mono_mutex_unlock (&threadpool->domains_lock);
474 domain_remove (ThreadPoolDomain *tpdomain)
480 mono_mutex_lock (&threadpool->domains_lock);
481 res = g_ptr_array_remove (threadpool->domains, tpdomain);
482 mono_mutex_unlock (&threadpool->domains_lock);
487 static ThreadPoolDomain *
488 domain_get_or_create (MonoDomain *domain)
490 ThreadPoolDomain *tpdomain = NULL;
495 mono_mutex_lock (&threadpool->domains_lock);
496 for (i = 0; i < threadpool->domains->len; ++i) {
497 ThreadPoolDomain *tmp = g_ptr_array_index (threadpool->domains, i);
498 if (tmp->domain == domain) {
504 tpdomain = g_new0 (ThreadPoolDomain, 1);
505 tpdomain->domain = domain;
506 domain_add (tpdomain);
508 mono_mutex_unlock (&threadpool->domains_lock);
513 domain_any_has_request ()
515 gboolean res = FALSE;
518 mono_mutex_lock (&threadpool->domains_lock);
519 for (i = 0; i < threadpool->domains->len; ++i) {
520 ThreadPoolDomain *tmp = g_ptr_array_index (threadpool->domains, i);
521 if (tmp->outstanding_request > 0) {
526 mono_mutex_unlock (&threadpool->domains_lock);
530 static ThreadPoolDomain *
531 domain_get_next (ThreadPoolDomain *current)
533 ThreadPoolDomain *tpdomain = NULL;
536 mono_mutex_lock (&threadpool->domains_lock);
537 len = threadpool->domains->len;
539 guint i, current_idx = -1;
541 for (i = 0; i < len; ++i) {
542 if (current == g_ptr_array_index (threadpool->domains, i)) {
547 g_assert (current_idx >= 0);
549 for (i = current_idx + 1; i < len + current_idx + 1; ++i) {
550 ThreadPoolDomain *tmp = g_ptr_array_index (threadpool->domains, i % len);
551 if (tmp->outstanding_request > 0) {
553 tpdomain->outstanding_request --;
554 g_assert (tpdomain->outstanding_request >= 0);
559 mono_mutex_unlock (&threadpool->domains_lock);
567 mono_cond_init (&cond, NULL);
569 mono_mutex_lock (&threadpool->parked_threads_lock);
570 g_ptr_array_add (threadpool->parked_threads, &cond);
571 mono_cond_wait (&cond, &threadpool->parked_threads_lock);
572 g_ptr_array_remove (threadpool->parked_threads, &cond);
573 mono_mutex_unlock (&threadpool->parked_threads_lock);
575 mono_cond_destroy (&cond);
579 worker_try_unpark (void)
581 gboolean res = FALSE;
584 mono_mutex_lock (&threadpool->parked_threads_lock);
585 len = threadpool->parked_threads->len;
587 mono_cond_t *cond = (mono_cond_t*) g_ptr_array_index (threadpool->parked_threads, len - 1);
588 mono_cond_signal (cond);
591 mono_mutex_unlock (&threadpool->parked_threads_lock);
596 worker_thread (gpointer data)
598 static MonoClass *threadpool_wait_callback_class = NULL;
599 static MonoMethod *perform_wait_callback_method = NULL;
600 MonoInternalThread *thread;
601 ThreadPoolDomain *tpdomain;
602 ThreadPoolCounter counter;
603 gboolean retire = FALSE;
605 g_assert (status >= STATUS_INITIALIZED);
609 g_assert (tpdomain->domain);
611 if (mono_runtime_is_shutting_down () || mono_domain_is_unloading (tpdomain->domain)) {
612 COUNTER_ATOMIC (counter, { counter._.active --; });
616 if (!threadpool_wait_callback_class)
617 threadpool_wait_callback_class = mono_class_from_name (mono_defaults.corlib, "System.Threading.Microsoft", "_ThreadPoolWaitCallback");
618 g_assert (threadpool_wait_callback_class);
620 if (!perform_wait_callback_method)
621 perform_wait_callback_method = mono_class_get_method_from_name (threadpool_wait_callback_class, "PerformWaitCallback", 0);
622 g_assert (perform_wait_callback_method);
624 g_assert (threadpool);
626 thread = mono_thread_internal_current ();
629 mono_mutex_lock (&threadpool->domains_lock);
635 g_assert (tpdomain->domain);
637 tpdomain->domain->threadpool_jobs ++;
639 mono_mutex_unlock (&threadpool->domains_lock);
641 mono_mutex_lock (&threadpool->working_threads_lock);
642 g_ptr_array_add (threadpool->working_threads, thread);
643 mono_mutex_unlock (&threadpool->working_threads_lock);
645 COUNTER_ATOMIC (counter, { counter._.working ++; });
647 mono_thread_push_appdomain_ref (tpdomain->domain);
648 if (mono_domain_set (tpdomain->domain, FALSE)) {
649 MonoObject *exc = NULL;
650 MonoObject *res = mono_runtime_invoke (perform_wait_callback_method, NULL, NULL, &exc);
652 mono_internal_thread_unhandled_exception (exc);
653 else if (res && *(MonoBoolean*) mono_object_unbox (res) == FALSE)
656 mono_thread_clr_state (thread , ~ThreadState_Background);
657 if (!mono_thread_test_state (thread , ThreadState_Background))
658 ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
660 mono_thread_pop_appdomain_ref ();
662 COUNTER_ATOMIC (counter, { counter._.working --; });
664 mono_mutex_lock (&threadpool->working_threads_lock);
665 g_ptr_array_remove_fast (threadpool->working_threads, thread);
666 mono_mutex_unlock (&threadpool->working_threads_lock);
668 mono_mutex_lock (&threadpool->domains_lock);
670 tpdomain->domain->threadpool_jobs --;
671 g_assert (tpdomain->domain->threadpool_jobs >= 0);
673 if (tpdomain->domain->threadpool_jobs == 0 && mono_domain_is_unloading (tpdomain->domain)) {
674 gboolean removed = domain_remove (tpdomain);
676 if (tpdomain->domain->cleanup_semaphore)
677 ReleaseSemaphore (tpdomain->domain->cleanup_semaphore, 1, NULL);
682 for (i = 0, c = 5; i < c; ++i) {
683 if (mono_runtime_is_shutting_down ())
687 tpdomain = domain_get_next (tpdomain);
693 gboolean park = TRUE;
695 COUNTER_ATOMIC (counter, {
696 if (counter._.active <= counter._.max_working) {
705 mono_mutex_unlock (&threadpool->domains_lock);
706 mono_gc_set_skip_thread (TRUE);
708 mono_gc_set_skip_thread (FALSE);
709 mono_mutex_lock (&threadpool->domains_lock);
711 COUNTER_ATOMIC (counter, {
720 } while (tpdomain && !mono_runtime_is_shutting_down ());
722 mono_mutex_unlock (&threadpool->domains_lock);
724 COUNTER_ATOMIC (counter, { counter._.active --; });
728 worker_try_create (ThreadPoolDomain *tpdomain)
731 g_assert (tpdomain->domain);
733 return mono_thread_create_internal (tpdomain->domain, worker_thread, tpdomain, TRUE, 0) != NULL;
736 static void monitor_ensure_running (void);
739 worker_request (MonoDomain *domain)
741 ThreadPoolDomain *tpdomain;
742 ThreadPoolCounter counter;
745 g_assert (threadpool);
747 if (mono_runtime_is_shutting_down () || mono_domain_is_unloading (domain))
750 mono_mutex_lock (&threadpool->domains_lock);
751 tpdomain = domain_get_or_create (domain);
753 tpdomain->outstanding_request ++;
754 mono_mutex_unlock (&threadpool->domains_lock);
756 if (threadpool->suspended)
759 monitor_ensure_running ();
761 if (worker_try_unpark ())
764 COUNTER_ATOMIC (counter, {
765 if (counter._.active >= counter._.max_working)
770 if (worker_try_create (tpdomain))
773 COUNTER_ATOMIC (counter, { counter._.active --; });
778 monitor_should_keep_running (void)
780 g_assert (monitor_status == MONITOR_STATUS_WAITING_FOR_REQUEST || monitor_status == MONITOR_STATUS_REQUESTED);
782 if (InterlockedExchange (&monitor_status, MONITOR_STATUS_WAITING_FOR_REQUEST) == MONITOR_STATUS_WAITING_FOR_REQUEST) {
783 if (mono_runtime_is_shutting_down () || !domain_any_has_request ()) {
784 if (InterlockedExchange (&monitor_status, MONITOR_STATUS_NOT_RUNNING) == MONITOR_STATUS_WAITING_FOR_REQUEST)
789 g_assert (monitor_status == MONITOR_STATUS_WAITING_FOR_REQUEST || monitor_status == MONITOR_STATUS_REQUESTED);
795 monitor_sufficient_delay_since_last_dequeue (void)
799 g_assert (threadpool);
801 if (threadpool->cpu_usage < CPU_USAGE_LOW) {
802 threshold = MONITOR_INTERVAL;
804 ThreadPoolCounter counter = COUNTER_READ ();
805 threshold = counter._.max_working * MONITOR_INTERVAL * 2;
808 return mono_msec_ticks () >= threadpool->heuristic_last_dequeue + threshold;
811 static void hill_climbing_force_change (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition);
814 monitor_thread (void)
816 MonoInternalThread *current_thread = mono_thread_internal_current ();
819 mono_cpu_usage (threadpool->cpu_usage_state);
822 MonoInternalThread *thread;
823 gboolean all_waitsleepjoin = TRUE;
824 gint32 interval_left = MONITOR_INTERVAL;
825 gint32 awake = 0; /* number of spurious awakes we tolerate before doing a round of rebalancing */
827 g_assert (monitor_status != MONITOR_STATUS_NOT_RUNNING);
829 mono_gc_set_skip_thread (TRUE);
834 if (mono_runtime_is_shutting_down ())
837 ts = mono_msec_ticks ();
838 if (SleepEx (interval_left, TRUE) == 0)
840 interval_left -= mono_msec_ticks () - ts;
842 if ((current_thread->state & (ThreadState_StopRequested | ThreadState_SuspendRequested)) != 0)
843 mono_thread_interruption_checkpoint ();
844 } while (interval_left > 0 && ++awake < 10);
846 mono_gc_set_skip_thread (FALSE);
848 if (threadpool->suspended)
851 if (mono_runtime_is_shutting_down () || !domain_any_has_request ())
854 mono_mutex_lock (&threadpool->working_threads_lock);
855 for (i = 0; i < threadpool->working_threads->len; ++i) {
856 thread = g_ptr_array_index (threadpool->working_threads, i);
857 if ((thread->state & ThreadState_WaitSleepJoin) == 0) {
858 all_waitsleepjoin = FALSE;
862 mono_mutex_unlock (&threadpool->working_threads_lock);
864 if (all_waitsleepjoin) {
865 ThreadPoolCounter counter;
866 COUNTER_ATOMIC (counter, { counter._.max_working ++; });
867 hill_climbing_force_change (counter._.max_working, TRANSITION_STARVATION);
870 threadpool->cpu_usage = mono_cpu_usage (threadpool->cpu_usage_state);
872 if (monitor_sufficient_delay_since_last_dequeue ()) {
873 for (i = 0; i < 5; ++i) {
874 ThreadPoolDomain *tpdomain;
875 ThreadPoolCounter counter;
878 if (mono_runtime_is_shutting_down ())
881 if (worker_try_unpark ())
884 COUNTER_TRY_ATOMIC (success, counter, {
885 if (counter._.active >= counter._.max_working)
893 tpdomain = domain_get_next (NULL);
894 if (tpdomain && worker_try_create (tpdomain))
897 COUNTER_ATOMIC (counter, { counter._.active --; });
900 } while (monitor_should_keep_running ());
904 monitor_ensure_running (void)
907 switch (monitor_status) {
908 case MONITOR_STATUS_REQUESTED:
910 case MONITOR_STATUS_WAITING_FOR_REQUEST:
911 InterlockedCompareExchange (&monitor_status, MONITOR_STATUS_REQUESTED, MONITOR_STATUS_WAITING_FOR_REQUEST);
913 case MONITOR_STATUS_NOT_RUNNING:
914 if (InterlockedCompareExchange (&monitor_status, MONITOR_STATUS_REQUESTED, MONITOR_STATUS_NOT_RUNNING) == MONITOR_STATUS_NOT_RUNNING) {
915 if (!mono_thread_create_internal (mono_get_root_domain (), monitor_thread, NULL, TRUE, SMALL_STACK))
916 monitor_status = MONITOR_STATUS_NOT_RUNNING;
920 default: g_assert_not_reached ();
926 hill_climbing_change_thread_count (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition)
928 ThreadPoolHillClimbing *hc;
930 g_assert (threadpool);
932 hc = &threadpool->heuristic_hill_climbing;
934 hc->last_thread_count = new_thread_count;
935 hc->current_sample_interval = rand_next (&hc->random_interval_generator, hc->sample_interval_low, hc->sample_interval_high);
936 hc->elapsed_since_last_change = 0;
937 hc->completions_since_last_change = 0;
941 hill_climbing_force_change (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition)
943 ThreadPoolHillClimbing *hc;
945 g_assert (threadpool);
947 hc = &threadpool->heuristic_hill_climbing;
949 if (new_thread_count != hc->last_thread_count) {
950 hc->current_control_setting += new_thread_count - hc->last_thread_count;
951 hill_climbing_change_thread_count (new_thread_count, transition);
955 static double complex
956 hill_climbing_get_wave_component (gdouble *samples, guint sample_count, gdouble period)
958 ThreadPoolHillClimbing *hc;
959 gdouble w, cosine, sine, coeff, q0, q1, q2;
962 g_assert (threadpool);
963 g_assert (sample_count >= period);
964 g_assert (period >= 2);
966 hc = &threadpool->heuristic_hill_climbing;
968 w = 2.0 * M_PI / period;
971 coeff = 2.0 * cosine;
974 for (i = 0; i < sample_count; ++i) {
975 q0 = coeff * q1 - q2 + samples [(hc->total_samples - sample_count + i) % hc->samples_to_measure];
980 return ((q1 - q2 * cosine) + (q2 * sine) * I) / ((gdouble) sample_count);
984 hill_climbing_update (gint16 current_thread_count, guint32 sample_duration, gint32 completions, guint32 *adjustment_interval)
986 ThreadPoolHillClimbing *hc;
987 ThreadPoolHeuristicStateTransition transition;
989 gdouble throughput_error_estimate;
995 gint new_thread_wave_magnitude;
996 gint new_thread_count;
997 double complex thread_wave_component;
998 double complex throughput_wave_component;
999 double complex ratio;
1001 g_assert (threadpool);
1002 g_assert (adjustment_interval);
1004 hc = &threadpool->heuristic_hill_climbing;
1006 /* If someone changed the thread count without telling us, update our records accordingly. */
1007 if (current_thread_count != hc->last_thread_count)
1008 hill_climbing_force_change (current_thread_count, TRANSITION_INITIALIZING);
1010 /* Update the cumulative stats for this thread count */
1011 hc->elapsed_since_last_change += sample_duration;
1012 hc->completions_since_last_change += completions;
1014 /* Add in any data we've already collected about this sample */
1015 sample_duration += hc->accumulated_sample_duration;
1016 completions += hc->accumulated_completion_count;
1018 /* We need to make sure we're collecting reasonably accurate data. Since we're just counting the end
1019 * of each work item, we are goinng to be missing some data about what really happened during the
1020 * sample interval. The count produced by each thread includes an initial work item that may have
1021 * started well before the start of the interval, and each thread may have been running some new
1022 * work item for some time before the end of the interval, which did not yet get counted. So
1023 * our count is going to be off by +/- threadCount workitems.
1025 * The exception is that the thread that reported to us last time definitely wasn't running any work
1026 * at that time, and the thread that's reporting now definitely isn't running a work item now. So
1027 * we really only need to consider threadCount-1 threads.
1029 * Thus the percent error in our count is +/- (threadCount-1)/numCompletions.
1031 * We cannot rely on the frequency-domain analysis we'll be doing later to filter out this error, because
1032 * of the way it accumulates over time. If this sample is off by, say, 33% in the negative direction,
1033 * then the next one likely will be too. The one after that will include the sum of the completions
1034 * we missed in the previous samples, and so will be 33% positive. So every three samples we'll have
1035 * two "low" samples and one "high" sample. This will appear as periodic variation right in the frequency
1036 * range we're targeting, which will not be filtered by the frequency-domain translation. */
1037 if (hc->total_samples > 0 && ((current_thread_count - 1.0) / completions) >= hc->max_sample_error) {
1038 /* Not accurate enough yet. Let's accumulate the data so
1039 * far, and tell the ThreadPool to collect a little more. */
1040 hc->accumulated_sample_duration = sample_duration;
1041 hc->accumulated_completion_count = completions;
1042 *adjustment_interval = 10;
1043 return current_thread_count;
1046 /* We've got enouugh data for our sample; reset our accumulators for next time. */
1047 hc->accumulated_sample_duration = 0;
1048 hc->accumulated_completion_count = 0;
1050 /* Add the current thread count and throughput sample to our history. */
1051 throughput = ((gdouble) completions) / sample_duration;
1053 sample_index = hc->total_samples % hc->samples_to_measure;
1054 hc->samples [sample_index] = throughput;
1055 hc->thread_counts [sample_index] = current_thread_count;
1056 hc->total_samples ++;
1058 /* Set up defaults for our metrics. */
1059 thread_wave_component = 0;
1060 throughput_wave_component = 0;
1061 throughput_error_estimate = 0;
1065 transition = TRANSITION_WARMUP;
1067 /* How many samples will we use? It must be at least the three wave periods we're looking for, and it must also
1068 * be a whole multiple of the primary wave's period; otherwise the frequency we're looking for will fall between
1069 * two frequency bands in the Fourier analysis, and we won't be able to measure it accurately. */
1070 sample_count = ((gint) MIN (hc->total_samples - 1, hc->samples_to_measure) / hc->wave_period) * hc->wave_period;
1072 if (sample_count > hc->wave_period) {
1074 gdouble average_throughput;
1075 gdouble average_thread_count;
1076 gdouble sample_sum = 0;
1077 gdouble thread_sum = 0;
1079 /* Average the throughput and thread count samples, so we can scale the wave magnitudes later. */
1080 for (i = 0; i < sample_count; ++i) {
1081 guint j = (hc->total_samples - sample_count + i) % hc->samples_to_measure;
1082 sample_sum += hc->samples [j];
1083 thread_sum += hc->thread_counts [j];
1086 average_throughput = sample_sum / sample_count;
1087 average_thread_count = thread_sum / sample_count;
1089 if (average_throughput > 0 && average_thread_count > 0) {
1090 gdouble noise_for_confidence, adjacent_period_1, adjacent_period_2;
1092 /* Calculate the periods of the adjacent frequency bands we'll be using to
1093 * measure noise levels. We want the two adjacent Fourier frequency bands. */
1094 adjacent_period_1 = sample_count / (((gdouble) sample_count) / ((gdouble) hc->wave_period) + 1);
1095 adjacent_period_2 = sample_count / (((gdouble) sample_count) / ((gdouble) hc->wave_period) - 1);
1097 /* Get the the three different frequency components of the throughput (scaled by average
1098 * throughput). Our "error" estimate (the amount of noise that might be present in the
1099 * frequency band we're really interested in) is the average of the adjacent bands. */
1100 throughput_wave_component = hill_climbing_get_wave_component (hc->samples, sample_count, hc->wave_period) / average_throughput;
1101 throughput_error_estimate = cabs (hill_climbing_get_wave_component (hc->samples, sample_count, adjacent_period_1) / average_throughput);
1103 if (adjacent_period_2 <= sample_count) {
1104 throughput_error_estimate = MAX (throughput_error_estimate, cabs (hill_climbing_get_wave_component (
1105 hc->samples, sample_count, adjacent_period_2) / average_throughput));
1108 /* Do the same for the thread counts, so we have something to compare to. We don't
1109 * measure thread count noise, because there is none; these are exact measurements. */
1110 thread_wave_component = hill_climbing_get_wave_component (hc->thread_counts, sample_count, hc->wave_period) / average_thread_count;
1112 /* Update our moving average of the throughput noise. We'll use this
1113 * later as feedback to determine the new size of the thread wave. */
1114 if (hc->average_throughput_noise == 0) {
1115 hc->average_throughput_noise = throughput_error_estimate;
1117 hc->average_throughput_noise = (hc->throughput_error_smoothing_factor * throughput_error_estimate)
1118 + ((1.0 + hc->throughput_error_smoothing_factor) * hc->average_throughput_noise);
1121 if (cabs (thread_wave_component) > 0) {
1122 /* Adjust the throughput wave so it's centered around the target wave,
1123 * and then calculate the adjusted throughput/thread ratio. */
1124 ratio = (throughput_wave_component - (hc->target_throughput_ratio * thread_wave_component)) / thread_wave_component;
1125 transition = TRANSITION_CLIMBING_MOVE;
1128 transition = TRANSITION_STABILIZING;
1131 noise_for_confidence = MAX (hc->average_throughput_noise, throughput_error_estimate);
1132 if (noise_for_confidence > 0) {
1133 confidence = cabs (thread_wave_component) / noise_for_confidence / hc->target_signal_to_noise_ratio;
1135 /* there is no noise! */
1141 /* We use just the real part of the complex ratio we just calculated. If the throughput signal
1142 * is exactly in phase with the thread signal, this will be the same as taking the magnitude of
1143 * the complex move and moving that far up. If they're 180 degrees out of phase, we'll move
1144 * backward (because this indicates that our changes are having the opposite of the intended effect).
1145 * If they're 90 degrees out of phase, we won't move at all, because we can't tell wether we're
1146 * having a negative or positive effect on throughput. */
1147 move = creal (ratio);
1148 move = CLAMP (move, -1.0, 1.0);
1150 /* Apply our confidence multiplier. */
1151 move *= CLAMP (confidence, -1.0, 1.0);
1153 /* Now apply non-linear gain, such that values around zero are attenuated, while higher values
1154 * are enhanced. This allows us to move quickly if we're far away from the target, but more slowly
1155 * if we're getting close, giving us rapid ramp-up without wild oscillations around the target. */
1156 gain = hc->max_change_per_second * sample_duration;
1157 move = pow (fabs (move), hc->gain_exponent) * (move >= 0.0 ? 1 : -1) * gain;
1158 move = MIN (move, hc->max_change_per_sample);
1160 /* If the result was positive, and CPU is > 95%, refuse the move. */
1161 if (move > 0.0 && threadpool->cpu_usage > CPU_USAGE_HIGH)
1164 /* Apply the move to our control setting. */
1165 hc->current_control_setting += move;
1167 /* Calculate the new thread wave magnitude, which is based on the moving average we've been keeping of the
1168 * throughput error. This average starts at zero, so we'll start with a nice safe little wave at first. */
1169 new_thread_wave_magnitude = (gint)(0.5 + (hc->current_control_setting * hc->average_throughput_noise
1170 * hc->target_signal_to_noise_ratio * hc->thread_magnitude_multiplier * 2.0));
1171 new_thread_wave_magnitude = CLAMP (new_thread_wave_magnitude, 1, hc->max_thread_wave_magnitude);
1173 /* Make sure our control setting is within the ThreadPool's limits. */
1174 hc->current_control_setting = CLAMP (hc->current_control_setting, threadpool->limit_worker_min, threadpool->limit_worker_max - new_thread_wave_magnitude);
1176 /* Calculate the new thread count (control setting + square wave). */
1177 new_thread_count = (gint)(hc->current_control_setting + new_thread_wave_magnitude * ((hc->total_samples / (hc->wave_period / 2)) % 2));
1179 /* Make sure the new thread count doesn't exceed the ThreadPool's limits. */
1180 new_thread_count = CLAMP (new_thread_count, threadpool->limit_worker_min, threadpool->limit_worker_max);
1182 if (new_thread_count != current_thread_count)
1183 hill_climbing_change_thread_count (new_thread_count, transition);
1185 if (creal (ratio) < 0.0 && new_thread_count == threadpool->limit_worker_min)
1186 *adjustment_interval = (gint)(0.5 + hc->current_sample_interval * (10.0 * MAX (-1.0 * creal (ratio), 1.0)));
1188 *adjustment_interval = hc->current_sample_interval;
1190 return new_thread_count;
1194 heuristic_notify_work_completed (void)
1196 g_assert (threadpool);
1198 InterlockedIncrement (&threadpool->heuristic_completions);
1199 threadpool->heuristic_last_dequeue = mono_msec_ticks ();
1203 heuristic_should_adjust ()
1205 g_assert (threadpool);
1207 if (threadpool->heuristic_last_dequeue > threadpool->heuristic_last_adjustment + threadpool->heuristic_adjustment_interval) {
1208 ThreadPoolCounter counter = COUNTER_READ ();
1209 if (counter._.active <= counter._.max_working)
1219 g_assert (threadpool);
1221 if (mono_mutex_trylock (&threadpool->heuristic_lock) == 0) {
1222 gint32 completions = InterlockedExchange (&threadpool->heuristic_completions, 0);
1223 guint32 sample_end = mono_msec_ticks ();
1224 guint32 sample_duration = sample_end - threadpool->heuristic_sample_start;
1226 if (sample_duration >= threadpool->heuristic_adjustment_interval / 2) {
1227 ThreadPoolCounter counter;
1228 gint16 new_thread_count;
1230 counter = COUNTER_READ ();
1231 new_thread_count = hill_climbing_update (counter._.max_working, sample_duration, completions, &threadpool->heuristic_adjustment_interval);
1233 COUNTER_ATOMIC (counter, { counter._.max_working = new_thread_count; });
1235 if (new_thread_count > counter._.max_working)
1236 worker_request (mono_domain_get ());
1238 threadpool->heuristic_sample_start = sample_end;
1239 threadpool->heuristic_last_adjustment = mono_msec_ticks ();
1242 mono_mutex_unlock (&threadpool->heuristic_lock);
1247 mono_threadpool_ms_cleanup (void)
1249 #ifndef DISABLE_SOCKETS
1250 mono_threadpool_ms_io_cleanup ();
1252 ensure_cleanedup ();
1256 mono_threadpool_ms_add (MonoObject *target, MonoMethodMessage *msg, MonoDelegate *async_callback, MonoObject *state)
1258 static MonoClass *async_call_klass = NULL;
1260 MonoAsyncResult *ares;
1263 if (!async_call_klass)
1264 async_call_klass = mono_class_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
1265 g_assert (async_call_klass);
1267 ensure_initialized (NULL);
1269 domain = mono_domain_get ();
1271 ac = (MonoAsyncCall*) mono_object_new (domain, async_call_klass);
1272 MONO_OBJECT_SETREF (ac, msg, msg);
1273 MONO_OBJECT_SETREF (ac, state, state);
1275 if (async_callback) {
1276 MONO_OBJECT_SETREF (ac, cb_method, mono_get_delegate_invoke (((MonoObject*) async_callback)->vtable->klass));
1277 MONO_OBJECT_SETREF (ac, cb_target, async_callback);
1280 ares = mono_async_result_new (domain, NULL, ac->state, NULL, (MonoObject*) ac);
1281 MONO_OBJECT_SETREF (ares, async_delegate, target);
1283 #ifndef DISABLE_SOCKETS
1284 if (mono_threadpool_ms_is_io (target, state))
1285 return mono_threadpool_ms_io_add (ares, (MonoSocketAsyncResult*) state);
1288 mono_threadpool_ms_enqueue_async_result (domain, ares);
1293 mono_threadpool_ms_finish (MonoAsyncResult *ares, MonoArray **out_args, MonoObject **exc)
1298 g_assert (out_args);
1303 /* check if already finished */
1304 mono_monitor_enter ((MonoObject*) ares);
1306 if (ares->endinvoke_called) {
1307 *exc = (MonoObject*) mono_get_exception_invalid_operation (NULL);
1308 mono_monitor_exit ((MonoObject*) ares);
1312 MONO_OBJECT_SETREF (ares, endinvoke_called, 1);
1314 /* wait until we are really finished */
1315 if (ares->completed) {
1316 mono_monitor_exit ((MonoObject *) ares);
1318 gpointer wait_event;
1320 wait_event = mono_wait_handle_get_handle ((MonoWaitHandle*) ares->handle);
1322 wait_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1323 g_assert(wait_event);
1324 MONO_OBJECT_SETREF (ares, handle, (MonoObject*) mono_wait_handle_new (mono_object_domain (ares), wait_event));
1326 mono_monitor_exit ((MonoObject*) ares);
1327 MONO_PREPARE_BLOCKING
1328 WaitForSingleObjectEx (wait_event, INFINITE, TRUE);
1329 MONO_FINISH_BLOCKING
1332 ac = (MonoAsyncCall*) ares->object_data;
1335 *exc = ac->msg->exc; /* FIXME: GC add write barrier */
1336 *out_args = ac->out_args;
1341 mono_threadpool_ms_remove_domain_jobs (MonoDomain *domain, int timeout)
1343 gboolean res = TRUE;
1348 g_assert (timeout >= -1);
1351 start = mono_msec_ticks ();
1353 #ifndef DISABLE_SOCKETS
1354 mono_threadpool_ms_io_remove_domain_jobs (domain);
1355 if (timeout != -1) {
1356 timeout -= mono_msec_ticks () - start;
1362 * There might be some threads out that could be about to execute stuff from the given domain.
1363 * We avoid that by setting up a semaphore to be pulsed by the thread that reaches zero.
1365 sem = domain->cleanup_semaphore = CreateSemaphore (NULL, 0, 1, NULL);
1368 * The memory barrier here is required to have global ordering between assigning to cleanup_semaphone
1369 * and reading threadpool_jobs. Otherwise this thread could read a stale version of threadpool_jobs
1372 mono_memory_write_barrier ();
1374 while (domain->threadpool_jobs) {
1375 MONO_PREPARE_BLOCKING
1376 WaitForSingleObject (sem, timeout);
1377 MONO_FINISH_BLOCKING
1378 if (timeout != -1) {
1379 timeout -= mono_msec_ticks () - start;
1387 domain->cleanup_semaphore = NULL;
1394 mono_threadpool_ms_suspend (void)
1396 threadpool->suspended = TRUE;
1400 mono_threadpool_ms_resume (void)
1402 threadpool->suspended = FALSE;
1406 ves_icall_System_Threading_MonoRuntimeWorkItem_ExecuteWorkItem (MonoRuntimeWorkItem *rwi)
1408 MonoAsyncResult *ares;
1409 MonoObject *exc = NULL;
1412 ares = rwi->async_result;
1415 mono_async_result_invoke (ares, &exc);
1417 mono_raise_exception ((MonoException*) exc);
1421 ves_icall_System_Threading_Microsoft_ThreadPool_GetAvailableThreadsNative (gint *worker_threads, gint *completion_port_threads)
1423 if (!worker_threads || !completion_port_threads)
1426 ensure_initialized (NULL);
1428 *worker_threads = threadpool->limit_worker_max;
1429 *completion_port_threads = threadpool->limit_io_max;
1433 ves_icall_System_Threading_Microsoft_ThreadPool_GetMinThreadsNative (gint *worker_threads, gint *completion_port_threads)
1435 if (!worker_threads || !completion_port_threads)
1438 ensure_initialized (NULL);
1440 *worker_threads = threadpool->limit_worker_min;
1441 *completion_port_threads = threadpool->limit_io_min;
1445 ves_icall_System_Threading_Microsoft_ThreadPool_GetMaxThreadsNative (gint *worker_threads, gint *completion_port_threads)
1447 if (!worker_threads || !completion_port_threads)
1450 ensure_initialized (NULL);
1452 *worker_threads = threadpool->limit_worker_max;
1453 *completion_port_threads = threadpool->limit_io_max;
1457 ves_icall_System_Threading_Microsoft_ThreadPool_SetMinThreadsNative (gint worker_threads, gint completion_port_threads)
1459 ensure_initialized (NULL);
1461 if (worker_threads <= 0 || worker_threads > threadpool->limit_worker_max)
1463 if (completion_port_threads <= 0 || completion_port_threads > threadpool->limit_io_max)
1466 threadpool->limit_worker_max = worker_threads;
1467 threadpool->limit_io_max = completion_port_threads;
1473 ves_icall_System_Threading_Microsoft_ThreadPool_SetMaxThreadsNative (gint worker_threads, gint completion_port_threads)
1475 gint cpu_count = mono_cpu_count ();
1477 ensure_initialized (NULL);
1479 if (worker_threads < threadpool->limit_worker_min || worker_threads < cpu_count)
1481 if (completion_port_threads < threadpool->limit_io_min || completion_port_threads < cpu_count)
1484 threadpool->limit_worker_max = worker_threads;
1485 threadpool->limit_io_max = completion_port_threads;
1491 ves_icall_System_Threading_Microsoft_ThreadPool_InitializeVMTp (MonoBoolean *enable_worker_tracking)
1493 ensure_initialized (enable_worker_tracking);
1497 ves_icall_System_Threading_Microsoft_ThreadPool_NotifyWorkItemComplete (void)
1499 ThreadPoolCounter counter;
1501 if (mono_domain_is_unloading (mono_domain_get ()) || mono_runtime_is_shutting_down ())
1504 heuristic_notify_work_completed ();
1506 if (heuristic_should_adjust ())
1507 heuristic_adjust ();
1509 counter = COUNTER_READ ();
1510 return counter._.active <= counter._.max_working;
1514 ves_icall_System_Threading_Microsoft_ThreadPool_NotifyWorkItemProgressNative (void)
1516 heuristic_notify_work_completed ();
1518 if (heuristic_should_adjust ())
1519 heuristic_adjust ();
1523 ves_icall_System_Threading_Microsoft_ThreadPool_ReportThreadStatus (MonoBoolean is_working)
1526 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1530 ves_icall_System_Threading_Microsoft_ThreadPool_RequestWorkerThread (void)
1532 return worker_request (mono_domain_get ());
1535 MonoBoolean G_GNUC_UNUSED
1536 ves_icall_System_Threading_Microsoft_ThreadPool_PostQueuedCompletionStatus (MonoNativeOverlapped *native_overlapped)
1538 /* This copy the behavior of the current Mono implementation */
1539 mono_raise_exception (mono_get_exception_not_implemented (NULL));
1543 MonoBoolean G_GNUC_UNUSED
1544 ves_icall_System_Threading_Microsoft_ThreadPool_BindIOCompletionCallbackNative (gpointer file_handle)
1546 /* This copy the behavior of the current Mono implementation */
1550 MonoBoolean G_GNUC_UNUSED
1551 ves_icall_System_Threading_Microsoft_ThreadPool_IsThreadPoolHosted (void)