2008-10-01 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / domain.c
index f142b04b3279c6dad1d0c5965376eba7e49ccd1a..ea3e19c272f18aadaaece22b2aaf83a6c2da8d2f 100644 (file)
@@ -34,6 +34,7 @@
 #include <mono/metadata/threads-types.h>
 #include <metadata/threads.h>
 #include <metadata/profiler-private.h>
+#include <mono/metadata/coree.h>
 
 /* #define DEBUG_DOMAIN_UNLOAD */
 
@@ -67,6 +68,8 @@ static guint16 appdomain_next = 0;
 static MonoDomain **appdomains_list = NULL;
 static MonoImage *exe_image;
 
+gboolean mono_dont_free_domains;
+
 #define mono_appdomains_lock() EnterCriticalSection (&appdomains_mutex)
 #define mono_appdomains_unlock() LeaveCriticalSection (&appdomains_mutex)
 static CRITICAL_SECTION appdomains_mutex;
@@ -112,13 +115,17 @@ 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} }        },
-       {"moonlight", "2.1", { {2,1,0,0},    {9,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"
 
+/* Callbacks installed by the JIT */
+static MonoCreateDomainFunc create_domain_hook;
+static MonoFreeDomainFunc free_domain_hook;
+
 /* This is intentionally not in the header file, so people don't misuse it. */
 extern void _mono_debug_init_corlib (MonoDomain *domain);
 
@@ -154,7 +161,8 @@ mono_domain_get_tls_offset (void)
 #define JIT_INFO_TABLE_LOW_WATERMARK(n)                ((n) / 2)
 #define JIT_INFO_TABLE_HIGH_WATERMARK(n)       ((n) * 5 / 6)
 
-#define IS_JIT_INFO_TOMBSTONE(ji)      ((ji)->method == NULL)
+#define JIT_INFO_TOMBSTONE_MARKER      ((MonoMethod*)NULL)
+#define IS_JIT_INFO_TOMBSTONE(ji)      ((ji)->method == JIT_INFO_TOMBSTONE_MARKER)
 
 #define JIT_INFO_TABLE_HAZARD_INDEX            0
 #define JIT_INFO_HAZARD_INDEX                  1
@@ -189,10 +197,11 @@ jit_info_table_new_chunk (void)
 }
 
 static MonoJitInfoTable *
-jit_info_table_new (void)
+jit_info_table_new (MonoDomain *domain)
 {
        MonoJitInfoTable *table = g_malloc0 (sizeof (MonoJitInfoTable) + sizeof (MonoJitInfoTableChunk*));
 
+       table->domain = domain;
        table->num_chunks = 1;
        table->chunks [0] = jit_info_table_new_chunk ();
 
@@ -204,6 +213,20 @@ jit_info_table_free (MonoJitInfoTable *table)
 {
        int i;
        int num_chunks = table->num_chunks;
+       MonoDomain *domain = table->domain;
+
+       mono_domain_lock (domain);
+
+       table->domain->num_jit_info_tables--;
+       if (table->domain->num_jit_info_tables <= 1) {
+               GSList *list;
+
+               for (list = table->domain->jit_info_free_queue; list; list = list->next)
+                       g_free (list->data);
+
+               g_slist_free (table->domain->jit_info_free_queue);
+               table->domain->jit_info_free_queue = NULL;
+       }
 
        /* At this point we assume that there are no other threads
           still accessing the table, so we don't have to worry about
@@ -228,6 +251,8 @@ jit_info_table_free (MonoJitInfoTable *table)
                g_free (chunk);
        }
 
+       mono_domain_unlock (domain);
+
        g_free (table);
 }
 
@@ -377,13 +402,14 @@ mono_jit_info_table_find (MonoDomain *domain, char *addr)
                           beyond what we're looking for, we have to end the
                           search. */
                        if ((gint8*)addr < (gint8*)ji->code_start)
-                               break;
+                               goto not_found;
                }
 
                ++chunk_pos;
                pos = 0;
-        } while (chunk_pos < table->num_chunks);
+       } while (chunk_pos < table->num_chunks);
 
+ not_found:
        mono_hazard_pointer_clear (hp, JIT_INFO_TABLE_HAZARD_INDEX);
        mono_hazard_pointer_clear (hp, JIT_INFO_HAZARD_INDEX);
 
@@ -459,6 +485,7 @@ jit_info_table_realloc (MonoJitInfoTable *old)
        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->domain = old->domain;
        new->num_chunks = num_chunks;
 
        for (i = 0; i < num_chunks; ++i)
@@ -530,6 +557,7 @@ jit_info_table_copy_and_split_chunk (MonoJitInfoTable *table, MonoJitInfoTableCh
                + sizeof (MonoJitInfoTableChunk*) * (table->num_chunks + 1));
        int i, j;
 
+       new_table->domain = table->domain;
        new_table->num_chunks = table->num_chunks + 1;
 
        j = 0;
@@ -577,6 +605,7 @@ jit_info_table_copy_and_purify_chunk (MonoJitInfoTable *table, MonoJitInfoTableC
                + sizeof (MonoJitInfoTableChunk*) * table->num_chunks);
        int i, j;
 
+       new_table->domain = table->domain;
        new_table->num_chunks = table->num_chunks;
 
        j = 0;
@@ -680,6 +709,7 @@ mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
 
                domain->jit_info_table = new_table;
                mono_memory_barrier ();
+               domain->num_jit_info_tables++;
                mono_thread_hazardous_free_or_queue (table, (MonoHazardousFreeFunc)jit_info_table_free);
                table = new_table;
 
@@ -730,11 +760,26 @@ mono_jit_info_make_tombstone (MonoJitInfo *ji)
 
        tombstone->code_start = ji->code_start;
        tombstone->code_size = ji->code_size;
-       tombstone->method = NULL;
+       tombstone->method = JIT_INFO_TOMBSTONE_MARKER;
 
        return tombstone;
 }
 
