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