Merge pull request #1699 from ludovic-henry/threadpool-managed
[mono.git] / mono / metadata / threadpool.c
1 /*
2  * threadpool.c: global thread pool
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7  *
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)
11  */
12
13 #include <config.h>
14 #include <glib.h>
15
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/metadata/object-internals.h>
30 #include <mono/io-layer/io-layer.h>
31 #include <mono/utils/mono-time.h>
32 #include <mono/utils/mono-proclib.h>
33 #include <mono/utils/mono-semaphore.h>
34 #include <mono/utils/atomic.h>
35 #include <errno.h>
36 #ifdef HAVE_SYS_TIME_H
37 #include <sys/time.h>
38 #endif
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #include <string.h>
45 #include <math.h>
46 #ifdef HAVE_SYS_SOCKET_H
47 #include <sys/socket.h>
48 #endif
49 #include <mono/utils/mono-poll.h>
50 #ifdef HAVE_EPOLL
51 #include <sys/epoll.h>
52 #endif
53 #ifdef HAVE_KQUEUE
54 #include <sys/event.h>
55 #endif
56
57
58 #ifndef DISABLE_SOCKETS
59 #include "mono/io-layer/socket-wrappers.h"
60 #endif
61
62 #include "threadpool.h"
63 #include "threadpool-ms.h"
64 #include "threadpool-ms-io.h"
65
66 static gboolean
67 use_ms_threadpool (void)
68 {
69         static gboolean use_ms_tp = -1;
70         const gchar *mono_threadpool_env;
71         if (use_ms_tp != -1)
72                 return use_ms_tp;
73         else if (!(mono_threadpool_env = g_getenv ("MONO_THREADPOOL")))
74                 return use_ms_tp = FALSE;
75         else if (strcmp (mono_threadpool_env, "microsoft") == 0)
76                 return use_ms_tp = TRUE;
77         else
78                 return use_ms_tp = FALSE;
79 }
80
81 #define THREAD_WANTS_A_BREAK(t) ((t->state & (ThreadState_StopRequested | \
82                                                 ThreadState_SuspendRequested)) != 0)
83
84 /* DEBUG: prints tp data every 2s */
85 #undef DEBUG 
86
87 /* mono_thread_pool_init called */
88 static volatile int tp_inited;
89
90 enum {
91         POLL_BACKEND,
92         EPOLL_BACKEND,
93         KQUEUE_BACKEND
94 };
95
96 enum {
97         MONITOR_STATE_AWAKE,
98         MONITOR_STATE_FALLING_ASLEEP,
99         MONITOR_STATE_SLEEPING
100 };
101
102 static SocketIOData socket_io_data;
103
104 typedef struct {
105         MonoSemType lock;
106         MonoCQ *queue; /* GC root */
107         MonoSemType new_job;
108         volatile gint waiting; /* threads waiting for a work item */
109
110         /**/
111         volatile gint pool_status; /* 0 -> not initialized, 1 -> initialized, 2 -> cleaning up */
112         /* min, max, n and busy -> Interlocked */
113         volatile gint min_threads;
114         volatile gint max_threads;
115         volatile gint nthreads;
116         volatile gint busy_threads;
117
118         void (*async_invoke) (gpointer data);
119         void *pc_nitems; /* Performance counter for total number of items in added */
120         void *pc_nthreads; /* Performance counter for total number of active threads */
121         /**/
122         volatile gint destroy_thread;
123 #if DEBUG
124         volatile gint32 njobs;
125 #endif
126         volatile gint32 nexecuted;
127         gboolean is_io;
128 } ThreadPool;
129
130 static ThreadPool async_tp;
131 static ThreadPool async_io_tp;
132
133 static void async_invoke_thread (gpointer data);
134 static MonoObject *mono_async_invoke (ThreadPool *tp, MonoAsyncResult *ares);
135 static void threadpool_free_queue (ThreadPool *tp);
136 static void threadpool_append_job (ThreadPool *tp, MonoObject *ar);
137 static void threadpool_append_jobs (ThreadPool *tp, MonoObject **jobs, gint njobs);
138 static void threadpool_init (ThreadPool *tp, int min_threads, int max_threads, void (*async_invoke) (gpointer));
139 static void threadpool_start_idle_threads (ThreadPool *tp);
140 static void threadpool_kill_idle_threads (ThreadPool *tp);
141 static gboolean threadpool_start_thread (ThreadPool *tp);
142 static void threadpool_kill_thread (ThreadPool *tp);
143 static void monitor_thread (gpointer data);
144 static int get_event_from_state (MonoSocketAsyncResult *state);
145
146 static MonoClass *async_call_klass;
147 static MonoClass *socket_async_call_klass;
148 static MonoClass *process_async_call_klass;
149
150 static GPtrArray *threads;
151 mono_mutex_t threads_lock;
152 static GPtrArray *wsqs;
153 mono_mutex_t wsqs_lock;
154 static gboolean suspended;
155
156 static volatile gint32 monitor_njobs = 0;
157 static volatile gint32 monitor_state;
158 static MonoSemType monitor_sem;
159 static MonoInternalThread *monitor_internal_thread;
160
161 /* Hooks */
162 static MonoThreadPoolFunc tp_start_func;
163 static MonoThreadPoolFunc tp_finish_func;
164 static gpointer tp_hooks_user_data;
165 static MonoThreadPoolItemFunc tp_item_begin_func;
166 static MonoThreadPoolItemFunc tp_item_end_func;
167 static gpointer tp_item_user_data;
168
169 enum {
170         AIO_OP_FIRST,
171         AIO_OP_ACCEPT = 0,
172         AIO_OP_CONNECT,
173         AIO_OP_RECEIVE,
174         AIO_OP_RECEIVEFROM,
175         AIO_OP_SEND,
176         AIO_OP_SENDTO,
177         AIO_OP_RECV_JUST_CALLBACK,
178         AIO_OP_SEND_JUST_CALLBACK,
179         AIO_OP_READPIPE,
180         AIO_OP_CONSOLE2,
181         AIO_OP_DISCONNECT,
182         AIO_OP_ACCEPTRECEIVE,
183         AIO_OP_RECEIVE_BUFFERS,
184         AIO_OP_SEND_BUFFERS,
185         AIO_OP_LAST
186 };
187
188 // #include <mono/metadata/tpool-poll.c>
189 gpointer tp_poll_init (SocketIOData *data);
190
191 #ifdef HAVE_EPOLL
192 #include <mono/metadata/tpool-epoll.c>
193 #elif defined(USE_KQUEUE_FOR_THREADPOOL)
194 #include <mono/metadata/tpool-kqueue.c>
195 #endif
196 /*
197  * Functions to check whenever a class is given system class. We need to cache things in MonoDomain since some of the
198  * assemblies can be unloaded.
199  */
200
201 static gboolean
202 is_system_type (MonoDomain *domain, MonoClass *klass)
203 {
204         if (domain->system_image == NULL)
205                 domain->system_image = mono_image_loaded ("System");
206
207         return klass->image == domain->system_image;
208 }
209
210 static gboolean
211 is_corlib_type (MonoDomain *domain, MonoClass *klass)
212 {
213         return klass->image == mono_defaults.corlib;
214 }
215
216 #define check_type_cached(domain, ASSEMBLY, _class, _namespace, _name, loc) do { \
217         if (*loc) \
218                 return *loc == _class; \
219         if (is_##ASSEMBLY##_type (domain, _class) && !strcmp (_name, _class->name) && !strcmp (_namespace, _class->name_space)) { \
220                 *loc = _class; \
221                 return TRUE; \
222         } \
223         return FALSE; \
224 } while (0) \
225
226 #define check_corlib_type_cached(domain, _class, _namespace, _name, loc) check_type_cached (domain, corlib, _class, _namespace, _name, loc)
227
228 #define check_system_type_cached(domain, _class, _namespace, _name, loc) check_type_cached (domain, system, _class, _namespace, _name, loc)
229
230 static gboolean
231 is_corlib_asyncresult (MonoDomain *domain, MonoClass *klass)
232 {
233         check_corlib_type_cached (domain, klass, "System.Runtime.Remoting.Messaging", "AsyncResult", &domain->corlib_asyncresult_class);
234 }
235
236 static gboolean
237 is_socketasyncresult (MonoDomain *domain, MonoClass *klass)
238 {
239         static MonoClass *socket_async_result_klass = NULL;
240         check_system_type_cached (domain, klass, "System.Net.Sockets", "SocketAsyncResult", &socket_async_result_klass);
241 }
242
243 static gboolean
244 is_socketasynccall (MonoDomain *domain, MonoClass *klass)
245 {
246         static MonoClass *socket_async_callback_klass = NULL;
247         check_system_type_cached (domain, klass, "System.Net.Sockets", "SocketAsyncCallback", &socket_async_callback_klass);
248 }
249
250 static gboolean
251 is_appdomainunloaded_exception (MonoDomain *domain, MonoClass *klass)
252 {
253         check_corlib_type_cached (domain, klass, "System", "AppDomainUnloadedException", &domain->ad_unloaded_ex_class);
254 }
255
256 static gboolean
257 is_sd_process (MonoDomain *domain, MonoClass *klass)
258 {
259         check_system_type_cached (domain, klass, "System.Diagnostics", "Process", &domain->process_class);
260 }
261
262 static gboolean
263 is_sdp_asyncreadhandler (MonoDomain *domain, MonoClass *klass)
264 {
265
266         return (klass->nested_in &&
267                         is_sd_process (domain, klass->nested_in) &&
268                 !strcmp (klass->name, "AsyncReadHandler"));
269 }
270
271
272 #ifdef DISABLE_SOCKETS
273
274 void
275 socket_io_cleanup (SocketIOData *data)
276 {
277 }
278
279 static int
280 get_event_from_state (MonoSocketAsyncResult *state)
281 {
282         g_assert_not_reached ();
283         return -1;
284 }
285
286 int
287 get_events_from_list (MonoMList *list)
288 {
289         return 0;
290 }
291
292 #else
293
294 void
295 socket_io_cleanup (SocketIOData *data)
296 {
297         mono_mutex_lock (&data->io_lock);
298         if (data->inited != 2) {
299                 mono_mutex_unlock (&data->io_lock);
300                 return;
301         }
302         data->inited = 3;
303         data->shutdown (data->event_data);
304         mono_mutex_unlock (&data->io_lock);
305 }
306
307 static int
308 get_event_from_state (MonoSocketAsyncResult *state)
309 {
310         switch (state->operation) {
311         case AIO_OP_ACCEPT:
312         case AIO_OP_RECEIVE:
313         case AIO_OP_RECV_JUST_CALLBACK:
314         case AIO_OP_RECEIVEFROM:
315         case AIO_OP_READPIPE:
316         case AIO_OP_ACCEPTRECEIVE:
317         case AIO_OP_RECEIVE_BUFFERS:
318                 return MONO_POLLIN;
319         case AIO_OP_SEND:
320         case AIO_OP_SEND_JUST_CALLBACK:
321         case AIO_OP_SENDTO:
322         case AIO_OP_CONNECT:
323         case AIO_OP_SEND_BUFFERS:
324         case AIO_OP_DISCONNECT:
325                 return MONO_POLLOUT;
326         default: /* Should never happen */
327                 g_message ("get_event_from_state: unknown value in switch!!!");
328                 return 0;
329         }
330 }
331
332 int
333 get_events_from_list (MonoMList *list)
334 {
335         MonoSocketAsyncResult *state;
336         int events = 0;
337
338         while (list && (state = (MonoSocketAsyncResult *)mono_mlist_get_data (list))) {
339                 events |= get_event_from_state (state);
340                 list = mono_mlist_next (list);
341         }
342
343         return events;
344 }
345
346 #define ICALL_RECV(x)   ves_icall_System_Net_Sockets_Socket_Receive_internal (\
347                                 (SOCKET)(gssize)x->handle, x->buffer, x->offset, x->size,\
348                                  x->socket_flags, &x->error);
349
350 #define ICALL_SEND(x)   ves_icall_System_Net_Sockets_Socket_Send_internal (\
351                                 (SOCKET)(gssize)x->handle, x->buffer, x->offset, x->size,\
352                                  x->socket_flags, &x->error);
353
354 #endif /* !DISABLE_SOCKETS */
355
356 static void
357 threadpool_jobs_inc (MonoObject *obj)
358 {
359         if (obj)
360                 InterlockedIncrement (&obj->vtable->domain->threadpool_jobs);
361 }
362
363 static gboolean
364 threadpool_jobs_dec (MonoObject *obj)
365 {
366         MonoDomain *domain;
367         int remaining_jobs;
368
369         if (obj == NULL)
370                 return FALSE;
371
372         domain = obj->vtable->domain;
373         remaining_jobs = InterlockedDecrement (&domain->threadpool_jobs);
374         if (remaining_jobs == 0 && domain->cleanup_semaphore) {
375                 ReleaseSemaphore (domain->cleanup_semaphore, 1, NULL);
376                 return TRUE;
377         }
378         return FALSE;
379 }
380
381 MonoObject *
382 get_io_event (MonoMList **list, gint event)
383 {
384         MonoObject *state;
385         MonoMList *current;
386         MonoMList *prev;
387
388         current = *list;
389         prev = NULL;
390         state = NULL;
391         while (current) {
392                 state = mono_mlist_get_data (current);
393                 if (get_event_from_state ((MonoSocketAsyncResult *) state) == event)
394                         break;
395
396                 state = NULL;
397                 prev = current;
398                 current = mono_mlist_next (current);
399         }
400
401         if (current) {
402                 if (prev) {
403                         mono_mlist_set_next (prev, mono_mlist_next (current));
404                 } else {
405                         *list = mono_mlist_next (*list);
406                 }
407         }
408
409         return state;
410 }
411
412 /*
413  * select/poll wake up when a socket is closed, but epoll just removes
414  * the socket from its internal list without notification.
415  */
416 void
417 mono_thread_pool_remove_socket (int sock)
418 {
419         MonoMList *list;
420         MonoSocketAsyncResult *state;
421         MonoObject *ares;
422
423         if (use_ms_threadpool ()) {
424 #ifndef DISABLE_SOCKETS
425                 mono_threadpool_ms_io_remove_socket (sock);
426 #endif
427                 return;
428         }
429
430         if (socket_io_data.inited == 0)
431                 return;
432
433         mono_mutex_lock (&socket_io_data.io_lock);
434         if (socket_io_data.sock_to_state == NULL) {
435                 mono_mutex_unlock (&socket_io_data.io_lock);
436                 return;
437         }
438         list = mono_g_hash_table_lookup (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
439         if (list)
440                 mono_g_hash_table_remove (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
441         mono_mutex_unlock (&socket_io_data.io_lock);
442         
443         while (list) {
444                 state = (MonoSocketAsyncResult *) mono_mlist_get_data (list);
445                 if (state->operation == AIO_OP_RECEIVE)
446                         state->operation = AIO_OP_RECV_JUST_CALLBACK;
447                 else if (state->operation == AIO_OP_SEND)
448                         state->operation = AIO_OP_SEND_JUST_CALLBACK;
449
450                 ares = get_io_event (&list, MONO_POLLIN);
451                 threadpool_append_job (&async_io_tp, ares);
452                 if (list) {
453                         ares = get_io_event (&list, MONO_POLLOUT);
454                         threadpool_append_job (&async_io_tp, ares);
455                 }
456         }
457 }
458
459 static void
460 init_event_system (SocketIOData *data)
461 {
462 #ifdef HAVE_EPOLL
463         if (data->event_system == EPOLL_BACKEND) {
464                 data->event_data = tp_epoll_init (data);
465                 if (data->event_data == NULL) {
466                         if (g_getenv ("MONO_DEBUG"))
467                                 g_message ("Falling back to poll()");
468                         data->event_system = POLL_BACKEND;
469                 }
470         }
471 #elif defined(USE_KQUEUE_FOR_THREADPOOL)
472         if (data->event_system == KQUEUE_BACKEND)
473                 data->event_data = tp_kqueue_init (data);
474 #endif
475         if (data->event_system == POLL_BACKEND)
476                 data->event_data = tp_poll_init (data);
477 }
478
479 static void
480 socket_io_init (SocketIOData *data)
481 {
482         int inited;
483
484         if (data->inited >= 2) // 2 -> initialized, 3-> cleaned up
485                 return;
486
487         inited = InterlockedCompareExchange (&data->inited, 1, 0);
488         if (inited >= 1) {
489                 while (TRUE) {
490                         if (data->inited >= 2)
491                                 return;
492                         SleepEx (1, FALSE);
493                 }
494         }
495
496         mono_mutex_lock (&data->io_lock);
497         data->sock_to_state = mono_g_hash_table_new_type (g_direct_hash, g_direct_equal, MONO_HASH_VALUE_GC);
498 #ifdef HAVE_EPOLL
499         data->event_system = EPOLL_BACKEND;
500 #elif defined(USE_KQUEUE_FOR_THREADPOOL)
501         data->event_system = KQUEUE_BACKEND;
502 #else
503         data->event_system = POLL_BACKEND;
504 #endif
505         if (g_getenv ("MONO_DISABLE_AIO") != NULL)
506                 data->event_system = POLL_BACKEND;
507
508         init_event_system (data);
509         mono_thread_create_internal (mono_get_root_domain (), data->wait, data, TRUE, SMALL_STACK);
510         mono_mutex_unlock (&data->io_lock);
511         data->inited = 2;
512         threadpool_start_thread (&async_io_tp);
513 }
514
515 static void
516 socket_io_add (MonoAsyncResult *ares, MonoSocketAsyncResult *state)
517 {
518         MonoMList *list;
519         SocketIOData *data = &socket_io_data;
520         int fd;
521         gboolean is_new;
522         int ievt;
523
524         socket_io_init (&socket_io_data);
525         if (mono_runtime_is_shutting_down () || data->inited == 3 || data->sock_to_state == NULL)
526                 return;
527         if (async_tp.pool_status == 2)
528                 return;
529
530         MONO_OBJECT_SETREF (state, ares, ares);
531
532         fd = GPOINTER_TO_INT (state->handle);
533         mono_mutex_lock (&data->io_lock);
534         if (data->sock_to_state == NULL) {
535                 mono_mutex_unlock (&data->io_lock);
536                 return;
537         }
538         list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
539         if (list == NULL) {
540                 list = mono_mlist_alloc ((MonoObject*)state);
541                 is_new = TRUE;
542         } else {
543                 list = mono_mlist_append (list, (MonoObject*)state);
544                 is_new = FALSE;
545         }
546
547         mono_g_hash_table_replace (data->sock_to_state, state->handle, list);
548         ievt = get_events_from_list (list);
549         /* The modify function leaves the io_lock critical section. */
550         data->modify (data, fd, state->operation, ievt, is_new);
551 }
552
553 #ifndef DISABLE_SOCKETS
554 static gboolean
555 socket_io_filter (MonoObject *target, MonoObject *state)
556 {
557         gint op;
558         MonoSocketAsyncResult *sock_res;
559         MonoClass *klass;
560         MonoDomain *domain;
561
562         if (target == NULL || state == NULL)
563                 return FALSE;
564
565         domain = target->vtable->domain;
566         klass = target->vtable->klass;
567         if (socket_async_call_klass == NULL && is_socketasynccall (domain, klass))
568                 socket_async_call_klass = klass;
569
570         if (process_async_call_klass == NULL && is_sdp_asyncreadhandler (domain, klass))
571                 process_async_call_klass = klass;
572
573         if (klass != socket_async_call_klass && klass != process_async_call_klass)
574                 return FALSE;
575
576         sock_res = (MonoSocketAsyncResult *) state;
577         op = sock_res->operation;
578         if (op < AIO_OP_FIRST || op >= AIO_OP_LAST)
579                 return FALSE;
580
581         return TRUE;
582 }
583 #endif /* !DISABLE_SOCKETS */
584
585 /* Returns the exception thrown when invoking, if any */
586 static MonoObject *
587 mono_async_invoke (ThreadPool *tp, MonoAsyncResult *ares)
588 {
589         MonoObject *exc = NULL;
590
591         mono_async_result_invoke (ares, &exc);
592
593 #if DEBUG
594         InterlockedDecrement (&tp->njobs);
595 #endif
596         if (!tp->is_io)
597                 InterlockedIncrement (&tp->nexecuted);
598
599         if (InterlockedDecrement (&monitor_njobs) == 0)
600                 monitor_state = MONITOR_STATE_FALLING_ASLEEP;
601
602         return exc;
603 }
604
605 static void
606 threadpool_start_idle_threads (ThreadPool *tp)
607 {
608         int n;
609         guint32 stack_size;
610
611         stack_size = (!tp->is_io) ? 0 : SMALL_STACK;
612         do {
613                 while (1) {
614                         n = tp->nthreads;
615                         if (n >= tp->min_threads)
616                                 return;
617                         if (InterlockedCompareExchange (&tp->nthreads, n + 1, n) == n)
618                                 break;
619                 }
620 #ifndef DISABLE_PERFCOUNTERS
621                 mono_perfcounter_update_value (tp->pc_nthreads, TRUE, 1);
622 #endif
623                 mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, tp, TRUE, stack_size);
624                 SleepEx (100, TRUE);
625         } while (1);
626 }
627
628 static void
629 threadpool_init (ThreadPool *tp, int min_threads, int max_threads, void (*async_invoke) (gpointer))
630 {
631         memset (tp, 0, sizeof (ThreadPool));
632         tp->min_threads = min_threads;
633         tp->max_threads = max_threads;
634         tp->async_invoke = async_invoke;
635         tp->queue = mono_cq_create ();
636         MONO_SEM_INIT (&tp->new_job, 0);
637 }
638
639 #ifndef DISABLE_PERFCOUNTERS
640 static void *
641 init_perf_counter (const char *category, const char *counter)
642 {
643         MonoString *category_str;
644         MonoString *counter_str;
645         MonoString *machine;
646         MonoDomain *root;
647         MonoBoolean custom;
648         int type;
649
650         if (category == NULL || counter == NULL)
651                 return NULL;
652         root = mono_get_root_domain ();
653         category_str = mono_string_new (root, category);
654         counter_str = mono_string_new (root, counter);
655         machine = mono_string_new (root, ".");
656         return mono_perfcounter_get_impl (category_str, counter_str, NULL, machine, &type, &custom);
657 }
658 #endif
659
660 #ifdef DEBUG
661 static void
662 print_pool_info (ThreadPool *tp)
663 {
664
665 //      if (tp->tail - tp->head == 0)
666 //              return;
667
668         g_print ("Pool status? %d\n", InterlockedCompareExchange (&tp->pool_status, 0, 0));
669         g_print ("Min. threads: %d\n", InterlockedCompareExchange (&tp->min_threads, 0, 0));
670         g_print ("Max. threads: %d\n", InterlockedCompareExchange (&tp->max_threads, 0, 0));
671         g_print ("nthreads: %d\n", InterlockedCompareExchange (&tp->nthreads, 0, 0));
672         g_print ("busy threads: %d\n", InterlockedCompareExchange (&tp->busy_threads, 0, 0));
673         g_print ("Waiting: %d\n", InterlockedCompareExchange (&tp->waiting, 0, 0));
674         g_print ("Queued: %d\n", (tp->tail - tp->head));
675         if (tp == &async_tp) {
676                 int i;
677                 mono_mutex_lock (&wsqs_lock);
678                 for (i = 0; i < wsqs->len; i++) {
679                         g_print ("\tWSQ %d: %d\n", i, mono_wsq_count (g_ptr_array_index (wsqs, i)));
680                 }
681                 mono_mutex_unlock (&wsqs_lock);
682         } else {
683                 g_print ("\tSockets: %d\n", mono_g_hash_table_size (socket_io_data.sock_to_state));
684         }
685         g_print ("-------------\n");
686 }
687
688 static void
689 signal_handler (int signo)
690 {
691         ThreadPool *tp;
692
693         tp = &async_tp;
694         g_print ("\n-----Non-IO-----\n");
695         print_pool_info (tp);
696         tp = &async_io_tp;
697         g_print ("\n-----IO-----\n");
698         print_pool_info (tp);
699         alarm (2);
700 }
701 #endif
702
703 #define SAMPLES_PERIOD 500
704 #define HISTORY_SIZE 10
705 /* number of iteration without any jobs
706    in the queue before going to sleep */
707 #define NUM_WAITING_ITERATIONS 10
708
709 typedef struct {
710         gint32 nexecuted;
711         gint32 nthreads;
712         gint8 nthreads_diff;
713 } SamplesHistory;
714
715 /*
716  * returns :
717  *  -  1 if the number of threads should increase
718  *  -  0 if it should not change
719  *  - -1 if it should decrease
720  *  - -2 in case of error
721  */
722 static gint8
723 monitor_heuristic (gint16 *current, gint16 *history_size, SamplesHistory *history, ThreadPool *tp)
724 {
725         int i;
726         gint8 decision G_GNUC_UNUSED;
727         gint16 cur, max = 0;
728         gboolean all_waitsleepjoin;
729         MonoInternalThread *thread;
730
731         /*
732          * The following heuristic tries to approach the optimal number of threads to maximize jobs throughput. To
733          * achieve this, it simply stores the number of jobs executed (nexecuted), the number of Threads (nthreads)
734          * and the decision (nthreads_diff) for the past HISTORY_SIZE periods of time, each period being of
735          * duration SAMPLES_PERIOD ms. This history gives us an insight into what happened, and to see if we should
736          * increase or reduce the number of threads by comparing the last period (current) to the best one.
737          *
738          * The algorithm can be describe as following :
739          *  - if we have a better throughput than the best period : we should either increase the number of threads
740          *     in case we already have more threads, either reduce the number of threads if we have less threads; this
741          *     is equivalent to move away from the number of threads of the best period, because we are currently better
742          *  - if we have a worse throughput than the best period : we should either decrease the number of threads if
743          *     we have more threads, either increase the number of threads if we have less threads;  this is equivalent
744          *     to get closer to the number of threads of the best period, because we are currently worse
745          */
746
747         *history_size = MIN (*history_size + 1, HISTORY_SIZE);
748         cur = *current = (*current + 1) % *history_size;
749
750         history [cur].nthreads = tp->nthreads;
751         history [cur].nexecuted = InterlockedExchange (&tp->nexecuted, 0);
752
753         if (tp->waiting) {
754                 /* if we have waiting thread in the pool, then do not create a new one */
755                 history [cur].nthreads_diff = tp->waiting > 1 ? -1 : 0;
756                 decision = 0;
757         } else if (tp->nthreads < tp->min_threads) {
758                 history [cur].nthreads_diff = 1;
759                 decision = 1;
760         } else if (*history_size <= 1) {
761                 /* first iteration, let's add a thread by default */
762                 history [cur].nthreads_diff = 1;
763                 decision = 2;
764         } else {
765                 mono_mutex_lock (&threads_lock);
766                 if (threads == NULL) {
767                         mono_mutex_unlock (&threads_lock);
768                         return -2;
769                 }
770                 all_waitsleepjoin = TRUE;
771                 for (i = 0; i < threads->len; ++i) {
772                         thread = g_ptr_array_index (threads, i);
773                         if (!(thread->state & ThreadState_WaitSleepJoin)) {
774                                 all_waitsleepjoin = FALSE;
775                                 break;
776                         }
777                 }
778                 mono_mutex_unlock (&threads_lock);
779
780                 if (all_waitsleepjoin) {
781                         /* we might be in a condition of starvation/deadlock with tasks waiting for each others */
782                         history [cur].nthreads_diff = 1;
783                         decision = 5;
784                 } else {
785                         max = cur == 0 ? 1 : 0;
786                         for (i = 0; i < *history_size; i++) {
787                                 if (i == cur)
788                                         continue;
789                                 if (history [i].nexecuted > history [max].nexecuted)
790                                         max = i;
791                         }
792
793                         if (history [cur].nexecuted >= history [max].nexecuted) {
794                                 /* we improved the situation, let's continue ! */
795                                 history [cur].nthreads_diff = history [cur].nthreads >= history [max].nthreads ? 1 : -1;
796                                 decision = 3;
797                         } else {
798                                 /* we made it worse, let's return to previous situation */
799                                 history [cur].nthreads_diff = history [cur].nthreads >= history [max].nthreads ? -1 : 1;
800                                 decision = 4;
801                         }
802                 }
803         }
804
805 #if DEBUG
806         printf ("monitor_thread: decision: %1d, history [current]: {nexecuted: %5d, nthreads: %3d, waiting: %2d, nthreads_diff: %2d}, history [max]: {nexecuted: %5d, nthreads: %3d}\n",
807                         decision, history [cur].nexecuted, history [cur].nthreads, tp->waiting, history [cur].nthreads_diff, history [max].nexecuted, history [max].nthreads);
808 #endif
809         
810         return history [cur].nthreads_diff;
811 }
812
813 static void
814 monitor_thread (gpointer unused)
815 {
816         ThreadPool *pools [2];
817         MonoInternalThread *thread;
818         int i;
819
820         guint32 ms;
821         gint8 num_waiting_iterations = 0;
822
823         gint16 history_size = 0, current = -1;
824         SamplesHistory *history = malloc (sizeof (SamplesHistory) * HISTORY_SIZE);
825
826         pools [0] = &async_tp;
827         pools [1] = &async_io_tp;
828         thread = mono_thread_internal_current ();
829         ves_icall_System_Threading_Thread_SetName_internal (thread, mono_string_new (mono_domain_get (), "Threadpool monitor"));
830         while (1) {
831                 ms = SAMPLES_PERIOD;
832                 i = 10; //number of spurious awakes we tolerate before doing a round of rebalancing.
833                 mono_gc_set_skip_thread (TRUE);
834                 MONO_PREPARE_BLOCKING
835                 do {
836                         guint32 ts;
837                         ts = mono_msec_ticks ();
838                         if (SleepEx (ms, TRUE) == 0)
839                                 break;
840                         ms -= (mono_msec_ticks () - ts);
841                         if (mono_runtime_is_shutting_down ())
842                                 break;
843                         check_for_interruption_critical ();
844                 } while (ms > 0 && i--);
845                 MONO_FINISH_BLOCKING
846                 mono_gc_set_skip_thread (FALSE);
847
848                 if (mono_runtime_is_shutting_down ())
849                         break;
850
851                 if (suspended)
852                         continue;
853
854                 /* threadpool is cleaning up */
855                 if (async_tp.pool_status == 2 || async_io_tp.pool_status == 2)
856                         break;
857
858                 MONO_PREPARE_BLOCKING
859                 switch (monitor_state) {
860                 case MONITOR_STATE_AWAKE:
861                         num_waiting_iterations = 0;
862                         break;
863                 case MONITOR_STATE_FALLING_ASLEEP:
864                         if (++num_waiting_iterations == NUM_WAITING_ITERATIONS) {
865                                 if (monitor_state == MONITOR_STATE_FALLING_ASLEEP && InterlockedCompareExchange (&monitor_state, MONITOR_STATE_SLEEPING, MONITOR_STATE_FALLING_ASLEEP) == MONITOR_STATE_FALLING_ASLEEP) {
866                                         MONO_SEM_WAIT (&monitor_sem);
867
868                                         num_waiting_iterations = 0;
869                                         current = -1;
870                                         history_size = 0;
871                                 }
872                         }
873                         break;
874                 case MONITOR_STATE_SLEEPING:
875                         g_assert_not_reached ();
876                 }
877                 MONO_FINISH_BLOCKING
878
879                 for (i = 0; i < 2; i++) {
880                         ThreadPool *tp;
881                         tp = pools [i];
882
883                         if (tp->is_io) {
884                                 if (!tp->waiting && mono_cq_count (tp->queue) > 0)
885                                         threadpool_start_thread (tp);
886                         } else {
887                                 gint8 nthreads_diff = monitor_heuristic (&current, &history_size, history, tp);
888
889                                 if (nthreads_diff == 1)
890                                         threadpool_start_thread (tp);
891                                 else if (nthreads_diff == -1)
892                                         threadpool_kill_thread (tp);
893                         }
894                 }
895         }
896 }
897
898 void
899 mono_thread_pool_init_tls (void)
900 {
901         if (use_ms_threadpool ()) {
902                 mono_threadpool_ms_init_tls ();
903                 return;
904         }
905
906         mono_wsq_init ();
907 }
908
909 void
910 mono_thread_pool_init (void)
911 {
912         gint threads_per_cpu = 1;
913         gint thread_count;
914         gint cpu_count;
915         int result;
916         
917         if (use_ms_threadpool ()) {
918                 mono_threadpool_ms_init ();
919                 return;
920         }
921
922         cpu_count = mono_cpu_count ();
923
924         if (tp_inited == 2)
925                 return;
926
927         result = InterlockedCompareExchange (&tp_inited, 1, 0);
928         if (result == 1) {
929                 while (1) {
930                         SleepEx (1, FALSE);
931                         if (tp_inited == 2)
932                                 return;
933                 }
934         }
935
936         MONO_GC_REGISTER_ROOT_FIXED (socket_io_data.sock_to_state);
937         mono_mutex_init_recursive (&socket_io_data.io_lock);
938         if (g_getenv ("MONO_THREADS_PER_CPU") != NULL) {
939                 threads_per_cpu = atoi (g_getenv ("MONO_THREADS_PER_CPU"));
940                 if (threads_per_cpu < 1)
941                         threads_per_cpu = 1;
942         }
943
944         thread_count = MIN (cpu_count * threads_per_cpu, 100 * cpu_count);
945         threadpool_init (&async_tp, thread_count, MAX (100 * cpu_count, thread_count), async_invoke_thread);
946         threadpool_init (&async_io_tp, cpu_count * 2, cpu_count * 4, async_invoke_thread);
947         async_io_tp.is_io = TRUE;
948
949         async_call_klass = mono_class_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
950         g_assert (async_call_klass);
951
952         mono_mutex_init (&threads_lock);
953         threads = g_ptr_array_sized_new (thread_count);
954         g_assert (threads);
955
956         mono_mutex_init_recursive (&wsqs_lock);
957         wsqs = g_ptr_array_sized_new (MAX (100 * cpu_count, thread_count));
958
959 #ifndef DISABLE_PERFCOUNTERS
960         async_tp.pc_nitems = init_perf_counter ("Mono Threadpool", "Work Items Added");
961         g_assert (async_tp.pc_nitems);
962
963         async_io_tp.pc_nitems = init_perf_counter ("Mono Threadpool", "IO Work Items Added");
964         g_assert (async_io_tp.pc_nitems);
965
966         async_tp.pc_nthreads = init_perf_counter ("Mono Threadpool", "# of Threads");
967         g_assert (async_tp.pc_nthreads);
968
969         async_io_tp.pc_nthreads = init_perf_counter ("Mono Threadpool", "# of IO Threads");
970         g_assert (async_io_tp.pc_nthreads);
971 #endif
972         tp_inited = 2;
973 #ifdef DEBUG
974         signal (SIGALRM, signal_handler);
975         alarm (2);
976 #endif
977
978         MONO_SEM_INIT (&monitor_sem, 0);
979         monitor_state = MONITOR_STATE_AWAKE;
980         monitor_njobs = 0;
981 }
982
983 static MonoAsyncResult *
984 create_simple_asyncresult (MonoObject *target, MonoObject *state)
985 {
986         MonoDomain *domain = mono_domain_get ();
987         MonoAsyncResult *ares;
988
989         /* Don't call mono_async_result_new() to avoid capturing the context */
990         ares = (MonoAsyncResult *) mono_object_new (domain, mono_defaults.asyncresult_class);
991         MONO_OBJECT_SETREF (ares, async_delegate, target);
992         MONO_OBJECT_SETREF (ares, async_state, state);
993         return ares;
994 }
995
996 void
997 icall_append_io_job (MonoObject *target, MonoSocketAsyncResult *state)
998 {
999         MonoAsyncResult *ares;
1000
1001         ares = create_simple_asyncresult (target, (MonoObject *) state);
1002
1003         if (use_ms_threadpool ()) {
1004 #ifndef DISABLE_SOCKETS
1005                 mono_threadpool_ms_io_add (ares, state);
1006 #endif
1007                 return;
1008         }
1009
1010         socket_io_add (ares, state);
1011 }
1012
1013 MonoAsyncResult *
1014 mono_thread_pool_add (MonoObject *target, MonoMethodMessage *msg, MonoDelegate *async_callback,
1015                       MonoObject *state)
1016 {
1017         MonoDomain *domain;
1018         MonoAsyncResult *ares;
1019         MonoAsyncCall *ac;
1020
1021         if (use_ms_threadpool ())
1022                 return mono_threadpool_ms_add (target, msg, async_callback, state);
1023
1024         domain = mono_domain_get ();
1025
1026         ac = (MonoAsyncCall*)mono_object_new (domain, async_call_klass);
1027         MONO_OBJECT_SETREF (ac, msg, msg);
1028         MONO_OBJECT_SETREF (ac, state, state);
1029
1030         if (async_callback) {
1031                 ac->cb_method = mono_get_delegate_invoke (((MonoObject *)async_callback)->vtable->klass);
1032                 MONO_OBJECT_SETREF (ac, cb_target, async_callback);
1033         }
1034
1035         ares = mono_async_result_new (domain, NULL, ac->state, NULL, (MonoObject*)ac);
1036         MONO_OBJECT_SETREF (ares, async_delegate, target);
1037
1038 #ifndef DISABLE_SOCKETS
1039         if (socket_io_filter (target, state)) {
1040                 socket_io_add (ares, (MonoSocketAsyncResult *) state);
1041                 return ares;
1042         }
1043 #endif
1044         threadpool_append_job (&async_tp, (MonoObject *) ares);
1045         return ares;
1046 }
1047
1048 MonoObject *
1049 mono_thread_pool_finish (MonoAsyncResult *ares, MonoArray **out_args, MonoObject **exc)
1050 {
1051         MonoAsyncCall *ac;
1052         HANDLE wait_event;
1053
1054         if (use_ms_threadpool ()) {
1055                 return mono_threadpool_ms_finish (ares, out_args, exc);
1056         }
1057
1058         *exc = NULL;
1059         *out_args = NULL;
1060
1061         /* check if already finished */
1062         mono_monitor_enter ((MonoObject *) ares);
1063         
1064         if (ares->endinvoke_called) {
1065                 *exc = (MonoObject *) mono_get_exception_invalid_operation (NULL);
1066                 mono_monitor_exit ((MonoObject *) ares);
1067                 return NULL;
1068         }
1069
1070         ares->endinvoke_called = 1;
1071         /* wait until we are really finished */
1072         if (!ares->completed) {
1073                 if (ares->handle == NULL) {
1074                         wait_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1075                         g_assert(wait_event != 0);
1076                         MONO_OBJECT_SETREF (ares, handle, (MonoObject *) mono_wait_handle_new (mono_object_domain (ares), wait_event));
1077                 } else {
1078                         wait_event = mono_wait_handle_get_handle ((MonoWaitHandle *) ares->handle);
1079                 }
1080                 mono_monitor_exit ((MonoObject *) ares);
1081                 MONO_PREPARE_BLOCKING
1082                 WaitForSingleObjectEx (wait_event, INFINITE, TRUE);
1083                 MONO_FINISH_BLOCKING
1084         } else {
1085                 mono_monitor_exit ((MonoObject *) ares);
1086         }
1087
1088         ac = (MonoAsyncCall *) ares->object_data;
1089         g_assert (ac != NULL);
1090         *exc = ac->msg->exc; /* FIXME: GC add write barrier */
1091         *out_args = ac->out_args;
1092
1093         return ac->res;
1094 }
1095
1096 static void
1097 threadpool_kill_idle_threads (ThreadPool *tp)
1098 {
1099         gint n;
1100
1101         n = (gint) InterlockedCompareExchange (&tp->max_threads, 0, -1);
1102         while (n) {
1103                 n--;
1104                 MONO_SEM_POST (&tp->new_job);
1105         }
1106 }
1107
1108 void
1109 mono_thread_pool_cleanup (void)
1110 {
1111         if (use_ms_threadpool ()) {
1112                 mono_threadpool_ms_cleanup ();
1113                 return;
1114         }
1115
1116         if (InterlockedExchange (&async_io_tp.pool_status, 2) == 1) {
1117                 socket_io_cleanup (&socket_io_data); /* Empty when DISABLE_SOCKETS is defined */
1118                 threadpool_kill_idle_threads (&async_io_tp);
1119         }
1120
1121         if (async_io_tp.queue != NULL) {
1122                 MONO_SEM_DESTROY (&async_io_tp.new_job);
1123                 threadpool_free_queue (&async_io_tp);
1124         }
1125
1126
1127         if (InterlockedExchange (&async_tp.pool_status, 2) == 1) {
1128                 threadpool_kill_idle_threads (&async_tp);
1129                 threadpool_free_queue (&async_tp);
1130         }
1131         
1132         if (threads) {
1133                 mono_mutex_lock (&threads_lock);
1134                 if (threads)
1135                         g_ptr_array_free (threads, FALSE);
1136                 threads = NULL;
1137                 mono_mutex_unlock (&threads_lock);
1138         }
1139
1140         if (wsqs) {
1141                 mono_mutex_lock (&wsqs_lock);
1142                 mono_wsq_cleanup ();
1143                 if (wsqs)
1144                         g_ptr_array_free (wsqs, TRUE);
1145                 wsqs = NULL;
1146                 mono_mutex_unlock (&wsqs_lock);
1147                 MONO_SEM_DESTROY (&async_tp.new_job);
1148         }
1149
1150         MONO_SEM_DESTROY (&monitor_sem);
1151 }
1152
1153 static gboolean
1154 threadpool_start_thread (ThreadPool *tp)
1155 {
1156         gint n;
1157         guint32 stack_size;
1158         MonoInternalThread *thread;
1159
1160         stack_size = (!tp->is_io) ? 0 : SMALL_STACK;
1161         while (!mono_runtime_is_shutting_down () && (n = tp->nthreads) < tp->max_threads) {
1162                 if (InterlockedCompareExchange (&tp->nthreads, n + 1, n) == n) {
1163 #ifndef DISABLE_PERFCOUNTERS
1164                         mono_perfcounter_update_value (tp->pc_nthreads, TRUE, 1);
1165 #endif
1166                         if (tp->is_io) {
1167                                 thread = mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, tp, TRUE, stack_size);
1168                         } else {
1169                                 mono_mutex_lock (&threads_lock);
1170                                 thread = mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, tp, TRUE, stack_size);
1171                                 g_assert (threads != NULL);
1172                                 g_ptr_array_add (threads, thread);
1173                                 mono_mutex_unlock (&threads_lock);
1174                         }
1175                         return TRUE;
1176                 }
1177         }
1178
1179         return FALSE;
1180 }
1181
1182 static void
1183 pulse_on_new_job (ThreadPool *tp)
1184 {
1185         if (tp->waiting)
1186                 MONO_SEM_POST (&tp->new_job);
1187 }
1188
1189 static void
1190 threadpool_kill_thread (ThreadPool *tp)
1191 {
1192         if (tp->destroy_thread == 0 && InterlockedCompareExchange (&tp->destroy_thread, 1, 0) == 0)
1193                 pulse_on_new_job (tp);
1194 }
1195
1196 void
1197 icall_append_job (MonoObject *ar)
1198 {
1199         threadpool_append_jobs (&async_tp, &ar, 1);
1200 }
1201
1202 static void
1203 threadpool_append_job (ThreadPool *tp, MonoObject *ar)
1204 {
1205         threadpool_append_jobs (tp, &ar, 1);
1206 }
1207
1208 void
1209 threadpool_append_async_io_jobs (MonoObject **jobs, gint njobs)
1210 {
1211         threadpool_append_jobs (&async_io_tp, jobs, njobs);
1212 }
1213
1214 static void
1215 threadpool_append_jobs (ThreadPool *tp, MonoObject **jobs, gint njobs)
1216 {
1217         MonoObject *ar;
1218         gint i;
1219
1220         if (mono_runtime_is_shutting_down ())
1221                 return;
1222
1223         if (tp->pool_status == 0 && InterlockedCompareExchange (&tp->pool_status, 1, 0) == 0) {
1224                 if (!tp->is_io) {
1225                         monitor_internal_thread = mono_thread_create_internal (mono_get_root_domain (), monitor_thread, NULL, TRUE, SMALL_STACK);
1226                         monitor_internal_thread->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
1227                         threadpool_start_thread (tp);
1228                 }
1229                 /* Create on demand up to min_threads to avoid startup penalty for apps that don't use
1230                  * the threadpool that much
1231                  */
1232                 if (mono_config_is_server_mode ()) {
1233                         mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, tp, TRUE, SMALL_STACK);
1234                 }
1235         }
1236
1237         InterlockedAdd (&monitor_njobs, njobs);
1238
1239         if (monitor_state == MONITOR_STATE_SLEEPING && InterlockedCompareExchange (&monitor_state, MONITOR_STATE_AWAKE, MONITOR_STATE_SLEEPING) == MONITOR_STATE_SLEEPING)
1240                 MONO_SEM_POST (&monitor_sem);
1241
1242         if (monitor_state == MONITOR_STATE_FALLING_ASLEEP)
1243                 InterlockedCompareExchange (&monitor_state, MONITOR_STATE_AWAKE, MONITOR_STATE_FALLING_ASLEEP);
1244
1245         for (i = 0; i < njobs; i++) {
1246                 ar = jobs [i];
1247                 if (ar == NULL || mono_domain_is_unloading (ar->vtable->domain))
1248                         continue; /* Might happen when cleaning domain jobs */
1249                 threadpool_jobs_inc (ar); 
1250 #ifndef DISABLE_PERFCOUNTERS
1251                 mono_perfcounter_update_value (tp->pc_nitems, TRUE, 1);
1252 #endif
1253                 if (!tp->is_io && mono_wsq_local_push (ar))
1254                         continue;
1255
1256                 mono_cq_enqueue (tp->queue, ar);
1257         }
1258
1259 #if DEBUG
1260         InterlockedAdd (&tp->njobs, njobs);
1261 #endif
1262
1263         for (i = 0; tp->waiting > 0 && i < MIN(njobs, tp->max_threads); i++)
1264                 pulse_on_new_job (tp);
1265 }
1266
1267 static void
1268 threadpool_clear_queue (ThreadPool *tp, MonoDomain *domain)
1269 {
1270         MonoObject *obj;
1271         MonoMList *other = NULL;
1272         MonoCQ *queue = tp->queue;
1273
1274         if (!queue)
1275                 return;
1276
1277         while (mono_cq_dequeue (queue, &obj)) {
1278                 if (obj == NULL)
1279                         continue;
1280                 if (obj->vtable->domain != domain)
1281                         other = mono_mlist_prepend (other, obj);
1282                 threadpool_jobs_dec (obj);
1283         }
1284
1285         if (mono_runtime_is_shutting_down ())
1286                 return;
1287
1288         while (other) {
1289                 threadpool_append_job (tp, (MonoObject *) mono_mlist_get_data (other));
1290                 other = mono_mlist_next (other);
1291         }
1292 }
1293
1294 static gboolean
1295 remove_sockstate_for_domain (gpointer key, gpointer value, gpointer user_data)
1296 {
1297         MonoMList *list = value;
1298         gboolean remove = FALSE;
1299         while (list) {
1300                 MonoObject *data = mono_mlist_get_data (list);
1301                 if (mono_object_domain (data) == user_data) {
1302                         remove = TRUE;
1303                         mono_mlist_set_data (list, NULL);
1304                 }
1305                 list = mono_mlist_next (list);
1306         }
1307         //FIXME is there some sort of additional unregistration we need to perform here?
1308         return remove;
1309 }
1310
1311 /*
1312  * Clean up the threadpool of all domain jobs.
1313  * Can only be called as part of the domain unloading process as
1314  * it will wait for all jobs to be visible to the interruption code. 
1315  */
1316 gboolean
1317 mono_thread_pool_remove_domain_jobs (MonoDomain *domain, int timeout)
1318 {
1319         HANDLE sem_handle;
1320         int result;
1321         guint32 start_time;
1322
1323         if (use_ms_threadpool ()) {
1324                 return mono_threadpool_ms_remove_domain_jobs (domain, timeout);
1325         }
1326
1327         result = TRUE;
1328         start_time = 0;
1329
1330         g_assert (domain->state == MONO_APPDOMAIN_UNLOADING);
1331
1332         threadpool_clear_queue (&async_tp, domain);
1333         threadpool_clear_queue (&async_io_tp, domain);
1334
1335         mono_mutex_lock (&socket_io_data.io_lock);
1336         if (socket_io_data.sock_to_state)
1337                 mono_g_hash_table_foreach_remove (socket_io_data.sock_to_state, remove_sockstate_for_domain, domain);
1338
1339         mono_mutex_unlock (&socket_io_data.io_lock);
1340         
1341         /*
1342          * There might be some threads out that could be about to execute stuff from the given domain.
1343          * We avoid that by setting up a semaphore to be pulsed by the thread that reaches zero.
1344          */
1345         sem_handle = CreateSemaphore (NULL, 0, 1, NULL);
1346
1347         domain->cleanup_semaphore = sem_handle;
1348         /*
1349          * The memory barrier here is required to have global ordering between assigning to cleanup_semaphone
1350          * and reading threadpool_jobs.
1351          * Otherwise this thread could read a stale version of threadpool_jobs and wait forever.
1352          */
1353         mono_memory_write_barrier ();
1354
1355         if (domain->threadpool_jobs && timeout != -1)
1356                 start_time = mono_msec_ticks ();
1357         while (domain->threadpool_jobs) {
1358                 MONO_PREPARE_BLOCKING
1359                 WaitForSingleObject (sem_handle, timeout);
1360                 MONO_FINISH_BLOCKING
1361                 if (timeout != -1 && (mono_msec_ticks () - start_time) > timeout) {
1362                         result = FALSE;
1363                         break;
1364                 }
1365         }
1366
1367         domain->cleanup_semaphore = NULL;
1368         CloseHandle (sem_handle);
1369         return result;
1370 }
1371
1372 static void
1373 threadpool_free_queue (ThreadPool *tp)
1374 {
1375         mono_cq_destroy (tp->queue);
1376         tp->queue = NULL;
1377 }
1378
1379 gboolean
1380 mono_thread_pool_is_queue_array (MonoArray *o)
1381 {
1382         if (use_ms_threadpool ()) {
1383                 return mono_threadpool_ms_is_queue_array (o);
1384         }
1385
1386         // gpointer obj = o;
1387
1388         // FIXME: need some fix in sgen code.
1389         return FALSE;
1390 }
1391
1392 static MonoWSQ *
1393 add_wsq (void)
1394 {
1395         int i;
1396         MonoWSQ *wsq;
1397
1398         mono_mutex_lock (&wsqs_lock);
1399         wsq = mono_wsq_create ();
1400         if (wsqs == NULL) {
1401                 mono_mutex_unlock (&wsqs_lock);
1402                 return NULL;
1403         }
1404         for (i = 0; i < wsqs->len; i++) {
1405                 if (g_ptr_array_index (wsqs, i) == NULL) {
1406                         wsqs->pdata [i] = wsq;
1407                         mono_mutex_unlock (&wsqs_lock);
1408                         return wsq;
1409                 }
1410         }
1411         g_ptr_array_add (wsqs, wsq);
1412         mono_mutex_unlock (&wsqs_lock);
1413         return wsq;
1414 }
1415
1416 static void
1417 remove_wsq (MonoWSQ *wsq)
1418 {
1419         gpointer data;
1420
1421         if (wsq == NULL)
1422                 return;
1423
1424         mono_mutex_lock (&wsqs_lock);
1425         if (wsqs == NULL) {
1426                 mono_mutex_unlock (&wsqs_lock);
1427                 return;
1428         }
1429         g_ptr_array_remove_fast (wsqs, wsq);
1430         data = NULL;
1431         /*
1432          * Only clean this up when shutting down, any other case will error out
1433          * if we're removing a queue that still has work items.
1434          */
1435         if (mono_runtime_is_shutting_down ()) {
1436                 while (mono_wsq_local_pop (&data)) {
1437                         threadpool_jobs_dec (data);
1438                         data = NULL;
1439                 }
1440         }
1441         mono_wsq_destroy (wsq);
1442         mono_mutex_unlock (&wsqs_lock);
1443 }
1444
1445 static void
1446 try_steal (MonoWSQ *local_wsq, gpointer *data, gboolean retry)
1447 {
1448         int i;
1449         int ms;
1450
1451         if (wsqs == NULL || data == NULL || *data != NULL)
1452                 return;
1453
1454         ms = 0;
1455         do {
1456                 if (mono_runtime_is_shutting_down ())
1457                         return;
1458
1459                 MONO_PREPARE_BLOCKING
1460                 mono_mutex_lock (&wsqs_lock);
1461                 MONO_FINISH_BLOCKING
1462                 for (i = 0; wsqs != NULL && i < wsqs->len; i++) {
1463                         MonoWSQ *wsq;
1464
1465                         wsq = wsqs->pdata [i];
1466                         if (wsq == local_wsq || mono_wsq_count (wsq) == 0)
1467                                 continue;
1468                         mono_wsq_try_steal (wsqs->pdata [i], data, ms);
1469                         if (*data != NULL) {
1470                                 mono_mutex_unlock (&wsqs_lock);
1471                                 return;
1472                         }
1473                 }
1474                 mono_mutex_unlock (&wsqs_lock);
1475                 ms += 10;
1476         } while (retry && ms < 11);
1477 }
1478
1479 static gboolean
1480 dequeue_or_steal (ThreadPool *tp, gpointer *data, MonoWSQ *local_wsq)
1481 {
1482         MonoCQ *queue = tp->queue;
1483         if (mono_runtime_is_shutting_down () || !queue)
1484                 return FALSE;
1485         mono_cq_dequeue (queue, (MonoObject **) data);
1486         if (!tp->is_io && !*data)
1487                 try_steal (local_wsq, data, FALSE);
1488         return (*data != NULL);
1489 }
1490
1491 static gboolean
1492 should_i_die (ThreadPool *tp)
1493 {
1494         gboolean result = FALSE;
1495         if (tp->destroy_thread == 1 && InterlockedCompareExchange (&tp->destroy_thread, 0, 1) == 1)
1496                 result = (tp->nthreads > tp->min_threads);
1497         return result;
1498 }
1499
1500 static void
1501 set_tp_thread_info (ThreadPool *tp)
1502 {
1503         const gchar *name;
1504         MonoInternalThread *thread = mono_thread_internal_current ();
1505
1506         mono_profiler_thread_start (thread->tid);
1507         name = (tp->is_io) ? "IO Threadpool worker" : "Threadpool worker";
1508         mono_thread_set_name_internal (thread, mono_string_new (mono_domain_get (), name), FALSE);
1509 }
1510
1511 static void
1512 clear_thread_state (void)
1513 {
1514         MonoInternalThread *thread = mono_thread_internal_current ();
1515         /* If the callee changes the background status, set it back to TRUE */
1516         mono_thread_clr_state (thread , ~ThreadState_Background);
1517         if (!mono_thread_test_state (thread , ThreadState_Background))
1518                 ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
1519 }
1520
1521 void
1522 check_for_interruption_critical (void)
1523 {
1524         MonoInternalThread *thread;
1525         /*RULE NUMBER ONE OF SKIP_THREAD: NEVER POKE MANAGED STATE.*/
1526         mono_gc_set_skip_thread (FALSE);
1527
1528         thread = mono_thread_internal_current ();
1529         if (THREAD_WANTS_A_BREAK (thread))
1530                 mono_thread_interruption_checkpoint ();
1531
1532         /*RULE NUMBER TWO OF SKIP_THREAD: READ RULE NUMBER ONE.*/
1533         mono_gc_set_skip_thread (TRUE);
1534 }
1535
1536 static void
1537 fire_profiler_thread_end (void)
1538 {
1539         MonoInternalThread *thread = mono_thread_internal_current ();
1540         mono_profiler_thread_end (thread->tid);
1541 }
1542
1543 static void
1544 async_invoke_thread (gpointer data)
1545 {
1546         MonoDomain *domain;
1547         MonoWSQ *wsq;
1548         ThreadPool *tp;
1549         gboolean must_die;
1550   
1551         tp = data;
1552         wsq = NULL;
1553         if (!tp->is_io)
1554                 wsq = add_wsq ();
1555
1556         set_tp_thread_info (tp);
1557
1558         if (tp_start_func)
1559                 tp_start_func (tp_hooks_user_data);
1560
1561         data = NULL;
1562         for (;;) {
1563                 MonoAsyncResult *ar;
1564                 MonoClass *klass;
1565                 gboolean is_io_task;
1566                 gboolean is_socket;
1567                 int n_naps = 0;
1568
1569                 is_io_task = FALSE;
1570                 ar = (MonoAsyncResult *) data;
1571                 if (ar) {
1572                         InterlockedIncrement (&tp->busy_threads);
1573                         domain = ((MonoObject *)ar)->vtable->domain;
1574 #ifndef DISABLE_SOCKETS
1575                         klass = ((MonoObject *) data)->vtable->klass;
1576                         is_io_task = !is_corlib_asyncresult (domain, klass);
1577                         is_socket = FALSE;
1578                         if (is_io_task) {
1579                                 MonoSocketAsyncResult *state = (MonoSocketAsyncResult *) data;
1580                                 is_socket = is_socketasyncresult (domain, klass);
1581                                 ar = state->ares;
1582                                 switch (state->operation) {
1583                                 case AIO_OP_RECEIVE:
1584                                         state->total = ICALL_RECV (state);
1585                                         break;
1586                                 case AIO_OP_SEND:
1587                                         state->total = ICALL_SEND (state);
1588                                         break;
1589                                 }
1590                         }
1591 #endif
1592                         /* worker threads invokes methods in different domains,
1593                          * so we need to set the right domain here */
1594                         g_assert (domain);
1595
1596                         if (mono_domain_is_unloading (domain) || mono_runtime_is_shutting_down ()) {
1597                                 threadpool_jobs_dec ((MonoObject *)ar);
1598                                 data = NULL;
1599                                 ar = NULL;
1600                                 InterlockedDecrement (&tp->busy_threads);
1601                         } else {
1602                                 mono_thread_push_appdomain_ref (domain);
1603                                 if (threadpool_jobs_dec ((MonoObject *)ar)) {
1604                                         data = NULL;
1605                                         ar = NULL;
1606                                         mono_thread_pop_appdomain_ref ();
1607                                         InterlockedDecrement (&tp->busy_threads);
1608                                         continue;
1609                                 }
1610
1611                                 if (mono_domain_set (domain, FALSE)) {
1612                                         MonoObject *exc;
1613
1614                                         if (tp_item_begin_func)
1615                                                 tp_item_begin_func (tp_item_user_data);
1616
1617                                         exc = mono_async_invoke (tp, ar);
1618                                         if (tp_item_end_func)
1619                                                 tp_item_end_func (tp_item_user_data);
1620                                         if (exc)
1621                                                 mono_internal_thread_unhandled_exception (exc);
1622                                         if (is_socket && tp->is_io) {
1623                                                 MonoSocketAsyncResult *state = (MonoSocketAsyncResult *) data;
1624
1625                                                 if (state->completed && state->callback) {
1626                                                         MonoAsyncResult *cb_ares;
1627                                                         cb_ares = create_simple_asyncresult ((MonoObject *) state->callback,
1628                                                                                                 (MonoObject *) state);
1629                                                         icall_append_job ((MonoObject *) cb_ares);
1630                                                 }
1631                                         }
1632                                         mono_domain_set (mono_get_root_domain (), TRUE);
1633                                 }
1634                                 mono_thread_pop_appdomain_ref ();
1635                                 InterlockedDecrement (&tp->busy_threads);
1636                                 clear_thread_state ();
1637                         }
1638                 }
1639
1640                 ar = NULL;
1641                 data = NULL;
1642                 must_die = should_i_die (tp);
1643                 if (must_die) {
1644                         mono_wsq_suspend (wsq);
1645                 } else {
1646                         if (tp->is_io || !mono_wsq_local_pop (&data))
1647                                 dequeue_or_steal (tp, &data, wsq);
1648                 }
1649
1650                 n_naps = 0;
1651                 while (!must_die && !data && n_naps < 4) {
1652                         gboolean res;
1653
1654                         InterlockedIncrement (&tp->waiting);
1655
1656                         // Another thread may have added a job into its wsq since the last call to dequeue_or_steal
1657                         // Check all the queues again before entering the wait loop
1658                         dequeue_or_steal (tp, &data, wsq);
1659                         if (data) {
1660                                 InterlockedDecrement (&tp->waiting);
1661                                 break;
1662                         }
1663
1664                         mono_gc_set_skip_thread (TRUE);
1665                         MONO_PREPARE_BLOCKING
1666
1667 #if defined(__OpenBSD__)
1668                         while (mono_cq_count (tp->queue) == 0 && (res = mono_sem_wait (&tp->new_job, TRUE)) == -1) {// && errno == EINTR) {
1669 #else
1670                         while (mono_cq_count (tp->queue) == 0 && (res = mono_sem_timedwait (&tp->new_job, 2000, TRUE)) == -1) {// && errno == EINTR) {
1671 #endif
1672                                 if (mono_runtime_is_shutting_down ())
1673                                         break;
1674                                 check_for_interruption_critical ();
1675                         }
1676                         InterlockedDecrement (&tp->waiting);
1677
1678                         MONO_FINISH_BLOCKING
1679                         mono_gc_set_skip_thread (FALSE);
1680
1681                         if (mono_runtime_is_shutting_down ())
1682                                 break;
1683                         must_die = should_i_die (tp);
1684                         dequeue_or_steal (tp, &data, wsq);
1685                         n_naps++;
1686                 }
1687
1688                 if (!data && !tp->is_io && !mono_runtime_is_shutting_down ()) {
1689                         mono_wsq_local_pop (&data);
1690                         if (data && must_die) {
1691                                 InterlockedCompareExchange (&tp->destroy_thread, 1, 0);
1692                                 pulse_on_new_job (tp);
1693                         }
1694                 }
1695
1696                 if (!data) {
1697                         gint nt;
1698                         gboolean down;
1699                         while (1) {
1700                                 nt = tp->nthreads;
1701                                 down = mono_runtime_is_shutting_down ();
1702                                 if (!down && nt <= tp->min_threads)
1703                                         break;
1704                                 if (down || InterlockedCompareExchange (&tp->nthreads, nt - 1, nt) == nt) {
1705 #ifndef DISABLE_PERFCOUNTERS
1706                                         mono_perfcounter_update_value (tp->pc_nthreads, TRUE, -1);
1707 #endif
1708                                         if (!tp->is_io) {
1709                                                 remove_wsq (wsq);
1710                                         }
1711
1712                                         fire_profiler_thread_end ();
1713
1714                                         if (tp_finish_func)
1715                                                 tp_finish_func (tp_hooks_user_data);
1716
1717                                         if (!tp->is_io) {
1718                                                 if (threads) {
1719                                                         mono_mutex_lock (&threads_lock);
1720                                                         if (threads)
1721                                                                 g_ptr_array_remove_fast (threads, mono_thread_current ()->internal_thread);
1722                                                         mono_mutex_unlock (&threads_lock);
1723                                                 }
1724                                         }
1725
1726                                         return;
1727                                 }
1728                         }
1729                 }
1730         }
1731
1732         g_assert_not_reached ();
1733 }
1734
1735 void
1736 ves_icall_System_Threading_ThreadPool_GetAvailableThreads (gint *workerThreads, gint *completionPortThreads)
1737 {
1738         *workerThreads = async_tp.max_threads - async_tp.busy_threads;
1739         *completionPortThreads = async_io_tp.max_threads - async_io_tp.busy_threads;
1740 }
1741
1742 void
1743 ves_icall_System_Threading_ThreadPool_GetMaxThreads (gint *workerThreads, gint *completionPortThreads)
1744 {
1745         *workerThreads = async_tp.max_threads;
1746         *completionPortThreads = async_io_tp.max_threads;
1747 }
1748
1749 void
1750 ves_icall_System_Threading_ThreadPool_GetMinThreads (gint *workerThreads, gint *completionPortThreads)
1751 {
1752         *workerThreads = async_tp.min_threads;
1753         *completionPortThreads = async_io_tp.min_threads;
1754 }
1755
1756 MonoBoolean
1757 ves_icall_System_Threading_ThreadPool_SetMinThreads (gint workerThreads, gint completionPortThreads)
1758 {
1759         gint max_threads;
1760         gint max_io_threads;
1761
1762         max_threads = async_tp.max_threads;
1763         if (workerThreads <= 0 || workerThreads > max_threads)
1764                 return FALSE;
1765
1766         max_io_threads = async_io_tp.max_threads;
1767         if (completionPortThreads <= 0 || completionPortThreads > max_io_threads)
1768                 return FALSE;
1769
1770         InterlockedExchange (&async_tp.min_threads, workerThreads);
1771         InterlockedExchange (&async_io_tp.min_threads, completionPortThreads);
1772         if (workerThreads > async_tp.nthreads)
1773                 mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, &async_tp, TRUE, SMALL_STACK);
1774         if (completionPortThreads > async_io_tp.nthreads)
1775                 mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, &async_io_tp, TRUE, SMALL_STACK);
1776         return TRUE;
1777 }
1778
1779 MonoBoolean
1780 ves_icall_System_Threading_ThreadPool_SetMaxThreads (gint workerThreads, gint completionPortThreads)
1781 {
1782         gint min_threads;
1783         gint min_io_threads;
1784         gint cpu_count;
1785
1786         cpu_count = mono_cpu_count ();
1787         min_threads = async_tp.min_threads;
1788         if (workerThreads < min_threads || workerThreads < cpu_count)
1789                 return FALSE;
1790
1791         /* We don't really have the concept of completion ports. Do we care here? */
1792         min_io_threads = async_io_tp.min_threads;
1793         if (completionPortThreads < min_io_threads || completionPortThreads < cpu_count)
1794                 return FALSE;
1795
1796         InterlockedExchange (&async_tp.max_threads, workerThreads);
1797         InterlockedExchange (&async_io_tp.max_threads, completionPortThreads);
1798         return TRUE;
1799 }
1800
1801 /**
1802  * mono_install_threadpool_thread_hooks
1803  * @start_func: the function to be called right after a new threadpool thread is created. Can be NULL.
1804  * @finish_func: the function to be called right before a thredpool thread is exiting. Can be NULL.
1805  * @user_data: argument passed to @start_func and @finish_func.
1806  *
1807  * @start_fun will be called right after a threadpool thread is created and @finish_func right before a threadpool thread exits.
1808  * The calls will be made from the thread itself.
1809  */
1810 void
1811 mono_install_threadpool_thread_hooks (MonoThreadPoolFunc start_func, MonoThreadPoolFunc finish_func, gpointer user_data)
1812 {
1813         tp_start_func = start_func;
1814         tp_finish_func = finish_func;
1815         tp_hooks_user_data = user_data;
1816 }
1817
1818 /**
1819  * mono_install_threadpool_item_hooks
1820  * @begin_func: the function to be called before a threadpool work item processing starts.
1821  * @end_func: the function to be called after a threadpool work item is finished.
1822  * @user_data: argument passed to @begin_func and @end_func.
1823  *
1824  * The calls will be made from the thread itself and from the same AppDomain
1825  * where the work item was executed.
1826  *
1827  */
1828 void
1829 mono_install_threadpool_item_hooks (MonoThreadPoolItemFunc begin_func, MonoThreadPoolItemFunc end_func, gpointer user_data)
1830 {
1831         tp_item_begin_func = begin_func;
1832         tp_item_end_func = end_func;
1833         tp_item_user_data = user_data;
1834 }
1835
1836 void
1837 mono_internal_thread_unhandled_exception (MonoObject* exc)
1838 {
1839         if (mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_CURRENT) {
1840                 gboolean unloaded;
1841                 MonoClass *klass;
1842
1843                 klass = exc->vtable->klass;
1844                 unloaded = is_appdomainunloaded_exception (exc->vtable->domain, klass);
1845                 if (!unloaded && klass != mono_defaults.threadabortexception_class) {
1846                         mono_unhandled_exception (exc);
1847                         if (mono_environment_exitcode_get () == 1)
1848                                 exit (255);
1849                 }
1850                 if (klass == mono_defaults.threadabortexception_class)
1851                  mono_thread_internal_reset_abort (mono_thread_internal_current ());
1852         }
1853 }
1854
1855 /*
1856  * Suspend creation of new threads.
1857  */
1858 void
1859 mono_thread_pool_suspend (void)
1860 {
1861         if (use_ms_threadpool ()) {
1862                 mono_threadpool_ms_suspend ();
1863                 return;
1864         }
1865         suspended = TRUE;
1866 }
1867
1868 /*
1869  * Resume creation of new threads.
1870  */
1871 void
1872 mono_thread_pool_resume (void)
1873 {
1874         if (use_ms_threadpool ()) {
1875                 mono_threadpool_ms_resume ();
1876                 return;
1877         }
1878         suspended = FALSE;
1879 }