+/*
+ * LOCKING: domain lock
+ */
+static void
+mono_jit_info_free_or_queue (MonoDomain *domain, MonoJitInfo *ji)
+{
+       if (domain->num_jit_info_tables <= 1) {
+               /* Can it actually happen that we only have one table
+                  but ji is still hazardous? */
+               mono_thread_hazardous_free_or_queue (ji, g_free);
+       } else {
+               domain->jit_info_free_queue = g_slist_prepend (domain->jit_info_free_queue, ji);
+       }
+}
+
 void
 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji)
 {
@@ -779,6 +824,8 @@ mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji)
        /* Debugging code, should be removed. */
        //jit_info_table_check (table);
 
+       mono_jit_info_free_or_queue (domain, ji);
+
        mono_domain_unlock (domain);
 }
 
@@ -955,6 +1002,18 @@ mono_jit_info_set_generic_sharing_context (MonoJitInfo *ji, MonoGenericSharingCo
 
        gi->generic_sharing_context = gsctx;
 }
+void
+mono_install_create_domain_hook (MonoCreateDomainFunc func)
+{
+       create_domain_hook = func;
+}
+
+void
+mono_install_free_domain_hook (MonoFreeDomainFunc func)
+{
+       free_domain_hook = func;
+}
 
 /**
  * mono_string_equal:
@@ -1112,24 +1171,30 @@ mono_domain_create (void)
        domain->static_data_array = NULL;
        mono_jit_code_hash_init (&domain->jit_code_hash);
        domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
-       domain->jit_info_table = jit_info_table_new ();
-       domain->class_init_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       domain->jump_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
+       domain->num_jit_info_tables = 1;
+       domain->jit_info_table = jit_info_table_new (domain);
+       domain->jit_info_free_queue = NULL;
        domain->finalizable_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       domain->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       domain->delegate_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
 
        InitializeCriticalSection (&domain->lock);
        InitializeCriticalSection (&domain->assemblies_lock);
+       InitializeCriticalSection (&domain->jit_code_hash_lock);
 
        domain->shared_generics_hash = NULL;
-
-       mono_debug_domain_create (domain);
+       domain->method_rgctx_hash = NULL;
 
        mono_appdomains_lock ();
        domain_id_alloc (domain);
        mono_appdomains_unlock ();
 
+       mono_perfcounters->loader_appdomains++;
+       mono_perfcounters->loader_total_appdomains++;
+
+       mono_debug_domain_create (domain);
+
+       if (create_domain_hook)
+               create_domain_hook (domain);
+
        mono_profiler_appdomain_loaded (domain, MONO_PROFILE_OK);
        
        return domain;
@@ -1160,6 +1225,13 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        if (domain)
                g_assert_not_reached ();
 
+#ifdef PLATFORM_WIN32
+       /* Avoid system error message boxes. */
+       SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
+
+       mono_load_coree (exe_filename);
+#endif
+
        mono_perfcounters_init ();
 
        mono_counters_register ("Max native code in a domain", MONO_COUNTER_INT|MONO_COUNTER_JIT, &max_domain_code_size);
