grammar updates
[mono.git] / mono / metadata / domain.c
index 2af0a4397ccc4d0576b18e2ce13c51701cefd5a5..91bfb1e42432c8587fc4081594b3a4a38f9d58b3 100644 (file)
@@ -20,6 +20,9 @@
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/cil-coff.h>
+#include <mono/metadata/rawbuffer.h>
+
+//#define DEBUG_DOMAIN_UNLOAD
 
 static guint32 appdomain_thread_id = -1;
 static guint32 context_thread_id = -1;
@@ -28,6 +31,8 @@ static gint32 appdomain_id_counter = 0;
 
 static MonoGHashTable * appdomains_list = NULL;
 
+static CRITICAL_SECTION appdomains_mutex;
+
 MonoDomain *mono_root_domain = NULL;
 
 static MonoJitInfoTable *
@@ -68,8 +73,11 @@ MonoJitInfo *
 mono_jit_info_table_find (MonoDomain *domain, char *addr)
 {
        MonoJitInfoTable *table = domain->jit_info_table;
-       int left = 0, right = table->len;
+       int left = 0, right;
 
+       mono_domain_lock (domain);
+
+       right = table->len;
        while (left < right) {
                int pos = (left + right) / 2;
                MonoJitInfo *ji = g_array_index (table, gpointer, pos);
@@ -80,11 +88,14 @@ mono_jit_info_table_find (MonoDomain *domain, char *addr)
                        right = pos;
                else if (addr >= end) 
                        left = pos + 1;
-               else
+               else {
+                       mono_domain_unlock (domain);
                        return ji;
+               }
        }
+       mono_domain_unlock (domain);
 
-       /* maybe irt is shared code, so we also search in the root domain */
+       /* maybe it is shared code, so we also search in the root domain */
        if (domain != mono_root_domain)
                return mono_jit_info_table_find (mono_root_domain, addr);
 
@@ -96,9 +107,13 @@ mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
 {
        MonoJitInfoTable *table = domain->jit_info_table;
        gpointer start = ji->code_start;
-       int pos = mono_jit_info_table_index (table, start);
+       int pos;
+
+       mono_domain_lock (domain);
+       pos = mono_jit_info_table_index (table, start);
 
        g_array_insert_val (table, pos, ji);
+       mono_domain_unlock (domain);
 }
 
 static int
@@ -189,11 +204,15 @@ mono_domain_create (void)
        domain->jit_code_hash = g_hash_table_new (NULL, NULL);
        domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
        domain->jit_info_table = mono_jit_info_table_new ();
+       domain->class_init_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
+       domain->finalizable_objects_hash = g_hash_table_new (NULL, NULL);
        domain->domain_id = InterlockedIncrement (&appdomain_id_counter);
 
        InitializeCriticalSection (&domain->lock);
 
+       EnterCriticalSection (&appdomains_mutex);
        mono_g_hash_table_insert(appdomains_list, GINT_TO_POINTER(domain->domain_id), domain);
+       LeaveCriticalSection (&appdomains_mutex);
 
        return domain;
 }
@@ -221,6 +240,14 @@ mono_init (const char *filename)
        appdomain_thread_id = TlsAlloc ();
        context_thread_id = TlsAlloc ();
 
+       InitializeCriticalSection (&appdomains_mutex);
+
+       mono_metadata_init ();
+       mono_raw_buffer_init ();
+       mono_images_init ();
+       mono_assemblies_init ();
+       mono_loader_init ();
+
        // FIXME: When should we release this memory?
        appdomains_list = mono_g_hash_table_new (g_direct_hash, g_direct_equal);
 
@@ -463,13 +490,13 @@ mono_domain_get ()
 }
 
 /**
- * mono_domain_set:
+ * mono_domain_set_internal:
  * @domain: the new domain
  *
  * Sets the current domain to @domain.
  */
 inline void
