2009-12-10 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / domain.c
index ebe13ad5568b4122f417702af92ce394563b8917..77985657b6efd296d271ac01e27b8a9f61d21940 100644 (file)
@@ -49,7 +49,7 @@ static guint32 appdomain_thread_id = -1;
  * Avoid calling TlsSetValue () if possible, since in the io-layer, it acquires
  * a global lock (!) so it is a contention point.
  */
-#if (defined(__i386__) || defined(__x86_64__)) && !defined(PLATFORM_WIN32)
+#if (defined(__i386__) || defined(__x86_64__)) && !defined(HOST_WIN32)
 #define NO_TLS_SET_VALUE
 #endif
  
@@ -77,8 +77,8 @@ static __thread MonoDomain * tls_appdomain MONO_TLS_FAST;
 
 #endif
 
-#define GET_APPCONTEXT() (mono_thread_current ()->current_appcontext)
-#define SET_APPCONTEXT(x) MONO_OBJECT_SETREF (mono_thread_current (), current_appcontext, (x))
+#define GET_APPCONTEXT() (mono_thread_internal_current ()->current_appcontext)
+#define SET_APPCONTEXT(x) MONO_OBJECT_SETREF (mono_thread_internal_current (), current_appcontext, (x))
 
 static guint16 appdomain_list_size = 0;
 static guint16 appdomain_next = 0;
@@ -132,13 +132,13 @@ static const MonoRuntimeInfo supported_runtimes[] = {
        {"v1.1.4322", "1.0", { {1,0,5000,0}, {7,0,5000,0} }     },
        {"v2.0.50215","2.0", { {2,0,0,0},    {8,0,0,0} }        },
        {"v2.0.50727","2.0", { {2,0,0,0},    {8,0,0,0} }        },
-       {"v4.0.20506","4.0", { {4,0,0,0},    {10,0,0,0} }   },
+       {"v4.0.21006","4.0", { {4,0,0,0},    {10,0,0,0} }   },
        {"moonlight", "2.1", { {2,0,5,0},    {9,0,0,0} }    },
 };
 
 
 /* The stable runtime version */
-#define DEFAULT_RUNTIME_VERSION "v1.1.4322"
+#define DEFAULT_RUNTIME_VERSION "v2.0.50727"
 
 /* Callbacks installed by the JIT */
 static MonoCreateDomainFunc create_domain_hook;
