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