Update tests exclusion
[mono-project.git] / mono / metadata / threadpool.c
blob7031a7780b24ee1aa2552a256168f1f09775d45b
1 /**
2 * \file
3 * Microsoft threadpool runtime support
5 * Author:
6 * Ludovic Henry (ludovic.henry@xamarin.com)
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.
13 // Copyright (c) Microsoft. All rights reserved.
14 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 // Files:
17 // - src/vm/comthreadpool.cpp
18 // - src/vm/win32threadpoolcpp
19 // - src/vm/threadpoolrequest.cpp
20 // - src/vm/hillclimbing.cpp
22 // Ported from C++ to C and adjusted to Mono runtime
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>
30 #include <mono/metadata/class-internals.h>
31 #include <mono/metadata/domain-internals.h>
32 #include <mono/metadata/exception.h>
33 #include <mono/metadata/gc-internals.h>
34 #include <mono/metadata/object.h>
35 #include <mono/metadata/object-internals.h>
36 #include <mono/metadata/threadpool.h>
37 #include <mono/metadata/threadpool-worker.h>
38 #include <mono/metadata/threadpool-io.h>
39 #include <mono/metadata/w32event.h>
40 #include <mono/utils/atomic.h>
41 #include <mono/utils/mono-compiler.h>
42 #include <mono/utils/mono-complex.h>
43 #include <mono/utils/mono-lazy-init.h>
44 #include <mono/utils/mono-logger.h>
45 #include <mono/utils/mono-logger-internals.h>
46 #include <mono/utils/mono-proclib.h>
47 #include <mono/utils/mono-threads.h>
48 #include <mono/utils/mono-time.h>
49 #include <mono/utils/refcount.h>
50 #include <mono/utils/mono-os-wait.h>
51 #include "monitor.h"
52 #include "icall-decl.h"
54 // consistency with coreclr https://github.com/dotnet/coreclr/blob/643b09f966e68e06d5f0930755985a01a2a2b096/src/vm/win32threadpool.h#L111
55 #define MAX_POSSIBLE_THREADS 0x7fff
57 typedef struct {
58 MonoDomain *domain;
59 /* Number of outstanding jobs */
60 gint32 outstanding_request;
61 /* Number of currently executing jobs */
62 gint32 threadpool_jobs;
63 /* Signalled when threadpool_jobs + outstanding_request is 0 */
64 /* Protected by threadpool.domains_lock */
65 MonoCoopCond cleanup_cond;
66 } ThreadPoolDomain;
68 typedef union {
69 struct {
70 gint16 starting; /* starting, but not yet in worker_callback */
71 gint16 working; /* executing worker_callback */
72 } _;
73 gint32 as_gint32;
74 } ThreadPoolCounter;
76 typedef struct {
77 MonoRefCount ref;
79 GPtrArray *domains; // ThreadPoolDomain* []
80 MonoCoopMutex domains_lock;
82 ThreadPoolCounter counters;
84 gint32 limit_io_min;
85 gint32 limit_io_max;
86 } ThreadPool;
88 static mono_lazy_init_t status = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
90 static ThreadPool threadpool;
92 #define COUNTER_ATOMIC(var,block) \
93 do { \
94 ThreadPoolCounter __old; \
95 do { \
96 (var) = __old = COUNTER_READ (); \
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 (mono_atomic_cas_i32 (&threadpool.counters.as_gint32, (var).as_gint32, __old.as_gint32) != __old.as_gint32); \
103 } while (0)
105 static ThreadPoolCounter
106 COUNTER_READ (void)
108 ThreadPoolCounter counter;
109 counter.as_gint32 = mono_atomic_load_i32 (&threadpool.counters.as_gint32);
110 return counter;
113 static void
114 domains_lock (void)
116 mono_coop_mutex_lock (&threadpool.domains_lock);
119 static void
120 domains_unlock (void)
122 mono_coop_mutex_unlock (&threadpool.domains_lock);
125 static void
126 destroy (gpointer unused)
128 g_ptr_array_free (threadpool.domains, TRUE);
129 mono_coop_mutex_destroy (&threadpool.domains_lock);
132 static void
133 worker_callback (void);
135 static void
136 initialize (void)
138 g_assert (sizeof (ThreadPoolCounter) == sizeof (gint32));
140 mono_refcount_init (&threadpool, destroy);
142 threadpool.domains = g_ptr_array_new ();
143 mono_coop_mutex_init (&threadpool.domains_lock);
145 threadpool.limit_io_min = mono_cpu_count ();
146 threadpool.limit_io_max = CLAMP (threadpool.limit_io_min * 100, MIN (threadpool.limit_io_min, 200), MAX (threadpool.limit_io_min, 200));
148 mono_threadpool_worker_init (worker_callback);
151 static void
152 cleanup (void)
154 mono_threadpool_worker_cleanup ();
156 mono_refcount_dec (&threadpool);
159 gboolean
160 mono_threadpool_enqueue_work_item (MonoDomain *domain, MonoObject *work_item, MonoError *error)
162 static MonoClass *threadpool_class = NULL;
163 static MonoMethod *unsafe_queue_custom_work_item_method = NULL;
164 MonoDomain *current_domain;
165 MonoBoolean f;
166 gpointer args [2];
168 error_init (error);
169 g_assert (work_item);
171 if (!threadpool_class)
172 threadpool_class = mono_class_load_from_name (mono_defaults.corlib, "System.Threading", "ThreadPool");
174 if (!unsafe_queue_custom_work_item_method) {
175 unsafe_queue_custom_work_item_method = mono_class_get_method_from_name_checked (threadpool_class, "UnsafeQueueCustomWorkItem", 2, 0, error);
176 mono_error_assert_ok (error);
178 g_assert (unsafe_queue_custom_work_item_method);
180 f = FALSE;
182 args [0] = (gpointer) work_item;
183 args [1] = (gpointer) &f;
185 current_domain = mono_domain_get ();
186 if (current_domain == domain) {
187 mono_runtime_invoke_checked (unsafe_queue_custom_work_item_method, NULL, args, error);
188 } else {
189 mono_thread_push_appdomain_ref (domain);
190 if (mono_domain_set_fast (domain, FALSE)) {
191 mono_runtime_invoke_checked (unsafe_queue_custom_work_item_method, NULL, args, error);
192 mono_domain_set_fast (current_domain, TRUE);
193 } else {
194 // mono_domain_set_fast failing still leads to success.
196 mono_thread_pop_appdomain_ref ();
198 return is_ok (error);
201 /* LOCKING: domains_lock must be held. */
202 static ThreadPoolDomain *
203 tpdomain_create (MonoDomain *domain)
205 ThreadPoolDomain *tpdomain;
207 tpdomain = g_new0 (ThreadPoolDomain, 1);
208 tpdomain->domain = domain;
209 mono_coop_cond_init (&tpdomain->cleanup_cond);
211 g_ptr_array_add (threadpool.domains, tpdomain);
213 return tpdomain;
216 /* LOCKING: domains_lock must be held. */
217 static gboolean
218 tpdomain_remove (ThreadPoolDomain *tpdomain)
220 g_assert (tpdomain);
221 return g_ptr_array_remove (threadpool.domains, tpdomain);
224 /* LOCKING: domains_lock must be held */
225 static ThreadPoolDomain *
226 tpdomain_get (MonoDomain *domain)
228 gint i;
230 g_assert (domain);
232 for (i = 0; i < threadpool.domains->len; ++i) {
233 ThreadPoolDomain *tpdomain;
235 tpdomain = (ThreadPoolDomain *)g_ptr_array_index (threadpool.domains, i);
236 if (tpdomain->domain == domain)
237 return tpdomain;
240 return NULL;
243 static void
244 tpdomain_free (ThreadPoolDomain *tpdomain)
246 g_free (tpdomain);
249 /* LOCKING: domains_lock must be held */
250 static ThreadPoolDomain *
251 tpdomain_get_next (ThreadPoolDomain *current)
253 ThreadPoolDomain *tpdomain = NULL;
254 gint len;
256 len = threadpool.domains->len;
257 if (len > 0) {
258 gint i, current_idx = -1;
259 if (current) {
260 for (i = 0; i < len; ++i) {
261 if (current == g_ptr_array_index (threadpool.domains, i)) {
262 current_idx = i;
263 break;
267 for (i = current_idx + 1; i < len + current_idx + 1; ++i) {
268 ThreadPoolDomain *tmp = (ThreadPoolDomain *)g_ptr_array_index (threadpool.domains, i % len);
269 if (tmp->outstanding_request > 0) {
270 tpdomain = tmp;
271 break;
276 return tpdomain;
279 static MonoObject*
280 try_invoke_perform_wait_callback (MonoObject** exc, MonoError *error)
282 HANDLE_FUNCTION_ENTER ();
283 error_init (error);
284 MonoObject * const res = mono_runtime_try_invoke (mono_defaults.threadpool_perform_wait_callback_method, NULL, NULL, exc, error);
285 HANDLE_FUNCTION_RETURN_VAL (res);
288 static gsize
289 set_thread_name (MonoInternalThread *thread)
291 return mono_thread_set_name_constant_ignore_error (thread, "Thread Pool Worker", MonoSetThreadNameFlag_Reset);
294 static void
295 worker_callback (void)
297 ThreadPoolDomain *tpdomain, *previous_tpdomain;
298 ThreadPoolCounter counter;
299 MonoInternalThread *thread;
301 if (!mono_refcount_tryinc (&threadpool))
302 return;
304 thread = mono_thread_internal_current ();
306 COUNTER_ATOMIC (counter, {
307 if (!(counter._.working < 32767 /* G_MAXINT16 */))
308 g_error ("%s: counter._.working = %d, but should be < 32767", __func__, counter._.working);
310 counter._.starting --;
311 counter._.working ++;
314 if (mono_runtime_is_shutting_down ()) {
315 COUNTER_ATOMIC (counter, {
316 counter._.working --;
319 mono_refcount_dec (&threadpool);
320 return;
324 * This is needed so there is always an lmf frame in the runtime invoke call below,
325 * so ThreadAbortExceptions are caught even if the thread is in native code.
327 mono_defaults.threadpool_perform_wait_callback_method->save_lmf = TRUE;
329 gsize name_generation = thread->name.generation;
330 /* Set the name if this is the first call to worker_callback on this thread */
331 if (name_generation == 0)
332 name_generation = set_thread_name (thread);
334 domains_lock ();
336 previous_tpdomain = NULL;
338 while (!mono_runtime_is_shutting_down ()) {
339 gboolean retire = FALSE;
341 if (thread->state & (ThreadState_AbortRequested | ThreadState_SuspendRequested)) {
342 domains_unlock ();
343 if (mono_thread_interruption_checkpoint_bool ()) {
344 domains_lock ();
345 continue;
347 domains_lock ();
350 tpdomain = tpdomain_get_next (previous_tpdomain);
351 if (!tpdomain)
352 break;
354 tpdomain->outstanding_request --;
355 g_assert (tpdomain->outstanding_request >= 0);
357 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker running in domain %p (outstanding requests %d)",
358 GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), tpdomain->domain, tpdomain->outstanding_request);
360 g_assert (tpdomain->threadpool_jobs >= 0);
361 tpdomain->threadpool_jobs ++;
363 domains_unlock ();
365 // Any thread can set any other thread name at any time.
366 // So this is unavoidably racy.
367 // This only partly fights against that -- i.e. not atomic and not a loop.
368 // It is reliable against the thread setting its own name, and somewhat
369 // reliable against other threads setting this thread's name.
370 if (name_generation != thread->name.generation)
371 name_generation = set_thread_name (thread);
373 mono_thread_clear_and_set_state (thread,
374 (MonoThreadState)~ThreadState_Background,
375 ThreadState_Background);
377 mono_thread_push_appdomain_ref (tpdomain->domain);
378 if (mono_domain_set_fast (tpdomain->domain, FALSE)) {
379 MonoObject *exc = NULL, *res;
381 ERROR_DECL (error);
383 res = try_invoke_perform_wait_callback (&exc, error);
384 if (exc || !is_ok(error)) {
385 if (exc == NULL)
386 exc = (MonoObject *) mono_error_convert_to_exception (error);
387 else
388 mono_error_cleanup (error);
389 mono_thread_internal_unhandled_exception (exc);
390 } else if (res && *(MonoBoolean*) mono_object_unbox_internal (res) == FALSE) {
391 retire = TRUE;
394 mono_domain_set_fast (mono_get_root_domain (), TRUE);
396 mono_thread_pop_appdomain_ref ();
398 /* Reset name after every callback */
399 if (name_generation != thread->name.generation)
400 name_generation = set_thread_name (thread);
402 domains_lock ();
404 tpdomain->threadpool_jobs --;
405 g_assert (tpdomain->threadpool_jobs >= 0);
407 if (tpdomain->outstanding_request + tpdomain->threadpool_jobs == 0 && mono_domain_is_unloading (tpdomain->domain)) {
408 gboolean removed;
410 removed = tpdomain_remove (tpdomain);
411 g_assert (removed);
413 mono_coop_cond_signal (&tpdomain->cleanup_cond);
414 tpdomain = NULL;
417 if (retire)
418 break;
420 previous_tpdomain = tpdomain;
423 domains_unlock ();
425 COUNTER_ATOMIC (counter, {
426 counter._.working --;
429 mono_refcount_dec (&threadpool);
432 void
433 mono_threadpool_cleanup (void)
435 #ifndef DISABLE_SOCKETS
436 mono_threadpool_io_cleanup ();
437 #endif
438 mono_lazy_cleanup (&status, cleanup);
441 MonoAsyncResult *
442 mono_threadpool_begin_invoke (MonoDomain *domain, MonoObject *target, MonoMethod *method, gpointer *params, MonoError *error)
444 static MonoClass *async_call_klass = NULL;
445 MonoMethodMessage *message;
446 MonoAsyncResult *async_result;
447 MonoAsyncCall *async_call;
448 MonoDelegate *async_callback = NULL;
449 MonoObject *state = NULL;
451 if (!async_call_klass)
452 async_call_klass = mono_class_load_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
454 error_init (error);
456 message = mono_method_call_message_new (method, params, mono_get_delegate_invoke_internal (method->klass), (params != NULL) ? (&async_callback) : NULL, (params != NULL) ? (&state) : NULL, error);
457 return_val_if_nok (error, NULL);
459 async_call = (MonoAsyncCall*) mono_object_new_checked (domain, async_call_klass, error);
460 return_val_if_nok (error, NULL);
462 MONO_OBJECT_SETREF_INTERNAL (async_call, msg, message);
463 MONO_OBJECT_SETREF_INTERNAL (async_call, state, state);
465 if (async_callback) {
466 MONO_OBJECT_SETREF_INTERNAL (async_call, cb_method, mono_get_delegate_invoke_internal (((MonoObject*) async_callback)->vtable->klass));
467 MONO_OBJECT_SETREF_INTERNAL (async_call, cb_target, async_callback);
470 async_result = mono_async_result_new (domain, NULL, async_call->state, NULL, (MonoObject*) async_call, error);
471 return_val_if_nok (error, NULL);
472 MONO_OBJECT_SETREF_INTERNAL (async_result, async_delegate, target);
474 mono_threadpool_enqueue_work_item (domain, (MonoObject*) async_result, error);
475 return_val_if_nok (error, NULL);
477 return async_result;
480 MonoObject *
481 mono_threadpool_end_invoke (MonoAsyncResult *ares, MonoArray **out_args, MonoObject **exc, MonoError *error)
483 MonoAsyncCall *ac;
485 error_init (error);
486 g_assert (exc);
487 g_assert (out_args);
489 *exc = NULL;
490 *out_args = NULL;
492 /* check if already finished */
493 mono_monitor_enter_internal ((MonoObject*) ares);
495 if (ares->endinvoke_called) {
496 mono_error_set_invalid_operation(error, "Delegate EndInvoke method called more than once");
497 mono_monitor_exit_internal ((MonoObject*) ares);
498 return NULL;
501 ares->endinvoke_called = 1;
503 /* wait until we are really finished */
504 if (ares->completed) {
505 mono_monitor_exit_internal ((MonoObject *) ares);
506 } else {
507 gpointer wait_event;
508 if (ares->handle) {
509 wait_event = mono_wait_handle_get_handle ((MonoWaitHandle*) ares->handle);
510 } else {
511 wait_event = mono_w32event_create (TRUE, FALSE);
512 g_assert(wait_event);
513 MonoWaitHandle *wait_handle = mono_wait_handle_new (mono_object_domain (ares), wait_event, error);
514 if (!is_ok (error)) {
515 mono_w32event_close (wait_event);
516 return NULL;
518 MONO_OBJECT_SETREF_INTERNAL (ares, handle, (MonoObject*) wait_handle);
520 mono_monitor_exit_internal ((MonoObject*) ares);
521 mono_w32handle_wait_one (wait_event, MONO_INFINITE_WAIT, TRUE);
524 ac = (MonoAsyncCall*) ares->object_data;
525 g_assert (ac);
527 *exc = ac->msg->exc; /* FIXME: GC add write barrier */
528 *out_args = ac->out_args;
529 return ac->res;
532 gboolean
533 mono_threadpool_remove_domain_jobs (MonoDomain *domain, int timeout)
535 gint64 end = 0;
536 ThreadPoolDomain *tpdomain;
537 gboolean ret;
539 g_assert (domain);
540 g_assert (timeout >= -1);
542 g_assert (mono_domain_is_unloading (domain));
544 if (timeout != -1)
545 end = mono_msec_ticks () + timeout;
547 #ifndef DISABLE_SOCKETS
548 mono_threadpool_io_remove_domain_jobs (domain);
549 if (timeout != -1) {
550 if (mono_msec_ticks () > end)
551 return FALSE;
553 #endif
556 * Wait for all threads which execute jobs in the domain to exit.
557 * The is_unloading () check in worker_request () ensures that
558 * no new jobs are added after we enter the lock below.
561 if (!mono_lazy_is_initialized (&status))
562 return TRUE;
564 mono_refcount_inc (&threadpool);
566 domains_lock ();
568 tpdomain = tpdomain_get (domain);
569 if (!tpdomain) {
570 domains_unlock ();
571 mono_refcount_dec (&threadpool);
572 return TRUE;
575 ret = TRUE;
577 while (tpdomain->outstanding_request + tpdomain->threadpool_jobs > 0) {
578 if (timeout == -1) {
579 mono_coop_cond_wait (&tpdomain->cleanup_cond, &threadpool.domains_lock);
580 } else {
581 gint64 now;
582 gint res;
584 now = mono_msec_ticks();
585 if (now > end) {
586 ret = FALSE;
587 break;
590 res = mono_coop_cond_timedwait (&tpdomain->cleanup_cond, &threadpool.domains_lock, end - now);
591 if (res != 0) {
592 ret = FALSE;
593 break;
598 /* Remove from the list the worker threads look at */
599 tpdomain_remove (tpdomain);
601 domains_unlock ();
603 mono_coop_cond_destroy (&tpdomain->cleanup_cond);
604 tpdomain_free (tpdomain);
606 mono_refcount_dec (&threadpool);
608 return ret;
611 void
612 mono_threadpool_suspend (void)
614 if (mono_lazy_is_initialized (&status))
615 mono_threadpool_worker_set_suspended (TRUE);
618 void
619 mono_threadpool_resume (void)
621 if (mono_lazy_is_initialized (&status))
622 mono_threadpool_worker_set_suspended (FALSE);
625 void
626 ves_icall_System_Threading_ThreadPool_GetAvailableThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads, MonoError *error)
628 ThreadPoolCounter counter;
630 if (!worker_threads || !completion_port_threads)
631 return;
633 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool)) {
634 *worker_threads = 0;
635 *completion_port_threads = 0;
636 return;
639 counter = COUNTER_READ ();
641 *worker_threads = MAX (0, mono_threadpool_worker_get_max () - counter._.working);
642 *completion_port_threads = threadpool.limit_io_max;
644 mono_refcount_dec (&threadpool);
647 void
648 ves_icall_System_Threading_ThreadPool_GetMinThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads, MonoError *error)
650 if (!worker_threads || !completion_port_threads)
651 return;
653 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool)) {
654 *worker_threads = 0;
655 *completion_port_threads = 0;
656 return;
659 *worker_threads = mono_threadpool_worker_get_min ();
660 *completion_port_threads = threadpool.limit_io_min;
662 mono_refcount_dec (&threadpool);
665 void
666 ves_icall_System_Threading_ThreadPool_GetMaxThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads, MonoError *error)
668 if (!worker_threads || !completion_port_threads)
669 return;
671 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool)) {
672 *worker_threads = 0;
673 *completion_port_threads = 0;
674 return;
677 *worker_threads = mono_threadpool_worker_get_max ();
678 *completion_port_threads = threadpool.limit_io_max;
680 mono_refcount_dec (&threadpool);
683 MonoBoolean
684 ves_icall_System_Threading_ThreadPool_SetMinThreadsNative (gint32 worker_threads, gint32 completion_port_threads, MonoError *error)
686 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool))
687 return FALSE;
689 if (completion_port_threads <= 0 || completion_port_threads > threadpool.limit_io_max)
690 return FALSE;
692 if (!mono_threadpool_worker_set_min (worker_threads)) {
693 mono_refcount_dec (&threadpool);
694 return FALSE;
697 threadpool.limit_io_min = completion_port_threads;
699 mono_refcount_dec (&threadpool);
700 return TRUE;
703 MonoBoolean
704 ves_icall_System_Threading_ThreadPool_SetMaxThreadsNative (gint32 worker_threads, gint32 completion_port_threads, MonoError *error)
706 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool))
707 return FALSE;
709 worker_threads = MIN (worker_threads, MAX_POSSIBLE_THREADS);
710 completion_port_threads = MIN (completion_port_threads, MAX_POSSIBLE_THREADS);
712 gint cpu_count = mono_cpu_count ();
714 if (completion_port_threads < threadpool.limit_io_min || completion_port_threads < cpu_count)
715 return FALSE;
717 if (!mono_threadpool_worker_set_max (worker_threads)) {
718 mono_refcount_dec (&threadpool);
719 return FALSE;
722 threadpool.limit_io_max = completion_port_threads;
724 mono_refcount_dec (&threadpool);
725 return TRUE;
728 #ifdef ENABLE_NETCORE
729 gint32
730 ves_icall_System_Threading_ThreadPool_GetThreadCount (MonoError *error)
732 return mono_threadpool_worker_get_threads_count ();
735 gint64
736 ves_icall_System_Threading_ThreadPool_GetCompletedWorkItemCount (MonoError *error)
738 return mono_threadpool_worker_get_completed_threads_count ();
740 #endif
742 void
743 ves_icall_System_Threading_ThreadPool_InitializeVMTp (MonoBoolean *enable_worker_tracking, MonoError *error)
745 if (enable_worker_tracking) {
746 // TODO implement some kind of switch to have the possibily to use it
747 *enable_worker_tracking = FALSE;
750 mono_lazy_initialize (&status, initialize);
753 MonoBoolean
754 ves_icall_System_Threading_ThreadPool_NotifyWorkItemComplete (MonoError *error)
756 if (mono_domain_is_unloading (mono_domain_get ()) || mono_runtime_is_shutting_down ())
757 return FALSE;
759 return mono_threadpool_worker_notify_completed ();
762 void
763 ves_icall_System_Threading_ThreadPool_NotifyWorkItemProgressNative (MonoError *error)
765 mono_threadpool_worker_notify_completed ();
768 void
769 ves_icall_System_Threading_ThreadPool_NotifyWorkItemQueued (MonoError *error)
770 // FIXME Move to managed.
772 #ifndef DISABLE_PERFCOUNTERS
773 mono_atomic_inc_i64 (&mono_perfcounters->threadpool_workitems);
774 #endif
777 void
778 ves_icall_System_Threading_ThreadPool_ReportThreadStatus (MonoBoolean is_working, MonoError *error)
780 // TODO
781 mono_error_set_not_implemented (error, "");
784 MonoBoolean
785 ves_icall_System_Threading_ThreadPool_RequestWorkerThread (MonoError *error)
787 MonoDomain *domain;
788 ThreadPoolDomain *tpdomain;
789 ThreadPoolCounter counter;
791 domain = mono_domain_get ();
792 if (mono_domain_is_unloading (domain))
793 return FALSE;
795 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool)) {
796 /* threadpool has been destroyed, we are shutting down */
797 return FALSE;
800 domains_lock ();
802 tpdomain = tpdomain_get (domain);
803 if (!tpdomain) {
804 /* synchronize with mono_threadpool_remove_domain_jobs */
805 if (mono_domain_is_unloading (domain)) {
806 domains_unlock ();
807 mono_refcount_dec (&threadpool);
808 return FALSE;
811 tpdomain = tpdomain_create (domain);
814 g_assert (tpdomain);
816 tpdomain->outstanding_request ++;
817 g_assert (tpdomain->outstanding_request >= 1);
819 domains_unlock ();
821 COUNTER_ATOMIC (counter, {
822 if (counter._.starting == 16) {
823 mono_refcount_dec (&threadpool);
824 return TRUE;
827 counter._.starting ++;
830 mono_threadpool_worker_request ();
832 mono_refcount_dec (&threadpool);
833 return TRUE;