@@ -222,7 +222,7 @@ jit_info_table_new_chunk (void)
 static MonoJitInfoTable *
 jit_info_table_new (MonoDomain *domain)
 {
-       MonoJitInfoTable *table = g_malloc0 (sizeof (MonoJitInfoTable) + sizeof (MonoJitInfoTableChunk*));
+       MonoJitInfoTable *table = g_malloc0 (MONO_SIZEOF_JIT_INFO_TABLE + sizeof (MonoJitInfoTableChunk*));
 
        table->domain = domain;
        table->num_chunks = 1;
@@ -507,7 +507,7 @@ jit_info_table_realloc (MonoJitInfoTable *old)
        required_size = (int)((long)num_elements * JIT_INFO_TABLE_FILL_RATIO_DENOM / JIT_INFO_TABLE_FILL_RATIO_NOM);
        num_chunks = (required_size + MONO_JIT_INFO_TABLE_CHUNK_SIZE - 1) / MONO_JIT_INFO_TABLE_CHUNK_SIZE;
 
-       new = g_malloc (sizeof (MonoJitInfoTable) + sizeof (MonoJitInfoTableChunk*) * num_chunks);
+       new = g_malloc (MONO_SIZEOF_JIT_INFO_TABLE + sizeof (MonoJitInfoTableChunk*) * num_chunks);
        new->domain = old->domain;
        new->num_chunks = num_chunks;
 
@@ -576,7 +576,7 @@ jit_info_table_split_chunk (MonoJitInfoTableChunk *chunk, MonoJitInfoTableChunk
 static MonoJitInfoTable*
 jit_info_table_copy_and_split_chunk (MonoJitInfoTable *table, MonoJitInfoTableChunk *chunk)
 {
-       MonoJitInfoTable *new_table = g_malloc (sizeof (MonoJitInfoTable)
+       MonoJitInfoTable *new_table = g_malloc (MONO_SIZEOF_JIT_INFO_TABLE
                + sizeof (MonoJitInfoTableChunk*) * (table->num_chunks + 1));
        int i, j;
 
@@ -624,7 +624,7 @@ jit_info_table_purify_chunk (MonoJitInfoTableChunk *old)
 static MonoJitInfoTable*
 jit_info_table_copy_and_purify_chunk (MonoJitInfoTable *table, MonoJitInfoTableChunk *chunk)
 {
-       MonoJitInfoTable *new_table = g_malloc (sizeof (MonoJitInfoTable)
+       MonoJitInfoTable *new_table = g_malloc (MONO_SIZEOF_JIT_INFO_TABLE
                + sizeof (MonoJitInfoTableChunk*) * table->num_chunks);
        int i, j;
 
@@ -1253,7 +1253,7 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        if (domain)
                g_assert_not_reached ();
 
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
        /* Avoid system error message boxes. */
        SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
 
@@ -1295,7 +1295,7 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
                 * exe_image, and close it during shutdown.
                 */
                get_runtimes_from_exe (exe_filename, &exe_image, runtimes);
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
                if (!exe_image) {
                        exe_image = mono_assembly_open_from_bundle (exe_filename, NULL, FALSE);
                        if (!exe_image)
@@ -1313,7 +1313,7 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
                runtimes [0] = default_runtime;
                runtimes [1] = NULL;
                g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
-               g_print ("Using default runtime: %s\n", default_runtime->runtime_version);
+               g_print ("Using default runtime: %s\n", default_runtime->runtime_version); 
        }
 
        /* The selected runtime will be the first one for which there is a mscrolib.dll */
@@ -1445,9 +1445,9 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
                "AsyncResult");
        g_assert (mono_defaults.asyncresult_class != 0 );
 
-       mono_defaults.waithandle_class = mono_class_from_name (
-               mono_defaults.corlib, "System.Threading", "WaitHandle");
-       g_assert (mono_defaults.waithandle_class != 0 );
+       mono_defaults.manualresetevent_class = mono_class_from_name (
+               mono_defaults.corlib, "System.Threading", "ManualResetEvent");
+       g_assert (mono_defaults.manualresetevent_class != 0 );
 
        mono_defaults.typehandle_class = mono_class_from_name (
                 mono_defaults.corlib, "System", "RuntimeTypeHandle");
@@ -1481,6 +1481,16 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
                 mono_defaults.corlib, "System.Threading", "Thread");
        g_assert (mono_defaults.thread_class != 0);
 
+       mono_defaults.internal_thread_class = mono_class_from_name (
+                mono_defaults.corlib, "System.Threading", "InternalThread");
+       if (!mono_defaults.internal_thread_class) {
+               /* This can happen with an old mscorlib */
+               fprintf (stderr, "Corlib too old for this runtime.\n");
+               fprintf (stderr, "Loaded from: %s\n",
+                                mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown");
+               exit (1);
+       }
+
        mono_defaults.appdomain_class = mono_class_from_name (
                 mono_defaults.corlib, "System", "AppDomain");
        g_assert (mono_defaults.appdomain_class != 0);
@@ -1757,10 +1767,16 @@ mono_domain_get ()
        return GET_APPDOMAIN ();
 }
 
+void
+mono_domain_unset (void)
+{
+       SET_APPDOMAIN (NULL);
+}
+
 void
 mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception)
 {
-       MonoThread *thread;
+       MonoInternalThread *thread;
 
        if (mono_domain_get () == domain)
                return;
@@ -1769,7 +1785,7 @@ mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exce
        SET_APPCONTEXT (domain->default_context);
 
        if (migrate_exception) {
-               thread = mono_thread_current ();
+               thread = mono_thread_internal_current ();
                if (!thread->abort_exc)
                        return;
 
@@ -1879,42 +1895,73 @@ mono_domain_free (MonoDomain *domain, gboolean force)
 
        mono_debug_domain_unload (domain);
 
-       mono_gc_clear_domain (domain);
-
        mono_appdomains_lock ();
        appdomains_list [domain->domain_id] = NULL;
        mono_appdomains_unlock ();
 
-       /* FIXME: free delegate_hash_table when it's used */
-       if (domain->search_path) {
-               g_strfreev (domain->search_path);
-               domain->search_path = NULL;
-       }
-       domain->create_proxy_for_type_method = NULL;
-       domain->private_invoke_method = NULL;
-       domain->default_context = NULL;
-       domain->out_of_memory_ex = NULL;
-       domain->null_reference_ex = NULL;
-       domain->stack_overflow_ex = NULL;
-       domain->entry_assembly = NULL;
        /* must do this early as it accesses fields and types */
        if (domain->special_static_fields) {
                mono_alloc_special_static_data_free (domain->special_static_fields);
                g_hash_table_destroy (domain->special_static_fields);
                domain->special_static_fields = NULL;
        }
+
+       /*
+        * We must destroy all these hash tables here because they
+        * contain references to managed objects belonging to the
+        * domain.  Once we let the GC clear the domain there must be
+        * no more such references, or we'll crash if a collection
+        * occurs.
+        */
+       mono_g_hash_table_destroy (domain->ldstr_table);
+       domain->ldstr_table = NULL;
+
+       mono_g_hash_table_destroy (domain->env);
+       domain->env = NULL;
+
+       mono_reflection_cleanup_domain (domain);
+
+       if (domain->type_hash) {
+               mono_g_hash_table_destroy (domain->type_hash);
+               domain->type_hash = NULL;
+       }
+       if (domain->type_init_exception_hash) {
+               mono_g_hash_table_destroy (domain->type_init_exception_hash);
+               domain->type_init_exception_hash = NULL;
+       }
+
        for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
                MonoAssembly *ass = tmp->data;
                mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading domain %s %p, assembly %s %p, refcount=%d\n", domain->friendly_name, domain, ass->aname.name, ass, ass->ref_count);
-               mono_assembly_close (ass);
+               if (!mono_assembly_close_except_image_pools (ass))
+                       tmp->data = NULL;
+       }
+
+       mono_gc_clear_domain (domain);
+
+       for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
+               MonoAssembly *ass = tmp->data;
+               if (ass)
+                       mono_assembly_close_finish (ass);
        }
        g_slist_free (domain->domain_assemblies);
        domain->domain_assemblies = NULL;
 
+       /* FIXME: free delegate_hash_table when it's used */
+       if (domain->search_path) {
+               g_strfreev (domain->search_path);
+               domain->search_path = NULL;
+       }
+       domain->create_proxy_for_type_method = NULL;
+       domain->private_invoke_method = NULL;
+       domain->default_context = NULL;
+       domain->out_of_memory_ex = NULL;
+       domain->null_reference_ex = NULL;
+       domain->stack_overflow_ex = NULL;
+       domain->entry_assembly = NULL;
+
        g_free (domain->friendly_name);
        domain->friendly_name = NULL;
-       mono_g_hash_table_destroy (domain->env);
-       domain->env = NULL;
        g_hash_table_destroy (domain->class_vtable_hash);
        domain->class_vtable_hash = NULL;
        g_hash_table_destroy (domain->proxy_vtable_hash);
@@ -1924,8 +1971,6 @@ mono_domain_free (MonoDomain *domain, gboolean force)
                domain->static_data_array = NULL;
        }
        mono_internal_hash_table_destroy (&domain->jit_code_hash);
-       mono_g_hash_table_destroy (domain->ldstr_table);
-       domain->ldstr_table = NULL;
 
        /*
         * There might still be jit info tables of this domain which
@@ -1955,16 +2000,6 @@ mono_domain_free (MonoDomain *domain, gboolean force)
        domain->code_mp = NULL;
 #endif 
 
-       mono_reflection_cleanup_domain (domain);
-       
-       if (domain->type_hash) {
-               mono_g_hash_table_destroy (domain->type_hash);
-               domain->type_hash = NULL;
-       }
-       if (domain->type_init_exception_hash) {
-               mono_g_hash_table_destroy (domain->type_init_exception_hash);
-               domain->type_init_exception_hash = NULL;
-       }
        g_hash_table_destroy (domain->finalizable_objects_hash);
        domain->finalizable_objects_hash = NULL;
 #ifndef HAVE_SGEN_GC
@@ -1990,6 +2025,8 @@ mono_domain_free (MonoDomain *domain, gboolean force)
        DeleteCriticalSection (&domain->lock);
        domain->setup = NULL;
 
+       mono_gc_deregister_root ((char*)&(domain->MONO_DOMAIN_FIRST_GC_TRACKED));
+
        /* FIXME: anything else required ? */
 
        mono_profiler_appdomain_event (domain, MONO_PROFILE_END_UNLOAD);