2 * threadpool.c: global thread pool
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2010 Novell, Inc (http://www.novell.com)
10 * Copyright 2001 Xamarin Inc (http://www.xamarin.com)
16 #include <mono/metadata/profiler-private.h>
17 #include <mono/metadata/threads.h>
18 #include <mono/metadata/threads-types.h>
19 #include <mono/metadata/threadpool-internals.h>
20 #include <mono/metadata/exception.h>
21 #include <mono/metadata/environment.h>
22 #include <mono/metadata/mono-config.h>
23 #include <mono/metadata/mono-mlist.h>
24 #include <mono/metadata/mono-perfcounters.h>
25 #include <mono/metadata/socket-io.h>
26 #include <mono/metadata/mono-cq.h>
27 #include <mono/metadata/mono-wsq.h>
28 #include <mono/metadata/mono-ptr-array.h>
29 #include <mono/io-layer/io-layer.h>
30 #include <mono/utils/mono-time.h>
31 #include <mono/utils/mono-proclib.h>
32 #include <mono/utils/mono-semaphore.h>
33 #include <mono/utils/atomic.h>
35 #ifdef HAVE_SYS_TIME_H
38 #include <sys/types.h>
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
48 #include <mono/utils/mono-poll.h>
50 #include <sys/epoll.h>
53 #include <sys/event.h>
57 #ifndef DISABLE_SOCKETS
58 #include "mono/io-layer/socket-wrappers.h"
61 #include "threadpool.h"
63 #define THREAD_WANTS_A_BREAK(t) ((t->state & (ThreadState_StopRequested | \
64 ThreadState_SuspendRequested)) != 0)
66 #define SMALL_STACK (128 * (sizeof (gpointer) / 4) * 1024)
68 /* DEBUG: prints tp data every 2s */
71 /* mono_thread_pool_init called */
72 static volatile int tp_inited;
82 MONITOR_STATE_FALLING_ASLEEP,
83 MONITOR_STATE_SLEEPING
86 static SocketIOData socket_io_data;
88 /* Keep in sync with the System.MonoAsyncCall class which provides GC tracking */
91 MonoMethodMessage *msg;
92 MonoMethod *cb_method;
93 MonoDelegate *cb_target;
101 MonoCQ *queue; /* GC root */
103 volatile gint waiting; /* threads waiting for a work item */
106 volatile gint pool_status; /* 0 -> not initialized, 1 -> initialized, 2 -> cleaning up */
107 /* min, max, n and busy -> Interlocked */
108 volatile gint min_threads;
109 volatile gint max_threads;
110 volatile gint nthreads;
111 volatile gint busy_threads;
113 void (*async_invoke) (gpointer data);
114 void *pc_nitems; /* Performance counter for total number of items in added */
115 void *pc_nthreads; /* Performance counter for total number of active threads */
117 volatile gint destroy_thread;
119 volatile gint32 njobs;
121 volatile gint32 nexecuted;
125 static ThreadPool async_tp;
126 static ThreadPool async_io_tp;
128 static void async_invoke_thread (gpointer data);
129 static MonoObject *mono_async_invoke (ThreadPool *tp, MonoAsyncResult *ares);
130 static void threadpool_free_queue (ThreadPool *tp);
131 static void threadpool_append_job (ThreadPool *tp, MonoObject *ar);
132 static void threadpool_append_jobs (ThreadPool *tp, MonoObject **jobs, gint njobs);
133 static void threadpool_init (ThreadPool *tp, int min_threads, int max_threads, void (*async_invoke) (gpointer));
134 static void threadpool_start_idle_threads (ThreadPool *tp);
135 static void threadpool_kill_idle_threads (ThreadPool *tp);
136 static gboolean threadpool_start_thread (ThreadPool *tp);
137 static void threadpool_kill_thread (ThreadPool *tp);
138 static void monitor_thread (gpointer data);
139 static int get_event_from_state (MonoSocketAsyncResult *state);
141 static MonoClass *async_call_klass;
142 static MonoClass *socket_async_call_klass;
143 static MonoClass *process_async_call_klass;
145 static GPtrArray *threads;
146 mono_mutex_t threads_lock;
147 static GPtrArray *wsqs;
148 mono_mutex_t wsqs_lock;
149 static gboolean suspended;
151 static volatile gint32 monitor_njobs = 0;
152 static volatile gint32 monitor_state;
153 static MonoSemType monitor_sem;
154 static MonoInternalThread *monitor_internal_thread;
157 static MonoThreadPoolFunc tp_start_func;
158 static MonoThreadPoolFunc tp_finish_func;
159 static gpointer tp_hooks_user_data;
160 static MonoThreadPoolItemFunc tp_item_begin_func;
161 static MonoThreadPoolItemFunc tp_item_end_func;
162 static gpointer tp_item_user_data;
172 AIO_OP_RECV_JUST_CALLBACK,
173 AIO_OP_SEND_JUST_CALLBACK,
177 AIO_OP_ACCEPTRECEIVE,
178 AIO_OP_RECEIVE_BUFFERS,
183 // #include <mono/metadata/tpool-poll.c>
185 #include <mono/metadata/tpool-epoll.c>
186 #elif defined(USE_KQUEUE_FOR_THREADPOOL)
187 #include <mono/metadata/tpool-kqueue.c>
190 * Functions to check whenever a class is given system class. We need to cache things in MonoDomain since some of the
191 * assemblies can be unloaded.
195 is_system_type (MonoDomain *domain, MonoClass *klass)
197 if (domain->system_image == NULL)
198 domain->system_image = mono_image_loaded ("System");
200 return klass->image == domain->system_image;
204 is_corlib_type (MonoDomain *domain, MonoClass *klass)
206 return klass->image == mono_defaults.corlib;
210 * Note that we call it is_socket_type() where 'socket' refers to the image
211 * that contains the System.Net.Sockets.Socket type.
214 is_socket_type (MonoDomain *domain, MonoClass *klass)
216 return is_system_type (domain, klass);
219 #define check_type_cached(domain, ASSEMBLY, _class, _namespace, _name, loc) do { \
221 return *loc == _class; \
222 if (is_##ASSEMBLY##_type (domain, _class) && !strcmp (_name, _class->name) && !strcmp (_namespace, _class->name_space)) { \
229 #define check_corlib_type_cached(domain, _class, _namespace, _name, loc) check_type_cached (domain, corlib, _class, _namespace, _name, loc)
231 #define check_socket_type_cached(domain, _class, _namespace, _name, loc) check_type_cached (domain, socket, _class, _namespace, _name, loc)
233 #define check_system_type_cached(domain, _class, _namespace, _name, loc) check_type_cached (domain, system, _class, _namespace, _name, loc)
236 is_corlib_asyncresult (MonoDomain *domain, MonoClass *klass)
238 check_corlib_type_cached (domain, klass, "System.Runtime.Remoting.Messaging", "AsyncResult", &domain->corlib_asyncresult_class);
242 is_socket (MonoDomain *domain, MonoClass *klass)
244 check_socket_type_cached (domain, klass, "System.Net.Sockets", "Socket", &domain->socket_class);
248 is_socketasyncresult (MonoDomain *domain, MonoClass *klass)
250 return (klass->nested_in &&
251 is_socket (domain, klass->nested_in) &&
252 !strcmp (klass->name, "SocketAsyncResult"));
256 is_socketasynccall (MonoDomain *domain, MonoClass *klass)
258 return (klass->nested_in &&
259 is_socket (domain, klass->nested_in) &&
260 !strcmp (klass->name, "SocketAsyncCall"));
264 is_appdomainunloaded_exception (MonoDomain *domain, MonoClass *klass)
266 check_corlib_type_cached (domain, klass, "System", "AppDomainUnloadedException", &domain->ad_unloaded_ex_class);
270 is_sd_process (MonoDomain *domain, MonoClass *klass)
272 check_system_type_cached (domain, klass, "System.Diagnostics", "Process", &domain->process_class);
276 is_sdp_asyncreadhandler (MonoDomain *domain, MonoClass *klass)
279 return (klass->nested_in &&
280 is_sd_process (domain, klass->nested_in) &&
281 !strcmp (klass->name, "AsyncReadHandler"));
285 #ifdef DISABLE_SOCKETS
288 socket_io_cleanup (SocketIOData *data)
293 get_event_from_state (MonoSocketAsyncResult *state)
295 g_assert_not_reached ();
300 get_events_from_list (MonoMList *list)
308 socket_io_cleanup (SocketIOData *data)
310 mono_mutex_lock (&data->io_lock);
311 if (data->inited != 2) {
312 mono_mutex_unlock (&data->io_lock);
316 data->shutdown (data->event_data);
317 mono_mutex_unlock (&data->io_lock);
321 get_event_from_state (MonoSocketAsyncResult *state)
323 switch (state->operation) {
326 case AIO_OP_RECV_JUST_CALLBACK:
327 case AIO_OP_RECEIVEFROM:
328 case AIO_OP_READPIPE:
329 case AIO_OP_ACCEPTRECEIVE:
330 case AIO_OP_RECEIVE_BUFFERS:
333 case AIO_OP_SEND_JUST_CALLBACK:
336 case AIO_OP_SEND_BUFFERS:
337 case AIO_OP_DISCONNECT:
339 default: /* Should never happen */
340 g_message ("get_event_from_state: unknown value in switch!!!");
346 get_events_from_list (MonoMList *list)
348 MonoSocketAsyncResult *state;
351 while (list && (state = (MonoSocketAsyncResult *)mono_mlist_get_data (list))) {
352 events |= get_event_from_state (state);
353 list = mono_mlist_next (list);
359 #define ICALL_RECV(x) ves_icall_System_Net_Sockets_Socket_Receive_internal (\
360 (SOCKET)(gssize)x->handle, x->buffer, x->offset, x->size,\
361 x->socket_flags, &x->error);
363 #define ICALL_SEND(x) ves_icall_System_Net_Sockets_Socket_Send_internal (\
364 (SOCKET)(gssize)x->handle, x->buffer, x->offset, x->size,\
365 x->socket_flags, &x->error);
367 #endif /* !DISABLE_SOCKETS */
370 threadpool_jobs_inc (MonoObject *obj)
373 InterlockedIncrement (&obj->vtable->domain->threadpool_jobs);
377 threadpool_jobs_dec (MonoObject *obj)
385 domain = obj->vtable->domain;
386 remaining_jobs = InterlockedDecrement (&domain->threadpool_jobs);
387 if (remaining_jobs == 0 && domain->cleanup_semaphore) {
388 ReleaseSemaphore (domain->cleanup_semaphore, 1, NULL);
395 get_io_event (MonoMList **list, gint event)
405 state = mono_mlist_get_data (current);
406 if (get_event_from_state ((MonoSocketAsyncResult *) state) == event)
411 current = mono_mlist_next (current);
416 mono_mlist_set_next (prev, mono_mlist_next (current));
418 *list = mono_mlist_next (*list);
426 * select/poll wake up when a socket is closed, but epoll just removes
427 * the socket from its internal list without notification.
430 mono_thread_pool_remove_socket (int sock)
433 MonoSocketAsyncResult *state;
436 if (socket_io_data.inited == 0)
439 mono_mutex_lock (&socket_io_data.io_lock);
440 if (socket_io_data.sock_to_state == NULL) {
441 mono_mutex_unlock (&socket_io_data.io_lock);
444 list = mono_g_hash_table_lookup (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
446 mono_g_hash_table_remove (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
447 mono_mutex_unlock (&socket_io_data.io_lock);
450 state = (MonoSocketAsyncResult *) mono_mlist_get_data (list);
451 if (state->operation == AIO_OP_RECEIVE)
452 state->operation = AIO_OP_RECV_JUST_CALLBACK;
453 else if (state->operation == AIO_OP_SEND)
454 state->operation = AIO_OP_SEND_JUST_CALLBACK;
456 ares = get_io_event (&list, MONO_POLLIN);
457 threadpool_append_job (&async_io_tp, ares);
459 ares = get_io_event (&list, MONO_POLLOUT);
460 threadpool_append_job (&async_io_tp, ares);
466 init_event_system (SocketIOData *data)
469 if (data->event_system == EPOLL_BACKEND) {
470 data->event_data = tp_epoll_init (data);
471 if (data->event_data == NULL) {
472 if (g_getenv ("MONO_DEBUG"))
473 g_message ("Falling back to poll()");
474 data->event_system = POLL_BACKEND;
477 #elif defined(USE_KQUEUE_FOR_THREADPOOL)
478 if (data->event_system == KQUEUE_BACKEND)
479 data->event_data = tp_kqueue_init (data);
481 if (data->event_system == POLL_BACKEND)
482 data->event_data = tp_poll_init (data);
486 socket_io_init (SocketIOData *data)
490 if (data->inited >= 2) // 2 -> initialized, 3-> cleaned up
493 inited = InterlockedCompareExchange (&data->inited, 1, 0);
496 if (data->inited >= 2)
502 mono_mutex_lock (&data->io_lock);
503 data->sock_to_state = mono_g_hash_table_new_type (g_direct_hash, g_direct_equal, MONO_HASH_VALUE_GC);
505 data->event_system = EPOLL_BACKEND;
506 #elif defined(USE_KQUEUE_FOR_THREADPOOL)
507 data->event_system = KQUEUE_BACKEND;
509 data->event_system = POLL_BACKEND;
511 if (g_getenv ("MONO_DISABLE_AIO") != NULL)
512 data->event_system = POLL_BACKEND;
514 init_event_system (data);
515 mono_thread_create_internal (mono_get_root_domain (), data->wait, data, TRUE, SMALL_STACK);
516 mono_mutex_unlock (&data->io_lock);
518 threadpool_start_thread (&async_io_tp);
522 socket_io_add (MonoAsyncResult *ares, MonoSocketAsyncResult *state)
525 SocketIOData *data = &socket_io_data;
530 socket_io_init (&socket_io_data);
531 if (mono_runtime_is_shutting_down () || data->inited == 3 || data->sock_to_state == NULL)
533 if (async_tp.pool_status == 2)
536 MONO_OBJECT_SETREF (state, ares, ares);
538 fd = GPOINTER_TO_INT (state->handle);
539 mono_mutex_lock (&data->io_lock);
540 if (data->sock_to_state == NULL) {
541 mono_mutex_unlock (&data->io_lock);
544 list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
546 list = mono_mlist_alloc ((MonoObject*)state);
549 list = mono_mlist_append (list, (MonoObject*)state);
553 mono_g_hash_table_replace (data->sock_to_state, state->handle, list);
554 ievt = get_events_from_list (list);
555 /* The modify function leaves the io_lock critical section. */
556 data->modify (data, fd, state->operation, ievt, is_new);
559 #ifndef DISABLE_SOCKETS
561 socket_io_filter (MonoObject *target, MonoObject *state)
564 MonoSocketAsyncResult *sock_res;
568 if (target == NULL || state == NULL)
571 domain = target->vtable->domain;
572 klass = target->vtable->klass;
573 if (socket_async_call_klass == NULL && is_socketasynccall (domain, klass))
574 socket_async_call_klass = klass;
576 if (process_async_call_klass == NULL && is_sdp_asyncreadhandler (domain, klass))
577 process_async_call_klass = klass;
579 if (klass != socket_async_call_klass && klass != process_async_call_klass)
582 sock_res = (MonoSocketAsyncResult *) state;
583 op = sock_res->operation;
584 if (op < AIO_OP_FIRST || op >= AIO_OP_LAST)
589 #endif /* !DISABLE_SOCKETS */
591 /* Returns the exception thrown when invoking, if any */
593 mono_async_invoke (ThreadPool *tp, MonoAsyncResult *ares)
595 ASyncCall *ac = (ASyncCall *)ares->object_data;
596 MonoObject *res, *exc = NULL;
597 MonoArray *out_args = NULL;
598 HANDLE wait_event = NULL;
599 MonoInternalThread *thread = mono_thread_internal_current ();
601 if (ares->execution_context) {
602 /* use captured ExecutionContext (if available) */
603 MONO_OBJECT_SETREF (ares, original_context, mono_thread_get_execution_context ());
604 mono_thread_set_execution_context (ares->execution_context);
606 ares->original_context = NULL;
610 /* Fast path from ThreadPool.*QueueUserWorkItem */
611 void *pa = ares->async_state;
612 /* The debugger needs this */
613 thread->async_invoke_method = ((MonoDelegate*)ares->async_delegate)->method;
614 res = mono_runtime_delegate_invoke (ares->async_delegate, &pa, &exc);
615 thread->async_invoke_method = NULL;
617 MonoObject *cb_exc = NULL;
620 res = mono_message_invoke (ares->async_delegate, ac->msg, &exc, &out_args);
621 MONO_OBJECT_SETREF (ac, res, res);
622 MONO_OBJECT_SETREF (ac, msg->exc, exc);
623 MONO_OBJECT_SETREF (ac, out_args, out_args);
625 mono_monitor_enter ((MonoObject *) ares);
627 if (ares->handle != NULL)
628 wait_event = mono_wait_handle_get_handle ((MonoWaitHandle *) ares->handle);
629 mono_monitor_exit ((MonoObject *) ares);
630 /* notify listeners */
631 if (wait_event != NULL)
632 SetEvent (wait_event);
634 /* call async callback if cb_method != null*/
635 if (ac != NULL && ac->cb_method) {
638 thread->async_invoke_method = ac->cb_method;
639 mono_runtime_invoke (ac->cb_method, ac->cb_target, pa, &cb_exc);
640 thread->async_invoke_method = NULL;
647 /* restore original thread execution context if flow isn't suppressed, i.e. non null */
648 if (ares->original_context) {
649 mono_thread_set_execution_context (ares->original_context);
650 ares->original_context = NULL;
654 InterlockedDecrement (&tp->njobs);
657 InterlockedIncrement (&tp->nexecuted);
659 if (InterlockedDecrement (&monitor_njobs) == 0)
660 monitor_state = MONITOR_STATE_FALLING_ASLEEP;
666 threadpool_start_idle_threads (ThreadPool *tp)
671 stack_size = (!tp->is_io) ? 0 : SMALL_STACK;
675 if (n >= tp->min_threads)
677 if (InterlockedCompareExchange (&tp->nthreads, n + 1, n) == n)
680 #ifndef DISABLE_PERFCOUNTERS
681 mono_perfcounter_update_value (tp->pc_nthreads, TRUE, 1);
683 mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, tp, TRUE, stack_size);
689 threadpool_init (ThreadPool *tp, int min_threads, int max_threads, void (*async_invoke) (gpointer))
691 memset (tp, 0, sizeof (ThreadPool));
692 tp->min_threads = min_threads;
693 tp->max_threads = max_threads;
694 tp->async_invoke = async_invoke;
695 tp->queue = mono_cq_create ();
696 MONO_SEM_INIT (&tp->new_job, 0);
699 #ifndef DISABLE_PERFCOUNTERS
701 init_perf_counter (const char *category, const char *counter)
703 MonoString *category_str;
704 MonoString *counter_str;
710 if (category == NULL || counter == NULL)
712 root = mono_get_root_domain ();
713 category_str = mono_string_new (root, category);
714 counter_str = mono_string_new (root, counter);
715 machine = mono_string_new (root, ".");
716 return mono_perfcounter_get_impl (category_str, counter_str, NULL, machine, &type, &custom);
722 print_pool_info (ThreadPool *tp)
725 // if (tp->tail - tp->head == 0)
728 g_print ("Pool status? %d\n", InterlockedCompareExchange (&tp->pool_status, 0, 0));
729 g_print ("Min. threads: %d\n", InterlockedCompareExchange (&tp->min_threads, 0, 0));
730 g_print ("Max. threads: %d\n", InterlockedCompareExchange (&tp->max_threads, 0, 0));
731 g_print ("nthreads: %d\n", InterlockedCompareExchange (&tp->nthreads, 0, 0));
732 g_print ("busy threads: %d\n", InterlockedCompareExchange (&tp->busy_threads, 0, 0));
733 g_print ("Waiting: %d\n", InterlockedCompareExchange (&tp->waiting, 0, 0));
734 g_print ("Queued: %d\n", (tp->tail - tp->head));
735 if (tp == &async_tp) {
737 mono_mutex_lock (&wsqs_lock);
738 for (i = 0; i < wsqs->len; i++) {
739 g_print ("\tWSQ %d: %d\n", i, mono_wsq_count (g_ptr_array_index (wsqs, i)));
741 mono_mutex_unlock (&wsqs_lock);
743 g_print ("\tSockets: %d\n", mono_g_hash_table_size (socket_io_data.sock_to_state));
745 g_print ("-------------\n");
749 signal_handler (int signo)
754 g_print ("\n-----Non-IO-----\n");
755 print_pool_info (tp);
757 g_print ("\n-----IO-----\n");
758 print_pool_info (tp);
763 #define SAMPLES_PERIOD 500
764 #define HISTORY_SIZE 10
765 /* number of iteration without any jobs
766 in the queue before going to sleep */
767 #define NUM_WAITING_ITERATIONS 10
777 * - 1 if the number of threads should increase
778 * - 0 if it should not change
779 * - -1 if it should decrease
780 * - -2 in case of error
783 monitor_heuristic (gint16 *current, gint16 *history_size, SamplesHistory *history, ThreadPool *tp)
786 gint8 decision G_GNUC_UNUSED;
788 gboolean all_waitsleepjoin;
789 MonoInternalThread *thread;
792 * The following heuristic tries to approach the optimal number of threads to maximize jobs throughput. To
793 * achieve this, it simply stores the number of jobs executed (nexecuted), the number of Threads (nthreads)
794 * and the decision (nthreads_diff) for the past HISTORY_SIZE periods of time, each period being of
795 * duration SAMPLES_PERIOD ms. This history gives us an insight into what happened, and to see if we should
796 * increase or reduce the number of threads by comparing the last period (current) to the best one.
798 * The algorithm can be describe as following :
799 * - if we have a better throughput than the best period : we should either increase the number of threads
800 * in case we already have more threads, either reduce the number of threads if we have less threads; this
801 * is equivalent to move away from the number of threads of the best period, because we are currently better
802 * - if we have a worse throughput than the best period : we should either decrease the number of threads if
803 * we have more threads, either increase the number of threads if we have less threads; this is equivalent
804 * to get closer to the number of threads of the best period, because we are currently worse
807 *history_size = MIN (*history_size + 1, HISTORY_SIZE);
808 cur = *current = (*current + 1) % *history_size;
810 history [cur].nthreads = tp->nthreads;
811 history [cur].nexecuted = InterlockedExchange (&tp->nexecuted, 0);
814 /* if we have waiting thread in the pool, then do not create a new one */
815 history [cur].nthreads_diff = tp->waiting > 1 ? -1 : 0;
817 } else if (tp->nthreads < tp->min_threads) {
818 history [cur].nthreads_diff = 1;
820 } else if (*history_size <= 1) {
821 /* first iteration, let's add a thread by default */
822 history [cur].nthreads_diff = 1;
825 mono_mutex_lock (&threads_lock);
826 if (threads == NULL) {
827 mono_mutex_unlock (&threads_lock);
830 all_waitsleepjoin = TRUE;
831 for (i = 0; i < threads->len; ++i) {
832 thread = g_ptr_array_index (threads, i);
833 if (!(thread->state & ThreadState_WaitSleepJoin)) {
834 all_waitsleepjoin = FALSE;
838 mono_mutex_unlock (&threads_lock);
840 if (all_waitsleepjoin) {
841 /* we might be in a condition of starvation/deadlock with tasks waiting for each others */
842 history [cur].nthreads_diff = 1;
845 max = cur == 0 ? 1 : 0;
846 for (i = 0; i < *history_size; i++) {
849 if (history [i].nexecuted > history [max].nexecuted)
853 if (history [cur].nexecuted >= history [max].nexecuted) {
854 /* we improved the situation, let's continue ! */
855 history [cur].nthreads_diff = history [cur].nthreads >= history [max].nthreads ? 1 : -1;
858 /* we made it worse, let's return to previous situation */
859 history [cur].nthreads_diff = history [cur].nthreads >= history [max].nthreads ? -1 : 1;
866 printf ("monitor_thread: decision: %1d, history [current]: {nexecuted: %5d, nthreads: %3d, waiting: %2d, nthreads_diff: %2d}, history [max]: {nexecuted: %5d, nthreads: %3d}\n",
867 decision, history [cur].nexecuted, history [cur].nthreads, tp->waiting, history [cur].nthreads_diff, history [max].nexecuted, history [max].nthreads);
870 return history [cur].nthreads_diff;
874 monitor_thread (gpointer unused)
876 ThreadPool *pools [2];
877 MonoInternalThread *thread;
881 gint8 num_waiting_iterations = 0;
883 gint16 history_size = 0, current = -1;
884 SamplesHistory *history = malloc (sizeof (SamplesHistory) * HISTORY_SIZE);
886 pools [0] = &async_tp;
887 pools [1] = &async_io_tp;
888 thread = mono_thread_internal_current ();
889 ves_icall_System_Threading_Thread_SetName_internal (thread, mono_string_new (mono_domain_get (), "Threadpool monitor"));
892 i = 10; //number of spurious awakes we tolerate before doing a round of rebalancing.
895 ts = mono_msec_ticks ();
896 if (SleepEx (ms, TRUE) == 0)
898 ms -= (mono_msec_ticks () - ts);
899 if (mono_runtime_is_shutting_down ())
901 if (THREAD_WANTS_A_BREAK (thread))
902 mono_thread_interruption_checkpoint ();
903 } while (ms > 0 && i--);
905 if (mono_runtime_is_shutting_down ())
911 /* threadpool is cleaning up */
912 if (async_tp.pool_status == 2 || async_io_tp.pool_status == 2)
915 switch (monitor_state) {
916 case MONITOR_STATE_AWAKE:
917 num_waiting_iterations = 0;
919 case MONITOR_STATE_FALLING_ASLEEP:
920 if (++num_waiting_iterations == NUM_WAITING_ITERATIONS) {
921 if (monitor_state == MONITOR_STATE_FALLING_ASLEEP && InterlockedCompareExchange (&monitor_state, MONITOR_STATE_SLEEPING, MONITOR_STATE_FALLING_ASLEEP) == MONITOR_STATE_FALLING_ASLEEP) {
922 MONO_SEM_WAIT (&monitor_sem);
924 num_waiting_iterations = 0;
930 case MONITOR_STATE_SLEEPING:
931 g_assert_not_reached ();
934 for (i = 0; i < 2; i++) {
939 if (!tp->waiting && mono_cq_count (tp->queue) > 0)
940 threadpool_start_thread (tp);
942 gint8 nthreads_diff = monitor_heuristic (¤t, &history_size, history, tp);
944 if (nthreads_diff == 1)
945 threadpool_start_thread (tp);
946 else if (nthreads_diff == -1)
947 threadpool_kill_thread (tp);
954 mono_thread_pool_init_tls (void)
960 mono_thread_pool_init (void)
962 gint threads_per_cpu = 1;
964 gint cpu_count = mono_cpu_count ();
970 result = InterlockedCompareExchange (&tp_inited, 1, 0);
979 MONO_GC_REGISTER_ROOT_FIXED (socket_io_data.sock_to_state);
980 mono_mutex_init_recursive (&socket_io_data.io_lock);
981 if (g_getenv ("MONO_THREADS_PER_CPU") != NULL) {
982 threads_per_cpu = atoi (g_getenv ("MONO_THREADS_PER_CPU"));
983 if (threads_per_cpu < 1)
987 thread_count = MIN (cpu_count * threads_per_cpu, 100 * cpu_count);
988 threadpool_init (&async_tp, thread_count, MAX (100 * cpu_count, thread_count), async_invoke_thread);
989 threadpool_init (&async_io_tp, cpu_count * 2, cpu_count * 4, async_invoke_thread);
990 async_io_tp.is_io = TRUE;
992 async_call_klass = mono_class_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
993 g_assert (async_call_klass);
995 mono_mutex_init (&threads_lock);
996 threads = g_ptr_array_sized_new (thread_count);
999 mono_mutex_init_recursive (&wsqs_lock);
1000 wsqs = g_ptr_array_sized_new (MAX (100 * cpu_count, thread_count));
1002 #ifndef DISABLE_PERFCOUNTERS
1003 async_tp.pc_nitems = init_perf_counter ("Mono Threadpool", "Work Items Added");
1004 g_assert (async_tp.pc_nitems);
1006 async_io_tp.pc_nitems = init_perf_counter ("Mono Threadpool", "IO Work Items Added");
1007 g_assert (async_io_tp.pc_nitems);
1009 async_tp.pc_nthreads = init_perf_counter ("Mono Threadpool", "# of Threads");
1010 g_assert (async_tp.pc_nthreads);
1012 async_io_tp.pc_nthreads = init_perf_counter ("Mono Threadpool", "# of IO Threads");
1013 g_assert (async_io_tp.pc_nthreads);
1017 signal (SIGALRM, signal_handler);
1021 MONO_SEM_INIT (&monitor_sem, 0);
1022 monitor_state = MONITOR_STATE_AWAKE;
1026 static MonoAsyncResult *
1027 create_simple_asyncresult (MonoObject *target, MonoObject *state)
1029 MonoDomain *domain = mono_domain_get ();
1030 MonoAsyncResult *ares;
1032 /* Don't call mono_async_result_new() to avoid capturing the context */
1033 ares = (MonoAsyncResult *) mono_object_new (domain, mono_defaults.asyncresult_class);
1034 MONO_OBJECT_SETREF (ares, async_delegate, target);
1035 MONO_OBJECT_SETREF (ares, async_state, state);
1040 icall_append_io_job (MonoObject *target, MonoSocketAsyncResult *state)
1042 MonoAsyncResult *ares;
1044 ares = create_simple_asyncresult (target, (MonoObject *) state);
1045 socket_io_add (ares, state);
1049 mono_thread_pool_add (MonoObject *target, MonoMethodMessage *msg, MonoDelegate *async_callback,
1052 MonoDomain *domain = mono_domain_get ();
1053 MonoAsyncResult *ares;
1056 ac = (ASyncCall*)mono_object_new (domain, async_call_klass);
1057 MONO_OBJECT_SETREF (ac, msg, msg);
1058 MONO_OBJECT_SETREF (ac, state, state);
1060 if (async_callback) {
1061 ac->cb_method = mono_get_delegate_invoke (((MonoObject *)async_callback)->vtable->klass);
1062 MONO_OBJECT_SETREF (ac, cb_target, async_callback);
1065 ares = mono_async_result_new (domain, NULL, ac->state, NULL, (MonoObject*)ac);
1066 MONO_OBJECT_SETREF (ares, async_delegate, target);
1068 #ifndef DISABLE_SOCKETS
1069 if (socket_io_filter (target, state)) {
1070 socket_io_add (ares, (MonoSocketAsyncResult *) state);
1074 threadpool_append_job (&async_tp, (MonoObject *) ares);
1079 mono_thread_pool_finish (MonoAsyncResult *ares, MonoArray **out_args, MonoObject **exc)
1087 /* check if already finished */
1088 mono_monitor_enter ((MonoObject *) ares);
1090 if (ares->endinvoke_called) {
1091 *exc = (MonoObject *) mono_get_exception_invalid_operation (NULL);
1092 mono_monitor_exit ((MonoObject *) ares);
1096 ares->endinvoke_called = 1;
1097 /* wait until we are really finished */
1098 if (!ares->completed) {
1099 if (ares->handle == NULL) {
1100 wait_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1101 g_assert(wait_event != 0);
1102 MONO_OBJECT_SETREF (ares, handle, (MonoObject *) mono_wait_handle_new (mono_object_domain (ares), wait_event));
1104 wait_event = mono_wait_handle_get_handle ((MonoWaitHandle *) ares->handle);
1106 mono_monitor_exit ((MonoObject *) ares);
1107 WaitForSingleObjectEx (wait_event, INFINITE, TRUE);
1109 mono_monitor_exit ((MonoObject *) ares);
1112 ac = (ASyncCall *) ares->object_data;
1113 g_assert (ac != NULL);
1114 *exc = ac->msg->exc; /* FIXME: GC add write barrier */
1115 *out_args = ac->out_args;
1121 threadpool_kill_idle_threads (ThreadPool *tp)
1125 n = (gint) InterlockedCompareExchange (&tp->max_threads, 0, -1);
1128 MONO_SEM_POST (&tp->new_job);
1133 mono_thread_pool_cleanup (void)
1135 if (InterlockedExchange (&async_io_tp.pool_status, 2) == 1) {
1136 socket_io_cleanup (&socket_io_data); /* Empty when DISABLE_SOCKETS is defined */
1137 threadpool_kill_idle_threads (&async_io_tp);
1140 if (async_io_tp.queue != NULL) {
1141 MONO_SEM_DESTROY (&async_io_tp.new_job);
1142 threadpool_free_queue (&async_io_tp);
1146 if (InterlockedExchange (&async_tp.pool_status, 2) == 1) {
1147 threadpool_kill_idle_threads (&async_tp);
1148 threadpool_free_queue (&async_tp);
1152 mono_mutex_lock (&threads_lock);
1154 g_ptr_array_free (threads, FALSE);
1156 mono_mutex_unlock (&threads_lock);
1160 mono_mutex_lock (&wsqs_lock);
1161 mono_wsq_cleanup ();
1163 g_ptr_array_free (wsqs, TRUE);
1165 mono_mutex_unlock (&wsqs_lock);
1166 MONO_SEM_DESTROY (&async_tp.new_job);
1169 MONO_SEM_DESTROY (&monitor_sem);
1173 threadpool_start_thread (ThreadPool *tp)
1177 MonoInternalThread *thread;
1179 stack_size = (!tp->is_io) ? 0 : SMALL_STACK;
1180 while (!mono_runtime_is_shutting_down () && (n = tp->nthreads) < tp->max_threads) {
1181 if (InterlockedCompareExchange (&tp->nthreads, n + 1, n) == n) {
1182 #ifndef DISABLE_PERFCOUNTERS
1183 mono_perfcounter_update_value (tp->pc_nthreads, TRUE, 1);
1186 thread = mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, tp, TRUE, stack_size);
1188 mono_mutex_lock (&threads_lock);
1189 thread = mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, tp, TRUE, stack_size);
1190 g_assert (threads != NULL);
1191 g_ptr_array_add (threads, thread);
1192 mono_mutex_unlock (&threads_lock);
1202 pulse_on_new_job (ThreadPool *tp)
1205 MONO_SEM_POST (&tp->new_job);
1209 threadpool_kill_thread (ThreadPool *tp)
1211 if (tp->destroy_thread == 0 && InterlockedCompareExchange (&tp->destroy_thread, 1, 0) == 0)
1212 pulse_on_new_job (tp);
1216 icall_append_job (MonoObject *ar)
1218 threadpool_append_jobs (&async_tp, &ar, 1);
1222 threadpool_append_job (ThreadPool *tp, MonoObject *ar)
1224 threadpool_append_jobs (tp, &ar, 1);
1228 threadpool_append_async_io_jobs (MonoObject **jobs, gint njobs)
1230 threadpool_append_jobs (&async_io_tp, jobs, njobs);
1234 threadpool_append_jobs (ThreadPool *tp, MonoObject **jobs, gint njobs)
1239 if (mono_runtime_is_shutting_down ())
1242 if (tp->pool_status == 0 && InterlockedCompareExchange (&tp->pool_status, 1, 0) == 0) {
1244 monitor_internal_thread = mono_thread_create_internal (mono_get_root_domain (), monitor_thread, NULL, TRUE, SMALL_STACK);
1245 monitor_internal_thread->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
1246 threadpool_start_thread (tp);
1248 /* Create on demand up to min_threads to avoid startup penalty for apps that don't use
1249 * the threadpool that much
1251 if (mono_config_is_server_mode ()) {
1252 mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, tp, TRUE, SMALL_STACK);
1256 InterlockedAdd (&monitor_njobs, njobs);
1258 if (monitor_state == MONITOR_STATE_SLEEPING && InterlockedCompareExchange (&monitor_state, MONITOR_STATE_AWAKE, MONITOR_STATE_SLEEPING) == MONITOR_STATE_SLEEPING)
1259 MONO_SEM_POST (&monitor_sem);
1261 if (monitor_state == MONITOR_STATE_FALLING_ASLEEP)
1262 InterlockedCompareExchange (&monitor_state, MONITOR_STATE_AWAKE, MONITOR_STATE_FALLING_ASLEEP);
1264 for (i = 0; i < njobs; i++) {
1266 if (ar == NULL || mono_domain_is_unloading (ar->vtable->domain))
1267 continue; /* Might happen when cleaning domain jobs */
1268 threadpool_jobs_inc (ar);
1269 #ifndef DISABLE_PERFCOUNTERS
1270 mono_perfcounter_update_value (tp->pc_nitems, TRUE, 1);
1272 if (!tp->is_io && mono_wsq_local_push (ar))
1275 mono_cq_enqueue (tp->queue, ar);
1279 InterlockedAdd (&tp->njobs, njobs);
1282 for (i = 0; tp->waiting > 0 && i < MIN(njobs, tp->max_threads); i++)
1283 pulse_on_new_job (tp);
1287 threadpool_clear_queue (ThreadPool *tp, MonoDomain *domain)
1290 MonoMList *other = NULL;
1291 MonoCQ *queue = tp->queue;
1296 while (mono_cq_dequeue (queue, &obj)) {
1299 if (obj->vtable->domain != domain)
1300 other = mono_mlist_prepend (other, obj);
1301 threadpool_jobs_dec (obj);
1304 if (mono_runtime_is_shutting_down ())
1308 threadpool_append_job (tp, (MonoObject *) mono_mlist_get_data (other));
1309 other = mono_mlist_next (other);
1314 remove_sockstate_for_domain (gpointer key, gpointer value, gpointer user_data)
1316 MonoMList *list = value;
1317 gboolean remove = FALSE;
1319 MonoObject *data = mono_mlist_get_data (list);
1320 if (mono_object_domain (data) == user_data) {
1322 mono_mlist_set_data (list, NULL);
1324 list = mono_mlist_next (list);
1326 //FIXME is there some sort of additional unregistration we need to perform here?
1331 * Clean up the threadpool of all domain jobs.
1332 * Can only be called as part of the domain unloading process as
1333 * it will wait for all jobs to be visible to the interruption code.
1336 mono_thread_pool_remove_domain_jobs (MonoDomain *domain, int timeout)
1340 guint32 start_time = 0;
1342 g_assert (domain->state == MONO_APPDOMAIN_UNLOADING);
1344 threadpool_clear_queue (&async_tp, domain);
1345 threadpool_clear_queue (&async_io_tp, domain);
1347 mono_mutex_lock (&socket_io_data.io_lock);
1348 if (socket_io_data.sock_to_state)
1349 mono_g_hash_table_foreach_remove (socket_io_data.sock_to_state, remove_sockstate_for_domain, domain);
1351 mono_mutex_unlock (&socket_io_data.io_lock);
1354 * There might be some threads out that could be about to execute stuff from the given domain.
1355 * We avoid that by setting up a semaphore to be pulsed by the thread that reaches zero.
1357 sem_handle = CreateSemaphore (NULL, 0, 1, NULL);
1359 domain->cleanup_semaphore = sem_handle;
1361 * The memory barrier here is required to have global ordering between assigning to cleanup_semaphone
1362 * and reading threadpool_jobs.
1363 * Otherwise this thread could read a stale version of threadpool_jobs and wait forever.
1365 mono_memory_write_barrier ();
1367 if (domain->threadpool_jobs && timeout != -1)
1368 start_time = mono_msec_ticks ();
1369 while (domain->threadpool_jobs) {
1370 WaitForSingleObject (sem_handle, timeout);
1371 if (timeout != -1 && (mono_msec_ticks () - start_time) > timeout) {
1377 domain->cleanup_semaphore = NULL;
1378 CloseHandle (sem_handle);
1383 threadpool_free_queue (ThreadPool *tp)
1385 mono_cq_destroy (tp->queue);
1390 mono_thread_pool_is_queue_array (MonoArray *o)
1392 // gpointer obj = o;
1394 // FIXME: need some fix in sgen code.
1404 mono_mutex_lock (&wsqs_lock);
1405 wsq = mono_wsq_create ();
1407 mono_mutex_unlock (&wsqs_lock);
1410 for (i = 0; i < wsqs->len; i++) {
1411 if (g_ptr_array_index (wsqs, i) == NULL) {
1412 wsqs->pdata [i] = wsq;
1413 mono_mutex_unlock (&wsqs_lock);
1417 g_ptr_array_add (wsqs, wsq);
1418 mono_mutex_unlock (&wsqs_lock);
1423 remove_wsq (MonoWSQ *wsq)
1430 mono_mutex_lock (&wsqs_lock);
1432 mono_mutex_unlock (&wsqs_lock);
1435 g_ptr_array_remove_fast (wsqs, wsq);
1438 * Only clean this up when shutting down, any other case will error out
1439 * if we're removing a queue that still has work items.
1441 if (mono_runtime_is_shutting_down ()) {
1442 while (mono_wsq_local_pop (&data)) {
1443 threadpool_jobs_dec (data);
1447 mono_wsq_destroy (wsq);
1448 mono_mutex_unlock (&wsqs_lock);
1452 try_steal (MonoWSQ *local_wsq, gpointer *data, gboolean retry)
1457 if (wsqs == NULL || data == NULL || *data != NULL)
1462 if (mono_runtime_is_shutting_down ())
1465 mono_mutex_lock (&wsqs_lock);
1466 for (i = 0; wsqs != NULL && i < wsqs->len; i++) {
1469 wsq = wsqs->pdata [i];
1470 if (wsq == local_wsq || mono_wsq_count (wsq) == 0)
1472 mono_wsq_try_steal (wsqs->pdata [i], data, ms);
1473 if (*data != NULL) {
1474 mono_mutex_unlock (&wsqs_lock);
1478 mono_mutex_unlock (&wsqs_lock);
1480 } while (retry && ms < 11);
1484 dequeue_or_steal (ThreadPool *tp, gpointer *data, MonoWSQ *local_wsq)
1486 MonoCQ *queue = tp->queue;
1487 if (mono_runtime_is_shutting_down () || !queue)
1489 mono_cq_dequeue (queue, (MonoObject **) data);
1490 if (!tp->is_io && !*data)
1491 try_steal (local_wsq, data, FALSE);
1492 return (*data != NULL);
1496 should_i_die (ThreadPool *tp)
1498 gboolean result = FALSE;
1499 if (tp->destroy_thread == 1 && InterlockedCompareExchange (&tp->destroy_thread, 0, 1) == 1)
1500 result = (tp->nthreads > tp->min_threads);
1505 set_tp_thread_info (ThreadPool *tp)
1508 MonoInternalThread *thread = mono_thread_internal_current ();
1510 mono_profiler_thread_start (thread->tid);
1511 name = (tp->is_io) ? "IO Threadpool worker" : "Threadpool worker";
1512 mono_thread_set_name_internal (thread, mono_string_new (mono_domain_get (), name), FALSE);
1516 clear_thread_state (void)
1518 MonoInternalThread *thread = mono_thread_internal_current ();
1519 /* If the callee changes the background status, set it back to TRUE */
1520 mono_thread_clr_state (thread , ~ThreadState_Background);
1521 if (!mono_thread_test_state (thread , ThreadState_Background))
1522 ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
1526 check_for_interruption_critical (void)
1528 MonoInternalThread *thread;
1529 /*RULE NUMBER ONE OF SKIP_THREAD: NEVER POKE MANAGED STATE.*/
1530 mono_gc_set_skip_thread (FALSE);
1532 thread = mono_thread_internal_current ();
1533 if (THREAD_WANTS_A_BREAK (thread))
1534 mono_thread_interruption_checkpoint ();
1536 /*RULE NUMBER TWO OF SKIP_THREAD: READ RULE NUMBER ONE.*/
1537 mono_gc_set_skip_thread (TRUE);
1541 fire_profiler_thread_end (void)
1543 MonoInternalThread *thread = mono_thread_internal_current ();
1544 mono_profiler_thread_end (thread->tid);
1548 async_invoke_thread (gpointer data)
1560 set_tp_thread_info (tp);
1563 tp_start_func (tp_hooks_user_data);
1567 MonoAsyncResult *ar;
1569 gboolean is_io_task;
1574 ar = (MonoAsyncResult *) data;
1576 InterlockedIncrement (&tp->busy_threads);
1577 domain = ((MonoObject *)ar)->vtable->domain;
1578 #ifndef DISABLE_SOCKETS
1579 klass = ((MonoObject *) data)->vtable->klass;
1580 is_io_task = !is_corlib_asyncresult (domain, klass);
1583 MonoSocketAsyncResult *state = (MonoSocketAsyncResult *) data;
1584 is_socket = is_socketasyncresult (domain, klass);
1586 switch (state->operation) {
1587 case AIO_OP_RECEIVE:
1588 state->total = ICALL_RECV (state);
1591 state->total = ICALL_SEND (state);
1596 /* worker threads invokes methods in different domains,
1597 * so we need to set the right domain here */
1600 if (mono_domain_is_unloading (domain) || mono_runtime_is_shutting_down ()) {
1601 threadpool_jobs_dec ((MonoObject *)ar);
1604 InterlockedDecrement (&tp->busy_threads);
1606 mono_thread_push_appdomain_ref (domain);
1607 if (threadpool_jobs_dec ((MonoObject *)ar)) {
1610 mono_thread_pop_appdomain_ref ();
1611 InterlockedDecrement (&tp->busy_threads);
1615 if (mono_domain_set (domain, FALSE)) {
1618 if (tp_item_begin_func)
1619 tp_item_begin_func (tp_item_user_data);
1621 exc = mono_async_invoke (tp, ar);
1622 if (tp_item_end_func)
1623 tp_item_end_func (tp_item_user_data);
1625 mono_internal_thread_unhandled_exception (exc);
1626 if (is_socket && tp->is_io) {
1627 MonoSocketAsyncResult *state = (MonoSocketAsyncResult *) data;
1629 if (state->completed && state->callback) {
1630 MonoAsyncResult *cb_ares;
1631 cb_ares = create_simple_asyncresult ((MonoObject *) state->callback,
1632 (MonoObject *) state);
1633 icall_append_job ((MonoObject *) cb_ares);
1636 mono_domain_set (mono_get_root_domain (), TRUE);
1638 mono_thread_pop_appdomain_ref ();
1639 InterlockedDecrement (&tp->busy_threads);
1640 clear_thread_state ();
1646 must_die = should_i_die (tp);
1648 mono_wsq_suspend (wsq);
1650 if (tp->is_io || !mono_wsq_local_pop (&data))
1651 dequeue_or_steal (tp, &data, wsq);
1655 while (!must_die && !data && n_naps < 4) {
1658 InterlockedIncrement (&tp->waiting);
1660 // Another thread may have added a job into its wsq since the last call to dequeue_or_steal
1661 // Check all the queues again before entering the wait loop
1662 dequeue_or_steal (tp, &data, wsq);
1664 InterlockedDecrement (&tp->waiting);
1668 mono_gc_set_skip_thread (TRUE);
1670 #if defined(__OpenBSD__)
1671 while (mono_cq_count (tp->queue) == 0 && (res = mono_sem_wait (&tp->new_job, TRUE)) == -1) {// && errno == EINTR) {
1673 while (mono_cq_count (tp->queue) == 0 && (res = mono_sem_timedwait (&tp->new_job, 2000, TRUE)) == -1) {// && errno == EINTR) {
1675 if (mono_runtime_is_shutting_down ())
1677 check_for_interruption_critical ();
1679 InterlockedDecrement (&tp->waiting);
1681 mono_gc_set_skip_thread (FALSE);
1683 if (mono_runtime_is_shutting_down ())
1685 must_die = should_i_die (tp);
1686 dequeue_or_steal (tp, &data, wsq);
1690 if (!data && !tp->is_io && !mono_runtime_is_shutting_down ()) {
1691 mono_wsq_local_pop (&data);
1692 if (data && must_die) {
1693 InterlockedCompareExchange (&tp->destroy_thread, 1, 0);
1694 pulse_on_new_job (tp);
1703 down = mono_runtime_is_shutting_down ();
1704 if (!down && nt <= tp->min_threads)
1706 if (down || InterlockedCompareExchange (&tp->nthreads, nt - 1, nt) == nt) {
1707 #ifndef DISABLE_PERFCOUNTERS
1708 mono_perfcounter_update_value (tp->pc_nthreads, TRUE, -1);
1714 fire_profiler_thread_end ();
1717 tp_finish_func (tp_hooks_user_data);
1721 mono_mutex_lock (&threads_lock);
1723 g_ptr_array_remove_fast (threads, mono_thread_current ()->internal_thread);
1724 mono_mutex_unlock (&threads_lock);
1734 g_assert_not_reached ();
1738 ves_icall_System_Threading_ThreadPool_GetAvailableThreads (gint *workerThreads, gint *completionPortThreads)
1740 *workerThreads = async_tp.max_threads - async_tp.busy_threads;
1741 *completionPortThreads = async_io_tp.max_threads - async_io_tp.busy_threads;
1745 ves_icall_System_Threading_ThreadPool_GetMaxThreads (gint *workerThreads, gint *completionPortThreads)
1747 *workerThreads = async_tp.max_threads;
1748 *completionPortThreads = async_io_tp.max_threads;
1752 ves_icall_System_Threading_ThreadPool_GetMinThreads (gint *workerThreads, gint *completionPortThreads)
1754 *workerThreads = async_tp.min_threads;
1755 *completionPortThreads = async_io_tp.min_threads;
1759 ves_icall_System_Threading_ThreadPool_SetMinThreads (gint workerThreads, gint completionPortThreads)
1762 gint max_io_threads;
1764 max_threads = async_tp.max_threads;
1765 if (workerThreads <= 0 || workerThreads > max_threads)
1768 max_io_threads = async_io_tp.max_threads;
1769 if (completionPortThreads <= 0 || completionPortThreads > max_io_threads)
1772 InterlockedExchange (&async_tp.min_threads, workerThreads);
1773 InterlockedExchange (&async_io_tp.min_threads, completionPortThreads);
1774 if (workerThreads > async_tp.nthreads)
1775 mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, &async_tp, TRUE, SMALL_STACK);
1776 if (completionPortThreads > async_io_tp.nthreads)
1777 mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, &async_io_tp, TRUE, SMALL_STACK);
1782 ves_icall_System_Threading_ThreadPool_SetMaxThreads (gint workerThreads, gint completionPortThreads)
1785 gint min_io_threads;
1788 cpu_count = mono_cpu_count ();
1789 min_threads = async_tp.min_threads;
1790 if (workerThreads < min_threads || workerThreads < cpu_count)
1793 /* We don't really have the concept of completion ports. Do we care here? */
1794 min_io_threads = async_io_tp.min_threads;
1795 if (completionPortThreads < min_io_threads || completionPortThreads < cpu_count)
1798 InterlockedExchange (&async_tp.max_threads, workerThreads);
1799 InterlockedExchange (&async_io_tp.max_threads, completionPortThreads);
1804 * mono_install_threadpool_thread_hooks
1805 * @start_func: the function to be called right after a new threadpool thread is created. Can be NULL.
1806 * @finish_func: the function to be called right before a thredpool thread is exiting. Can be NULL.
1807 * @user_data: argument passed to @start_func and @finish_func.
1809 * @start_fun will be called right after a threadpool thread is created and @finish_func right before a threadpool thread exits.
1810 * The calls will be made from the thread itself.
1813 mono_install_threadpool_thread_hooks (MonoThreadPoolFunc start_func, MonoThreadPoolFunc finish_func, gpointer user_data)
1815 tp_start_func = start_func;
1816 tp_finish_func = finish_func;
1817 tp_hooks_user_data = user_data;
1821 * mono_install_threadpool_item_hooks
1822 * @begin_func: the function to be called before a threadpool work item processing starts.
1823 * @end_func: the function to be called after a threadpool work item is finished.
1824 * @user_data: argument passed to @begin_func and @end_func.
1826 * The calls will be made from the thread itself and from the same AppDomain
1827 * where the work item was executed.
1831 mono_install_threadpool_item_hooks (MonoThreadPoolItemFunc begin_func, MonoThreadPoolItemFunc end_func, gpointer user_data)
1833 tp_item_begin_func = begin_func;
1834 tp_item_end_func = end_func;
1835 tp_item_user_data = user_data;
1839 mono_internal_thread_unhandled_exception (MonoObject* exc)
1841 if (mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_CURRENT) {
1845 klass = exc->vtable->klass;
1846 unloaded = is_appdomainunloaded_exception (exc->vtable->domain, klass);
1847 if (!unloaded && klass != mono_defaults.threadabortexception_class) {
1848 mono_unhandled_exception (exc);
1849 if (mono_environment_exitcode_get () == 1)
1852 if (klass == mono_defaults.threadabortexception_class)
1853 mono_thread_internal_reset_abort (mono_thread_internal_current ());
1858 * Suspend creation of new threads.
1861 mono_thread_pool_suspend (void)
1867 * Resume creation of new threads.
1870 mono_thread_pool_resume (void)