3 * Microsoft threadpool runtime support
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.
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
25 #include <mono/utils/mono-compiler.h>
27 #ifndef ENABLE_NETCORE
30 #define _USE_MATH_DEFINES // needed by MSVC to define math constants
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>
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
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
;
74 gint16 starting
; /* starting, but not yet in worker_callback */
75 gint16 working
; /* executing worker_callback */
83 GPtrArray
*domains
; // ThreadPoolDomain* []
84 MonoCoopMutex domains_lock
;
86 ThreadPoolCounter counters
;
92 static mono_lazy_init_t status
= MONO_LAZY_INIT_STATUS_NOT_INITIALIZED
;
94 static ThreadPool threadpool
;
96 #define COUNTER_ATOMIC(var,block) \
98 ThreadPoolCounter __old; \
100 (var) = __old = COUNTER_READ (); \
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); \
109 static ThreadPoolCounter
112 ThreadPoolCounter counter
;
113 counter
.as_gint32
= mono_atomic_load_i32 (&threadpool
.counters
.as_gint32
);
120 mono_coop_mutex_lock (&threadpool
.domains_lock
);
124 domains_unlock (void)
126 mono_coop_mutex_unlock (&threadpool
.domains_lock
);
130 destroy (gpointer unused
)
132 g_ptr_array_free (threadpool
.domains
, TRUE
);
133 mono_coop_mutex_destroy (&threadpool
.domains_lock
);
137 worker_callback (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
);
158 mono_threadpool_worker_cleanup ();
160 mono_refcount_dec (&threadpool
);
164 mono_threadpool_enqueue_work_item (MonoDomain
*domain
, MonoObject
*work_item
, MonoError
*error
)
166 MonoDomain
*current_domain
;
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
);
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
);
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
);
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
);
224 /* LOCKING: domains_lock must be held. */
226 tpdomain_remove (ThreadPoolDomain
*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
)
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
)
252 tpdomain_free (ThreadPoolDomain
*tpdomain
)
257 /* LOCKING: domains_lock must be held */
258 static ThreadPoolDomain
*
259 tpdomain_get_next (ThreadPoolDomain
*current
)
261 ThreadPoolDomain
*tpdomain
= NULL
;
264 len
= threadpool
.domains
->len
;
266 gint i
, current_idx
= -1;
268 for (i
= 0; i
< len
; ++i
) {
269 if (current
== g_ptr_array_index (threadpool
.domains
, i
)) {
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) {
288 try_invoke_perform_wait_callback (MonoObject
** exc
, MonoError
*error
)
290 HANDLE_FUNCTION_ENTER ();
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
);
297 set_thread_name (MonoInternalThread
*thread
)
299 return mono_thread_set_name_constant_ignore_error (thread
, "Thread Pool Worker", MonoSetThreadNameFlag_Reset
);
303 worker_callback (void)
305 ThreadPoolDomain
*tpdomain
, *previous_tpdomain
;
306 ThreadPoolCounter counter
;
307 MonoInternalThread
*thread
;
309 if (!mono_refcount_tryinc (&threadpool
))
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
);
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
);
344 previous_tpdomain
= NULL
;
346 while (!mono_runtime_is_shutting_down ()) {
347 gboolean retire
= FALSE
;
349 if (thread
->state
& (ThreadState_AbortRequested
| ThreadState_SuspendRequested
)) {
351 if (mono_thread_interruption_checkpoint_bool ()) {
358 tpdomain
= tpdomain_get_next (previous_tpdomain
);
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
++;
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
;
391 res
= try_invoke_perform_wait_callback (&exc
, error
);
392 if (exc
|| !is_ok(error
)) {
394 exc
= (MonoObject
*) mono_error_convert_to_exception (error
);
396 mono_error_cleanup (error
);
397 mono_thread_internal_unhandled_exception (exc
);
398 } else if (res
&& *(MonoBoolean
*) mono_object_unbox_internal (res
) == FALSE
) {
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
);
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
)) {
418 removed
= tpdomain_remove (tpdomain
);
421 mono_coop_cond_signal (&tpdomain
->cleanup_cond
);
428 previous_tpdomain
= tpdomain
;
433 COUNTER_ATOMIC (counter
, {
434 counter
._
.working
--;
437 mono_refcount_dec (&threadpool
);
441 mono_threadpool_cleanup (void)
443 #ifndef DISABLE_SOCKETS
444 mono_threadpool_io_cleanup ();
446 mono_lazy_cleanup (&status
, cleanup
);
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
)
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
);
491 mono_threadpool_end_invoke (MonoAsyncResult
*ares
, MonoArray
**out_args
, MonoObject
**exc
, MonoError
*error
)
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
);
511 ares
->endinvoke_called
= 1;
513 /* wait until we are really finished */
514 if (ares
->completed
) {
515 mono_monitor_exit_internal ((MonoObject
*) ares
);
519 wait_event
= mono_wait_handle_get_handle ((MonoWaitHandle
*) ares
->handle
);
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
);
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
;
537 *exc
= ac
->msg
->exc
; /* FIXME: GC add write barrier */
538 *out_args
= ac
->out_args
;
543 mono_threadpool_remove_domain_jobs (MonoDomain
*domain
, int timeout
)
546 ThreadPoolDomain
*tpdomain
;
550 g_assert (timeout
>= -1);
552 g_assert (mono_domain_is_unloading (domain
));
555 end
= mono_msec_ticks () + timeout
;
557 #ifndef DISABLE_SOCKETS
558 mono_threadpool_io_remove_domain_jobs (domain
);
560 if (mono_msec_ticks () > end
)
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
))
574 mono_refcount_inc (&threadpool
);
578 tpdomain
= tpdomain_get (domain
);
581 mono_refcount_dec (&threadpool
);
587 while (tpdomain
->outstanding_request
+ tpdomain
->threadpool_jobs
> 0) {
589 mono_coop_cond_wait (&tpdomain
->cleanup_cond
, &threadpool
.domains_lock
);
594 now
= mono_msec_ticks();
600 res
= mono_coop_cond_timedwait (&tpdomain
->cleanup_cond
, &threadpool
.domains_lock
, end
- now
);
608 /* Remove from the list the worker threads look at */
609 tpdomain_remove (tpdomain
);
613 mono_coop_cond_destroy (&tpdomain
->cleanup_cond
);
614 tpdomain_free (tpdomain
);
616 mono_refcount_dec (&threadpool
);
622 mono_threadpool_suspend (void)
624 if (mono_lazy_is_initialized (&status
))
625 mono_threadpool_worker_set_suspended (TRUE
);
629 mono_threadpool_resume (void)
631 if (mono_lazy_is_initialized (&status
))
632 mono_threadpool_worker_set_suspended (FALSE
);
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
)
643 if (!mono_lazy_initialize (&status
, initialize
) || !mono_refcount_tryinc (&threadpool
)) {
645 *completion_port_threads
= 0;
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
);
658 ves_icall_System_Threading_ThreadPool_GetMinThreadsNative (gint32
*worker_threads
, gint32
*completion_port_threads
, MonoError
*error
)
660 if (!worker_threads
|| !completion_port_threads
)
663 if (!mono_lazy_initialize (&status
, initialize
) || !mono_refcount_tryinc (&threadpool
)) {
665 *completion_port_threads
= 0;
669 *worker_threads
= mono_threadpool_worker_get_min ();
670 *completion_port_threads
= threadpool
.limit_io_min
;
672 mono_refcount_dec (&threadpool
);
676 ves_icall_System_Threading_ThreadPool_GetMaxThreadsNative (gint32
*worker_threads
, gint32
*completion_port_threads
, MonoError
*error
)
678 if (!worker_threads
|| !completion_port_threads
)
681 if (!mono_lazy_initialize (&status
, initialize
) || !mono_refcount_tryinc (&threadpool
)) {
683 *completion_port_threads
= 0;
687 *worker_threads
= mono_threadpool_worker_get_max ();
688 *completion_port_threads
= threadpool
.limit_io_max
;
690 mono_refcount_dec (&threadpool
);
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
))
699 if (completion_port_threads
<= 0 || completion_port_threads
> threadpool
.limit_io_max
)
702 if (!mono_threadpool_worker_set_min (worker_threads
)) {
703 mono_refcount_dec (&threadpool
);
707 threadpool
.limit_io_min
= completion_port_threads
;
709 mono_refcount_dec (&threadpool
);
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
))
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
)
727 if (!mono_threadpool_worker_set_max (worker_threads
)) {
728 mono_refcount_dec (&threadpool
);
732 threadpool
.limit_io_max
= completion_port_threads
;
734 mono_refcount_dec (&threadpool
);
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
);
750 ves_icall_System_Threading_ThreadPool_NotifyWorkItemComplete (MonoError
*error
)
752 if (mono_domain_is_unloading (mono_domain_get ()) || mono_runtime_is_shutting_down ())
755 return mono_threadpool_worker_notify_completed ();
759 ves_icall_System_Threading_ThreadPool_NotifyWorkItemProgressNative (MonoError
*error
)
761 mono_threadpool_worker_notify_completed ();
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
);
774 ves_icall_System_Threading_ThreadPool_ReportThreadStatus (MonoBoolean is_working
, MonoError
*error
)
777 mono_error_set_not_implemented (error
, "");
781 ves_icall_System_Threading_ThreadPool_RequestWorkerThread (MonoError
*error
)
784 ThreadPoolDomain
*tpdomain
;
785 ThreadPoolCounter counter
;
787 domain
= mono_domain_get ();
788 if (mono_domain_is_unloading (domain
))
791 if (!mono_lazy_initialize (&status
, initialize
) || !mono_refcount_tryinc (&threadpool
)) {
792 /* threadpool has been destroyed, we are shutting down */
798 tpdomain
= tpdomain_get (domain
);
800 /* synchronize with mono_threadpool_remove_domain_jobs */
801 if (mono_domain_is_unloading (domain
)) {
803 mono_refcount_dec (&threadpool
);
807 tpdomain
= tpdomain_create (domain
);
812 tpdomain
->outstanding_request
++;
813 g_assert (tpdomain
->outstanding_request
>= 1);
817 COUNTER_ATOMIC (counter
, {
818 if (counter
._
.starting
== 16) {
819 mono_refcount_dec (&threadpool
);
823 counter
._
.starting
++;
826 mono_threadpool_worker_request ();
828 mono_refcount_dec (&threadpool
);
832 #endif /* !ENABLE_NETCORE */
834 MONO_EMPTY_SOURCE_FILE (threadpool
);