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