Fix on-demand initialization race conditions [threadpool.c]. (#18165)
[mono-project.git] / mono / metadata / threadpool.c
bloba1d3e49c265fca696e791f6fcfe87884b4b6a140
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 <config.h>
25 #include <mono/utils/mono-compiler.h>
27 #ifndef ENABLE_NETCORE
29 #include <stdlib.h>
30 #define _USE_MATH_DEFINES // needed by MSVC to define math constants
31 #include <math.h>
32 #include <glib.h>
34 #include <mono/metadata/class-internals.h>
35 #include <mono/metadata/domain-internals.h>
36 #include <mono/metadata/exception.h>
37 #include <mono/metadata/gc-internals.h>
38 #include <mono/metadata/object.h>
39 #include <mono/metadata/object-internals.h>
40 #include <mono/metadata/threadpool.h>
41 #include <mono/metadata/threadpool-worker.h>
42 #include <mono/metadata/threadpool-io.h>
43 #include <mono/metadata/w32event.h>
44 #include <mono/utils/atomic.h>
45 #include <mono/utils/mono-compiler.h>
46 #include <mono/utils/mono-complex.h>
47 #include <mono/utils/mono-lazy-init.h>
48 #include <mono/utils/mono-logger.h>
49 #include <mono/utils/mono-logger-internals.h>
50 #include <mono/utils/mono-proclib.h>
51 #include <mono/utils/mono-threads.h>
52 #include <mono/utils/mono-time.h>
53 #include <mono/utils/refcount.h>
54 #include <mono/utils/mono-os-wait.h>
55 #include "monitor.h"
56 #include "icall-decl.h"
58 // consistency with coreclr https://github.com/dotnet/coreclr/blob/643b09f966e68e06d5f0930755985a01a2a2b096/src/vm/win32threadpool.h#L111
59 #define MAX_POSSIBLE_THREADS 0x7fff
61 typedef struct {
62 MonoDomain *domain;
63 /* Number of outstanding jobs */
64 gint32 outstanding_request;
65 /* Number of currently executing jobs */
66 gint32 threadpool_jobs;
67 /* Signalled when threadpool_jobs + outstanding_request is 0 */
68 /* Protected by threadpool.domains_lock */
69 MonoCoopCond cleanup_cond;
70 } ThreadPoolDomain;
72 typedef union {
73 struct {
74 gint16 starting; /* starting, but not yet in worker_callback */
75 gint16 working; /* executing worker_callback */
76 } _;
77 gint32 as_gint32;
78 } ThreadPoolCounter;
80 typedef struct {
81 MonoRefCount ref;
83 GPtrArray *domains; // ThreadPoolDomain* []
84 MonoCoopMutex domains_lock;
86 ThreadPoolCounter counters;
88 gint32 limit_io_min;
89 gint32 limit_io_max;
90 } ThreadPool;
92 static mono_lazy_init_t status = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
94 static ThreadPool threadpool;
96 #define COUNTER_ATOMIC(var,block) \
97 do { \
98 ThreadPoolCounter __old; \
99 do { \
100 (var) = __old = COUNTER_READ (); \
101 { block; } \
102 if (!(counter._.starting >= 0)) \
103 g_error ("%s: counter._.starting = %d, but should be >= 0", __func__, counter._.starting); \
104 if (!(counter._.working >= 0)) \
105 g_error ("%s: counter._.working = %d, but should be >= 0", __func__, counter._.working); \
106 } while (mono_atomic_cas_i32 (&threadpool.counters.as_gint32, (var).as_gint32, __old.as_gint32) != __old.as_gint32); \
107 } while (0)
109 static ThreadPoolCounter
110 COUNTER_READ (void)
112 ThreadPoolCounter counter;
113 counter.as_gint32 = mono_atomic_load_i32 (&threadpool.counters.as_gint32);
114 return counter;
117 static void
118 domains_lock (void)
120 mono_coop_mutex_lock (&threadpool.domains_lock);
123 static void
124 domains_unlock (void)
126 mono_coop_mutex_unlock (&threadpool.domains_lock);
129 static void
130 destroy (gpointer unused)
132 g_ptr_array_free (threadpool.domains, TRUE);
133 mono_coop_mutex_destroy (&threadpool.domains_lock);
136 static void
137 worker_callback (void);
139 static void
140 initialize (void)
142 g_assert (sizeof (ThreadPoolCounter) == sizeof (gint32));
144 mono_refcount_init (&threadpool, destroy);
146 threadpool.domains = g_ptr_array_new ();
147 mono_coop_mutex_init (&threadpool.domains_lock);
149 threadpool.limit_io_min = mono_cpu_count ();
150 threadpool.limit_io_max = CLAMP (threadpool.limit_io_min * 100, MIN (threadpool.limit_io_min, 200), MAX (threadpool.limit_io_min, 200));
152 mono_threadpool_worker_init (worker_callback);
155 static void
156 cleanup (void)
158 mono_threadpool_worker_cleanup ();
160 mono_refcount_dec (&threadpool);
163 gboolean
164 mono_threadpool_enqueue_work_item (MonoDomain *domain, MonoObject *work_item, MonoError *error)
166 MonoDomain *current_domain;
167 MonoBoolean f;
168 gpointer args [2];
170 error_init (error);
171 g_assert (work_item);
173 MONO_STATIC_POINTER_INIT (MonoClass, threadpool_class)
175 threadpool_class = mono_class_load_from_name (mono_defaults.corlib, "System.Threading", "ThreadPool");
177 MONO_STATIC_POINTER_INIT_END (MonoClass, threadpool_class)
179 MONO_STATIC_POINTER_INIT (MonoMethod, unsafe_queue_custom_work_item_method)
181 unsafe_queue_custom_work_item_method = mono_class_get_method_from_name_checked (threadpool_class, "UnsafeQueueCustomWorkItem", 2, 0, error);
182 mono_error_assert_ok (error);
184 MONO_STATIC_POINTER_INIT_END (MonoMethod, unsafe_queue_custom_work_item_method)
186 g_assert (unsafe_queue_custom_work_item_method);
188 f = FALSE;
190 args [0] = (gpointer) work_item;
191 args [1] = (gpointer) &f;
193 current_domain = mono_domain_get ();
194 if (current_domain == domain) {
195 mono_runtime_invoke_checked (unsafe_queue_custom_work_item_method, NULL, args, error);
196 } else {
197 mono_thread_push_appdomain_ref (domain);
198 if (mono_domain_set_fast (domain, FALSE)) {
199 mono_runtime_invoke_checked (unsafe_queue_custom_work_item_method, NULL, args, error);
200 mono_domain_set_fast (current_domain, TRUE);
201 } else {
202 // mono_domain_set_fast failing still leads to success.
204 mono_thread_pop_appdomain_ref ();
206 return is_ok (error);
209 /* LOCKING: domains_lock must be held. */
210 static ThreadPoolDomain *
211 tpdomain_create (MonoDomain *domain)
213 ThreadPoolDomain *tpdomain;
215 tpdomain = g_new0 (ThreadPoolDomain, 1);
216 tpdomain->domain = domain;
217 mono_coop_cond_init (&tpdomain->cleanup_cond);
219 g_ptr_array_add (threadpool.domains, tpdomain);
221 return tpdomain;
224 /* LOCKING: domains_lock must be held. */
225 static gboolean
226 tpdomain_remove (ThreadPoolDomain *tpdomain)
228 g_assert (tpdomain);
229 return g_ptr_array_remove (threadpool.domains, tpdomain);
232 /* LOCKING: domains_lock must be held */
233 static ThreadPoolDomain *
234 tpdomain_get (MonoDomain *domain)
236 gint i;
238 g_assert (domain);
240 for (i = 0; i < threadpool.domains->len; ++i) {
241 ThreadPoolDomain *tpdomain;
243 tpdomain = (ThreadPoolDomain *)g_ptr_array_index (threadpool.domains, i);
244 if (tpdomain->domain == domain)
245 return tpdomain;
248 return NULL;
251 static void
252 tpdomain_free (ThreadPoolDomain *tpdomain)
254 g_free (tpdomain);
257 /* LOCKING: domains_lock must be held */
258 static ThreadPoolDomain *
259 tpdomain_get_next (ThreadPoolDomain *current)
261 ThreadPoolDomain *tpdomain = NULL;
262 gint len;
264 len = threadpool.domains->len;
265 if (len > 0) {
266 gint i, current_idx = -1;
267 if (current) {
268 for (i = 0; i < len; ++i) {
269 if (current == g_ptr_array_index (threadpool.domains, i)) {
270 current_idx = i;
271 break;
275 for (i = current_idx + 1; i < len + current_idx + 1; ++i) {
276 ThreadPoolDomain *tmp = (ThreadPoolDomain *)g_ptr_array_index (threadpool.domains, i % len);
277 if (tmp->outstanding_request > 0) {
278 tpdomain = tmp;
279 break;
284 return tpdomain;
287 static MonoObject*
288 try_invoke_perform_wait_callback (MonoObject** exc, MonoError *error)
290 HANDLE_FUNCTION_ENTER ();
291 error_init (error);
292 MonoObject * const res = mono_runtime_try_invoke (mono_defaults.threadpool_perform_wait_callback_method, NULL, NULL, exc, error);
293 HANDLE_FUNCTION_RETURN_VAL (res);
296 static gsize
297 set_thread_name (MonoInternalThread *thread)
299 return mono_thread_set_name_constant_ignore_error (thread, "Thread Pool Worker", MonoSetThreadNameFlag_Reset);
302 static void
303 worker_callback (void)
305 ThreadPoolDomain *tpdomain, *previous_tpdomain;
306 ThreadPoolCounter counter;
307 MonoInternalThread *thread;
309 if (!mono_refcount_tryinc (&threadpool))
310 return;
312 thread = mono_thread_internal_current ();
314 COUNTER_ATOMIC (counter, {
315 if (!(counter._.working < 32767 /* G_MAXINT16 */))
316 g_error ("%s: counter._.working = %d, but should be < 32767", __func__, counter._.working);
318 counter._.starting --;
319 counter._.working ++;
322 if (mono_runtime_is_shutting_down ()) {
323 COUNTER_ATOMIC (counter, {
324 counter._.working --;
327 mono_refcount_dec (&threadpool);
328 return;
332 * This is needed so there is always an lmf frame in the runtime invoke call below,
333 * so ThreadAbortExceptions are caught even if the thread is in native code.
335 mono_defaults.threadpool_perform_wait_callback_method->save_lmf = TRUE;
337 gsize name_generation = thread->name.generation;
338 /* Set the name if this is the first call to worker_callback on this thread */
339 if (name_generation == 0)
340 name_generation = set_thread_name (thread);
342 domains_lock ();
344 previous_tpdomain = NULL;
346 while (!mono_runtime_is_shutting_down ()) {
347 gboolean retire = FALSE;
349 if (thread->state & (ThreadState_AbortRequested | ThreadState_SuspendRequested)) {
350 domains_unlock ();
351 if (mono_thread_interruption_checkpoint_bool ()) {
352 domains_lock ();
353 continue;
355 domains_lock ();
358 tpdomain = tpdomain_get_next (previous_tpdomain);
359 if (!tpdomain)
360 break;
362 tpdomain->outstanding_request --;
363 g_assert (tpdomain->outstanding_request >= 0);
365 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker running in domain %p (outstanding requests %d)",
366 GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), tpdomain->domain, tpdomain->outstanding_request);
368 g_assert (tpdomain->threadpool_jobs >= 0);
369 tpdomain->threadpool_jobs ++;
371 domains_unlock ();
373 // Any thread can set any other thread name at any time.
374 // So this is unavoidably racy.
375 // This only partly fights against that -- i.e. not atomic and not a loop.
376 // It is reliable against the thread setting its own name, and somewhat
377 // reliable against other threads setting this thread's name.
378 if (name_generation != thread->name.generation)
379 name_generation = set_thread_name (thread);
381 mono_thread_clear_and_set_state (thread,
382 (MonoThreadState)~ThreadState_Background,
383 ThreadState_Background);
385 mono_thread_push_appdomain_ref (tpdomain->domain);
386 if (mono_domain_set_fast (tpdomain->domain, FALSE)) {
387 MonoObject *exc = NULL, *res;
389 ERROR_DECL (error);
391 res = try_invoke_perform_wait_callback (&exc, error);
392 if (exc || !is_ok(error)) {
393 if (exc == NULL)
394 exc = (MonoObject *) mono_error_convert_to_exception (error);
395 else
396 mono_error_cleanup (error);
397 mono_thread_internal_unhandled_exception (exc);
398 } else if (res && *(MonoBoolean*) mono_object_unbox_internal (res) == FALSE) {
399 retire = TRUE;
402 mono_domain_set_fast (mono_get_root_domain (), TRUE);
404 mono_thread_pop_appdomain_ref ();
406 /* Reset name after every callback */
407 if (name_generation != thread->name.generation)
408 name_generation = set_thread_name (thread);
410 domains_lock ();
412 tpdomain->threadpool_jobs --;
413 g_assert (tpdomain->threadpool_jobs >= 0);
415 if (tpdomain->outstanding_request + tpdomain->threadpool_jobs == 0 && mono_domain_is_unloading (tpdomain->domain)) {
416 gboolean removed;
418 removed = tpdomain_remove (tpdomain);
419 g_assert (removed);
421 mono_coop_cond_signal (&tpdomain->cleanup_cond);
422 tpdomain = NULL;
425 if (retire)
426 break;
428 previous_tpdomain = tpdomain;
431 domains_unlock ();
433 COUNTER_ATOMIC (counter, {
434 counter._.working --;
437 mono_refcount_dec (&threadpool);
440 void
441 mono_threadpool_cleanup (void)
443 #ifndef DISABLE_SOCKETS
444 mono_threadpool_io_cleanup ();
445 #endif
446 mono_lazy_cleanup (&status, cleanup);
449 MonoAsyncResult *
450 mono_threadpool_begin_invoke (MonoDomain *domain, MonoObject *target, MonoMethod *method, gpointer *params, MonoError *error)
452 MonoMethodMessage *message;
453 MonoAsyncResult *async_result;
454 MonoAsyncCall *async_call;
455 MonoDelegate *async_callback = NULL;
456 MonoObject *state = NULL;
458 MONO_STATIC_POINTER_INIT (MonoClass, async_call_klass)
460 async_call_klass = mono_class_load_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
462 MONO_STATIC_POINTER_INIT_END (MonoClass, async_call_klass)
464 error_init (error);
466 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);
467 return_val_if_nok (error, NULL);
469 async_call = (MonoAsyncCall*) mono_object_new_checked (domain, async_call_klass, error);
470 return_val_if_nok (error, NULL);
472 MONO_OBJECT_SETREF_INTERNAL (async_call, msg, message);
473 MONO_OBJECT_SETREF_INTERNAL (async_call, state, state);
475 if (async_callback) {
476 MONO_OBJECT_SETREF_INTERNAL (async_call, cb_method, mono_get_delegate_invoke_internal (((MonoObject*) async_callback)->vtable->klass));
477 MONO_OBJECT_SETREF_INTERNAL (async_call, cb_target, async_callback);
480 async_result = mono_async_result_new (domain, NULL, async_call->state, NULL, (MonoObject*) async_call, error);
481 return_val_if_nok (error, NULL);
482 MONO_OBJECT_SETREF_INTERNAL (async_result, async_delegate, target);
484 mono_threadpool_enqueue_work_item (domain, (MonoObject*) async_result, error);
485 return_val_if_nok (error, NULL);
487 return async_result;
490 MonoObject *
491 mono_threadpool_end_invoke (MonoAsyncResult *ares, MonoArray **out_args, MonoObject **exc, MonoError *error)
493 MonoAsyncCall *ac;
495 error_init (error);
496 g_assert (exc);
497 g_assert (out_args);
499 *exc = NULL;
500 *out_args = NULL;
502 /* check if already finished */
503 mono_monitor_enter_internal ((MonoObject*) ares);
505 if (ares->endinvoke_called) {
506 mono_error_set_invalid_operation(error, "Delegate EndInvoke method called more than once");
507 mono_monitor_exit_internal ((MonoObject*) ares);
508 return NULL;
511 ares->endinvoke_called = 1;
513 /* wait until we are really finished */
514 if (ares->completed) {
515 mono_monitor_exit_internal ((MonoObject *) ares);
516 } else {
517 gpointer wait_event;
518 if (ares->handle) {
519 wait_event = mono_wait_handle_get_handle ((MonoWaitHandle*) ares->handle);
520 } else {
521 wait_event = mono_w32event_create (TRUE, FALSE);
522 g_assert(wait_event);
523 MonoWaitHandle *wait_handle = mono_wait_handle_new (mono_object_domain (ares), wait_event, error);
524 if (!is_ok (error)) {
525 mono_w32event_close (wait_event);
526 return NULL;
528 MONO_OBJECT_SETREF_INTERNAL (ares, handle, (MonoObject*) wait_handle);
530 mono_monitor_exit_internal ((MonoObject*) ares);
531 mono_w32handle_wait_one (wait_event, MONO_INFINITE_WAIT, TRUE);
534 ac = (MonoAsyncCall*) ares->object_data;
535 g_assert (ac);
537 *exc = ac->msg->exc; /* FIXME: GC add write barrier */
538 *out_args = ac->out_args;
539 return ac->res;
542 gboolean
543 mono_threadpool_remove_domain_jobs (MonoDomain *domain, int timeout)
545 gint64 end = 0;
546 ThreadPoolDomain *tpdomain;
547 gboolean ret;
549 g_assert (domain);
550 g_assert (timeout >= -1);
552 g_assert (mono_domain_is_unloading (domain));
554 if (timeout != -1)
555 end = mono_msec_ticks () + timeout;
557 #ifndef DISABLE_SOCKETS
558 mono_threadpool_io_remove_domain_jobs (domain);
559 if (timeout != -1) {
560 if (mono_msec_ticks () > end)
561 return FALSE;
563 #endif
566 * Wait for all threads which execute jobs in the domain to exit.
567 * The is_unloading () check in worker_request () ensures that
568 * no new jobs are added after we enter the lock below.
571 if (!mono_lazy_is_initialized (&status))
572 return TRUE;
574 mono_refcount_inc (&threadpool);
576 domains_lock ();
578 tpdomain = tpdomain_get (domain);
579 if (!tpdomain) {
580 domains_unlock ();
581 mono_refcount_dec (&threadpool);
582 return TRUE;
585 ret = TRUE;
587 while (tpdomain->outstanding_request + tpdomain->threadpool_jobs > 0) {
588 if (timeout == -1) {
589 mono_coop_cond_wait (&tpdomain->cleanup_cond, &threadpool.domains_lock);
590 } else {
591 gint64 now;
592 gint res;
594 now = mono_msec_ticks();
595 if (now > end) {
596 ret = FALSE;
597 break;
600 res = mono_coop_cond_timedwait (&tpdomain->cleanup_cond, &threadpool.domains_lock, end - now);
601 if (res != 0) {
602 ret = FALSE;
603 break;
608 /* Remove from the list the worker threads look at */
609 tpdomain_remove (tpdomain);
611 domains_unlock ();
613 mono_coop_cond_destroy (&tpdomain->cleanup_cond);
614 tpdomain_free (tpdomain);
616 mono_refcount_dec (&threadpool);
618 return ret;
621 void
622 mono_threadpool_suspend (void)
624 if (mono_lazy_is_initialized (&status))
625 mono_threadpool_worker_set_suspended (TRUE);
628 void
629 mono_threadpool_resume (void)
631 if (mono_lazy_is_initialized (&status))
632 mono_threadpool_worker_set_suspended (FALSE);
635 void
636 ves_icall_System_Threading_ThreadPool_GetAvailableThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads, MonoError *error)
638 ThreadPoolCounter counter;
640 if (!worker_threads || !completion_port_threads)
641 return;
643 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool)) {
644 *worker_threads = 0;
645 *completion_port_threads = 0;
646 return;
649 counter = COUNTER_READ ();
651 *worker_threads = MAX (0, mono_threadpool_worker_get_max () - counter._.working);
652 *completion_port_threads = threadpool.limit_io_max;
654 mono_refcount_dec (&threadpool);
657 void
658 ves_icall_System_Threading_ThreadPool_GetMinThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads, MonoError *error)
660 if (!worker_threads || !completion_port_threads)
661 return;
663 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool)) {
664 *worker_threads = 0;
665 *completion_port_threads = 0;
666 return;
669 *worker_threads = mono_threadpool_worker_get_min ();
670 *completion_port_threads = threadpool.limit_io_min;
672 mono_refcount_dec (&threadpool);
675 void
676 ves_icall_System_Threading_ThreadPool_GetMaxThreadsNative (gint32 *worker_threads, gint32 *completion_port_threads, MonoError *error)
678 if (!worker_threads || !completion_port_threads)
679 return;
681 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool)) {
682 *worker_threads = 0;
683 *completion_port_threads = 0;
684 return;
687 *worker_threads = mono_threadpool_worker_get_max ();
688 *completion_port_threads = threadpool.limit_io_max;
690 mono_refcount_dec (&threadpool);
693 MonoBoolean
694 ves_icall_System_Threading_ThreadPool_SetMinThreadsNative (gint32 worker_threads, gint32 completion_port_threads, MonoError *error)
696 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool))
697 return FALSE;
699 if (completion_port_threads <= 0 || completion_port_threads > threadpool.limit_io_max)
700 return FALSE;
702 if (!mono_threadpool_worker_set_min (worker_threads)) {
703 mono_refcount_dec (&threadpool);
704 return FALSE;
707 threadpool.limit_io_min = completion_port_threads;
709 mono_refcount_dec (&threadpool);
710 return TRUE;
713 MonoBoolean
714 ves_icall_System_Threading_ThreadPool_SetMaxThreadsNative (gint32 worker_threads, gint32 completion_port_threads, MonoError *error)
716 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool))
717 return FALSE;
719 worker_threads = MIN (worker_threads, MAX_POSSIBLE_THREADS);
720 completion_port_threads = MIN (completion_port_threads, MAX_POSSIBLE_THREADS);
722 gint cpu_count = mono_cpu_count ();
724 if (completion_port_threads < threadpool.limit_io_min || completion_port_threads < cpu_count)
725 return FALSE;
727 if (!mono_threadpool_worker_set_max (worker_threads)) {
728 mono_refcount_dec (&threadpool);
729 return FALSE;
732 threadpool.limit_io_max = completion_port_threads;
734 mono_refcount_dec (&threadpool);
735 return TRUE;
738 void
739 ves_icall_System_Threading_ThreadPool_InitializeVMTp (MonoBoolean *enable_worker_tracking, MonoError *error)
741 if (enable_worker_tracking) {
742 // TODO implement some kind of switch to have the possibily to use it
743 *enable_worker_tracking = FALSE;
746 mono_lazy_initialize (&status, initialize);
749 MonoBoolean
750 ves_icall_System_Threading_ThreadPool_NotifyWorkItemComplete (MonoError *error)
752 if (mono_domain_is_unloading (mono_domain_get ()) || mono_runtime_is_shutting_down ())
753 return FALSE;
755 return mono_threadpool_worker_notify_completed ();
758 void
759 ves_icall_System_Threading_ThreadPool_NotifyWorkItemProgressNative (MonoError *error)
761 mono_threadpool_worker_notify_completed ();
764 void
765 ves_icall_System_Threading_ThreadPool_NotifyWorkItemQueued (MonoError *error)
766 // FIXME Move to managed.
768 #ifndef DISABLE_PERFCOUNTERS
769 mono_atomic_inc_i64 (&mono_perfcounters->threadpool_workitems);
770 #endif
773 void
774 ves_icall_System_Threading_ThreadPool_ReportThreadStatus (MonoBoolean is_working, MonoError *error)
776 // TODO
777 mono_error_set_not_implemented (error, "");
780 MonoBoolean
781 ves_icall_System_Threading_ThreadPool_RequestWorkerThread (MonoError *error)
783 MonoDomain *domain;
784 ThreadPoolDomain *tpdomain;
785 ThreadPoolCounter counter;
787 domain = mono_domain_get ();
788 if (mono_domain_is_unloading (domain))
789 return FALSE;
791 if (!mono_lazy_initialize (&status, initialize) || !mono_refcount_tryinc (&threadpool)) {
792 /* threadpool has been destroyed, we are shutting down */
793 return FALSE;
796 domains_lock ();
798 tpdomain = tpdomain_get (domain);
799 if (!tpdomain) {
800 /* synchronize with mono_threadpool_remove_domain_jobs */
801 if (mono_domain_is_unloading (domain)) {
802 domains_unlock ();
803 mono_refcount_dec (&threadpool);
804 return FALSE;
807 tpdomain = tpdomain_create (domain);
810 g_assert (tpdomain);
812 tpdomain->outstanding_request ++;
813 g_assert (tpdomain->outstanding_request >= 1);
815 domains_unlock ();
817 COUNTER_ATOMIC (counter, {
818 if (counter._.starting == 16) {
819 mono_refcount_dec (&threadpool);
820 return TRUE;
823 counter._.starting ++;
826 mono_threadpool_worker_request ();
828 mono_refcount_dec (&threadpool);
829 return TRUE;
832 #endif /* !ENABLE_NETCORE */
834 MONO_EMPTY_SOURCE_FILE (threadpool);