2009-08-12 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / threadpool.c
index 4df375eb5c18ef12b655381cd250f6cc377bcf05..4bc6db7340703b4560481c0c0e3ce0c9114661ed 100644 (file)
@@ -5,8 +5,8 @@
  *   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 <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"
 
@@ -151,6 +155,9 @@ enum {
        AIO_OP_LAST
 };
 
+#ifdef DISABLE_SOCKETS
+#define socket_io_cleanup(x)
+#else
 static void
 socket_io_cleanup (SocketIOData *data)
 {
@@ -231,6 +238,7 @@ get_events_from_list (MonoMList *list)
                                (SOCKET)(gssize)x->handle, x->buffer, x->offset, x->size,\
                                 x->socket_flags, &x->error);
 
+#endif /* !DISABLE_SOCKETS */
 
 static void
 unregister_job (MonoAsyncResult *obj)
@@ -259,6 +267,7 @@ threadpool_jobs_dec (MonoObject *obj)
        return FALSE;
 }
 
+#ifndef DISABLE_SOCKETS
 static void
 async_invoke_io_thread (gpointer data)
 {
@@ -337,7 +346,7 @@ async_invoke_io_thread (gpointer data)
                        
                                timeout -= mono_msec_ticks () - start_time;
                        
-                               if (wr != WAIT_TIMEOUT)
+                               if (wr != WAIT_TIMEOUT && wr != WAIT_IO_COMPLETION)
                                        data = dequeue_job (&io_queue_lock, &async_io_queue);
                        }
                        while (!data && timeout > 0);
@@ -806,7 +815,6 @@ socket_io_init (SocketIOData *data)
        }
        io_job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
        g_assert (io_job_added != NULL);
-       InitializeCriticalSection (&io_queue_lock);
        if (data->epoll_disabled) {
                mono_thread_create_internal (mono_get_root_domain (), socket_io_poll_main, data, TRUE);
        }
@@ -827,7 +835,7 @@ socket_io_add_poll (MonoSocketAsyncResult *state)
        MonoMList *list;
        SocketIOData *data = &socket_io_data;
 
-#if defined(PLATFORM_MACOSX) || defined(PLATFORM_BSD6) || defined(PLATFORM_WIN32) || defined(PLATFORM_SOLARIS)
+#if defined(PLATFORM_MACOSX) || defined(PLATFORM_BSD) || defined(PLATFORM_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 */
@@ -966,6 +974,7 @@ socket_io_filter (MonoObject *target, MonoObject *state)
 
        return TRUE;
 }
+#endif /* !DISABLE_SOCKETS */
 
 static void
 mono_async_invoke (MonoAsyncResult *ares)
@@ -978,8 +987,8 @@ mono_async_invoke (MonoAsyncResult *ares)
        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;
        }
@@ -1005,7 +1014,7 @@ 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;
        }
 
@@ -1069,6 +1078,7 @@ mono_thread_pool_init ()
        MONO_GC_REGISTER_ROOT (socket_io_data.sock_to_state);
        InitializeCriticalSection (&socket_io_data.io_lock);
        InitializeCriticalSection (&ares_lock);
+       InitializeCriticalSection (&io_queue_lock);
        ares_htable = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_VALUE_GC);
        job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
        g_assert (job_added != NULL);
@@ -1106,15 +1116,20 @@ mono_thread_pool_add (MonoObject *target, MonoMethodMessage *msg, MonoDelegate *
        ares = mono_async_result_new (domain, NULL, ac->state, NULL, (MonoObject*)ac);
        MONO_OBJECT_SETREF (ares, async_delegate, target);
 
+       if (domain->state == MONO_APPDOMAIN_UNLOADED || domain->state == MONO_APPDOMAIN_UNLOADING)
+               return ares;
+
        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;
        }
-
+#endif
+       
        start_thread_or_queue (ares);
        return ares;
 }
@@ -1212,14 +1227,14 @@ append_job (CRITICAL_SECTION *cs, TPQueue *list, MonoObject *ar)
        }
        if (!list->array) {
                MONO_GC_REGISTER_ROOT (list->array);
-               list->array = mono_array_new (mono_get_root_domain (), mono_defaults.object_class, 16);
+               list->array = mono_array_new_cached (mono_get_root_domain (), mono_defaults.object_class, 16);
        } else {
                int count = list->next_elem - list->first_elem;
                /* slide the array or create a larger one if it's full */
                if (list->first_elem) {
                        mono_array_memcpy_refs (list->array, 0, list->array, list->first_elem, count);
                } else {
-                       MonoArray *newa = mono_array_new (mono_get_root_domain (), mono_defaults.object_class, mono_array_length (list->array) * 2);
+                       MonoArray *newa = mono_array_new_cached (mono_get_root_domain (), mono_defaults.object_class, mono_array_length (list->array) * 2);
                        mono_array_memcpy_refs (newa, 0, list->array, list->first_elem, count);
                        list->array = newa;
                }
@@ -1319,11 +1334,12 @@ dequeue_job (CRITICAL_SECTION *cs, TPQueue *list)
                return NULL;
        }
        ar = mono_array_get (list->array, MonoObject*, list->first_elem);
+       mono_array_setref (list->array, list->first_elem, NULL);
        list->first_elem++;
        count = list->next_elem - list->first_elem;
        /* reduce the size of the array if it's mostly empty */
        if (mono_array_length (list->array) > 16 && count < (mono_array_length (list->array) / 3)) {
-               MonoArray *newa = mono_array_new (mono_get_root_domain (), mono_defaults.object_class, mono_array_length (list->array) / 2);
+               MonoArray *newa = mono_array_new_cached (mono_get_root_domain (), mono_defaults.object_class, mono_array_length (list->array) / 2);
                mono_array_memcpy_refs (newa, 0, list->array, list->first_elem, count);
                list->array = newa;
                list->first_elem = 0;
@@ -1408,7 +1424,7 @@ async_invoke_thread (gpointer data)
                        
                                timeout -= mono_msec_ticks () - start_time;
                        
-                               if (wr != WAIT_TIMEOUT)
+                               if (wr != WAIT_TIMEOUT && wr != WAIT_IO_COMPLETION)
                                        data = dequeue_job (&mono_delegate_section, &async_call_queue);
                        }
                        while (!data && timeout > 0);