@@ -1196,6 +1268,14 @@ 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
+               if (!exe_image) {
+                       exe_image = mono_assembly_open_from_bundle (exe_filename, NULL, FALSE);
+                       if (!exe_image)
+                               exe_image = mono_image_open (exe_filename, NULL);
+               }
+               mono_fixup_exe_image (exe_image);
+#endif
        } else if (runtime_version != NULL) {
                runtimes [0] = get_runtime_by_version (runtime_version);
                runtimes [1] = NULL;
@@ -1599,8 +1679,7 @@ mono_init_com_types (void)
 void
 mono_cleanup (void)
 {
-       if (exe_image)
-               mono_image_close (exe_image);
+       mono_close_exe_image ();
 
        mono_loader_cleanup ();
        mono_classes_cleanup ();
@@ -1614,6 +1693,13 @@ mono_cleanup (void)
        DeleteCriticalSection (&appdomains_mutex);
 }
 
+void
+mono_close_exe_image (void)
+{
+       if (exe_image)
+               mono_image_close (exe_image);
+}
+
 /**
  * mono_get_root_domain:
  *
@@ -1729,20 +1815,6 @@ mono_domain_register_shared_generic (MonoDomain *domain, MonoMethod *method, Mon
        g_hash_table_insert (domain->shared_generics_hash, method, jit_info);
 }
 
-static void
-dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
-{
-       MonoJitDynamicMethodInfo *di = value;
-       mono_code_manager_destroy (di->code_mp);
-       g_free (di);
-}
-
-static void
-delete_jump_list (gpointer key, gpointer value, gpointer user_data)
-{
-       g_slist_free (value);
-}
-
 void
 mono_domain_free (MonoDomain *domain, gboolean force)
 {
@@ -1753,8 +1825,14 @@ mono_domain_free (MonoDomain *domain, gboolean force)
                return;
        }
 
+       if (mono_dont_free_domains)
+               return;
+
        mono_profiler_appdomain_event (domain, MONO_PROFILE_START_UNLOAD);
 
+       if (free_domain_hook)
+               free_domain_hook (domain);
+
        mono_debug_domain_unload (domain);
 
        mono_appdomains_lock ();
@@ -1800,15 +1878,19 @@ mono_domain_free (MonoDomain *domain, gboolean force)
                domain->static_data_array = NULL;
        }
        mono_internal_hash_table_destroy (&domain->jit_code_hash);
-       if (domain->dynamic_code_hash) {
-               g_hash_table_foreach (domain->dynamic_code_hash, dynamic_method_info_free, NULL);
-               g_hash_table_destroy (domain->dynamic_code_hash);
-               domain->dynamic_code_hash = NULL;
-       }
        mono_g_hash_table_destroy (domain->ldstr_table);
        domain->ldstr_table = NULL;
+
+       /*
+        * There might still be jit info tables of this domain which
+        * are not freed.  Since the domain cannot be in use anymore,
+        * this will free them.
+        */
+       mono_thread_hazardous_try_free_all ();
+       g_assert (domain->num_jit_info_tables == 1);
        jit_info_table_free (domain->jit_info_table);
        domain->jit_info_table = NULL;
+       g_assert (!domain->jit_info_free_queue);
 
        /* collect statistics */
        code_alloc = mono_code_manager_size (domain->code_mp, &code_size);
@@ -1816,22 +1898,16 @@ mono_domain_free (MonoDomain *domain, gboolean force)
        max_domain_code_alloc = MAX (max_domain_code_alloc, code_alloc);
        max_domain_code_size = MAX (max_domain_code_size, code_size);
 
-       mono_class_unregister_domain_generic_vtables (domain);
-
 #ifdef DEBUG_DOMAIN_UNLOAD
        mono_mempool_invalidate (domain->mp);
        mono_code_manager_invalidate (domain->code_mp);
 #else
+       mono_perfcounters->loader_bytes -= mono_mempool_get_allocated (domain->mp);
        mono_mempool_destroy (domain->mp);
        domain->mp = NULL;
        mono_code_manager_destroy (domain->code_mp);
        domain->code_mp = NULL;
 #endif 
-       if (domain->jump_target_hash) {
-               g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
-               g_hash_table_destroy (domain->jump_target_hash);
-               domain->jump_target_hash = NULL;
-       }
        if (domain->type_hash) {
                mono_g_hash_table_destroy (domain->type_hash);
                domain->type_hash = NULL;
@@ -1844,22 +1920,23 @@ mono_domain_free (MonoDomain *domain, gboolean force)
                mono_g_hash_table_destroy (domain->type_init_exception_hash);
                domain->type_init_exception_hash = NULL;
        }
-       g_hash_table_destroy (domain->class_init_trampoline_hash);
-       domain->class_init_trampoline_hash = NULL;
-       g_hash_table_destroy (domain->jump_trampoline_hash);
-       domain->jump_trampoline_hash = NULL;
        g_hash_table_destroy (domain->finalizable_objects_hash);
        domain->finalizable_objects_hash = NULL;
-       g_hash_table_destroy (domain->jit_trampoline_hash);
-       domain->jit_trampoline_hash = NULL;
-       g_hash_table_destroy (domain->delegate_trampoline_hash);
-       domain->delegate_trampoline_hash = NULL;
        if (domain->shared_generics_hash) {
                g_hash_table_destroy (domain->shared_generics_hash);
                domain->shared_generics_hash = NULL;
        }
+       if (domain->method_rgctx_hash) {
+               g_hash_table_destroy (domain->method_rgctx_hash);
+               domain->method_rgctx_hash = NULL;
+       }
+       if (domain->generic_virtual_cases) {
+               g_hash_table_destroy (domain->generic_virtual_cases);
+               domain->generic_virtual_cases = NULL;
+       }
 
        DeleteCriticalSection (&domain->assemblies_lock);
+       DeleteCriticalSection (&domain->jit_code_hash_lock);
        DeleteCriticalSection (&domain->lock);
        domain->setup = NULL;
 
@@ -1869,6 +1946,8 @@ mono_domain_free (MonoDomain *domain, gboolean force)
 
        mono_gc_free_fixed (domain);
 
+       mono_perfcounters->loader_appdomains--;
+
        if ((domain == mono_root_domain))
                mono_root_domain = NULL;
 }
@@ -1900,6 +1979,20 @@ mono_domain_get_id (MonoDomain *domain)
        return domain->domain_id;
 }
 
+gpointer
+mono_domain_alloc (MonoDomain *domain, guint size)
+{
+       mono_perfcounters->loader_bytes += size;
+       return mono_mempool_alloc (domain->mp, size);
+}
+
+gpointer
+mono_domain_alloc0 (MonoDomain *domain, guint size)
+{
+       mono_perfcounters->loader_bytes += size;
+       return mono_mempool_alloc0 (domain->mp, size);
+}
+
 void 
 mono_context_set (MonoAppContext * new_context)
 {