-mono_domain_set (MonoDomain *domain)
+mono_domain_set_internal (MonoDomain *domain)
 {
        TlsSetValue (appdomain_thread_id, domain);
        TlsSetValue (context_thread_id, domain->default_context);
@@ -480,6 +507,14 @@ typedef struct {
        gpointer user_data;
 } DomainInfo;
 
+static void
+copy_hash_entry (gpointer key, gpointer data, gpointer user_data)
+{
+       MonoGHashTable *dest = (MonoGHashTable*)user_data;
+
+       mono_g_hash_table_insert (dest, key, data);
+}
+
 static void
 foreach_domain (gpointer key, gpointer data, gpointer user_data)
 {
@@ -492,10 +527,23 @@ void
 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
 {
        DomainInfo dom_info;
+       MonoGHashTable *copy;
+
+       /*
+        * Create a copy of the hashtable to avoid calling the user callback
+        * inside the lock because that could lead to deadlocks.
+        * We can do this because this function is not perf. critical.
+        */
+       copy = mono_g_hash_table_new (NULL, NULL);
+       EnterCriticalSection (&appdomains_mutex);
+       mono_g_hash_table_foreach (appdomains_list, copy_hash_entry, copy);
+       LeaveCriticalSection (&appdomains_mutex);
 
        dom_info.func = func;
        dom_info.user_data = user_data;
-       mono_g_hash_table_foreach (appdomains_list, foreach_domain, &dom_info);
+       mono_g_hash_table_foreach (copy, foreach_domain, &dom_info);
+
+       mono_g_hash_table_destroy (copy);
 }
 
 /**
@@ -510,8 +558,12 @@ mono_domain_assembly_open (MonoDomain *domain, const char *name)
 {
        MonoAssembly *ass;
 
-       if ((ass = g_hash_table_lookup (domain->assemblies, name)))
+       mono_domain_lock (domain);
+       if ((ass = g_hash_table_lookup (domain->assemblies, name))) {
+               mono_domain_unlock (domain);
                return ass;
+       }
+       mono_domain_unlock (domain);
 
        if (!(ass = mono_assembly_open (name, NULL)))
                return NULL;
@@ -532,14 +584,16 @@ delete_jump_list (gpointer key, gpointer value, gpointer user_data)
 }
 
 void
-mono_domain_unload (MonoDomain *domain, gboolean force)
+mono_domain_free (MonoDomain *domain, gboolean force)
 {
        if ((domain == mono_root_domain) && !force) {
                g_warning ("cant unload root domain");
                return;
        }
 
-       mono_g_hash_table_remove(appdomains_list, GINT_TO_POINTER(domain->domain_id));
+       EnterCriticalSection (&appdomains_mutex);
+       mono_g_hash_table_remove (appdomains_list, GINT_TO_POINTER(domain->domain_id));
+       LeaveCriticalSection (&appdomains_mutex);
        
        g_free (domain->friendly_name);
        g_hash_table_foreach (domain->assemblies, remove_assembly, NULL);
@@ -552,12 +606,19 @@ mono_domain_unload (MonoDomain *domain, gboolean force)
        g_hash_table_destroy (domain->jit_code_hash);
        mono_g_hash_table_destroy (domain->ldstr_table);
        mono_jit_info_table_free (domain->jit_info_table);
+#ifdef DEBUG_DOMAIN_UNLOAD
+       mono_mempool_invalidate (domain->mp);
+       mono_mempool_invalidate (domain->code_mp);
+#else
        mono_mempool_destroy (domain->mp);
        mono_mempool_destroy (domain->code_mp);
+#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);
        }
+       mono_g_hash_table_destroy (domain->class_init_trampoline_hash);
+       g_hash_table_destroy (domain->finalizable_objects_hash);
        if (domain->thread_static_fields)
                g_hash_table_destroy (domain->thread_static_fields);
        DeleteCriticalSection (&domain->lock);
@@ -584,10 +645,11 @@ mono_domain_get_by_id (gint32 domainid)
 {
        MonoDomain * domain;
 
-       if ((domain = mono_g_hash_table_lookup(appdomains_list, GINT_TO_POINTER(domainid)))) 
-               return domain;
+       EnterCriticalSection (&appdomains_mutex);
+       domain = mono_g_hash_table_lookup (appdomains_list, GINT_TO_POINTER(domainid));
+       LeaveCriticalSection (&appdomains_mutex);
 
-       return NULL;
+       return domain;
 }
 
 void