2009-12-04 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / threadpool.c
index 612090b0cececef2c8c028a705563c1d325f1cda..4bada712599d22932d262dd2356cf68299fb4f90 100644 (file)
@@ -5,19 +5,16 @@
  *   Dietmar Maurer (dietmar@ximian.com)
  *   Gonzalo Paniagua Javier (gonzalo@ximian.com)
  *
- * (C) 2001-2003 Ximian, Inc.
- * (c) 2004,2005 Novell, Inc. (http://www.novell.com)
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 
 #include <config.h>
 #include <glib.h>
 
-#ifdef PLATFORM_WIN32
-#define WINVER 0x0500
-#define _WIN32_WINNT 0x0500
-#endif
-
-#define THREADS_PER_CPU        5 /* 20 + THREADS_PER_CPU * number of CPUs */
+#define THREADS_PER_CPU        10 /* 8 + THREADS_PER_CPU * number of CPUs = max threads */
+#define THREAD_EXIT_TIMEOUT 1000
+#define INITIAL_QUEUE_LENGTH 128
 
 #include <mono/metadata/domain-internals.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/file-io.h>
 #include <mono/metadata/monitor.h>
+#include <mono/metadata/mono-mlist.h>
 #include <mono/metadata/marshal.h>
+#include <mono/metadata/mono-perfcounters.h>
 #include <mono/metadata/socket-io.h>
 #include <mono/io-layer/io-layer.h>
-#include <mono/os/gc_wrapper.h>
+#include <mono/metadata/gc-internal.h>
+#include <mono/utils/mono-time.h>
+#include <mono/utils/mono-proclib.h>
 #include <errno.h>
+#ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
+#endif
 #include <sys/types.h>
 #include <fcntl.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <string.h>
-
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
 #include <mono/utils/mono-poll.h>
 #ifdef HAVE_EPOLL
 #include <sys/epoll.h>
 #endif
 
+#ifndef DISABLE_SOCKETS
 #include "mono/io-layer/socket-wrappers.h"
+#endif
 
 #include "threadpool.h"
 
                                                ThreadState_SuspendRequested)) != 0)
 
 #undef EPOLL_DEBUG
-
-/* maximum number of worker threads */
-static int mono_max_worker_threads;
-static int mono_min_worker_threads;
-static int mono_io_max_worker_threads;
-
-/* current number of worker threads */
-static int mono_worker_threads = 0;
-static int io_worker_threads = 0;
-
-/* current number of busy threads */
-static int busy_worker_threads = 0;
-static int busy_io_worker_threads;
+//
+/* map of CounterSample.cs */
+struct _MonoCounterSample {
+       gint64 rawValue;
+       gint64 baseValue;
+       gint64 counterFrequency;
+       gint64 systemFrequency;
+       gint64 timeStamp;
+       gint64 timeStamp100nSec;
+       gint64 counterTimeStamp;
+       int counterType;
+};
 
 /* mono_thread_pool_init called */
 static int tp_inited;
 
