217384853490ca102fbde6ddfbb81730f6c5b18b
[mono.git] / mono / metadata / threadpool.c
1 /*
2  * threadpool.c: Microsoft threadpool runtime support
3  *
4  * Author:
5  *      Ludovic Henry (ludovic.henry@xamarin.com)
6  *
7  * Copyright 2015 Xamarin, Inc (http://www.xamarin.com)
8  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9  */
10
11 //
12 // Copyright (c) Microsoft. All rights reserved.
13 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 //
15 // Files:
16 //  - src/vm/comthreadpool.cpp
17 //  - src/vm/win32threadpoolcpp
18 //  - src/vm/threadpoolrequest.cpp
19 //  - src/vm/hillclimbing.cpp
20 //
21 // Ported from C++ to C and adjusted to Mono runtime
22
23 #include <stdlib.h>
24 #define _USE_MATH_DEFINES // needed by MSVC to define math constants
25 #include <math.h>
26 #include <config.h>
27 #include <glib.h>
28
29 #include <mono/metadata/class-internals.h>
30 #include <mono/metadata/exception.h>
31 #include <mono/metadata/gc-internals.h>
32 #include <mono/metadata/object.h>
33 #include <mono/metadata/object-internals.h>
34 #include <mono/metadata/threadpool.h>
35 #include <mono/metadata/threadpool-worker.h>
36 #include <mono/metadata/threadpool-io.h>
37 #include <mono/metadata/w32event.h>
38 #include <mono/utils/atomic.h>
39 #include <mono/utils/mono-compiler.h>
40 #include <mono/utils/mono-complex.h>
41 #include <mono/utils/mono-lazy-init.h>
42 #include <mono/utils/mono-logger.h>
43 #include <mono/utils/mono-logger-internals.h>
44 #include <mono/utils/mono-proclib.h>
45 #include <mono/utils/mono-threads.h>
46 #include <mono/utils/mono-time.h>
47 #include <mono/utils/refcount.h>
48 #include <mono/io-layer/io-layer.h>
49
50 typedef struct {
51         MonoDomain *domain;
52         /* Number of outstanding jobs */
53         gint32 outstanding_request;
54         /* Number of currently executing jobs */
55         gint32 threadpool_jobs;
56         /* Signalled when threadpool_jobs + outstanding_request is 0 */
57         /* Protected by threadpool->domains_lock */
58         MonoCoopCond cleanup_cond;
59 } ThreadPoolDomain;
60
61 typedef union {
62         struct {
63                 gint16 starting; /* starting, but not yet in worker_callback */
64                 gint16 working; /* executing worker_callback */
65         } _;
66         gint32 as_gint32;
67 } ThreadPoolCounter;
68
69 typedef struct {
70         MonoRefCount ref;
71
72         GPtrArray *domains; // ThreadPoolDomain* []
73         MonoCoopMutex domains_lock;
74
75         GPtrArray *threads; // MonoInternalThread* []
76         MonoCoopMutex threads_lock;
77         MonoCoopCond threads_exit_cond;
78
79         ThreadPoolCounter counters;
80
81         gint32 limit_io_min;
82         gint32 limit_io_max;
83
84         MonoThreadPoolWorker *worker;
85 } ThreadPool;
86
87 static mono_lazy_init_t status = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
88
89 static ThreadPool* threadpool;
90
91 #define COUNTER_ATOMIC(threadpool,var,block) \
92         do { \
93                 ThreadPoolCounter __old; \
94                 do { \
95                         g_assert (threadpool); \
96                         (var) = __old = COUNTER_READ (threadpool); \
97                         { block; } \
98                         if (!(counter._.starting >= 0)) \
99                                 g_error ("%s: counter._.starting = %d, but should be >= 0", __func__, counter._.starting); \
100                         if (!(counter._.working >= 0)) \
101                                 g_error ("%s: counter._.working = %d, but should be >= 0", __func__, counter._.working); \
102                 } while (InterlockedCompareExchange (&threadpool->counters.as_gint32, (var).as_gint32, __old.as_gint32) != __old.as_gint32); \
103         } while (0)
104
105 static inline ThreadPoolCounter
106 COUNTER_READ (ThreadPool *threadpool)
107 {
108         ThreadPoolCounter counter;
109         counter.as_gint32 = InterlockedRead (&threadpool->counters.as_gint32);
110         return counter;
111 }
112
113 static inline void
114 domains_lock (void)
115 {
116         mono_coop_mutex_lock (&threadpool->domains_lock);
117 }
118
119 static inline void
120 domains_unlock (void)
121 {
122         mono_coop_mutex_unlock (&threadpool->domains_lock);
123 }
124
125 static void
126 destroy (gpointer unused)
127 {
128         g_ptr_array_free (threadpool->domains, TRUE);
129         mono_coop_mutex_destroy (&threadpool->domains_lock);
130
131         g_ptr_array_free (threadpool->threads, TRUE);
132         mono_coop_mutex_destroy (&threadpool->threads_lock);
133         mono_coop_cond_destroy (&threadpool->threads_exit_cond);
134
135         g_free (threadpool);
136 }
137
138 static void
139 initialize (void)
140 {
141         g_assert (!threadpool);
142         threadpool = g_new0 (ThreadPool, 1);
143         g_assert (threadpool);
144
145         g_assert (sizeof (ThreadPoolCounter) == sizeof (gint32));
146
147         mono_refcount_init (threadpool, destroy);
148
149         threadpool->domains = g_ptr_array_new ();
150         mono_coop_mutex_init (&threadpool->domains_lock);
151
152         threadpool->threads = g_ptr_array_new ();
153         mono_coop_mutex_init (&threadpool->threads_lock);
154         mono_coop_cond_init (&threadpool->threads_exit_cond);
155
156         threadpool->limit_io_min = mono_cpu_count ();
157         threadpool->limit_io_max = CLAMP (threadpool->limit_io_min * 100, MIN (threadpool->limit_io_min, 200), MAX (threadpool->limit_io_min, 200));
158
159         mono_threadpool_worker_init (&threadpool->worker);
160 }
161
162 static void
163 cleanup (void)
164 {
165         guint i;
166         MonoInternalThread *current;
167
168         /* we make the assumption along the code that we are
169          * cleaning up only if the runtime is shutting down */
170         g_assert (mono_runtime_is_shutting_down ());
171
172         current = mono_thread_internal_current ();
173
174         mono_coop_mutex_lock (&threadpool->threads_lock);
175
176         /* stop all threadpool->threads */
177         for (i = 0; i < threadpool->threads->len; ++i) {
178                 MonoInternalThread *thread = (MonoInternalThread*) g_ptr_array_index (threadpool->threads, i);
179                 if (thread != current)
180                         mono_thread_internal_abort (thread);
181         }
182
183         mono_coop_mutex_unlock (&threadpool->threads_lock);
184
185         /* give a chance to the other threads to exit */
186         mono_thread_info_yield ();
187
188         mono_coop_mutex_lock (&threadpool->threads_lock);
189
190         for (;;) {
191                 if (threadpool->threads->len == 0)
192                         break;
193
194                 if (threadpool->threads->len == 1 && g_ptr_array_index (threadpool->threads, 0) == current) {
195                         /* We are waiting on ourselves */
196                         break;
197                 }
198
199                 mono_coop_cond_wait (&threadpool->threads_exit_cond, &threadpool->threads_lock);
200         }
201
202         mono_coop_mutex_unlock (&threadpool->threads_lock);
203
204         mono_threadpool_worker_cleanup (threadpool->worker);
205
206         mono_refcount_dec (threadpool);
207 }
208
209 gboolean
210 mono_threadpool_enqueue_work_item (MonoDomain *domain, MonoObject *work_item, MonoError *error)
211 {
212         static MonoClass *threadpool_class = NULL;
213         static MonoMethod *unsafe_queue_custom_work_item_method = NULL;
214         MonoDomain *current_domain;
215         MonoBoolean f;
216         gpointer args [2];
217
218         mono_error_init (error);
219         g_assert (work_item);
220
221         if (!threadpool_class)
222                 threadpool_class = mono_class_load_from_name (mono_defaults.corlib, "System.Threading", "ThreadPool");
223
224         if (!unsafe_queue_custom_work_item_method)
225                 unsafe_queue_custom_work_item_method = mono_class_get_method_from_name (threadpool_class, "UnsafeQueueCustomWorkItem", 2);
226         g_assert (unsafe_queue_custom_work_item_method);
227
228         f = FALSE;
229
230         args [0] = (gpointer) work_item;
231         args [1] = (gpointer) &f;
232
233         current_domain = mono_domain_get ();
234         if (current_domain == domain) {
235                 mono_runtime_invoke_checked (unsafe_queue_custom_work_item_method, NULL, args, error);
236                 return_val_if_nok (error, FALSE);
237         } else {
238                 mono_thread_push_appdomain_ref (domain);
239                 if (mono_domain_set (domain, FALSE)) {
240                         mono_runtime_invoke_checked (unsafe_queue_custom_work_item_method, NULL, args, error);
241                         if (!is_ok (error)) {
242                                 mono_thread_pop_appdomain_ref ();
243                                 return FALSE;
244                         }
245                         mono_domain_set (current_domain, TRUE);
246                 }
247                 mono_thread_pop_appdomain_ref ();
248         }
249         return TRUE;
250 }
251
252 /* LOCKING: domains_lock must be held */
253 static void
254 tpdomain_add (ThreadPoolDomain *tpdomain)
255 {
256         guint i, len;
257
258         g_assert (tpdomain);
259
260         len = threadpool->domains->len;
261         for (i = 0; i < len; ++i) {
262                 if (g_ptr_array_index (threadpool->domains, i) == tpdomain)
263                         break;
264         }
265
266         if (i == len)
267                 g_ptr_array_add (threadpool->domains, tpdomain);
268 }
269
270 /* LOCKING: domains_lock must be held. */
271 static gboolean
272 tpdomain_remove (ThreadPoolDomain *tpdomain)
273 {
274         g_assert (tpdomain);
275         return g_ptr_array_remove (threadpool->domains, tpdomain);
276 }
277
278 /* LOCKING: domains_lock must be held */
279 static ThreadPoolDomain *
280 tpdomain_get (MonoDomain *domain, gboolean create)
281 {
282         guint i;
283         ThreadPoolDomain *tpdomain;
284
285         g_assert (domain);
286
287         for (i = 0; i < threadpool->domains->len; ++i) {
288                 ThreadPoolDomain *tpdomain;
289
290                 tpdomain = (ThreadPoolDomain *)g_ptr_array_index (threadpool->domains, i);
291                 if (tpdomain->domain == domain)
292                         return tpdomain;
293         }
294
295         if (!create)
296                 return NULL;
297
298         tpdomain = g_new0 (ThreadPoolDomain, 1);
299         tpdomain->domain = domain;
300         mono_coop_cond_init (&tpdomain->cleanup_cond);
301
302         tpdomain_add (tpdomain);
303
304         return tpdomain;
305 }
306
307 static void
308 tpdomain_free (ThreadPoolDomain *tpdomain)
309 {
310         g_free (tpdomain);
311 }
312
313 /* LOCKING: domains_lock must be held */
314 static ThreadPoolDomain *
315 tpdomain_get_next (ThreadPoolDomain *current)
316 {
317         ThreadPoolDomain *tpdomain = NULL;
318         guint len;
319
320         len = threadpool->domains->len;
321         if (len > 0) {
322                 guint i, current_idx = -1;
323                 if (current) {
324                         for (i = 0; i < len; ++i) {
325                                 if (current == g_ptr_array_index (threadpool->domains, i)) {
326                                         current_idx = i;
327                                         break;
328                                 }
329                         }
330                         g_assert (current_idx != (guint)-1);
331                 }
332                 for (i = current_idx + 1; i < len + current_idx + 1; ++i) {
333                         ThreadPoolDomain *tmp = (ThreadPoolDomain *)g_ptr_array_index (threadpool->domains, i % len);
334                         if (tmp->outstanding_request > 0) {
335                                 tpdomain = tmp;
336                                 break;
337                         }
338                 }
339         }
340
341         return tpdomain;
342 }
343
344 static MonoObject*
345 try_invoke_perform_wait_callback (MonoObject** exc, MonoError *error)
346 {
347         HANDLE_FUNCTION_ENTER ();
348         mono_error_init (error);
349         MonoObject *res = mono_runtime_try_invoke (mono_defaults.threadpool_perform_wait_callback_method, NULL, NULL, exc, error);
350         HANDLE_FUNCTION_RETURN_VAL (res);
351 }
352
353 static void
354 worker_callback (gpointer unused)
355 {
356         MonoError error;
357         ThreadPoolDomain *tpdomain, *previous_tpdomain;
358         ThreadPoolCounter counter;
359         MonoInternalThread *thread;
360
361         thread = mono_thread_internal_current ();
362
363         COUNTER_ATOMIC (threadpool, counter, {
364                 if (!(counter._.working < 32767 /* G_MAXINT16 */))
365                         g_error ("%s: counter._.working = %d, but should be < 32767", __func__, counter._.working);
366
367                 counter._.starting --;
368                 counter._.working ++;
369         });
370
371         if (mono_runtime_is_shutting_down ()) {
372                 COUNTER_ATOMIC (threadpool, counter, {
373                         counter._.working --;
374                 });
375
376                 mono_refcount_dec (threadpool);
377                 return;
378         }
379
380         mono_coop_mutex_lock (&threadpool->threads_lock);
381         g_ptr_array_add (threadpool->threads, thread);
382         mono_coop_mutex_unlock (&threadpool->threads_lock);
383
384         /*
385          * This is needed so there is always an lmf frame in the runtime invoke call below,
386          * so ThreadAbortExceptions are caught even if the thread is in native code.
387          */
388         mono_defaults.threadpool_perform_wait_callback_method->save_lmf = TRUE;
389
390         domains_lock ();
391
392         previous_tpdomain = NULL;
393
394         while (!mono_runtime_is_shutting_down ()) {
395                 gboolean retire = FALSE;
396
397                 if ((thread->state & (ThreadState_AbortRequested | ThreadState_SuspendRequested)) != 0) {
398                         domains_unlock ();
399                         mono_thread_interruption_checkpoint ();
400                         domains_lock ();
401                 }
402
403                 tpdomain = tpdomain_get_next (previous_tpdomain);
404                 if (!tpdomain)
405                         break;
406
407                 tpdomain->outstanding_request --;
408                 g_assert (tpdomain->outstanding_request >= 0);
409
410                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker running in domain %p (outstanding requests %d)",
411                         mono_native_thread_id_get (), tpdomain->domain, tpdomain->outstanding_request);
412
413                 g_assert (tpdomain->threadpool_jobs >= 0);
414                 tpdomain->threadpool_jobs ++;
415
416                 domains_unlock ();
417
418                 mono_thread_clr_state (thread, (MonoThreadState)~ThreadState_Background);
419                 if (!mono_thread_test_state (thread , ThreadState_Background))
420                         ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
421
422                 mono_thread_push_appdomain_ref (tpdomain->domain);
423                 if (mono_domain_set (tpdomain->domain, FALSE)) {
424                         MonoObject *exc = NULL, *res;
425
426                         res = try_invoke_perform_wait_callback (&exc, &error);
427                         if (exc || !mono_error_ok(&error)) {
428                                 if (exc == NULL)
429                                         exc = (MonoObject *) mono_error_convert_to_exception (&error);
430                                 else
431                                         mono_error_cleanup (&error);
432                                 mono_thread_internal_unhandled_exception (exc);
433                         } else if (res && *(MonoBoolean*) mono_object_unbox (res) == FALSE) {
434                                 retire = TRUE;
435                         }
436
437                         mono_domain_set (mono_get_root_domain (), TRUE);
438                 }
439                 mono_thread_pop_appdomain_ref ();
440
441                 domains_lock ();
442
443                 tpdomain->threadpool_jobs --;
444                 g_assert (tpdomain->threadpool_jobs >= 0);
445
446                 if (tpdomain->outstanding_request + tpdomain->threadpool_jobs == 0 && mono_domain_is_unloading (tpdomain->domain)) {
447                         gboolean removed;
448
449                         removed = tpdomain_remove (tpdomain);
450                         g_assert (removed);
451
452                         mono_coop_cond_signal (&tpdomain->cleanup_cond);
453                         tpdomain = NULL;
454                 }
455
456                 if (retire)
457                         break;
458
459                 previous_tpdomain = tpdomain;
460         }
461
462         domains_unlock ();
463
464         mono_coop_mutex_lock (&threadpool->threads_lock);
465
466         g_ptr_array_remove_fast (threadpool->threads, thread);
467
468         mono_coop_cond_signal (&threadpool->threads_exit_cond);
469
470         mono_coop_mutex_unlock (&threadpool->threads_lock);
471
472         COUNTER_ATOMIC (threadpool, counter, {
473                 counter._.working --;
474         });
475
476         mono_refcount_dec (threadpool);
477 }
478
479 void
480 mono_threadpool_cleanup (void)
481 {
482 #ifndef DISABLE_SOCKETS
483         mono_threadpool_io_cleanup ();
484 #endif
485         mono_lazy_cleanup (&status, cleanup);
486 }
487
488 MonoAsyncResult *
489 mono_threadpool_begin_invoke (MonoDomain *domain, MonoObject *target, MonoMethod *method, gpointer *params, MonoError *error)
490 {
491         static MonoClass *async_call_klass = NULL;
492         MonoMethodMessage *message;
493         MonoAsyncResult *async_result;
494         MonoAsyncCall *async_call;
495         MonoDelegate *async_callback = NULL;
496         MonoObject *state = NULL;
497
498         if (!async_call_klass)
499                 async_call_klass = mono_class_load_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
500
501         mono_lazy_initialize (&status, initialize);
502
503         mono_error_init (error);
504
505         message = mono_method_call_message_new (method, params, mono_get_delegate_invoke (method->klass), (params != NULL) ? (&async_callback) : NULL, (params != NULL) ? (&state) : NULL, error);
506         return_val_if_nok (error, NULL);
507
508         async_call = (MonoAsyncCall*) mono_object_new_checked (domain, async_call_klass, error);
509         return_val_if_nok (error, NULL);
510
511         MONO_OBJECT_SETREF (async_call, msg, message);
512         MONO_OBJECT_SETREF (async_call, state, state);
513
514         if (async_callback) {
515                 MONO_OBJECT_SETREF (async_call, cb_method, mono_get_delegate_invoke (((MonoObject*) async_callback)->vtable->klass));
516                 MONO_OBJECT_SETREF (async_call, cb_target, async_callback);
517         }
518
519         async_result = mono_async_result_new (domain, NULL, async_call->state, NULL, (MonoObject*) async_call, error);
520         return_val_if_nok (error, NULL);
521         MONO_OBJECT_SETREF (async_result, async_delegate, target);
522
523         mono_threadpool_enqueue_work_item (domain, (MonoObject*) async_result, error);
524         return_val_if_nok (error, NULL);
525
526         return async_result;
527 }
528
529 MonoObject *
530 mono_threadpool_end_invoke (MonoAsyncResult *ares, MonoArray **out_args, MonoObject **exc, MonoError *error)
531 {
532         MonoAsyncCall *ac;
533
534         mono_error_init (error);
535         g_assert (exc);
536         g_assert (out_args);
537
538         *exc = NULL;
539         *out_args = NULL;
540
541         /* check if already finished */
542         mono_monitor_enter ((MonoObject*) ares);
543
544         if (ares->endinvoke_called) {
545                 mono_error_set_invalid_operation(error, "Delegate EndInvoke method called more than once");
546                 mono_monitor_exit ((MonoObject*) ares);
547                 return NULL;
548         }
549
550         ares->endinvoke_called = 1;
551
552         /* wait until we are really finished */
553         if (ares->completed) {
554                 mono_monitor_exit ((MonoObject *) ares);
555         } else {
556                 gpointer wait_event;
557                 if (ares->handle) {
558                         wait_event = mono_wait_handle_get_handle ((MonoWaitHandle*) ares->handle);
559                 } else {
560                         wait_event = mono_w32event_create (TRUE, FALSE);
561                         g_assert(wait_event);
562                         MonoWaitHandle *wait_handle = mono_wait_handle_new (mono_object_domain (ares), wait_event, error);
563                         if (!is_ok (error)) {
564                                 CloseHandle (wait_event);
565                                 return NULL;
566                         }
567                         MONO_OBJECT_SETREF (ares, handle, (MonoObject*) wait_handle);
568                 }
569                 mono_monitor_exit ((MonoObject*) ares);
570                 MONO_ENTER_GC_SAFE;
571 #ifdef HOST_WIN32
572                 WaitForSingleObjectEx (wait_event, INFINITE, TRUE);
573 #else
574                 mono_w32handle_wait_one (wait_event, MONO_INFINITE_WAIT, TRUE);
575 #endif
576                 MONO_EXIT_GC_SAFE;
577         }
578
579         ac = (MonoAsyncCall*) ares->object_data;
580         g_assert (ac);
581
582         *exc = ac->msg->exc; /* FIXME: GC add write barrier */
583         *out_args = ac->out_args;
584         return ac->res;
585 }
586
587 gboolean
588 mono_threadpool_remove_domain_jobs (MonoDomain *domain, int timeout)
589 {
590         gint64 end;
591         ThreadPoolDomain *tpdomain;
592         gboolean ret;
593
594         g_assert (domain);
595         g_assert (timeout >= -1);
596
597         g_assert (mono_domain_is_unloading (domain));
598
599         if (timeout != -1)
600                 end = mono_msec_ticks () + timeout;
601
602 #ifndef DISABLE_SOCKETS
603         mono_threadpool_io_remove_domain_jobs (domain);
604         if (timeout != -1) {
605                 if (mono_msec_ticks () > end)
606                         return FALSE;
607         }
608 #endif
609
610         /*
611          * Wait for all threads which execute jobs in the domain to exit.
612          * The is_unloading () check in worker_request () ensures that
613          * no new jobs are added after we enter the lock below.
614          */
615
616         if (!mono_lazy_is_initialized (&status))
617                 return TRUE;
618
619         mono_refcount_inc (threadpool);
620
621         domains_lock ();
622
623         tpdomain = tpdomain_get (domain, FALSE);
624         if (!tpdomain) {
625                 domains_unlock ();
626                 mono_refcount_dec (threadpool);
627                 return TRUE;
628         }
629
630         ret = TRUE;
631
632         while (tpdomain->outstanding_request + tpdomain->threadpool_jobs > 0) {
633                 if (timeout == -1) {
634                         mono_coop_cond_wait (&tpdomain->cleanup_cond, &threadpool->domains_lock);
635                 } else {
636                         gint64 now;
637                         gint res;
638
639                         now = mono_msec_ticks();
640                         if (now > end) {
641                                 ret = FALSE;
642                                 break;
643                         }
644
645                         res = mono_coop_cond_timedwait (&tpdomain->cleanup_cond, &threadpool->domains_lock, end - now);
646                         if (res != 0) {
647                                 ret = FALSE;
648                                 break;
649                         }
650                 }
651         }
652
653         /* Remove from the list the worker threads look at */
654         tpdomain_remove (tpdomain);
655
656         domains_unlock ();
657
658         mono_coop_cond_destroy (&tpdomain->cleanup_cond);
659         tpdomain_free (tpdomain);
660
661         mono_refcount_dec (threadpool);
662
663         return ret;
664 }
665
666 void
667 mono_threadpool_suspend (void)
668 {
669         if (threadpool)
670                 mono_threadpool_worker_set_suspended (threadpool->worker, TRUE);
671 }
672
673 void
674 mono_threadpool_resume (void)
675 {
676         if (threadpool)
677                 mono_threadpool_worker_set_suspended (threadpool->worker, FALSE);
678 }
679
680 void
681 ves_icall_System_Threading_ThreadPool_GetAvailableThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads)
682 {
683         ThreadPoolCounter counter;
684
685         if (!worker_threads || !completion_port_threads)
686                 return;
687
688         mono_lazy_initialize (&status, initialize);
689
690         counter = COUNTER_READ (threadpool);
691
692         *worker_threads = MAX (0, mono_threadpool_worker_get_max (threadpool->worker) - counter._.working);
693         *completion_port_threads = threadpool->limit_io_max;
694 }
695
696 void
697 ves_icall_System_Threading_ThreadPool_GetMinThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads)
698 {
699         if (!worker_threads || !completion_port_threads)
700                 return;
701
702         mono_lazy_initialize (&status, initialize);
703
704         *worker_threads = mono_threadpool_worker_get_min (threadpool->worker);
705         *completion_port_threads = threadpool->limit_io_min;
706 }
707
708 void
709 ves_icall_System_Threading_ThreadPool_GetMaxThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads)
710 {
711         if (!worker_threads || !completion_port_threads)
712                 return;
713
714         mono_lazy_initialize (&status, initialize);
715
716         *worker_threads = mono_threadpool_worker_get_max (threadpool->worker);
717         *completion_port_threads = threadpool->limit_io_max;
718 }
719
720 MonoBoolean
721 ves_icall_System_Threading_ThreadPool_SetMinThreadsNative (gint32 worker_threads, gint32 completion_port_threads)
722 {
723         mono_lazy_initialize (&status, initialize);
724
725         if (completion_port_threads <= 0 || completion_port_threads > threadpool->limit_io_max)
726                 return FALSE;
727
728         if (!mono_threadpool_worker_set_min (threadpool->worker, worker_threads))
729                 return FALSE;
730
731         threadpool->limit_io_min = completion_port_threads;
732
733         return TRUE;
734 }
735
736 MonoBoolean
737 ves_icall_System_Threading_ThreadPool_SetMaxThreadsNative (gint32 worker_threads, gint32 completion_port_threads)
738 {
739         gint cpu_count = mono_cpu_count ();
740
741         mono_lazy_initialize (&status, initialize);
742
743         if (completion_port_threads < threadpool->limit_io_min || completion_port_threads < cpu_count)
744                 return FALSE;
745
746         if (!mono_threadpool_worker_set_max (threadpool->worker, worker_threads))
747                 return FALSE;
748
749         threadpool->limit_io_max = completion_port_threads;
750
751         return TRUE;
752 }
753
754 void
755 ves_icall_System_Threading_ThreadPool_InitializeVMTp (MonoBoolean *enable_worker_tracking)
756 {
757         if (enable_worker_tracking) {
758                 // TODO implement some kind of switch to have the possibily to use it
759                 *enable_worker_tracking = FALSE;
760         }
761
762         mono_lazy_initialize (&status, initialize);
763 }
764
765 MonoBoolean
766 ves_icall_System_Threading_ThreadPool_NotifyWorkItemComplete (void)
767 {
768         if (mono_domain_is_unloading (mono_domain_get ()) || mono_runtime_is_shutting_down ())
769                 return FALSE;
770
771         return mono_threadpool_worker_notify_completed (threadpool->worker);
772 }
773
774 void
775 ves_icall_System_Threading_ThreadPool_NotifyWorkItemProgressNative (void)
776 {
777         mono_threadpool_worker_notify_completed (threadpool->worker);
778 }
779
780 void
781 ves_icall_System_Threading_ThreadPool_ReportThreadStatus (MonoBoolean is_working)
782 {
783         // TODO
784         MonoError error;
785         mono_error_set_not_implemented (&error, "");
786         mono_error_set_pending_exception (&error);
787 }
788
789 MonoBoolean
790 ves_icall_System_Threading_ThreadPool_RequestWorkerThread (void)
791 {
792         MonoDomain *domain;
793         ThreadPoolDomain *tpdomain;
794         ThreadPoolCounter counter;
795
796         domain = mono_domain_get ();
797         if (mono_domain_is_unloading (domain))
798                 return FALSE;
799
800         domains_lock ();
801
802         /* synchronize with mono_threadpool_remove_domain_jobs */
803         if (mono_domain_is_unloading (domain)) {
804                 domains_unlock ();
805                 return FALSE;
806         }
807
808         tpdomain = tpdomain_get (domain, TRUE);
809         g_assert (tpdomain);
810
811         tpdomain->outstanding_request ++;
812         g_assert (tpdomain->outstanding_request >= 1);
813
814         domains_unlock ();
815
816         COUNTER_ATOMIC (threadpool, counter, {
817                 if (counter._.starting == 16)
818                         return TRUE;
819
820                 counter._.starting ++;
821         });
822
823         mono_refcount_inc (threadpool);
824
825         mono_threadpool_worker_enqueue (threadpool->worker, worker_callback, NULL);
826
827         return TRUE;
828 }
829
830 MonoBoolean G_GNUC_UNUSED
831 ves_icall_System_Threading_ThreadPool_PostQueuedCompletionStatus (MonoNativeOverlapped *native_overlapped)
832 {
833         /* This copy the behavior of the current Mono implementation */
834         MonoError error;
835         mono_error_set_not_implemented (&error, "");
836         mono_error_set_pending_exception (&error);
837         return FALSE;
838 }
839
840 MonoBoolean G_GNUC_UNUSED
841 ves_icall_System_Threading_ThreadPool_BindIOCompletionCallbackNative (gpointer file_handle)
842 {
843         /* This copy the behavior of the current Mono implementation */
844         return TRUE;
845 }
846
847 MonoBoolean G_GNUC_UNUSED
848 ves_icall_System_Threading_ThreadPool_IsThreadPoolHosted (void)
849 {
850         return FALSE;
851 }