-/* we use this to store a reference to the AsyncResult to avoid GC */
-static MonoGHashTable *ares_htable = NULL;
-
-static CRITICAL_SECTION ares_lock;
-static CRITICAL_SECTION io_queue_lock;
 static int pending_io_items;
 
 typedef struct {
        CRITICAL_SECTION io_lock; /* access to sock_to_state */
        int inited;
        int pipe [2];
-       GHashTable *sock_to_state;
+       MonoGHashTable *sock_to_state;
 
        HANDLE new_sem; /* access to newpfd and write side of the pipe */
        mono_pollfd *newpfd;
@@ -91,10 +94,6 @@ typedef struct {
 
 static SocketIOData socket_io_data;
 
-/* we append a job */
-static HANDLE job_added;
-static HANDLE io_job_added;
-
 /* Keep in sync with the System.MonoAsyncCall class which provides GC tracking */
 typedef struct {
        MonoObject         object;
@@ -105,22 +104,64 @@ typedef struct {
        MonoObject        *res;
        MonoArray         *out_args;
        /* This is a HANDLE, we use guint64 so the managed object layout remains constant */
+       /* THIS FIELD IS NOT USED ANY MORE. Remove it when we feel like breaking corlib compatibility with 2.6 */
        guint64           wait_event;
 } ASyncCall;
 
+typedef struct {
+       CRITICAL_SECTION lock;
+       MonoArray *array;
+       int first_elem;
+       int next_elem;
+
+       /**/
+       GQueue *idle_threads;
+       int idle_started; /* Have we started the idle threads? Interlocked */
+       /* min, max, n and busy -> Interlocked */
+       int min_threads;
+       int max_threads;
+       int nthreads;
+       int busy_threads;
+
+       void (*async_invoke) (gpointer data);
+       void *pc_nitems; /* Performance counter for total number of items in added */
+       /* We don't need the rate here since we can compute the different ourselves */
+       /* void *perfc_rate; */
+       MonoCounterSample last_sample;
+
+} ThreadPool;
+
+static ThreadPool async_tp;
+static ThreadPool async_io_tp;
+
+typedef struct {
+       HANDLE wait_handle;
+       gpointer data;
+       gint timeout;
+       gboolean die;
+} IdleThreadData;
 static void async_invoke_thread (gpointer data);
-static void append_job (CRITICAL_SECTION *cs, GList **plist, gpointer ar);
-static void start_thread_or_queue (MonoAsyncResult *ares);
 static void mono_async_invoke (MonoAsyncResult *ares);
-static gpointer dequeue_job (CRITICAL_SECTION *cs, GList **plist);
-
-static GList *async_call_queue = NULL;
-static GList *async_io_queue = NULL;
+static void threadpool_free_queue (ThreadPool *tp);
+static void threadpool_append_job (ThreadPool *tp, MonoObject *ar);
+static void *threadpool_queue_idle_thread (ThreadPool *tp, IdleThreadData *it);
+static void threadpool_init (ThreadPool *tp, int min_threads, int max_threads, void (*async_invoke) (gpointer));
+static void threadpool_start_idle_threads (ThreadPool *tp);
+static void threadpool_kill_idle_threads (ThreadPool *tp);
 
 static MonoClass *async_call_klass;
 static MonoClass *socket_async_call_klass;
 static MonoClass *process_async_call_klass;
 
+/* Hooks */
+static MonoThreadPoolFunc tp_start_func;
+static MonoThreadPoolFunc tp_finish_func;
+static gpointer tp_hooks_user_data;
+static MonoThreadPoolItemFunc tp_item_begin_func;
+static MonoThreadPoolItemFunc tp_item_end_func;
+static gpointer tp_item_user_data;
+
 #define INIT_POLLFD(a, b, c) {(a)->fd = b; (a)->events = c; (a)->revents = 0;}
 enum {
        AIO_OP_FIRST,
@@ -136,17 +177,18 @@ enum {
        AIO_OP_LAST
 };
 
+#ifdef DISABLE_SOCKETS
+#define socket_io_cleanup(x)
+#else
 static void
 socket_io_cleanup (SocketIOData *data)
 {
-       gint release;
-
        if (data->inited == 0)
                return;
 
        EnterCriticalSection (&data->io_lock);
        data->inited = 0;
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
        closesocket (data->pipe [0]);
        closesocket (data->pipe [1]);
 #else
@@ -158,13 +200,12 @@ socket_io_cleanup (SocketIOData *data)
        if (data->new_sem)
                CloseHandle (data->new_sem);
        data->new_sem = NULL;
-       g_hash_table_destroy (data->sock_to_state);
+       mono_g_hash_table_destroy (data->sock_to_state);
        data->sock_to_state = NULL;
-       g_list_free (async_io_queue);
-       async_io_queue = NULL;
-       release = (gint) InterlockedCompareExchange (&io_worker_threads, 0, -1);
-       if (io_job_added)
-               ReleaseSemaphore (io_job_added, release, NULL);
+       EnterCriticalSection (&async_io_tp.lock);
+       threadpool_free_queue (&async_io_tp);
+       threadpool_kill_idle_threads (&async_io_tp);
+       LeaveCriticalSection (&async_io_tp.lock);
        g_free (data->newpfd);
        data->newpfd = NULL;
 #ifdef HAVE_EPOLL
@@ -196,37 +237,65 @@ get_event_from_state (MonoSocketAsyncResult *state)
 }
 
 static int
-get_events_from_list (GSList *list)
+get_events_from_list (MonoMList *list)
 {
        MonoSocketAsyncResult *state;
        int events = 0;
 
-       while (list && list->data) {
-               state = (MonoSocketAsyncResult *) list->data;
+       while (list && (state = (MonoSocketAsyncResult *)mono_mlist_get_data (list))) {
                events |= get_event_from_state (state);
-               list = list->next;
+               list = mono_mlist_next (list);
        }
 
        return events;
 }
 
 #define ICALL_RECV(x)  ves_icall_System_Net_Sockets_Socket_Receive_internal (\
-                               (SOCKET) x->handle, x->buffer, x->offset, x->size,\
+                               (SOCKET)(gssize)x->handle, x->buffer, x->offset, x->size,\
                                 x->socket_flags, &x->error);
 
 #define ICALL_SEND(x)  ves_icall_System_Net_Sockets_Socket_Send_internal (\
-                               (SOCKET) x->handle, x->buffer, x->offset, x->size,\
+                               (SOCKET)(gssize)x->handle, x->buffer, x->offset, x->size,\
                                 x->socket_flags, &x->error);
 
+#endif /* !DISABLE_SOCKETS */
+
+static void
+threadpool_jobs_inc (MonoObject *obj)
+{
+       if (obj)
+               InterlockedIncrement (&obj->vtable->domain->threadpool_jobs);
+}
+
+static gboolean
+threadpool_jobs_dec (MonoObject *obj)
+{
+       MonoDomain *domain = obj->vtable->domain;
+       int remaining_jobs = InterlockedDecrement (&domain->threadpool_jobs);
+       if (remaining_jobs == 0 && domain->cleanup_semaphore) {
+               ReleaseSemaphore (domain->cleanup_semaphore, 1, NULL);
+               return TRUE;
+       }
+       return FALSE;
+}
+
+#ifndef DISABLE_SOCKETS
 static void
 async_invoke_io_thread (gpointer data)
 {
        MonoDomain *domain;
-       MonoThread *thread;
-       thread = mono_thread_current ();
-       thread->threadpool_thread = TRUE;
-       ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
-
+       MonoInternalThread *thread;
+       const gchar *version;
+       IdleThreadData idle_data = {0};
+  
+       idle_data.timeout = INFINITE;
+       idle_data.wait_handle = CreateEvent (NULL, FALSE, FALSE, NULL);
+
+       thread = mono_thread_internal_current ();
+       if (tp_start_func)
+               tp_start_func (tp_hooks_user_data);
+
+       version = mono_get_runtime_info ()->framework_version;
        for (;;) {
                MonoSocketAsyncResult *state;
                MonoAsyncResult *ar;
@@ -247,103 +316,94 @@ async_invoke_io_thread (gpointer data)
                        /* worker threads invokes methods in different domains,
                         * so we need to set the right domain here */
                        domain = ((MonoObject *)ar)->vtable->domain;
-                       mono_thread_push_appdomain_ref (domain);
-                       if (mono_domain_set (domain, FALSE)) {
-                               ASyncCall *ac;
-
-                               mono_async_invoke (ar);
-                               ac = (ASyncCall *) ar->object_data;
-                               /*
-                               if (ac->msg->exc != NULL)
-                                       mono_unhandled_exception (ac->msg->exc);
-                               */
-                               mono_domain_set (mono_get_root_domain (), TRUE);
+
+                       g_assert (domain);
+
+                       if (domain->state == MONO_APPDOMAIN_UNLOADED || domain->state == MONO_APPDOMAIN_UNLOADING) {
+                               threadpool_jobs_dec ((MonoObject *)ar);
+                               data = NULL;
+                       } else {
+                               mono_thread_push_appdomain_ref (domain);
+                               if (threadpool_jobs_dec ((MonoObject *)ar)) {
+                                       data = NULL;
+                                       mono_thread_pop_appdomain_ref ();
+                                       continue;
+                               }
+                               if (mono_domain_set (domain, FALSE)) {
+                                       /* ASyncCall *ac; */
+
+                                       if (tp_item_begin_func)
+                                               tp_item_begin_func (tp_item_user_data);
+                                       mono_async_invoke (ar);
+                                       if (tp_item_end_func)
+                                               tp_item_end_func (tp_item_user_data);
+                                       /*
+                                       ac = (ASyncCall *) ar->object_data;
+                                       if (ac->msg->exc != NULL)
+                                               mono_unhandled_exception (ac->msg->exc);
+                                       */
+                                       mono_domain_set (mono_get_root_domain (), TRUE);
+                               }
+                               mono_thread_pop_appdomain_ref ();
+                               InterlockedDecrement (&async_io_tp.busy_threads);
+                               /* If the callee changes the background status, set it back to TRUE */
+                               if (*version != '1' && !mono_thread_test_state (thread , ThreadState_Background))
+                                       ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
                        }
-                       mono_thread_pop_appdomain_ref ();
-                       InterlockedDecrement (&busy_io_worker_threads);
                }
 
-               data = dequeue_job (&io_queue_lock, &async_io_queue);
-       
-               if (!data) {
+               data = threadpool_queue_idle_thread (&async_io_tp, &idle_data);
+               while (!idle_data.die && !data) {
                        guint32 wr;
-                       int timeout = 10000;
-                       guint32 start_time = GetTickCount ();
-                       
-                       do {
-                               wr = WaitForSingleObjectEx (io_job_added, (guint32)timeout, TRUE);
-                               if (THREAD_WANTS_A_BREAK (thread))
-                                       mono_thread_interruption_checkpoint ();
-                       
-                               timeout -= GetTickCount () - start_time;
-                       
-                               if (wr != WAIT_TIMEOUT)
-                                       data = dequeue_job (&io_queue_lock, &async_io_queue);
+                       wr = WaitForSingleObjectEx (idle_data.wait_handle, idle_data.timeout, TRUE);
+                       if (THREAD_WANTS_A_BREAK (thread))
+                               mono_thread_interruption_checkpoint ();
+               
+                       if (wr != WAIT_TIMEOUT && wr != WAIT_IO_COMPLETION) {
+                               data = idle_data.data;
+                               idle_data.data = NULL;
+                               break; /* We have to exit */
                        }
-                       while (!data && timeout > 0);
                }
 
                if (!data) {
-                       if (InterlockedDecrement (&io_worker_threads) < 2) {
-                               /* If we have pending items, keep the thread alive */
-                               if (InterlockedCompareExchange (&pending_io_items, 0, 0) != 0) {
-                                       InterlockedIncrement (&io_worker_threads);
-                                       continue;
-                               }
-                       }
+                       InterlockedDecrement (&async_io_tp.nthreads);
+                       CloseHandle (idle_data.wait_handle);
+                       idle_data.wait_handle = NULL;
+                       if (tp_finish_func)
+                               tp_finish_func (tp_hooks_user_data);
                        return;
                }
                
-               InterlockedIncrement (&busy_io_worker_threads);
+               InterlockedIncrement (&async_io_tp.busy_threads);
        }
 
        g_assert_not_reached ();
 }
 
-static void
-start_io_thread_or_queue (MonoSocketAsyncResult *ares)
-{
-       int busy, worker;
-       MonoDomain *domain;
-
-       busy = (int) InterlockedCompareExchange (&busy_io_worker_threads, 0, -1);
-       worker = (int) InterlockedCompareExchange (&io_worker_threads, 0, -1); 
-       if (worker <= ++busy &&
-           worker < mono_io_max_worker_threads) {
-               InterlockedIncrement (&busy_io_worker_threads);
-               InterlockedIncrement (&io_worker_threads);
-               domain = ((ares) ? ((MonoObject *) ares)->vtable->domain : mono_domain_get ());
-               mono_thread_create (mono_get_root_domain (), async_invoke_io_thread, ares);
-       } else {
-               append_job (&io_queue_lock, &async_io_queue, ares);
-               ReleaseSemaphore (io_job_added, 1, NULL);
-       }
-}
-
-static GSList *
-process_io_event (GSList *list, int event)
+static MonoMList *
+process_io_event (MonoMList *list, int event)
 {
        MonoSocketAsyncResult *state;
-       GSList *oldlist;
+       MonoMList *oldlist;
 
        oldlist = list;
        state = NULL;
        while (list) {
-               state = (MonoSocketAsyncResult *) list->data;
+               state = (MonoSocketAsyncResult *) mono_mlist_get_data (list);
                if (get_event_from_state (state) == event)
                        break;
                
-               list = list->next;
+               list = mono_mlist_next (list);
        }
 
        if (list != NULL) {
-               oldlist = g_slist_remove_link (oldlist, list);
-               g_slist_free_1 (list);
+               oldlist = mono_mlist_remove_item (oldlist, list);
 #ifdef EPOLL_DEBUG
-               g_print ("Dispatching event %d on socket %d\n", event, state->handle);
+               g_print ("Dispatching event %d on socket %p\n", event, state->handle);
 #endif
                InterlockedIncrement (&pending_io_items);
-               start_io_thread_or_queue (state);
+               threadpool_append_job (&async_io_tp, (MonoObject *) state);
        }
 
        return oldlist;
@@ -383,11 +443,9 @@ socket_io_poll_main (gpointer p)
        gint maxfd = 1;
        gint allocated;
        gint i;
-       MonoThread *thread;
+       MonoInternalThread *thread;
 
-       thread = mono_thread_current ();
-       thread->threadpool_thread = TRUE;
-       ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
+       thread = mono_thread_internal_current ();
 
        allocated = INITIAL_POLLFD_SIZE;
        pfds = g_new0 (mono_pollfd, allocated);
@@ -399,7 +457,7 @@ socket_io_poll_main (gpointer p)
                int nsock = 0;
                mono_pollfd *pfd;
                char one [1];
-               GSList *list;
+               MonoMList *list;
 
                do {
                        if (nsock == -1) {
@@ -457,7 +515,7 @@ socket_io_poll_main (gpointer p)
                                for (; i < allocated; i++)
                                        INIT_POLLFD (&pfds [i], -1, 0);
                        }
-#ifndef PLATFORM_WIN32
+#ifndef HOST_WIN32
                        nread = read (data->pipe [0], one, 1);
 #else
                        nread = recv ((SOCKET) data->pipe [0], one, 1, 0);
@@ -480,6 +538,7 @@ socket_io_poll_main (gpointer p)
                EnterCriticalSection (&data->io_lock);
                if (data->inited == 0) {
                        g_free (pfds);
+                       LeaveCriticalSection (&data->io_lock);
                        return; /* cleanup called */
                }
 
@@ -489,7 +548,7 @@ socket_io_poll_main (gpointer p)
                                continue;
 
                        nsock--;
-                       list = g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (pfd->fd));
+                       list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (pfd->fd));
                        if (list != NULL && (pfd->revents & (MONO_POLLIN | POLL_ERRORS)) != 0) {
                                list = process_io_event (list, MONO_POLLIN);
                        }
@@ -499,10 +558,10 @@ socket_io_poll_main (gpointer p)
                        }
 
                        if (list != NULL) {
-                               g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (pfd->fd), list);
+                               mono_g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (pfd->fd), list);
                                pfd->events = get_events_from_list (list);
                        } else {
-                               g_hash_table_remove (data->sock_to_state, GINT_TO_POINTER (pfd->fd));
+                               mono_g_hash_table_remove (data->sock_to_state, GINT_TO_POINTER (pfd->fd));
                                pfd->fd = -1;
                                if (i == maxfd - 1)
                                        maxfd--;
@@ -519,16 +578,14 @@ socket_io_epoll_main (gpointer p)
 {
        SocketIOData *data;
        int epollfd;
-       MonoThread *thread;
+       MonoInternalThread *thread;
        struct epoll_event *events, *evt;
        const int nevents = 512;
        int ready = 0, i;
 
        data = p;
        epollfd = data->epollfd;
-       thread = mono_thread_current ();
-       thread->threadpool_thread = TRUE;
-       ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
+       thread = mono_thread_internal_current ();
        events = g_new0 (struct epoll_event, nevents);
 
        while (1) {
@@ -572,13 +629,13 @@ socket_io_epoll_main (gpointer p)
 
                for (i = 0; i < ready; i++) {
                        int fd;
-                       GSList *list;
+                       MonoMList *list;
 
                        evt = &events [i];
                        fd = evt->data.fd;
-                       list = g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
+                       list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
 #ifdef EPOLL_DEBUG
-                       g_print ("Event %d on %d list length: %d\n", evt->events, fd, g_slist_length (list));
+                       g_print ("Event %d on %d list length: %d\n", evt->events, fd, mono_mlist_length (list));
 #endif
                        if (list != NULL && (evt->events & (EPOLLIN | EPOLL_ERRORS)) != 0) {
                                list = process_io_event (list, MONO_POLLIN);
@@ -589,7 +646,7 @@ socket_io_epoll_main (gpointer p)
                        }
 
                        if (list != NULL) {
-                               g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (fd), list);
+                               mono_g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (fd), list);
                                evt->events = get_events_from_list (list);
 #ifdef EPOLL_DEBUG
                                g_print ("MOD %d to %d\n", fd, evt->events);
@@ -604,7 +661,7 @@ socket_io_epoll_main (gpointer p)
                                        }
                                }
                        } else {
-                               g_hash_table_remove (data->sock_to_state, GINT_TO_POINTER (fd));
+                               mono_g_hash_table_remove (data->sock_to_state, GINT_TO_POINTER (fd));
 #ifdef EPOLL_DEBUG
                                g_print ("DEL %d\n", fd);
 #endif
@@ -623,38 +680,36 @@ socket_io_epoll_main (gpointer p)
 void
 mono_thread_pool_remove_socket (int sock)
 {
-#ifdef HAVE_EPOLL
-       GSList *list, *next;
+       MonoMList *list, *next;
        MonoSocketAsyncResult *state;
 
-       if (socket_io_data.epoll_disabled == TRUE || socket_io_data.inited == FALSE)
+       if (socket_io_data.inited == FALSE)
                return;
 
        EnterCriticalSection (&socket_io_data.io_lock);
-       list = g_hash_table_lookup (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
+       list = mono_g_hash_table_lookup (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
        if (list) {
-               g_hash_table_remove (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
+               mono_g_hash_table_remove (socket_io_data.sock_to_state, GINT_TO_POINTER (sock));
        }
        LeaveCriticalSection (&socket_io_data.io_lock);
        
        while (list) {
-               state = (MonoSocketAsyncResult *) list->data;
+               state = (MonoSocketAsyncResult *) mono_mlist_get_data (list);
                if (state->operation == AIO_OP_RECEIVE)
                        state->operation = AIO_OP_RECV_JUST_CALLBACK;
                else if (state->operation == AIO_OP_SEND)
                        state->operation = AIO_OP_SEND_JUST_CALLBACK;
 
-               next = g_slist_remove_link (list, list);
+               next = mono_mlist_remove_item (list, list);
                list = process_io_event (list, MONO_POLLIN);
                if (list)
                        process_io_event (list, MONO_POLLOUT);
 
                list = next;
        }
-#endif
 }
 
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
 static void
 connect_hack (gpointer x)
 {
@@ -674,7 +729,7 @@ connect_hack (gpointer x)
 static void
 socket_io_init (SocketIOData *data)
 {
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
        struct sockaddr_in server;
        struct sockaddr_in client;
        SOCKET srv;
@@ -707,7 +762,7 @@ socket_io_init (SocketIOData *data)
        data->epoll_disabled = TRUE;
 #endif
 
-#ifndef PLATFORM_WIN32
+#ifndef HOST_WIN32
        if (data->epoll_disabled) {
                if (pipe (data->pipe) != 0) {
                        int err = errno;
@@ -741,22 +796,19 @@ socket_io_init (SocketIOData *data)
        g_assert (data->pipe [0] != INVALID_SOCKET);
        closesocket (srv);
 #endif
-       mono_io_max_worker_threads = mono_max_worker_threads / 2;
-       if (mono_io_max_worker_threads < 10)
-               mono_io_max_worker_threads = 10;
+       data->sock_to_state = mono_g_hash_table_new_type (g_direct_hash, g_direct_equal, MONO_HASH_VALUE_GC);
+       mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, &async_io_tp, TRUE);
 
-       data->sock_to_state = g_hash_table_new (g_direct_hash, g_direct_equal);
-
-       if (data->epoll_disabled)
+       if (data->epoll_disabled) {
                data->new_sem = CreateSemaphore (NULL, 1, 1, NULL);
-       io_job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
-       InitializeCriticalSection (&io_queue_lock);
+               g_assert (data->new_sem != NULL);
+       }
        if (data->epoll_disabled) {
-               mono_thread_create (mono_get_root_domain (), socket_io_poll_main, data);
+               mono_thread_create_internal (mono_get_root_domain (), socket_io_poll_main, data, TRUE);
        }
 #ifdef HAVE_EPOLL
        else {
-               mono_thread_create (mono_get_root_domain (), socket_io_epoll_main, data);
+               mono_thread_create_internal (mono_get_root_domain (), socket_io_epoll_main, data, TRUE);
        }
 #endif
        InterlockedCompareExchange (&data->inited, 1, 0);
@@ -768,15 +820,16 @@ socket_io_add_poll (MonoSocketAsyncResult *state)
 {
        int events;
        char msg [1];
-       GSList *list;
+       MonoMList *list;
        SocketIOData *data = &socket_io_data;
+       int w;
 
-#if defined(PLATFORM_MACOSX) || defined(PLATFORM_BSD6) || defined(PLATFORM_WIN32)
+#if defined(PLATFORM_MACOSX) || defined(PLATFORM_BSD) || defined(HOST_WIN32) || defined(PLATFORM_SOLARIS)
        /* select() for connect() does not work well on the Mac. Bug #75436. */
        /* Bug #77637 for the BSD 6 case */
        /* Bug #78888 for the Windows case */
        if (state->operation == AIO_OP_CONNECT && state->blocking == TRUE) {
-               start_io_thread_or_queue (state);
+               threadpool_append_job (&async_io_tp, (MonoObject *) state);
                return;
        }
 #endif
@@ -785,21 +838,22 @@ socket_io_add_poll (MonoSocketAsyncResult *state)
                data->newpfd = g_new0 (mono_pollfd, 1);
 
        EnterCriticalSection (&data->io_lock);
-       list = g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (state->handle));
+       /* FIXME: 64 bit issue: handle can be a pointer on windows? */
+       list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (state->handle));
        if (list == NULL) {
-               list = g_slist_alloc ();
-               list->data = state;
+               list = mono_mlist_alloc ((MonoObject*)state);
        } else {
-               list = g_slist_append (list, state);
+               list = mono_mlist_append (list, (MonoObject*)state);
        }
 
        events = get_events_from_list (list);
        INIT_POLLFD (data->newpfd, GPOINTER_TO_INT (state->handle), events);
-       g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (state->handle), list);
+       mono_g_hash_table_replace (data->sock_to_state, GINT_TO_POINTER (state->handle), list);
        LeaveCriticalSection (&data->io_lock);
        *msg = (char) state->operation;
-#ifndef PLATFORM_WIN32
-       write (data->pipe [1], msg, 1);
+#ifndef HOST_WIN32
+       w = write (data->pipe [1], msg, 1);
+       w = w;
 #else
        send ((SOCKET) data->pipe [1], msg, 1, 0);
 #endif
@@ -809,7 +863,7 @@ socket_io_add_poll (MonoSocketAsyncResult *state)
 static gboolean
 socket_io_add_epoll (MonoSocketAsyncResult *state)
 {
-       GSList *list;
+       MonoMList *list;
        SocketIOData *data = &socket_io_data;
        struct epoll_event event;
        int epoll_op, ievt;
@@ -818,13 +872,12 @@ socket_io_add_epoll (MonoSocketAsyncResult *state)
        memset (&event, 0, sizeof (struct epoll_event));
        fd = GPOINTER_TO_INT (state->handle);
        EnterCriticalSection (&data->io_lock);
-       list = g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
+       list = mono_g_hash_table_lookup (data->sock_to_state, GINT_TO_POINTER (fd));
        if (list == NULL) {
-               list = g_slist_alloc ();
-               list->data = state;
+               list = mono_mlist_alloc ((MonoObject*)state);
                epoll_op = EPOLL_CTL_ADD;
        } else {
-               list = g_slist_append (list, state);
+               list = mono_mlist_append (list, (MonoObject*)state);
                epoll_op = EPOLL_CTL_MOD;
        }
 
@@ -834,7 +887,7 @@ socket_io_add_epoll (MonoSocketAsyncResult *state)
        if ((ievt & MONO_POLLOUT) != 0)
                event.events |= EPOLLOUT;
 
-       g_hash_table_replace (data->sock_to_state, state->handle, list);
+       mono_g_hash_table_replace (data->sock_to_state, state->handle, list);
        event.data.fd = fd;
 #ifdef EPOLL_DEBUG
        g_print ("%s %d with %d\n", epoll_op == EPOLL_CTL_ADD ? "ADD" : "MOD", fd, event.events);
@@ -911,27 +964,38 @@ socket_io_filter (MonoObject *target, MonoObject *state)
 
        return TRUE;
 }
+#endif /* !DISABLE_SOCKETS */
 
 static void
 mono_async_invoke (MonoAsyncResult *ares)
 {
        ASyncCall *ac = (ASyncCall *)ares->object_data;
-       MonoThread *thread = NULL;
+       MonoObject *res, *exc = NULL;
+       MonoArray *out_args = NULL;
+       HANDLE wait_event = NULL;
 
        if (ares->execution_context) {
                /* use captured ExecutionContext (if available) */
-               thread = mono_thread_current ();
-               MONO_OBJECT_SETREF (ares, original_context, thread->execution_context);
-               MONO_OBJECT_SETREF (thread, execution_context, ares->execution_context);
+               MONO_OBJECT_SETREF (ares, original_context, mono_thread_get_execution_context ());
+               mono_thread_set_execution_context (ares->execution_context);
        } else {
                ares->original_context = NULL;
        }
 
        ac->msg->exc = NULL;
-       ac->res = mono_message_invoke (ares->async_delegate, ac->msg, 
-                                      &ac->msg->exc, &ac->out_args);
+       res = mono_message_invoke (ares->async_delegate, ac->msg, &exc, &out_args);
+       MONO_OBJECT_SETREF (ac, res, res);
+       MONO_OBJECT_SETREF (ac, msg->exc, exc);
+       MONO_OBJECT_SETREF (ac, out_args, out_args);
 
+       mono_monitor_enter ((MonoObject *) ares);
        ares->completed = 1;
+       if (ares->handle != NULL)
+               wait_event = mono_wait_handle_get_handle ((MonoWaitHandle *) ares->handle);
+       mono_monitor_exit ((MonoObject *) ares);
+       /* notify listeners */
+       if (wait_event != NULL)
+               SetEvent (wait_event);
 
        /* call async callback if cb_method != null*/
        if (ac->cb_method) {
@@ -946,48 +1010,94 @@ mono_async_invoke (MonoAsyncResult *ares)
 
        /* restore original thread execution context if flow isn't suppressed, i.e. non null */
        if (ares->original_context) {
-               MONO_OBJECT_SETREF (thread, execution_context, ares->original_context);
+               mono_thread_set_execution_context (ares->original_context);
                ares->original_context = NULL;
        }
 
-       /* notify listeners */
-       mono_monitor_enter ((MonoObject *) ares);
-       if (ares->handle != NULL) {
-               ac->wait_event = (gsize)((MonoWaitHandle *) ares->handle)->handle;
-               SetEvent ((gpointer)(gsize)ac->wait_event);
-       }
-       mono_monitor_exit ((MonoObject *) ares);
+}
+
+static void
+threadpool_start_idle_threads (ThreadPool *tp)
+{
+       int needed;
+       int existing;
+
+       needed = (int) InterlockedCompareExchange (&tp->min_threads, 0, -1); 
+       do {
+               existing = (int) InterlockedCompareExchange (&tp->nthreads, 0, -1); 
+               if (existing >= needed)
+                       break;
+               InterlockedIncrement (&tp->nthreads);
+               mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, NULL, TRUE);
+               SleepEx (250, TRUE);
+       } while (1);
+}
+
+static void
+threadpool_init (ThreadPool *tp, int min_threads, int max_threads, void (*async_invoke) (gpointer))
+{
+       memset (tp, 0, sizeof (ThreadPool));
+       InitializeCriticalSection (&tp->lock);
+       tp->min_threads = min_threads;
+       tp->max_threads = max_threads;
+       tp->async_invoke = async_invoke;
+       tp->idle_threads = g_queue_new ();
+}
 
-       EnterCriticalSection (&ares_lock);
-       mono_g_hash_table_remove (ares_htable, ares);
-       LeaveCriticalSection (&ares_lock);
+static void *
+init_perf_counter (const char *category, const char *counter)
+{
+       MonoString *category_str;
+       MonoString *counter_str;
+       MonoString *machine;
+       MonoDomain *root;
+       MonoBoolean custom;
+       int type;
+
+       if (category == NULL || counter == NULL)
+               return NULL;
+       root = mono_get_root_domain ();
+       category_str = mono_string_new (root, category);
+       counter_str = mono_string_new (root, counter);
+       machine = mono_string_new (root, ".");
+       return mono_perfcounter_get_impl (category_str, counter_str, NULL, machine, &type, &custom);
 }
 
 void
 mono_thread_pool_init ()
 {
-       SYSTEM_INFO info;
        int threads_per_cpu = THREADS_PER_CPU;
+       int cpu_count;
+       int n;
 
        if ((int) InterlockedCompareExchange (&tp_inited, 1, 0) == 1)
                return;
 
-       MONO_GC_REGISTER_ROOT (ares_htable);
+       MONO_GC_REGISTER_ROOT (socket_io_data.sock_to_state);
        InitializeCriticalSection (&socket_io_data.io_lock);
-       InitializeCriticalSection (&ares_lock);
-       ares_htable = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_VALUE_GC);
-       job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
-       GetSystemInfo (&info);
        if (g_getenv ("MONO_THREADS_PER_CPU") != NULL) {
                threads_per_cpu = atoi (g_getenv ("MONO_THREADS_PER_CPU"));
-               if (threads_per_cpu <= 0)
+               if (threads_per_cpu < THREADS_PER_CPU)
                        threads_per_cpu = THREADS_PER_CPU;
        }
 
-       mono_max_worker_threads = 20 + threads_per_cpu * info.dwNumberOfProcessors;
+       cpu_count = mono_cpu_count ();
+       n = 8 + 2 * cpu_count; /* 8 is minFreeThreads for ASP.NET */
+       threadpool_init (&async_tp, n, n + threads_per_cpu * cpu_count, async_invoke_thread);
+#ifndef DISABLE_SOCKETS
+       threadpool_init (&async_io_tp, 2 * cpu_count, 8 * cpu_count, async_invoke_io_thread);
+#endif
 
        async_call_klass = mono_class_from_name (mono_defaults.corlib, "System", "MonoAsyncCall");
        g_assert (async_call_klass);
+
+       async_tp.pc_nitems = init_perf_counter ("Mono Threadpool", "Work Items Added");
+       g_assert (async_tp.pc_nitems);
+       mono_perfcounter_get_sample (async_tp.pc_nitems, FALSE, &async_tp.last_sample);
+
+       async_io_tp.pc_nitems = init_perf_counter ("Mono Threadpool", "IO Work Items Added");
+       g_assert (async_io_tp.pc_nitems);
+       mono_perfcounter_get_sample (async_io_tp.pc_nitems, FALSE, &async_io_tp.last_sample);
 }
 
 MonoAsyncResult *
@@ -998,61 +1108,36 @@ mono_thread_pool_add (MonoObject *target, MonoMethodMessage *msg, MonoDelegate *
        MonoAsyncResult *ares;
        ASyncCall *ac;
 
-#ifdef HAVE_BOEHM_GC
-       ac = GC_MALLOC (sizeof (ASyncCall));
-#elif defined(HAVE_SGEN_GC)
-       ac = mono_object_new (mono_domain_get (), async_call_klass);
-#else
-       /* We'll leak the event if creaated... */
-       ac = g_new0 (ASyncCall, 1);
-#endif
-       ac->wait_event = 0;
+       ac = (ASyncCall*)mono_object_new (mono_domain_get (), async_call_klass);
        MONO_OBJECT_SETREF (ac, msg, msg);
        MONO_OBJECT_SETREF (ac, state, state);
 
        if (async_callback) {
                ac->cb_method = mono_get_delegate_invoke (((MonoObject *)async_callback)->vtable->klass);
-               ac->cb_target = async_callback;
+               MONO_OBJECT_SETREF (ac, cb_target, async_callback);
        }
 
        ares = mono_async_result_new (domain, NULL, ac->state, NULL, (MonoObject*)ac);
        MONO_OBJECT_SETREF (ares, async_delegate, target);
 
-       EnterCriticalSection (&ares_lock);
-       mono_g_hash_table_insert (ares_htable, ares, ares);
-       LeaveCriticalSection (&ares_lock);
-
+#ifndef DISABLE_SOCKETS
        if (socket_io_filter (target, state)) {
                socket_io_add (ares, (MonoSocketAsyncResult *) state);
                return ares;
        }
-
-       start_thread_or_queue (ares);
+#endif
+       if (InterlockedCompareExchange (&async_tp.idle_started, 1, 0) == 0)
+               mono_thread_create_internal (mono_get_root_domain (), threadpool_start_idle_threads, &async_tp, TRUE);
+       
+       threadpool_append_job (&async_tp, (MonoObject *) ares);
        return ares;
 }
 
-static void
-start_thread_or_queue (MonoAsyncResult *ares)
-{
-       int busy, worker;
-
-       busy = (int) InterlockedCompareExchange (&busy_worker_threads, 0, -1);
-       worker = (int) InterlockedCompareExchange (&mono_worker_threads, 0, -1); 
-       if (worker <= ++busy &&
-           worker < mono_max_worker_threads) {
-               InterlockedIncrement (&mono_worker_threads);
-               InterlockedIncrement (&busy_worker_threads);
-               mono_thread_create (mono_get_root_domain (), async_invoke_thread, ares);
-       } else {
-               append_job (&mono_delegate_section, &async_call_queue, ares);
-               ReleaseSemaphore (job_added, 1, NULL);
-       }
-}
-
 MonoObject *
 mono_thread_pool_finish (MonoAsyncResult *ares, MonoArray **out_args, MonoObject **exc)
 {
        ASyncCall *ac;
+       HANDLE wait_event;
 
        *exc = NULL;
        *out_args = NULL;
@@ -1068,103 +1153,356 @@ mono_thread_pool_finish (MonoAsyncResult *ares, MonoArray **out_args, MonoObject
        }
 
        ares->endinvoke_called = 1;
-       ac = (ASyncCall *)ares->object_data;
-
-       g_assert (ac != NULL);
-
        /* wait until we are really finished */
        if (!ares->completed) {
                if (ares->handle == NULL) {
-                       ac->wait_event = (gsize)CreateEvent (NULL, TRUE, FALSE, NULL);
-                       MONO_OBJECT_SETREF (ares, handle, (MonoObject *) mono_wait_handle_new (mono_object_domain (ares), (gpointer)(gsize)ac->wait_event));
+                       wait_event = CreateEvent (NULL, TRUE, FALSE, NULL);
+                       g_assert(wait_event != 0);
+                       MONO_OBJECT_SETREF (ares, handle, (MonoObject *) mono_wait_handle_new (mono_object_domain (ares), wait_event));
+               } else {
+                       wait_event = mono_wait_handle_get_handle ((MonoWaitHandle *) ares->handle);
                }
                mono_monitor_exit ((MonoObject *) ares);
-               WaitForSingleObjectEx ((gpointer)(gsize)ac->wait_event, INFINITE, TRUE);
+               WaitForSingleObjectEx (wait_event, INFINITE, TRUE);
        } else {
                mono_monitor_exit ((MonoObject *) ares);
        }
 
+       ac = (ASyncCall *) ares->object_data;
+       g_assert (ac != NULL);
        *exc = ac->msg->exc; /* FIXME: GC add write barrier */
        *out_args = ac->out_args;
 
        return ac->res;
 }
 
+static void
+threadpool_kill_idle_threads (ThreadPool *tp)
+{
+       IdleThreadData *it;
+
+       if (!tp || !tp->idle_threads)
+               return;
+
+       while ((it = g_queue_pop_head (tp->idle_threads)) != NULL) {
+               it->data = NULL;
+               it->die = TRUE;
+               SetEvent (it->wait_handle);
+       }
+       g_queue_free (tp->idle_threads);
+       tp->idle_threads = NULL;
+}
+
 void
 mono_thread_pool_cleanup (void)
 {
-       gint release;
+       EnterCriticalSection (&async_tp.lock);
+       threadpool_free_queue (&async_tp);
+       threadpool_kill_idle_threads (&async_tp);
+       LeaveCriticalSection (&async_tp.lock);
+       socket_io_cleanup (&socket_io_data); /* Empty when DISABLE_SOCKETS is defined */
+       /* Do we want/need these?
+       DeleteCriticalSection (&async_tp.lock);
+       DeleteCriticalSection (&async_tp.table_lock);
+       DeleteCriticalSection (&socket_io_data.io_lock);
+       */
+}
+
+static void
+null_array (MonoArray *a, int first, int last)
+{
+       /* We must null the old array because it might
+          contain cross-appdomain references, which
+          will crash the GC when the domains are
+          unloaded. */
+       memset (mono_array_addr (a, MonoObject*, first), 0, sizeof (MonoObject*) * (last - first));
+}
 
-       EnterCriticalSection (&mono_delegate_section);
-       g_list_free (async_call_queue);
-       async_call_queue = NULL;
-       release = (gint) InterlockedCompareExchange (&mono_worker_threads, 0, -1);
-       LeaveCriticalSection (&mono_delegate_section);
-       if (job_added)
-               ReleaseSemaphore (job_added, release, NULL);
+/* Caller must enter &tp->lock */
+static MonoObject*
+dequeue_job_nolock (ThreadPool *tp)
+{
+       MonoObject *ar;
+       int count;
 
-       socket_io_cleanup (&socket_io_data);
+       if (!tp->array || tp->first_elem == tp->next_elem)
+               return NULL;
+       ar = mono_array_get (tp->array, MonoObject*, tp->first_elem);
+       mono_array_set (tp->array, MonoObject*, tp->first_elem, NULL);
+       tp->first_elem++;
+       count = tp->next_elem - tp->first_elem;
+       /* reduce the size of the array if it's mostly empty */
+       if (mono_array_length (tp->array) > INITIAL_QUEUE_LENGTH && count < (mono_array_length (tp->array) / 3)) {
+               MonoArray *newa = mono_array_new_cached (mono_get_root_domain (), mono_defaults.object_class, mono_array_length (tp->array) / 2);
+               mono_array_memcpy_refs (newa, 0, tp->array, tp->first_elem, count);
+               null_array (tp->array, tp->first_elem, tp->next_elem);
+               tp->array = newa;
+               tp->first_elem = 0;
+               tp->next_elem = count;
+       }
+       return ar;
 }
 
-static void
-append_job (CRITICAL_SECTION *cs, GList **plist, gpointer ar)
+/* Call after entering &tp->lock */
+static int
+signal_idle_threads (ThreadPool *tp)
 {
-       GList *tmp, *list;
+       IdleThreadData *it;
+       int result = 0;
+       int njobs;
+
+       njobs = tp->next_elem - tp->first_elem;
+       while (njobs > 0 && (it = g_queue_pop_head (tp->idle_threads)) != NULL) {
+               it->data = dequeue_job_nolock (tp);
+               if (it->data == NULL)
+                       break; /* Should never happen */
+               result++;
+               njobs--;
+               it->timeout = INFINITE;
+               SetEvent (it->wait_handle);
+       }
+       return njobs;
+}
+
+/* Call after entering &tp->lock */
+static gboolean
+threadpool_start_thread (ThreadPool *tp, gpointer arg)
+{
+       gint max;
+       gint n;
+
+       max = (gint) InterlockedCompareExchange (&tp->max_threads, 0, -1);
+       n = (gint) InterlockedCompareExchange (&tp->nthreads, 0, -1);
+       if (max <= n)
+               return FALSE;
+       InterlockedIncrement (&tp->nthreads);
+       mono_thread_create_internal (mono_get_root_domain (), tp->async_invoke, arg, TRUE);
+       return TRUE;
+}
+
+/*
+static const char *
+get_queue_name (ThreadPool *tp)
+{
+       if (tp == &async_tp)
+               return "TP";
+       if (tp == &async_io_tp)
+               return "IO";
+       return "(Unknown)";
+}
+*/
+
+static gpointer
+threadpool_queue_idle_thread (ThreadPool *tp, IdleThreadData *it)
+{
+       /*
+       MonoCounterSample sample;
+       float rate;
+       */
+       gpointer result = NULL;
+       CRITICAL_SECTION *cs = &tp->lock;
 
        EnterCriticalSection (cs);
-       list = *plist;
-       if (list == NULL) {
-               list = g_list_append (list, ar); 
+       if (tp->idle_threads == NULL) {
+               it->die = TRUE;
+               LeaveCriticalSection (cs);
+               return NULL; /* We are shutting down */
+       }
+       /*
+       if (mono_100ns_ticks () - tp->last_sample.timeStamp > 10000 * 1000) {
+               float elapsed_ticks;
+               mono_perfcounter_get_sample (tp->pc_nitems, FALSE, &sample);
+
+               elapsed_ticks = (float) (sample.timeStamp - tp->last_sample.timeStamp);
+               rate = ((float) (sample.rawValue - tp->last_sample.rawValue)) / elapsed_ticks * 10000000;
+               printf ("Queue: %s NThreads: %d Rate: %.2f Total items: %lld Time(ms): %.2f\n", get_queue_name (tp),
+                                               InterlockedCompareExchange (&tp->nthreads, 0, -1), rate,
+                                               sample.rawValue - tp->last_sample.rawValue, elapsed_ticks / 10000);
+               memcpy (&tp->last_sample, &sample, sizeof (sample));
+       }
+       */
+
+       it->data = result = dequeue_job_nolock (tp);
+       if (result != NULL) {
+               signal_idle_threads (tp);
        } else {
-               for (tmp = list; tmp && tmp->data != NULL; tmp = tmp->next);
-               if (tmp == NULL) {
-                       list = g_list_append (list, ar); 
+               int min, n;
+               min = (gint) InterlockedCompareExchange (&tp->min_threads, 0, -1);
+               n = (gint) InterlockedCompareExchange (&tp->nthreads, 0, -1);
+               if (n <= min) {
+                       g_queue_push_tail (tp->idle_threads, it);
                } else {
-                       tmp->data = ar;
+                       /* TODO: figure out when threads should be told to die */
+                       /* it->die = TRUE; */
+                       g_queue_push_tail (tp->idle_threads, it);
                }
        }
-       *plist = list;
        LeaveCriticalSection (cs);
+       return result;
 }
 
-static gpointer
-dequeue_job (CRITICAL_SECTION *cs, GList **plist)
+static void
+threadpool_append_job (ThreadPool *tp, MonoObject *ar)
 {
-       gpointer ar = NULL;
-       GList *tmp, *tmp2, *list;
+       CRITICAL_SECTION *cs;
 
+       cs = &tp->lock;
+       threadpool_jobs_inc (ar); 
        EnterCriticalSection (cs);
-       list = *plist;
-       tmp = list;
-       if (tmp) {
-               ar = tmp->data;
-               tmp->data = NULL;
-               tmp2 = tmp;
-               for (tmp2 = tmp; tmp2->next != NULL; tmp2 = tmp2->next);
-               if (tmp2 != tmp) {
-                       list = tmp->next;
-                       tmp->next = NULL;
-                       tmp2->next = tmp;
-                       tmp->prev = tmp2;
+       if (tp->idle_threads == NULL) { 
+               LeaveCriticalSection (cs);
+               return; /* We are shutting down */
+       }
+       if (ar->vtable->domain->state == MONO_APPDOMAIN_UNLOADING ||
+                       ar->vtable->domain->state == MONO_APPDOMAIN_UNLOADED) {
+               LeaveCriticalSection (cs);
+               return;
+       }
+
+       mono_perfcounter_update_value (tp->pc_nitems, TRUE, 1);
+       if (tp->array && (tp->next_elem < mono_array_length (tp->array))) {
+               mono_array_setref (tp->array, tp->next_elem, ar);
+               tp->next_elem++;
+               if (signal_idle_threads (tp) > 0 && threadpool_start_thread (tp, ar)) {
+                       tp->next_elem--;
+                       mono_array_setref (tp->array, tp->next_elem, NULL);
                }
+               LeaveCriticalSection (cs);
+               return;
+       }
+
+       if (!tp->array) {
+               MONO_GC_REGISTER_ROOT (tp->array);
+               tp->array = mono_array_new_cached (mono_get_root_domain (), mono_defaults.object_class, INITIAL_QUEUE_LENGTH);
+       } else {
+               int count = tp->next_elem - tp->first_elem;
+               /* slide the array or create a larger one if it's full */
+               if (tp->first_elem) {
+                       mono_array_memcpy_refs (tp->array, 0, tp->array, tp->first_elem, count);
+                       null_array (tp->array, count, tp->next_elem);
+               } else {
+                       MonoArray *newa = mono_array_new_cached (mono_get_root_domain (), mono_defaults.object_class, mono_array_length (tp->array) * 2);
+                       mono_array_memcpy_refs (newa, 0, tp->array, tp->first_elem, count);
+                       null_array (tp->array, count, tp->next_elem);
+                       tp->array = newa;
+               }
+               tp->first_elem = 0;
+               tp->next_elem = count;
+       }
+       mono_array_setref (tp->array, tp->next_elem, ar);
+       tp->next_elem++;
+       if (signal_idle_threads (tp) > 0 && threadpool_start_thread (tp, ar)) {
+               tp->next_elem--;
+               mono_array_setref (tp->array, tp->next_elem, NULL);
        }
-       *plist = list;
        LeaveCriticalSection (cs);
+}
 
-       return ar;
+
+static void
+threadpool_clear_queue (ThreadPool *tp, MonoDomain *domain)
+{
+       int i, count = 0;
+       EnterCriticalSection (&tp->lock);
+       /*remove*/
+       for (i = tp->first_elem; i < tp->next_elem; ++i) {
+               MonoObject *obj = mono_array_get (tp->array, MonoObject*, i);
+               if (obj->vtable->domain == domain) {
+                       mono_array_set (tp->array, MonoObject*, i, NULL);
+                       InterlockedDecrement (&domain->threadpool_jobs);
+                       ++count;
+               }
+       }
+       /*compact*/
+       if (count) {
+               int idx = 0;
+               for (i = tp->first_elem; i < tp->next_elem; ++i) {
+                       MonoObject *obj = mono_array_get (tp->array, MonoObject*, i);
+                       if (obj)
+                               mono_array_set (tp->array, MonoObject*, idx++, obj);
+               }
+               tp->first_elem = 0;
+               tp->next_elem = count;
+       }
+       LeaveCriticalSection (&tp->lock);
+}
+
+/*
+ * Clean up the threadpool of all domain jobs.
+ * Can only be called as part of the domain unloading process as
+ * it will wait for all jobs to be visible to the interruption code. 
+ */
+gboolean
+mono_thread_pool_remove_domain_jobs (MonoDomain *domain, int timeout)
+{
+       HANDLE sem_handle;
+       int result = TRUE;
+       guint32 start_time = 0;
+
+       g_assert (domain->state == MONO_APPDOMAIN_UNLOADING);
+
+       threadpool_clear_queue (&async_tp, domain);
+       threadpool_clear_queue (&async_io_tp, domain);
+
+       /*
+        * There might be some threads out that could be about to execute stuff from the given domain.
+        * We avoid that by setting up a semaphore to be pulsed by the thread that reaches zero.
+        */
+       sem_handle = CreateSemaphore (NULL, 0, 1, NULL);
+       
+       domain->cleanup_semaphore = sem_handle;
+       /*
+        * The memory barrier here is required to have global ordering between assigning to cleanup_semaphone
+        * and reading threadpool_jobs.
+        * Otherwise this thread could read a stale version of threadpool_jobs and wait forever.
+        */
+       mono_memory_write_barrier ();
+
+       if (domain->threadpool_jobs && timeout != -1)
+               start_time = mono_msec_ticks ();
+       while (domain->threadpool_jobs) {
+               WaitForSingleObject (sem_handle, timeout);
+               if (timeout != -1 && (mono_msec_ticks () - start_time) > timeout) {
+                       result = FALSE;
+                       break;
+               }
+       }
+
+       domain->cleanup_semaphore = NULL;
+       CloseHandle (sem_handle);
+       return result;
+}
+
+static void
+threadpool_free_queue (ThreadPool *tp)
+{
+       if (tp->array)
+               null_array (tp->array, tp->first_elem, tp->next_elem);
+       tp->array = NULL;
+       tp->first_elem = tp->next_elem = 0;
+}
+
+gboolean
+mono_thread_pool_is_queue_array (MonoArray *o)
+{
+       return o == async_tp.array || o == async_io_tp.array;
 }
 
 static void
 async_invoke_thread (gpointer data)
 {
        MonoDomain *domain;
-       MonoThread *thread;
-       int workers, min;
+       MonoInternalThread *thread;
+       const gchar *version;
+       IdleThreadData idle_data = {0};
+  
+       idle_data.timeout = INFINITE;
+       idle_data.wait_handle = CreateEvent (NULL, FALSE, FALSE, NULL);
  
-       thread = mono_thread_current ();
-       thread->threadpool_thread = TRUE;
-       ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
-
+       thread = mono_thread_internal_current ();
+       if (tp_start_func)
+               tp_start_func (tp_hooks_user_data);
+       version = mono_get_runtime_info ()->framework_version;
        for (;;) {
                MonoAsyncResult *ar;
 
@@ -1173,63 +1511,66 @@ async_invoke_thread (gpointer data)
                        /* worker threads invokes methods in different domains,
                         * so we need to set the right domain here */
                        domain = ((MonoObject *)ar)->vtable->domain;
-                       mono_thread_push_appdomain_ref (domain);
-                       if (mono_domain_set (domain, FALSE)) {
-                               ASyncCall *ac;
-
-                               mono_async_invoke (ar);
-                               ac = (ASyncCall *) ar->object_data;
-                               /*
-                               if (ac->msg->exc != NULL)
-                                       mono_unhandled_exception (ac->msg->exc);
-                               */
-                               mono_domain_set (mono_get_root_domain (), TRUE);
-                       }
-                       mono_thread_pop_appdomain_ref ();
-                       InterlockedDecrement (&busy_worker_threads);
-               }
 
-               data = dequeue_job (&mono_delegate_section, &async_call_queue);
+                       g_assert (domain);
 
-               if (!data) {
-                       guint32 wr;
-                       int timeout = 10000;
-                       guint32 start_time = GetTickCount ();
-                       
-                       do {
-                               wr = WaitForSingleObjectEx (job_added, (guint32)timeout, TRUE);
-                               if (THREAD_WANTS_A_BREAK (thread))
-                                       mono_thread_interruption_checkpoint ();
-                       
-                               timeout -= GetTickCount () - start_time;
-                       
-                               if (wr != WAIT_TIMEOUT)
-                                       data = dequeue_job (&mono_delegate_section, &async_call_queue);
+                       if (domain->state == MONO_APPDOMAIN_UNLOADED || domain->state == MONO_APPDOMAIN_UNLOADING) {
+                               threadpool_jobs_dec ((MonoObject *)ar);
+                               data = NULL;
+                       } else {
+                               mono_thread_push_appdomain_ref (domain);
+                               if (threadpool_jobs_dec ((MonoObject *)ar)) {
+                                       data = NULL;
+                                       mono_thread_pop_appdomain_ref ();
+                                       continue;
+                               }
+
+                               if (mono_domain_set (domain, FALSE)) {
+                                       /* ASyncCall *ac; */
+
+                                       if (tp_item_begin_func)
+                                               tp_item_begin_func (tp_item_user_data);
+                                       mono_async_invoke (ar);
+                                       if (tp_item_end_func)
+                                               tp_item_end_func (tp_item_user_data);
+                                       /*
+                                       ac = (ASyncCall *) ar->object_data;
+                                       if (ac->msg->exc != NULL)
+                                               mono_unhandled_exception (ac->msg->exc);
+                                       */
+                                       mono_domain_set (mono_get_root_domain (), TRUE);
+                               }
+                               mono_thread_pop_appdomain_ref ();
+                               InterlockedDecrement (&async_tp.busy_threads);
+                               /* If the callee changes the background status, set it back to TRUE */
+                               if (*version != '1' && !mono_thread_test_state (thread , ThreadState_Background))
+                                       ves_icall_System_Threading_Thread_SetState (thread, ThreadState_Background);
                        }
-                       while (!data && timeout > 0);
                }
-
-               if (!data) {
-                       workers = (int) InterlockedCompareExchange (&mono_worker_threads, 0, -1); 
-                       min = (int) InterlockedCompareExchange (&mono_min_worker_threads, 0, -1); 
-       
-                       while (!data && workers <= min) {
-                               WaitForSingleObjectEx (job_added, INFINITE, TRUE);
-                               if (THREAD_WANTS_A_BREAK (thread))
-                                       mono_thread_interruption_checkpoint ();
-                       
-                               data = dequeue_job (&mono_delegate_section, &async_call_queue);
-                               workers = (int) InterlockedCompareExchange (&mono_worker_threads, 0, -1); 
-                               min = (int) InterlockedCompareExchange (&mono_min_worker_threads, 0, -1); 
+               data = threadpool_queue_idle_thread (&async_tp, &idle_data);
+               while (!idle_data.die && !data) {
+                       guint32 wr;
+                       wr = WaitForSingleObjectEx (idle_data.wait_handle, idle_data.timeout, TRUE);
+                       if (THREAD_WANTS_A_BREAK (thread))
+                               mono_thread_interruption_checkpoint ();
+               
+                       if (wr != WAIT_TIMEOUT && wr != WAIT_IO_COMPLETION) {
+                               data = idle_data.data;
+                               break; /* We have to exit */
                        }
                }
-       
+               idle_data.data = NULL;
+
                if (!data) {
-                       InterlockedDecrement (&mono_worker_threads);
+                       InterlockedDecrement (&async_tp.nthreads);
+                       CloseHandle (idle_data.wait_handle);
+                       idle_data.wait_handle = NULL;
+                       if (tp_finish_func)
+                               tp_finish_func (tp_hooks_user_data);
                        return;
                }
                
-               InterlockedIncrement (&busy_worker_threads);
+               InterlockedIncrement (&async_tp.busy_threads);
        }
 
        g_assert_not_reached ();
@@ -1238,13 +1579,14 @@ async_invoke_thread (gpointer data)
 void
 ves_icall_System_Threading_ThreadPool_GetAvailableThreads (gint *workerThreads, gint *completionPortThreads)
 {
-       gint busy;
+       gint busy, busy_io;
 
        MONO_ARCH_SAVE_REGS;
 
-       busy = (gint) InterlockedCompareExchange (&busy_worker_threads, 0, -1);
-       *workerThreads = mono_max_worker_threads - busy;
-       *completionPortThreads = 0;
+       busy = (gint) InterlockedCompareExchange (&async_tp.busy_threads, 0, -1);
+       busy_io = (gint) InterlockedCompareExchange (&async_io_tp.busy_threads, 0, -1);
+       *workerThreads = async_tp.max_threads - busy;
+       *completionPortThreads = async_io_tp.max_threads - busy_io;
 }
 
 void
@@ -1252,20 +1594,28 @@ ves_icall_System_Threading_ThreadPool_GetMaxThreads (gint *workerThreads, gint *
 {
        MONO_ARCH_SAVE_REGS;
 
-       *workerThreads = mono_max_worker_threads;
-       *completionPortThreads = 0;
+       *workerThreads = (gint) InterlockedCompareExchange (&async_tp.max_threads, 0, -1);
+       *completionPortThreads = (gint) InterlockedCompareExchange (&async_io_tp.max_threads, 0, -1);
 }
 
 void
 ves_icall_System_Threading_ThreadPool_GetMinThreads (gint *workerThreads, gint *completionPortThreads)
 {
-       gint workers;
+       gint workers, workers_io;
 
        MONO_ARCH_SAVE_REGS;
 
-       workers = (gint) InterlockedCompareExchange (&mono_min_worker_threads, 0, -1);
+       workers = (gint) InterlockedCompareExchange (&async_tp.min_threads, 0, -1);
+       workers_io = (gint) InterlockedCompareExchange (&async_io_tp.min_threads, 0, -1);
+
        *workerThreads = workers;
-       *completionPortThreads = 0;
+       *completionPortThreads = workers_io;
+}
+
+static void
+start_idle_threads (void)
+{
+       threadpool_start_idle_threads (&async_tp);
 }
 
 MonoBoolean
@@ -1273,10 +1623,68 @@ ves_icall_System_Threading_ThreadPool_SetMinThreads (gint workerThreads, gint co
 {
        MONO_ARCH_SAVE_REGS;
 
-       if (workerThreads < 0 || workerThreads > mono_max_worker_threads)
+       if (workerThreads < 0 || workerThreads > async_tp.max_threads)
+               return FALSE;
+
+       if (completionPortThreads < 0 || completionPortThreads > async_io_tp.max_threads)
                return FALSE;
-       InterlockedExchange (&mono_min_worker_threads, workerThreads);
-       /* FIXME: should actually start the idle threads if needed */
+
+       InterlockedExchange (&async_tp.min_threads, workerThreads);
+       InterlockedExchange (&async_io_tp.min_threads, completionPortThreads);
+       mono_thread_create_internal (mono_get_root_domain (), start_idle_threads, NULL, TRUE);
+       return TRUE;
+}
+
+MonoBoolean
+ves_icall_System_Threading_ThreadPool_SetMaxThreads (gint workerThreads, gint completionPortThreads)
+{
+       MONO_ARCH_SAVE_REGS;
+
+       if (workerThreads < async_tp.max_threads)
+               return FALSE;
+
+       /* We don't really have the concept of completion ports. Do we care here? */
+       if (completionPortThreads < async_io_tp.max_threads)
+               return FALSE;
+
+       InterlockedExchange (&async_tp.max_threads, workerThreads);
+       InterlockedExchange (&async_io_tp.max_threads, completionPortThreads);
        return TRUE;
 }
 
+/**
+ * mono_install_threadpool_thread_hooks
+ * @start_func: the function to be called right after a new threadpool thread is created. Can be NULL.
+ * @finish_func: the function to be called right before a thredpool thread is exiting. Can be NULL.
+ * @user_data: argument passed to @start_func and @finish_func.
+ *
+ * @start_fun will be called right after a threadpool thread is created and @finish_func right before a threadpool thread exits.
+ * The calls will be made from the thread itself.
+ */
+void
+mono_install_threadpool_thread_hooks (MonoThreadPoolFunc start_func, MonoThreadPoolFunc finish_func, gpointer user_data)
+{
+       tp_start_func = start_func;
+       tp_finish_func = finish_func;
+       tp_hooks_user_data = user_data;
+}
+
+/**
+ * mono_install_threadpool_item_hooks
+ * @begin_func: the function to be called before a threadpool work item processing starts.
+ * @end_func: the function to be called after a threadpool work item is finished.
+ * @user_data: argument passed to @begin_func and @end_func.
+ *
+ * The calls will be made from the thread itself and from the same AppDomain
+ * where the work item was executed.
+ *
+ */
+void
+mono_install_threadpool_item_hooks (MonoThreadPoolItemFunc begin_func, MonoThreadPoolItemFunc end_func, gpointer user_data)
+{
+       tp_item_begin_func = begin_func;
+       tp_item_end_func = end_func;
+       tp_item_user_data = user_data;
+}
+
+